From efec12bd4827ddefd9b19ccc0eee3ca1c62908f3 Mon Sep 17 00:00:00 2001 From: jiazx0107@163.com <jiazx0107@163.com> Date: 星期五, 12 一月 2024 19:09:01 +0800 Subject: [PATCH] 添加手动推送粮情接口,以便第三方接口调用 --- src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java | 246 ++++++++++++++++++++++-------------------------- 1 files changed, 114 insertions(+), 132 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 13c0715..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,28 +1,36 @@ 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.*; +import com.fzzy.api.utils.ContextUtil; import com.fzzy.api.view.repository.ApiLogRep; -import com.fzzy.gateway.data.BaseResp; -import com.fzzy.gateway.service.GatewayConfService; -import com.fzzy.gateway.util.GatewayHttpUtil; +import com.fzzy.data.ConfigData; +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.io.UnsupportedEncodingException; -import java.util.Comparator; +import java.util.Collection; +import java.util.Date; import java.util.HashMap; import java.util.Map; -import java.util.TreeMap; @Slf4j @@ -31,11 +39,17 @@ public class HxGatewayRemoteServiceImpl implements GatewayRemoteService { + String charset = "utf-8"; + @Resource private ApiLogRep apiLogRep; @Resource private GatewayConfService gatewayConfService; + @Resource + private KafkaDeviceReportService kafkaDeviceReportService; + @Resource + private ConfigData configData; @Override @@ -73,9 +87,10 @@ //鑾峰彇 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); @@ -98,111 +113,126 @@ @Override public void heartbeat(GatewayConf gatewayConf) { + + log.info("------------瀹氭椂蹇冭烦鎵ц---------{}", gatewayConf.getKqmc()); + try { - if (StringUtils.isEmpty(gatewayConf.getApiUrl())) { + + gatewayConf = getCacheConf(gatewayConf.getKqdm()); + + + 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; } - 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"; - - String jsonStr = GatewayHttpUtil.doGet(url, params); - // log.info("---缃戝叧蹇冭烦鎺ュ彛-杩斿洖---{}", jsonStr); + kafkaDeviceReportService.publishWithTopic(messageInfo, topic); } catch (Exception e) { log.error("------缃戝叧蹇冭烦鎺ュ彛--鎵ц澶辫触-----{}", e); } + //鎵ц褰撳墠缃戝叧鐨勮澶囩姸鎬佹帹閫� + log.info("------------瀹氭椂璁惧鐘舵�佹墽琛�---------{}", gatewayConf.getKqmc()); + pushDeviceStatus(gatewayConf); } @Override public void pushInfo(GatewayConf gatewayConf) { - try { + //DO NOTHING + } - if (StringUtils.isEmpty(gatewayConf.getApiUrl())) { + + @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; } - 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("gatewayCPU", gatewayConf.getGatewayCPU()); - params.put("gatewayMem", gatewayConf.getGatewayMem()); - params.put("gatewayHardDisk", gatewayConf.getGatewayHardDisk()); - params.put("timestamp", System.currentTimeMillis() + ""); + //鎺ㄩ�佽澶囩姸鎬� + kafkaDeviceReportService.publishWithTopic(messageInfo, topic); - String sign = getSign(params, gatewayConf.getPrivateKey()); - params.put("sign", sign); - - String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat"; - - String jsonStr = GatewayHttpUtil.doGet(url, params); - log.info("---鎺ㄩ�佺綉鍏充俊鎭�-杩斿洖---{}", jsonStr); - - } catch (Exception e) { - log.error("------鎺ㄩ�佺綉鍏充俊鎭�--鎵ц澶辫触-----{}", e); } } - public String getSign(Map<String, String> parames, String priKey) { - //鍙傛暟鎷兼帴 - String msg = ""; - parames = sortMapKey(parames); - for (Map.Entry<String, String> param : parames.entrySet()) { - msg += param.getKey() + "=" + param.getValue() + "&"; - } - msg = msg.substring(0, msg.length() - 1); + public String getSign(Map<String, String> parames, GatewayConf gatewayConf) throws Exception { - log.debug("------寰呭姞瀵嗕俊鎭�-----{}", msg); + //鍙傛暟璋冩暣 + String signContent = OpenApiSignatureUtils.getSignContent(parames); + log.debug("------寰呭姞瀵嗕俊鎭�-----{}", signContent); //MD5鍔犲瘑 - String md5sign = MyMD5Util.getMD5(msg); - log.debug("------md5鍔犲瘑-----{}", md5sign); + String md5sign = OpenApiSignatureUtils.getMd5Content(signContent, charset); //RSA鍔犲瘑 - String result = GatewayRSAUtils.encryptByPrivate(md5sign, priKey); - log.debug("------RSA鍔犲瘑-----{}", result); + String singValue = OpenApiSignatureUtils.doSignByHex(gatewayConf.getGatewayId(), md5sign, charset, gatewayConf.getPublicKey(), gatewayConf.getPrivateKey()); + log.debug("------RSA鍔犲瘑绛惧悕-----{}", singValue); - return result; - } - public static String getSign2(Map<String, String> parames, String priKey) { - //鍙傛暟鎷兼帴 - String msg = ""; - parames = sortMapKey(parames); - for (Map.Entry<String, String> param : parames.entrySet()) { - msg += param.getKey() + "=" + param.getValue() + "&"; - } - msg = msg.substring(0, msg.length() - 1); - - log.debug("------寰呭姞瀵嗕俊鎭�-----{}", msg); - - //MD5鍔犲瘑 - String md5sign = MyMD5Util.getMD5(msg); - log.debug("------md5鍔犲瘑-----{}", md5sign); - - //RSA鍔犲瘑 - String result = GatewayRSAUtils.encryptByPrivate(md5sign, priKey); - log.debug("------RSA鍔犲瘑-----{}", result); - - return result; + return singValue; } /** @@ -214,56 +244,8 @@ } private void updateAuthToken(GatewayConf conf) { - gatewayConfService.updateCache(conf); - } - private static Map<String, String> sortMapKey(Map<String, String> map){ - if(map == null || map.isEmpty()){ - return null; - } - - - Map<String, String> sortMap = new TreeMap<>(new Comparator<String>() { - @Override - public int compare(String o1, String o2) { - return o1.compareTo(o2); - } - }); - sortMap.putAll(map); - return sortMap; - } - - - public static void main(String[] args) throws UnsupportedEncodingException { - //缃戝叧蹇冭烦鎺ュ彛 -// Map<String, String> params = new HashMap<>(); -// //params.put("token", ""); -// params.put("gatewayId", "6e3d92ff71b911eea5e50250f2000002"); -// params.put("gatewayIp", "123"); -// params.put("gatewayMac", "123"); -// params.put("gatewayCPU","123"); -// params.put("gatewayMem", "123"); -// params.put("gatewayHardDisk", "123"); -// params.put("timestamp", "2023-10-31 12:12:12"); -// String sign = getSign2(params,"MIIBVQIBADANBgkqhkiG9w0BAQEFAASCAT8wggE7AgEAAkEA0lg35sJGRyUFEDzt\np9pWvc0JD+0+SmiZIgMbUaEM5gqBsvUJ9LnzB0rpMTbuqAxAFbUH1Nw4qXdJVnKS\nRYfPgQIDAQABAkBPkxIq2Uou9gFRYEtkGcSA70VDYjQTgDiWVmiKXAv+JkEJmOno\\nMrHYQGAiP0q4xW66F+CLNCrGPVJns2ywMxwxAiEA/vyNX5M0/t+RHAxpMp484i9T\nb5Qf+HvepOV3c0UGZx0CIQDTLj4+qgoXGOLCM9/dzVJZ04VIjK6u546z22hXFWgI\ntQIhAMWFP8JMlx3kc1UF7Cuw1jrkLD7fwJBM7CBSPODHFHmlAiEAhv4Hcf02B/+z\nbBrG3rzSCHOKvo6XV1zTvqrhLtGTOAUCIGmoaIDgTuS0ZF71+c9mYvPVNL0T02GQ\nE8Y3ExreQphx"); -// params.put("sign", sign); -// -// System.out.println(sign); - byte[] encryptedData = GatewayRSAUtils.decryptBase64("yx7jNj9DGS7AkH/sXo5IrwaGNCSUnAZFmjXwWovMBfvYGVQur1RELXUu4fqIMZZ/ck6CAA3ESOJCS+aCD0aVdA=="); - StringBuilder hexString = new StringBuilder(); - for (byte b : encryptedData) { - String hex = Integer.toHexString(0xFF & b); - if (hex.length() == 1) { - hexString.append('0'); - } - hexString.append(hex); - } - String hexStringResult = hexString.toString(); - System.out.println(hexStringResult); - System.out.println(BytesUtil.bytesToString(GatewayRSAUtils.decryptBase64("mgtJOOn5iZBvmQI3hzvaw2J4BiZUcXhLTsgLk8++Ig2iIZcPaky7V+ylkA+BAYxIBS5ovHXjRo4w26TTEq9k7Q=="))); - - } } -- Gitblit v1.9.3