From 69e8acc5dd1f760eb60e914472c151bfa8126a52 Mon Sep 17 00:00:00 2001
From: vince <757871790@qq.com>
Date: 星期三, 03 七月 2024 17:08:20 +0800
Subject: [PATCH] 修改测温
---
src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java | 254 ++++++++++++++++++++++++++++++++++++++------------
1 files changed, 193 insertions(+), 61 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 d95db6b..c85f7c6 100644
--- a/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java
+++ b/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java
@@ -1,23 +1,34 @@
package com.fzzy.gateway.hx2023.service;
-import com.fzzy.api.data.AuthToken;
+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.entity.ApiLog;
import com.fzzy.api.utils.ContextUtil;
-import com.fzzy.api.utils.RedisConst;
-import com.fzzy.api.utils.RedisUtil;
import com.fzzy.api.view.repository.ApiLogRep;
+import com.fzzy.data.ConfigData;
+import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayRemoteService;
-import com.fzzy.gateway.data.GatewayResponse;
+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.push.gb2022.HttpClientUtil;
+import com.fzzy.gateway.hx2023.data.CloudResp;
+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 org.springframework.stereotype.Service;
import javax.annotation.Resource;
+import java.util.Collection;
+import java.util.Date;
import java.util.HashMap;
import java.util.Map;
@@ -28,10 +39,17 @@
public class HxGatewayRemoteServiceImpl implements GatewayRemoteService {
+ String charset = "utf-8";
+
@Resource
private ApiLogRep apiLogRep;
+
@Resource
- private RedisUtil redisUtil;
+ private GatewayConfService gatewayConfService;
+ @Resource
+ private KafkaDeviceReportService kafkaDeviceReportService;
+ @Resource
+ private ConfigData configData;
@Override
@@ -40,79 +58,193 @@
}
@Override
- public GatewayResponse authorize(GatewayConf conf) {
+ public void init(GatewayConf gatewayConf) {
+ try {
- //娣诲姞LOG
- ApiLog apiLog = new ApiLog();
- apiLog.setData("閴存潈鎺ュ彛");
- apiLog.setId(ContextUtil.getUUID());
- apiLog.setKqdm(conf.getKqdm());
+ if (StringUtils.isEmpty(gatewayConf.getApiUrl())) {
+ return;
+ }
+
+ //鑾峰彇鍏閽ユ帴鍙�
+ Map<String, String> params = new HashMap<>();
+ params.put("appId", gatewayConf.getGatewayId());
+
+ String url = gatewayConf.getApiUrl() + "reserver/api/key/apply";
+ String jsonStr = GatewayHttpUtil.doGet(url, params);
+
+ log.info("---鑾峰彇鍏閽ユ帴鍙�-杩斿洖---{}", jsonStr);
+
+ CloudResp respKey = JSONObject.parseObject(jsonStr, CloudResp.class);
+
+ if (BaseResp.CODE_200 == respKey.getCode()) {
+ JSONObject object = respKey.getData();
+ String pubKey = (String) object.get("pubKey");
+ String priKey = (String) object.get("priKey");
+
+ gatewayConf.setPublicKey(pubKey);
+ gatewayConf.setPrivateKey(priKey);
+ }
+
+
+ //鑾峰彇 AccessToken 鎺ュ彛
+ 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 (BaseResp.CODE_200 == respToken.getCode()) {
+ JSONObject object = respKey.getData();
+ if (null != object) {
+ String token = (String) object.get("token");
+ gatewayConf.setAccessToken(token);
+ }
+ }
+
+ //鏇存柊缂撳瓨
+ updateAuthToken(gatewayConf);
+
+ } catch (Exception e) {
+ log.error("------鍒濆鍖栧け璐�-----{}", e);
+ }
+ }
+
+ @Override
+ public void heartbeat(GatewayConf gatewayConf) {
+
+ log.info("------------瀹氭椂蹇冭烦鎵ц---------{}", gatewayConf.getKqmc());
try {
- Map<String, Object> map = new HashMap<>();
- map.put("username", conf.getUserName());
- map.put("username", conf.getPassword());
- log.debug("-----------------鏁版嵁鎶ユ枃----------------{}", map);
+ gatewayConf = getCacheConf(gatewayConf.getKqdm());
- String url = conf.getApiUrl() + ScConstant.API_URL_AUTH;
+ HeartBeatData heartBeatData = new HeartBeatData();
+
+ 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);
+
+ //濡傛灉鏄祴璇曟ā寮忎笉鎵ц鎺ㄩ��
+ if (configData.getActive().indexOf("dev") >= 0) {
+
+ log.info("----------------------------鎺ㄩ�並AFKA淇℃伅锛屾敞锛氳皟璇曟ā寮忎笉鎺ㄩ��---------------------------");
+ log.info("-----TOPIC-----{}", topic);
+ log.info("-----Message-----{}", messageInfo);
+
+ return;
+ }
- GatewayResponse responseDto = HttpClientUtil.pushGateway(url, map);
- apiLog.setStatus(responseDto.getStatus());
- apiLog.setResult(responseDto.getMessage());
- apiLogRep.save(apiLog);
+ kafkaDeviceReportService.publishWithTopic(messageInfo, topic);
- updateAuthToken(responseDto, conf);
-
- return responseDto;
} catch (Exception e) {
- apiLog.setStatus(99);
- apiLog.setResult("澶辫触锛�" + e.getMessage());
- apiLogRep.save(apiLog);
- log.error(e.getMessage(), e);
- return new GatewayResponse(99, e.getMessage());
+ log.error("------缃戝叧蹇冭烦鎺ュ彛--鎵ц澶辫触-----{}", e);
}
+
+ //鎵ц褰撳墠缃戝叧鐨勮澶囩姸鎬佹帹閫�
+ log.info("------------瀹氭椂璁惧鐘舵�佹墽琛�---------{}", gatewayConf.getKqmc());
+ pushDeviceStatus(gatewayConf);
+ }
+
+ @Override
+ public void pushInfo(GatewayConf gatewayConf) {
+ //DO NOTHING
+ }
+
+
+ @Override
+ public void pushDeviceStatus(GatewayConf gatewayConf) {
+
+ //鑾峰彇璁惧鍒楄〃
+ Collection<GatewayDevice> list = GatewayUtils.allCacheDevice();
+ if (null == list || list.isEmpty()) {
+ log.info("--------绯荤粺鏈幏鍙栧埌褰撳墠绯荤粺璁惧鍒楄〃锛屼笉鎵ц鐘舵�佹帹閫�-----");
+ return;
+ }
+
+ //灏佽璁惧鎶ユ枃淇℃伅
+ DeviceStatusData statusData;
+ String messageInfo;
+ String topic = ScConstant.TOPIC_EQUIPMENT_STATUS;
+ topic = topic.replace("{kqdm}", gatewayConf.getKqdm());
+
+ 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");
+
+ //閽堝绮儏璁惧
+ if (GatewayDeviceType.TYPE_07.getCode().equals(device.getType())) {
+ if (Constant.YN_N.equals(device.getStatus())) statusData.setStatus(0);
+ }
+
+ messageInfo = JSONObject.toJSONString(statusData);
+
+ //濡傛灉鏄祴璇曟ā寮忎笉鎵ц鎺ㄩ��
+ if (configData.getActive().indexOf("dev") >= 0) {
+
+ log.info("----------------------------鎺ㄩ�並AFKA淇℃伅锛屾敞锛氳皟璇曟ā寮忎笉鎺ㄩ��---------------------------");
+ log.info("-----TOPIC-----{}", topic);
+ log.info("-----Message-----{}", messageInfo);
+
+ return;
+ }
+
+ //鎺ㄩ�佽澶囩姸鎬�
+ 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
* @return
*/
- public AuthToken getAuthToken(String kqdm) {
- try {
- String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, kqdm);
-
- AuthToken token = (AuthToken) redisUtil.get(key);
-
- if (null == token) {
- log.error("------------------鏈幏鍙栧埌TOKEN---------------");
- return null;
- }
-
- return token;
- } catch (Exception e) {
- log.error(e.getMessage(), e);
- return null;
- }
+ public GatewayConf getCacheConf(String kqdm) {
+ return gatewayConfService.getCacheConf(kqdm);
}
- private void updateAuthToken(GatewayResponse dto, GatewayConf conf) {
- String key = RedisConst.buildKey(RedisConst.KYE_TOKEN, conf.getKqdm());
- AuthToken token = getAuthToken(conf.getKqdm());
-
- if (null == token) {
- token = new AuthToken();
- token.setKqdm(conf.getKqdm());
- }
-
- if (null != dto.getResult()) {
- token.setToken(dto.getResult().getToken());
- }
-
- redisUtil.set(key, token);
+ private void updateAuthToken(GatewayConf conf) {
+ gatewayConfService.updateCache(conf);
}
--
Gitblit v1.9.3