From 46203ee88249d9a4046f3d453deb41edc562cf6c Mon Sep 17 00:00:00 2001
From: jiazx0107@163.com <jiazx0107@163.com>
Date: 星期二, 12 十二月 2023 12:36:53 +0800
Subject: [PATCH] 提交网关心跳和设备状态

---
 src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java |  150 ++++++++++++++++++++++++++++++++++----------------
 1 files changed, 102 insertions(+), 48 deletions(-)

diff --git a/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java b/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java
index a6088d8..f8b3c0a 100644
--- a/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java
+++ b/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java
@@ -1,25 +1,33 @@
 package com.fzzy.gateway.hx2023.service;
 
 import com.alibaba.fastjson2.JSONObject;
+import com.fzzy.api.Constant;
+import com.fzzy.api.data.GatewayDeviceType;
 import com.fzzy.api.data.PushProtocol;
-import com.fzzy.api.utils.MyMD5Util;
-import com.fzzy.api.utils.RSAUtils;
-import com.fzzy.api.utils.RedisConst;
-import com.fzzy.api.utils.RedisUtil;
+import com.fzzy.api.utils.ContextUtil;
 import com.fzzy.api.view.repository.ApiLogRep;
-import com.fzzy.gateway.service.GatewayConfService;
-import com.fzzy.gateway.util.GatewayHttpUtil;
+import com.fzzy.gateway.GatewayUtils;
 import com.fzzy.gateway.api.GatewayRemoteService;
+import com.fzzy.gateway.data.BaseResp;
 import com.fzzy.gateway.entity.GatewayConf;
+import com.fzzy.gateway.entity.GatewayDevice;
 import com.fzzy.gateway.hx2023.ScConstant;
 import com.fzzy.gateway.hx2023.data.CloudResp;
-import com.fzzy.gateway.util.GatewayRSAUtils;
+import com.fzzy.gateway.hx2023.data.DeviceStatusData;
+import com.fzzy.gateway.hx2023.data.HeartBeatData;
+import com.fzzy.gateway.hx2023.kafka.KafkaDeviceReportService;
+import com.fzzy.gateway.hx2023.util.OpenApiSignatureUtils;
+import com.fzzy.gateway.service.GatewayConfService;
+import com.fzzy.gateway.util.GatewayHttpUtil;
 import lombok.Data;
 import lombok.extern.slf4j.Slf4j;
 import org.apache.commons.lang.StringUtils;
