From 328eba66ddc6fdf2f324b9cd04cd6acec9f642de Mon Sep 17 00:00:00 2001 From: jiazx0107@163.com <jiazx0107@163.com> Date: 星期六, 18 十一月 2023 13:48:49 +0800 Subject: [PATCH] 增加HTTP地磅协议实现 --- src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java | 12 +- src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java | 2 src/main/java/com/fzzy/protocol/ProtocolRunner.java | 2 src/main/java/com/fzzy/gateway/GatewayUtils.java | 21 ++++- src/main/java/com/fzzy/protocol/weightyh/GatewaySyncWeightImplHttp.java | 82 ++++++++++++++++++++ src/main/java/com/fzzy/protocol/weightyh/MessageConsumer.java | 5 + src/main/java/com/fzzy/gateway/GatewayRunner.java | 5 src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java | 49 +++++++++++- src/main/java/com/fzzy/gateway/api/GatewaySyncWeightService.java | 3 9 files changed, 161 insertions(+), 20 deletions(-) diff --git a/src/main/java/com/fzzy/gateway/GatewayRunner.java b/src/main/java/com/fzzy/gateway/GatewayRunner.java index 92c537b..a74979c 100644 --- a/src/main/java/com/fzzy/gateway/GatewayRunner.java +++ b/src/main/java/com/fzzy/gateway/GatewayRunner.java @@ -20,10 +20,9 @@ @Resource private ApiInitService apiInitService; - @Autowired + @Resource private MqttPublishService mqttPublishService; - - @Autowired + @Resource private GatewayTimerScheduled scheduled; diff --git a/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java b/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java index 379583d..147089f 100644 --- a/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java +++ b/src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java @@ -2,13 +2,16 @@ import com.alibaba.fastjson.JSON; import com.fzzy.api.data.ApiParam; +import com.fzzy.api.data.GatewayDeviceProtocol; import com.fzzy.api.entity.ApiConfs; import com.fzzy.api.service.*; import com.fzzy.api.utils.ContextUtil; import com.fzzy.api.utils.RedisUtil; import com.fzzy.gateway.api.GatewayRemoteManager; +import com.fzzy.gateway.data.BaseReqData; import com.fzzy.gateway.data.WeatherWebDto; import com.fzzy.gateway.entity.GatewayConf; +import com.fzzy.gateway.entity.GatewayDevice; import com.fzzy.gateway.service.GatewayConfService; import com.fzzy.gateway.util.GatewayHttpUtil; import lombok.extern.slf4j.Slf4j; @@ -19,6 +22,7 @@ import org.springframework.stereotype.Component; import javax.annotation.Resource; +import java.util.Collection; import java.util.Date; import java.util.List; @@ -39,7 +43,6 @@ @Resource private GatewayConfService confService; - @Resource private GatewayRemoteManager gatewayRemoteManager; @@ -48,13 +51,25 @@ * <p> * 鍥哄畾鏃堕棿锛氭瘡闂撮殧10鍒嗛挓鎵ц涓�娆� */ - @Scheduled(cron = "0 0/10 * * * ? ") + @Scheduled(cron = "0 0/10 * * * ?") public void scheduled() { //缃戝叧鐨勫績璺虫墽琛� doHeartbeat(); } + + /** + * <p> + * 姣忛棿闅�3绉掓墽琛屼竴娆′簨浠� + */ + @Scheduled(cron = "0/3 * * * * ?") + public void scheduled3() { + + //鎵цHTTP鍦扮璇锋眰 + exeHttpWeightReq(); + } + /** * 姣忛棿闅�30鍒嗛挓鎵ц涓�娆� @@ -73,7 +88,7 @@ url = url.replace("{appId}", DEFAULT_APP_ID).replace("{appsecret}", DEFAULT_APP_SECRET).replace("{cityid}", DEFAULT_CITYID); - log.debug("------姘旇薄URL---{}",url); + log.debug("------姘旇薄URL---{}", url); String result = GatewayHttpUtil.doGet(url, null); if (null == result) { @@ -89,7 +104,7 @@ WeatherWebDto.contextMap.put("default", dto); - log.info("===========================绯荤粺瀹氭椂鑾疯幏鍙栨皵璞′俊鎭�===={}==================",dto); + log.info("===========================绯荤粺瀹氭椂鑾疯幏鍙栨皵璞′俊鎭�===={}==================", dto); } catch (Exception e) { @@ -114,4 +129,30 @@ } } + + /** + * 鎵ц鍦扮HTTP璇锋眰鍗忚 + */ + private void exeHttpWeightReq() { + //鑾峰彇璁惧 + Collection<GatewayDevice> list = GatewayUtils.listDeviceWeight(); + + if (null == list || list.isEmpty()) return; + + BaseReqData reqData; + for (GatewayDevice device : list) { + + if (!GatewayDeviceProtocol.DEVICE_WEIGHT_HTTP.getCode().equals(device.getSyncProtocol())) { + continue; + } + + reqData = new BaseReqData(); + reqData.setDevice(device); + reqData.setDeviceName(device.getDeviceName()); + reqData.setProductId(device.getProductId()); + reqData.setDeviceId(device.getDeviceId()); + + gatewayRemoteManager.getSyncWeightService(device.getSyncProtocol()).syncWeightInfo(reqData); + } + } } diff --git a/src/main/java/com/fzzy/gateway/GatewayUtils.java b/src/main/java/com/fzzy/gateway/GatewayUtils.java index 7704a2f..0de33a5 100644 --- a/src/main/java/com/fzzy/gateway/GatewayUtils.java +++ b/src/main/java/com/fzzy/gateway/GatewayUtils.java @@ -1,12 +1,10 @@ package com.fzzy.gateway; +import com.fzzy.api.data.GatewayDeviceType; import com.fzzy.gateway.entity.GatewayDevice; import org.springframework.stereotype.Component; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; +import java.util.*; /** * 甯搁噺 @@ -31,10 +29,20 @@ */ public static Map<String, GatewayDevice> cacheMapDeviceSn = new HashMap<>(); + /** + * 璁惧缂撳瓨-鍙拡瀵� + */ + public static Map<String, GatewayDevice> cacheMapDeviceWeight = new HashMap<>(); + public static void add2Cache(GatewayDevice device) { cacheMapDeviceId.put(device.getDeviceId(), device); cacheMapDeviceSn.put(device.getDeviceSn(), device); + + //淇濆瓨鍦扮 + if (GatewayDeviceType.TYPE_01.getCode().equals(device.getType())) { + cacheMapDeviceWeight.put(device.getDeviceId(), device); + } } public static GatewayDevice getCacheByDeviceId(String deviceId) { @@ -79,4 +87,9 @@ if (null == value || "N".equals(value)) return false; return true; } + + + public static Collection<GatewayDevice> listDeviceWeight(){ + return cacheMapDeviceWeight.values(); + } } diff --git a/src/main/java/com/fzzy/gateway/api/GatewaySyncWeightService.java b/src/main/java/com/fzzy/gateway/api/GatewaySyncWeightService.java index f1ad3ba..1925279 100644 --- a/src/main/java/com/fzzy/gateway/api/GatewaySyncWeightService.java +++ b/src/main/java/com/fzzy/gateway/api/GatewaySyncWeightService.java @@ -2,6 +2,7 @@ import com.fzzy.gateway.data.BaseReqData; +import com.fzzy.gateway.data.BaseResp; import com.fzzy.gateway.hx2023.data.*; /** @@ -22,5 +23,5 @@ * * @return */ - public WeightInfo syncWeightInfo(BaseReqData reqData); + public BaseResp syncWeightInfo(BaseReqData reqData); } 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 e486592..13c0715 100644 --- a/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java +++ b/src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java @@ -120,7 +120,7 @@ String url = gatewayConf.getApiUrl() + "reserver/api/iot/equipment/heartbeat"; String jsonStr = GatewayHttpUtil.doGet(url, params); - log.info("---缃戝叧蹇冭烦鎺ュ彛-杩斿洖---{}", jsonStr); + // log.info("---缃戝叧蹇冭烦鎺ュ彛-杩斿洖---{}", jsonStr); } catch (Exception e) { log.error("------缃戝叧蹇冭烦鎺ュ彛--鎵ц澶辫触-----{}", e); diff --git a/src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java b/src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java index 8a3b796..3426d39 100644 --- a/src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java +++ b/src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java @@ -20,12 +20,13 @@ /** * 鎵цGET璇锋眰 + * * @param url * @param paramsMap * @return * @throws Exception */ - public static String doGet(String url,Map<String, String> paramsMap) throws Exception{ + public static String doGet(String url, Map<String, String> paramsMap) throws Exception { CloseableHttpClient client = HttpClients.createDefault(); String responseText = ""; CloseableHttpResponse response = null; @@ -33,7 +34,7 @@ String getUrl = url + "?"; if (paramsMap != null) { for (Map.Entry<String, String> param : paramsMap.entrySet()) { - // getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8") + "&"; + // getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8") + "&"; getUrl += param.getKey() + "=" + URLEncoder.encode(param.getValue(), "UTF-8") + "&"; } } @@ -47,18 +48,17 @@ HttpEntity entity = response.getEntity(); if (entity != null) { - responseText = EntityUtils.toString(entity,"UTF-8"); + responseText = EntityUtils.toString(entity, "UTF-8"); } } catch (Exception e) { - log.error("http request failed", e); + log.error("http request failed--{}", e.getMessage()); } finally { try { - response.close(); + if (null != response) response.close(); } catch (Exception e) { log.error("", e); } } - return responseText; } diff --git a/src/main/java/com/fzzy/protocol/ProtocolRunner.java b/src/main/java/com/fzzy/protocol/ProtocolRunner.java index c64169f..7ec0b23 100644 --- a/src/main/java/com/fzzy/protocol/ProtocolRunner.java +++ b/src/main/java/com/fzzy/protocol/ProtocolRunner.java @@ -17,6 +17,8 @@ public void run(String... args) throws Exception { YhScaleServerEngine.start(); + + } } diff --git a/src/main/java/com/fzzy/protocol/weightyh/GatewaySyncWeightImplHttp.java b/src/main/java/com/fzzy/protocol/weightyh/GatewaySyncWeightImplHttp.java new file mode 100644 index 0000000..de1317f --- /dev/null +++ b/src/main/java/com/fzzy/protocol/weightyh/GatewaySyncWeightImplHttp.java @@ -0,0 +1,82 @@ +package com.fzzy.protocol.weightyh; + +import com.alibaba.fastjson2.JSONObject; +import com.fzzy.api.data.GatewayDeviceProtocol; +import com.fzzy.gateway.api.GatewayRemoteManager; +import com.fzzy.gateway.api.GatewaySyncWeightService; +import com.fzzy.gateway.data.BaseReqData; +import com.fzzy.gateway.data.BaseResp; +import com.fzzy.gateway.entity.GatewayDevice; +import com.fzzy.gateway.util.GatewayHttpUtil; +import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang.StringUtils; +import org.springframework.stereotype.Component; + +import javax.annotation.Resource; +import java.util.HashMap; +import java.util.Map; + +/** + * HTTP鏂瑰紡鑾峰彇鍦扮淇℃伅 + * <p> + * 娉細褰撳墠鍗忚鍖呭惈JSON鍜孞SONP涓ょ鏀寔锛岃嚜鍔ㄨ瘑鍒� + */ +@Slf4j +@Component +public class GatewaySyncWeightImplHttp implements GatewaySyncWeightService { + + + @Resource + private GatewayRemoteManager gatewayRemoteManager; + + @Override + public String getWeightProtocol() { + return GatewayDeviceProtocol.DEVICE_WEIGHT_HTTP.getCode(); + } + + @Override + public BaseResp syncWeightInfo(BaseReqData reqData) { + GatewayDevice device = reqData.getDevice(); + + if (null == device) return new BaseResp(500, "娌℃湁鑾峰彇鍒拌澶囦俊鎭�"); + + if (StringUtils.isEmpty(device.getHttpUrl())) new BaseResp(500, "娌℃湁閰嶇疆HTTP璇锋眰鍦板潃"); + try { + + Map<String, String> paramsMap = new HashMap<>(); + paramsMap.put("time_", System.currentTimeMillis() + ""); + + String responseText = GatewayHttpUtil.doGet(device.getHttpUrl(), paramsMap); + + log.debug("--HTTP-WEIGHT-璇锋眰杩斿洖--{}", responseText); + + String respJson; + JSONObject resp; + //璇存槑鏄疛SONP鏍煎紡-jsonpCallback({"content":""}) + if (responseText.indexOf("jsonpCallback") >= 0) { + respJson = responseText.substring(responseText.indexOf("(") + 1, responseText.length() - 1); + resp = JSONObject.parseObject(respJson); + resp.put("code", 200); + } else { + respJson = responseText; + resp = JSONObject.parseObject(respJson); + } + + if (200 == (Integer) resp.get("code")) { + if (null == resp.get("content") || "".equals(resp.get("content"))) { + reqData.setWeight(0.0); + } else { + reqData.setWeight((Double) resp.get("content")); + } + } + + //鎵ц鎺ㄩ�� + if (reqData.getWeight() > 0) { + gatewayRemoteManager.getDeviceReportService(device.getPushProtocol()).reportWeightData(reqData); + } + } catch (Exception e) { + log.error("--------------鍦扮-HTTP鍗忚鎵ц寮傚父----{}", e.getMessage()); + } + return new BaseResp(500, "鍚庡彴鎵ц澶辫触"); + } +} diff --git a/src/main/java/com/fzzy/protocol/weightyh/MessageConsumer.java b/src/main/java/com/fzzy/protocol/weightyh/MessageConsumer.java index b41d6c9..a0253fc 100644 --- a/src/main/java/com/fzzy/protocol/weightyh/MessageConsumer.java +++ b/src/main/java/com/fzzy/protocol/weightyh/MessageConsumer.java @@ -82,7 +82,10 @@ reqData.setProductId(device.getProductId()); reqData.setDeviceName(device.getDeviceName()); reqData.setWeight(weigh); - gatewayRemoteManager.getDeviceReportService(device.getPushProtocol()).reportWeightData(reqData); + + if (weigh > 0) { + gatewayRemoteManager.getDeviceReportService(device.getPushProtocol()).reportWeightData(reqData); + } } /** -- Gitblit v1.9.3