459242451@qq.com пре 3 година
родитељ
комит
dd9845b60f

+ 32 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/task/DhTask.java

@@ -0,0 +1,32 @@
+package com.ruoyi.web.controller.task;
+
+import cn.hutool.core.util.NumberUtil;
+import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.utils.RedisUtils;
+import com.ruoyi.zhdd.service.IDhService;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.stereotype.Component;
+
+/**
+ * @Description: 大华相关定时任务
+ * @Author: huangcheng
+ * @Date: 2022/1/20
+ * @Version V1.0
+ */
+@Component("dhTask")
+public class DhTask {
+
+    @Autowired
+    private IDhService dhService;
+
+    /**
+     * 大华token保活处理
+     */
+    public void keepalive() {
+        long ttl = RedisUtils.getTtl(Constants.CACHE_DH_TOKEN);
+        if (NumberUtil.compare(ttl, 0) == 1 && NumberUtil.compare(ttl, 20) == -1) {
+            // 保活token
+            dhService.tokenKeepalive(RedisUtils.getCacheObject(Constants.CACHE_DH_TOKEN));
+        }
+    }
+}

+ 35 - 0
ruoyi-admin/src/main/java/com/ruoyi/web/controller/zhdd/DhController.java

@@ -0,0 +1,35 @@
+package com.ruoyi.web.controller.zhdd;
+
+/**
+ * @Description: TODO
+ * @Author: huangcheng
+ * @Date: 2022/1/19
+ * @Version V1.0
+ */
+
+import com.ruoyi.common.core.domain.AjaxResult;
+import com.ruoyi.zhdd.service.IDhService;
+import io.swagger.annotations.Api;
+import io.swagger.annotations.ApiOperation;
+import lombok.RequiredArgsConstructor;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.validation.annotation.Validated;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@Validated
+@Api(value = "大华接口控制器", tags = {"大华接口控制器"})
+@RequiredArgsConstructor(onConstructor_ = @Autowired)
+@RestController
+@RequestMapping("/zhdd/dh")
+public class DhController {
+
+    private final IDhService dhService;
+
+    @ApiOperation("查询设备列表")
+    @GetMapping("/deviceList")
+    public AjaxResult list() {
+        return AjaxResult.success(dhService.getDeviceTree());
+    }
+}

+ 9 - 0
ruoyi-common/src/main/java/com/ruoyi/common/constant/Constants.java

@@ -150,4 +150,13 @@ public class Constants {
 
     public static final String NOTICE_PUSH = "notice_push";
     public static final String PROCESS_RECORD = "process_record";
+
+    /**
+     * 大华tokon缓存
+     */
+    public static final String CACHE_DH_TOKEN = "dh:token";
+    /**
+     * 大华tokon缓存=用于刷新
+     */
+    public static final String CACHE_DH_TOKEN_REFRESH = "dh:token:refresh";
 }

+ 29 - 1
ruoyi-common/src/main/java/com/ruoyi/common/utils/RedisUtils.java

@@ -4,7 +4,13 @@ import com.google.common.collect.Lists;
 import com.ruoyi.common.utils.spring.SpringUtils;
 import lombok.AccessLevel;
 import lombok.NoArgsConstructor;
-import org.redisson.api.*;
+import org.redisson.api.RBatch;
+import org.redisson.api.RBucket;
+import org.redisson.api.RList;
+import org.redisson.api.RMap;
+import org.redisson.api.RSet;
+import org.redisson.api.RTopic;
+import org.redisson.api.RedissonClient;
 
 import java.util.Collection;
 import java.util.List;
@@ -104,6 +110,28 @@ public class RedisUtils {
     }
 
     /**
+     * 获取是否存在某个key
+     *
+     * @param key
+     * @return
+     */
+    public static boolean exist(final String key) {
+        RBucket rBucket = client.getBucket(key);
+        return rBucket.isExists();
+    }
+
+    /**
+     * 获取过期时间
+     *
+     * @param key
+     * @return
+     */
+    public static long getTtl(final String key) {
+        RBucket rBucket = client.getBucket(key);
+        return rBucket.remainTimeToLive();
+    }
+
+    /**
      * 获得缓存的基本对象。
      *
      * @param key 缓存键值

+ 23 - 0
ruoyi-zhdd/src/main/java/com/ruoyi/zhdd/service/IDhService.java

@@ -0,0 +1,23 @@
+package com.ruoyi.zhdd.service;
+
+/**
+ * @Description: TODO
+ * @Author: huangcheng
+ * @Date: 2022/1/19
+ * @Version V1.0
+ */
+public interface IDhService {
+
+    /**
+     * 获取大华鉴权token
+     *
+     * @return
+     */
+    String getToken();
+
+    String authorize();
+
+    void tokenKeepalive(String token);
+
+    Object getDeviceTree();
+}

+ 143 - 0
ruoyi-zhdd/src/main/java/com/ruoyi/zhdd/service/impl/DhServiceImpl.java