+import org.apache.commons.lang.time.DateFormatUtils;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
+import java.util.Collection;
+import java.util.Date;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -30,12 +38,15 @@
 public class HxGatewayRemoteServiceImpl implements GatewayRemoteService {
 
 
+    String charset = "utf-8";
+
     @Resource
     private ApiLogRep apiLogRep;
 
-
     @Resource
     private GatewayConfService gatewayConfService;
+    @Resource
+    private KafkaDeviceReportService kafkaDeviceReportService;
 
 
     @Override
@@ -62,7 +73,7 @@
 
             CloudResp respKey = JSONObject.parseObject(jsonStr, CloudResp.class);
 
-            if (ScConstant.CODE_200 == respKey.getCode()) {
+            if (BaseResp.CODE_200 == respKey.getCode()) {
                 JSONObject object = respKey.getData();
                 String pubKey = (String) object.get("pubKey");
                 String priKey = (String) object.get("priKey");
@@ -73,14 +84,15 @@
 
 
             //鑾峰彇 AccessToken 鎺ュ彛
-            String sign = getSign(params, gatewayConf.getPrivateKey());
+            String sign = getSign(params, gatewayConf);
             params.put("sign", sign);
             url = gatewayConf.getApiUrl() + "reserver/api/token/apply";
+
             jsonStr = GatewayHttpUtil.doGet(url, params);
             log.info("---鑾峰彇AccessToken鎺ュ彛-杩斿洖---{}", jsonStr);
             CloudResp respToken = JSONObject.parseObject(jsonStr, CloudResp.class);
 
-            if (ScConstant.CODE_200 == respToken.getCode()) {
+            if (BaseResp.CODE_200 == respToken.getCode()) {
                 JSONObject object = respKey.getData();
                 if (null != object) {
                     String token = (String) object.get("token");
@@ -98,34 +110,42 @@
 
     @Override
     public void heartbeat(GatewayConf gatewayConf) {
+
+        log.info("------------瀹氭椂蹇冭烦鎵ц---------{}", gatewayConf.getKqmc());
+
         try {
 
-            if (StringUtils.isEmpty(gatewayConf.getApiUrl())) {
-                return;
-            }
 
             gatewayConf = getCacheConf(gatewayConf.getKqdm());
-            //缃戝叧蹇冭烦鎺ュ彛
-            Map<String, String> params = new HashMap<>();
-            params.put("token", gatewayConf.getAccessToken());
-            params.put("gatewayId", gatewayConf.getGatewayId());
-            params.put("gatewayIp", gatewayConf.getGatewayIp());
-            params.put("gatewayMac", gatewayConf.getGatewayMac());
-            params.put("heartbeat", "1");
-            params.put("timestamp", System.currentTimeMillis() + "");
 
-            String sign = getSign(params, gatewayConf.getPrivateKey());
-            params.put("sign", sign);
 
-            String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat";
+            HeartBeatData heartBeatData = new HeartBeatData();
 
-            String jsonStr = GatewayHttpUtil.doGet(url, params);
-            log.info("---缃戝叧蹇冭烦鎺ュ彛-杩斿洖---{}", jsonStr);
+            heartBeatData.setMessageId(ContextUtil.getUUID());
+            heartBeatData.setApISource(ScConstant.API_SOURCE_TARGET_EQUIPMENT_HEARTBEAT);
+            heartBeatData.setGatewayId(gatewayConf.getGatewayId());
+            heartBeatData.setGatewayIp(gatewayConf.getGatewayIp());
+            heartBeatData.setGatewayMac(null == gatewayConf.getGatewayMac() ? "鏃�" : gatewayConf.getGatewayMac());
+            heartBeatData.setHeartbeat(1);
+            heartBeatData.setReportTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd hh:mm:ss"));
+            heartBeatData.setMemo("FZZY");
+
+            String topic = ScConstant.TOPIC_EQUIPMENT_HEARTBEAT;
+            topic = topic.replace("{kqdm}", gatewayConf.getKqdm());
+
+            String messageInfo = JSONObject.toJSONString(heartBeatData);
+
+            log.info("---缃戝叧蹇冭烦鎺ㄩ��--{}-{}", topic, messageInfo);
+
+            kafkaDeviceReportService.publishWithTopic(messageInfo, topic);
+
 
         } catch (Exception e) {
             log.error("------缃戝叧蹇冭烦鎺ュ彛--鎵ц澶辫触-----{}", e);
         }
 
+        //鎵ц褰撳墠缃戝叧鐨勮澶囩姸鎬佹帹閫�
+        pushDeviceStatus(gatewayConf);
     }
 
     @Override
@@ -142,13 +162,13 @@
             params.put("token", gatewayConf.getAccessToken());
             params.put("gatewayId", gatewayConf.getGatewayId());
             params.put("gatewayIp", gatewayConf.getGatewayIp());
-            params.put("gatewayMac", gatewayConf.getGatewayMac());
-            params.put("gatewayCPU", gatewayConf.getGatewayCPU());
-            params.put("gatewayMem", gatewayConf.getGatewayMem());
-            params.put("gatewayHardDisk", gatewayConf.getGatewayHardDisk());
+            params.put("gatewayMac", null == gatewayConf.getGatewayMac() ? "鏃�" : gatewayConf.getGatewayMac());
+            params.put("gatewayCPU", null == gatewayConf.getGatewayCPU() ? "鏃�" : gatewayConf.getGatewayCPU());
+            params.put("gatewayMem", null == gatewayConf.getGatewayMem() ? "鏃�" : gatewayConf.getGatewayMem());
+            params.put("gatewayHardDisk", null == gatewayConf.getGatewayHardDisk() ? "鏃�" : gatewayConf.getGatewayHardDisk());
             params.put("timestamp", System.currentTimeMillis() + "");
 
-            String sign = getSign(params, gatewayConf.getPrivateKey());
+            String sign = getSign(params, gatewayConf);
             params.put("sign", sign);
 
             String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat";
@@ -162,27 +182,63 @@
     }
 
 
-    public String getSign(Map<String, String> parames, String priKey) {
-        //鍙傛暟鎷兼帴
-        String msg = "";
-        for (Map.Entry<String, String> param : parames.entrySet()) {
-            msg += param.getKey() + "=" + param.getValue() + "&";
+    @Override
+    public void pushDeviceStatus(GatewayConf gatewayConf) {
+
+        //鑾峰彇璁惧鍒楄〃
+        Collection<GatewayDevice> list = GatewayUtils.allCacheDevice();
+        if (null == list || list.isEmpty()) {
+            log.info("--------绯荤粺鏈幏鍙栧埌褰撳墠绯荤粺璁惧鍒楄〃锛屼笉鎵ц鐘舵�佹帹閫�-----");
+            return;
         }
-        msg = msg.substring(0, msg.length() - 1);
 
-        log.debug("------寰呭姞瀵嗕俊鎭�-----{}", msg);
+        //灏佽璁惧鎶ユ枃淇℃伅
+        DeviceStatusData statusData;
+        String messageInfo;
+        String topic = ScConstant.TOPIC_EQUIPMENT_STATUS;
+        topic = topic.replace("{kqdm}", gatewayConf.getKqdm());
 
-        //MD5鍔犲瘑
-        String md5sign = MyMD5Util.getMD5(msg);
-        log.debug("------md5鍔犲瘑-----{}", md5sign);
+        for (GatewayDevice device : list) {
+            //濡傛灉璁惧娌℃湁閰嶇疆productId鐩存帴璺宠繃
+            if (StringUtils.isEmpty(device.getProductId())) continue;
+            statusData = new DeviceStatusData();
+            statusData.setMessageId(ContextUtil.getUUID());
+            statusData.setApISource(ScConstant.API_SOURCE_TARGET_EQUIPMENT_STATUS);
+            statusData.setGatewayId(gatewayConf.getGatewayId());
+            statusData.setGatewayIp(gatewayConf.getGatewayIp());
+            statusData.setProductId(device.getProductId());
+            statusData.setEquipId(device.getDeviceId());
+            statusData.setReportTime(DateFormatUtils.format(new Date(), "yyyy-MM-dd hh:mm:ss"));
+            statusData.setMemo("FZZY");
 
-        //RSA鍔犲瘑
-        String result = GatewayRSAUtils.encryptByPrivate(md5sign, priKey);
-        log.debug("------RSA鍔犲瘑-----{}", result);
+            //閽堝绮儏璁惧
+            if (GatewayDeviceType.TYPE_07.getCode().equals(device.getType())) {
+                if (Constant.YN_N.equals(device.getStatus())) statusData.setStatus(0);
+            }
 
-        return result;
+            messageInfo = JSONObject.toJSONString(statusData);
+            //鎺ㄩ�佽澶囩姸鎬�
+            kafkaDeviceReportService.publishWithTopic(messageInfo, topic);
+
+        }
     }
 
+
+    public String getSign(Map<String, String> parames, GatewayConf gatewayConf) throws Exception {
+
+        //鍙傛暟璋冩暣
+        String signContent = OpenApiSignatureUtils.getSignContent(parames);
+        log.debug("------寰呭姞瀵嗕俊鎭�-----{}", signContent);
+
+        //MD5鍔犲瘑
+        String md5sign = OpenApiSignatureUtils.getMd5Content(signContent, charset);
+
+        //RSA鍔犲瘑
+        String singValue = OpenApiSignatureUtils.doSignByHex(gatewayConf.getGatewayId(), md5sign, charset, gatewayConf.getPublicKey(), gatewayConf.getPrivateKey());
+        log.debug("------RSA鍔犲瘑绛惧悕-----{}", singValue);
+
+        return singValue;
+    }
 
     /**
      * @param kqdm
@@ -193,9 +249,7 @@
     }
 
     private void updateAuthToken(GatewayConf conf) {
-
         gatewayConfService.updateCache(conf);
-
     }
 
 

--
Gitblit v1.9.3