src/main/java/com/fzzy/gateway/GatewayRunner.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/gateway/GatewayTimerScheduled.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/gateway/GatewayUtils.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/gateway/api/GatewaySyncWeightService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/gateway/hx2023/service/HxGatewayRemoteServiceImpl.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/gateway/util/GatewayHttpUtil.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/ProtocolRunner.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/weightyh/GatewaySyncWeightImplHttp.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/weightyh/MessageConsumer.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
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; 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); } } } 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(); } } 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); } 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); 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; } src/main/java/com/fzzy/protocol/ProtocolRunner.java
@@ -17,6 +17,8 @@ public void run(String... args) throws Exception { YhScaleServerEngine.start(); } } 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åJSONPä¸¤ç§æ¯æï¼èªå¨è¯å« */ @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; //è¯´ææ¯JSONPæ ¼å¼-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, "åå°æ§è¡å¤±è´¥"); } } 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); } } /**