@@ -0,0 +1,143 @@
+package com.ruoyi.zhdd.service.impl;
+
+import cn.hutool.crypto.SecureUtil;
+import cn.hutool.http.HttpRequest;
+import cn.hutool.http.HttpUtil;
+import cn.hutool.json.JSONObject;
+import cn.hutool.json.JSONUtil;
+import com.ruoyi.common.constant.Constants;
+import com.ruoyi.common.exception.ServiceException;
+import com.ruoyi.common.utils.RedisUtils;
+import com.ruoyi.system.service.ISysConfigService;
+import com.ruoyi.zhdd.service.IDhService;
+import lombok.extern.slf4j.Slf4j;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.stereotype.Service;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * @Description: TODO
+ * @Author: huangcheng
+ * @Date: 2022/1/19
+ * @Version V1.0
+ */
+@Service
+@Slf4j
+public class DhServiceImpl implements IDhService {
+
+    @Value("${third.dhAuthorize}")
+    private String dhAuthorizeUrl;
+    @Value("${third.keepalive}")
+    private String keepaliveUrl;
+    @Value("${third.dhDeviceTree}")
+    private String dhDeviceTreeUrl;
+    @Autowired
+    private ISysConfigService sysConfigService;
+
+    @Override
+
+    public String getToken() {
+        String token = RedisUtils.getCacheObject(Constants.CACHE_DH_TOKEN);
+        if (token == null) {
+            token = authorize();
+        }
+        return token;
+    }
+
+    /**
+     * 创建会话
+     *
+     * @return
+     */
+    public String authorize() {
+        // 从大华接口获取token
+        String token = "";
+        String userName = "shujuceshi";
+        JSONObject jsonOne = new JSONObject();
+        jsonOne.set("userName", userName);
+        jsonOne.set("clientType", "other");
+        String postOne = HttpUtil.post(dhAuthorizeUrl, jsonOne.toString());
+        JSONObject postOneJson = JSONUtil.parseObj(postOne);
+        if (postOneJson.containsKey("realm")) {
+            String randomKey = postOneJson.getStr("randomKey");
+            String realm = postOneJson.getStr("realm");
+            String encryptType = postOneJson.getStr("encryptType");
+            // 第二次交互
+            String password = "Admin123";
+            String signature = SecureUtil.md5(
+                SecureUtil.md5(userName + ":" + realm + ":" +
+                    SecureUtil.md5(
+                        SecureUtil.md5(
+                            userName +
+                                SecureUtil.md5(password)))) + ":" + randomKey);
+            jsonOne.set("signature", signature);
+            jsonOne.set("randomKey", randomKey);
+            jsonOne.set("encryptType", encryptType);
+            String postTwo = HttpUtil.post(dhAuthorizeUrl, jsonOne.toString());
+            JSONObject postTwoJson = JSONUtil.parseObj(postTwo);
+            if (postTwoJson.containsKey("token")) {
+                token = postTwoJson.getStr("token");
+                // 存入redis缓存
+                RedisUtils.setCacheObject(Constants.CACHE_DH_TOKEN, token, 120, TimeUnit.SECONDS);
+//                RedisUtils.setCacheObject(Constants.CACHE_DH_TOKEN_REFRESH, token);
+            } else {
+                log.error("大华=第二次创建会话失败:{}", postTwoJson);
+                if ("user is repeated landing".equals(postTwoJson.getStr("message"))) {
+                    // 说明已经登录过,那么退出,重新获取
+
+                }
+            }
+        } else {
+            log.error("大华=第一次创建会话失败:{}", postOneJson);
+        }
+        return token;
+    }
+
+    /**
+     * 保活大华token
+     *
+     * @param token
+     */
+    public void tokenKeepalive(String token) {
+        JSONObject json = new JSONObject();
+        json.set("token", token);
+        json.set("duration", 120);
+        String put = HttpRequest.put(keepaliveUrl).timeout(-1).body(json.toString()).execute().body();
+        JSONObject putJson = JSONUtil.parseObj(put);
+        if (putJson.getInt("code") == 200) {
+            // 存入redis缓存
+            RedisUtils.setCacheObject(Constants.CACHE_DH_TOKEN, token, 120, TimeUnit.SECONDS);
+        } else {
+            log.info("保活失败:{}。重新获取", putJson);
+            authorize();
+        }
+    }
+
+    @Override
+    public Object getDeviceTree() {
+        Map<String, Object> param = new HashMap<>();
+        String id = sysConfigService.selectConfigByKey("dh.device.orgId");
+        param.put("id", id);
+        param.put("nodeType", 1);
+        param.put("typeCode", "01;1;ALL");
+        param.put("page", 1);
+        param.put("pageSize", 100);
+        String get = HttpRequest.get(dhDeviceTreeUrl)
+            .header("X-Subject-Token", this.getToken())
+            .form(param)
+            .timeout(-1)
+            .execute().body();
+        JSONObject getJson = JSONUtil.parseObj(get);
+        if (getJson.containsKey("code")) {
+            // 说明有错误
+            log.error("获取设备树出错:{}", getJson);
+            throw new ServiceException("获取设备树出错.稍后再试");
+        }
+        return getJson.getJSONArray("results");
+    }
+
+}

+ 8 - 0
sql/20211209修改.sql

@@ -148,7 +148,15 @@ on column b_chemical_data.content is '内容';
 
 -- 以上已同步
 
+alter table b_incident_process
+    add incident_status smallint;
+
+comment
+on column b_incident_process.incident_status is '时间处置过程当前状态';
 
+INSERT INTO public.sys_config (config_id, config_name, config_key, config_value, config_type, create_by, create_time, update_by, update_time, remark)
+VALUES (12, '大华组织id', 'dh.device.orgId', 'S4NbecfYB1DFLAIM9FFHQ8', 'N', 'admin', '2021-09-16 03:32:56', '', null, '用于获取设备树');
+-- 以上已同步