src/main/java/com/fzzy/protocol/ProtocolScheduled.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/data/THDto.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisTH.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder1128.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/zldz/server/HeartbeatProviderImpl.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/zldz/server/SessionListener.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
src/main/java/com/fzzy/protocol/zldz/service/ZldzGatewayGrainService.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
src/main/java/com/fzzy/protocol/ProtocolScheduled.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,85 @@ package com.fzzy.protocol; import com.fzzy.api.data.GatewayDeviceProtocol; import com.fzzy.gateway.GatewayUtils; import com.fzzy.gateway.data.BaseReqData; import com.fzzy.gateway.entity.GatewayDevice; import com.fzzy.protocol.zldz.service.ZldzGatewayGrainService; import lombok.extern.slf4j.Slf4j; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.Collection; import java.util.Date; /** * åè®®ç¸å ³ç宿¶å¨ï¼å ¨å±å®æ¶å¨ï¼å 嫿æçåè®®ï¼é»è¾å¤çæ¯å¦æ§è¡ */ @Slf4j @Component(ProtocolScheduled.BEAN_ID) public class ProtocolScheduled { public static final String BEAN_ID = "protocol.Scheduled"; @Resource private ZldzGatewayGrainService zldzGatewayGrainService; /** * ä»6-19ç¹æ¯å°æ¶æ§è¡ä¸æ¬¡ * cron = "0 0 6-19 * * ? *" */ @Scheduled(cron = "0 0 6-19 * * ? ") public void scheduled() { Date cur = new Date(); log.info("--------------ç³»ç»æ§è¡å®æ¶æ°æ®åæ¥æä½--------------"); //æ£æ¥çµååè®®-宿¶ä»»å¡ this.scheduledZldz(cur); } /** * æ£æ¥çµå宿¶ä»»å¡ * * @param cur */ private void scheduledZldz(Date cur) { //è·å设å¤å表 Collection<GatewayDevice> list = GatewayUtils.allCacheDevice(); try { BaseReqData reqData; for (GatewayDevice device : list) { if (null == device.getSyncProtocol()) continue; if (!GatewayDeviceProtocol.GRAIN_FZZY_ZLDZ_WEB.getCode().equals(device.getSyncProtocol())) { continue; } reqData = new BaseReqData(); reqData.setDeviceId(device.getDeviceId()); reqData.setProductId(device.getProductId()); reqData.setDeviceName(device.getDeviceName()); reqData.setDevice(device); zldzGatewayGrainService.syncGrainTh(reqData); Thread.sleep(500); } } catch (Exception e) { } } } src/main/java/com/fzzy/protocol/data/THDto.java
@@ -8,32 +8,31 @@ /** * ä¸å¡æ°æ®ï¼ç²®é£æ¸©åº¦æ¹¿åº¦ç»è®¡ä¿¡æ¯ * * @author Andy * * @author Andy */ @Data public class THDto implements Serializable { private static final long serialVersionUID = 1L; private String serId;//æå±åæº private static final long serialVersionUID = 1L; private String thAddr = "1";//æå±éé private String serId;//æå±åæº @PropertyDef(label = "æå±ä»åº", description = "ä»åºç¼å·") private String depotId; private String thAddr = "1";//æå±éé @PropertyDef(label = "ä»å 温度", description = "åä½â") private Double tempIn; @PropertyDef(label = "æå±ä»åº", description = "ä»åºç¼å·") private String depotId; @PropertyDef(label = "ä»å 湿度", description = "åä½%RH") private Double humidityIn; @PropertyDef(label = "æ¥æ¶æ¶é´") private Date receiveDate = new Date(); @PropertyDef(label = "ä»å 温度", description = "åä½â") private Double tempIn; private String remark; @PropertyDef(label = "ä»å 湿度", description = "åä½%RH") private Double humidityIn; @PropertyDef(label = "æ¥æ¶æ¶é´") private Date receiveDate = new Date(); private String remark; } src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java
@@ -1,11 +1,22 @@ package com.fzzy.protocol.zldz.analysis; import com.alibaba.fastjson.JSONObject; import com.fzzy.api.data.GatewayDeviceType; import com.fzzy.api.utils.BytesUtil; import com.fzzy.api.utils.ContextUtil; import com.fzzy.api.utils.NumberUtil; import com.fzzy.gateway.GatewayUtils; import com.fzzy.gateway.api.GatewayDeviceReportService; import com.fzzy.gateway.api.GatewayRemoteManager; import com.fzzy.gateway.data.BaseReqData; import com.fzzy.gateway.data.WeatherWebDto; import com.fzzy.gateway.entity.GatewayDevice; import com.fzzy.gateway.hx2023.ScConstant; import com.fzzy.gateway.hx2023.data.*; import com.fzzy.io.request.BaseRequest; import com.fzzy.protocol.ProtocolUtils; import com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder; import com.fzzy.protocol.data.THDto; import com.fzzy.protocol.zldz.data.ReMessage; import com.fzzy.protocol.zldz.service.ZldzGatewayGrainService; import lombok.extern.slf4j.Slf4j; @@ -33,6 +44,10 @@ @Resource private ZldzGatewayGrainService zldzGatewayGrainService; @Resource private GatewayRemoteManager gatewayRemoteManager; public static Map<String, Map<String, String>> contextMap = new HashMap<>(); @@ -249,8 +264,6 @@ // å¤ç¨å¼ if (tempValue == ERROR_CHECK_TAG) { tempValue = ProtocolUtils.ERROR_TEMP; //éªè¯æ¯ä¸æ¯é¥å½¢ä»è¡¥å¿å¼ curLay = (i % layMax) + 1; curRoot = (i / layMax) + 1; @@ -298,9 +311,7 @@ * @param packetMap * @param sumPackets */ private void analysisStep1(ReMessage msg, GatewayDevice device, BaseReqData reqData, Map<String, String> packetMap, int sumPackets) { private void analysisStep1(ReMessage msg, GatewayDevice device, BaseReqData reqData, Map<String, String> packetMap, int sumPackets) { // è·å宿´çç²®æ å ä¿¡æ¯ String strPoints = ""; for (int i = 1; i <= sumPackets; i++) { @@ -343,7 +354,7 @@ // å°éåè§£ææåæ æ°æ® addPoint1(temps, msg, device, reqData); addPoint1(temps, device, reqData, cableX, cableY, cableZ); } private String buildCurKey(ReMessage msg, int curPacket) { @@ -360,11 +371,131 @@ * @param temps * @throws Exception */ private void addPoint1(List<Double> temps, ReMessage msg, GatewayDevice device, BaseReqData reqData) { //TODO private void addPoint1(List<Double> temps, GatewayDevice device, BaseReqData reqData, int cableX, int cableY, int cableZ) { //æ°æ®å°è£ GrainData grain = new GrainData(); grain.setMessageId(ScConstant.getMessageId()); grain.setDeviceId(device.getDeviceId()); grain.setTimestamp(System.currentTimeMillis() + ""); ClientHeaders headers = new ClientHeaders(); headers.setDeviceName(device.getDeviceName()); headers.setProductId(device.getProductId()); headers.setOrgId(device.getOrgId()); headers.setMsgId(reqData.getMessageId()); grain.setHeaders(headers); GrainOutPut outPut = new GrainOutPut(); double max = ReMessageBuilder.MAX_TEMP, min = ReMessageBuilder.MIN_TEMP, sumT = 0.0, sumNum = cableX * cableY * cableZ; List<GrainTemp> temperature = new ArrayList<>(); //æ ¹å· int cableNum = 1, position = 0; double curTemp; int x = 0, y = 0, z = 0; for (int i = 0; i < temps.size(); i++) { curTemp = temps.get(i); position = i; z = i % cableZ + 1; x = i / (cableZ * cableY); y = x * (cableZ * cableY); y = (i - y) / cableZ; //æ ¹å· cableNum = (i / cableZ) + 1; temperature.add(new GrainTemp(cableNum + "", z + "", curTemp + "", position + "")); //æ±æå¤§æå°å¼ if (curTemp < -900) { sumNum--; } else { sumT += curTemp; if (curTemp > max) { max = curTemp; } if (curTemp < min) { min = curTemp; } } } if (sumNum == 0) { sumNum = 1; log.warn("---å½åç²®æ ééå¼å¸¸--"); } //è¿æ»¤æ¯è¾ç¨çæå¤§æå°å¼ if (max == ReMessageBuilder.MAX_TEMP) { max = 0.0; } if (min == ReMessageBuilder.MIN_TEMP) { min = 0.0; } outPut.setTemperature(temperature); outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + ""); outPut.setMinTemperature(min + ""); outPut.setMaxTemperature(min + ""); List<GrainTH> ths = new ArrayList<>(); THDto thDto = ProtocolUtils.getCacheTh(device.getDeviceId()); if (null == thDto) thDto = new THDto(); ths.add(new GrainTH(thDto.getTempIn() != null ? thDto.getTempIn() + "" : "", thDto.getHumidityIn() != null ? thDto.getHumidityIn() + "" : "", "1")); outPut.setTemperatureAndhumidity(ths); grain.setOutput(JSONObject.toJSONString(outPut)); GatewayDevice gatewayDeviceWeather = GatewayUtils.getCacheByDeviceTypeOne(GatewayDeviceType.TYPE_09.getCode()); //ç³»ç»æ°è±¡ç«ä¿¡æ¯ WeatherWebDto weather = WeatherWebDto.contextMap.get("default"); //æ°è±¡ä¿¡æ¯ GrainWeather weatherStation = new GrainWeather(); weatherStation.setMessageId(ScConstant.getMessageId()); weatherStation.setMessgeId(weatherStation.getMessageId()); if (null != gatewayDeviceWeather) { weatherStation.setId(gatewayDeviceWeather.getDeviceId()); } else { weatherStation.setId(device.getDeviceId()); } weatherStation.setAirPressure(weather.getPressure()); weatherStation.setHumidity(weather.getHumidity()); weatherStation.setPm(weather.getAir_pm25()); weatherStation.setRadiation("0"); weatherStation.setRainfallAmount(weather.getWea()); weatherStation.setTemperature(weather.getTem()); weatherStation.setWindDirection(weather.getWin()); weatherStation.setWindPower(weather.getWin_meter()); weatherStation.setWindSpeed(weather.getWin_speed()); grain.setWeatherStation(JSONObject.toJSONString(weatherStation)); //å°è£ å¥½çæ°æ® log.info("---ç²®æ æºæ¢°å°è£ 宿----å¼å§æ§è¡æ¨é"); reqData.setData(JSONObject.toJSONString(grain)); doPushGrain(reqData); } private void doPushGrain(BaseReqData reqData) { GatewayDeviceReportService reportService = gatewayRemoteManager.getDeviceReportService(reqData.getDevice().getPushProtocol()); if (null == reportService) { log.error("------------ç²®æ æ¨é失败ï¼ç³»ç»ä¸åå¨å½ååè®®æ§è¡ç±»----{}", reqData.getDevice().getDeviceName()); return; } reportService.reportGrainData(reqData); } /** * åçä»è§£ææ¥éª¤ * src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisTH.java
@@ -27,11 +27,11 @@ * è§£æä»åºæ¸©æ¹¿åº¦ä¿¡æ¯ï¼å°ä¿¡æ¯æ¾å¨ç¼åä¸ï¼ä»¥ä¾¿ç²®æ ä½¿ç¨ * * @param msg * @param ser * @param device */ public void analysis8828(ReMessage msg, GatewayDevice ser) { public void analysis8828(ReMessage msg, GatewayDevice device) { log.debug("-----------analysis8828------------{}.{}", ser.getIp(), ser.getPort()); log.debug("-----------analysis8828------------{}.{}", device.getIp(), device.getPort()); String content = msg.getBody().getContent(); @@ -55,8 +55,8 @@ t = BytesUtil.hexToInt("FF" + temp); } if (t == ERROR_TH_TAG) { log.error("{}温湿度解æå¼å¸¸ï¼åå ï¼æ²¡ææ£æµå°ä¼ æå¨", ser.getDeviceName()); th.setRemark(ser.getDeviceName() + "温湿度解æå¼å¸¸ï¼åå ï¼æ²¡ææ£æµå°ä¼ æå¨"); log.error("{}温湿度解æå¼å¸¸ï¼åå ï¼æ²¡ææ£æµå°ä¼ æå¨", device.getDeviceName()); th.setRemark(device.getDeviceName() + "温湿度解æå¼å¸¸ï¼åå ï¼æ²¡ææ£æµå°ä¼ æå¨"); } else { th.setTempIn(Double.valueOf(t)); } @@ -70,17 +70,17 @@ h = BytesUtil.hexToInt("FF" + temp); } if (h == ERROR_TH_TAG) { log.error("{}温湿度解æå¼å¸¸ï¼åå ï¼æ²¡ææ£æµå°ä¼ æå¨", ser.getDeviceName()); th.setRemark(ser.getDeviceName() + "温湿度解æå¼å¸¸ï¼åå ï¼æ²¡ææ£æµå°ä¼ æå¨"); log.error("{}温湿度解æå¼å¸¸ï¼åå ï¼æ²¡ææ£æµå°ä¼ æå¨", device.getDeviceName()); th.setRemark(device.getDeviceName() + "温湿度解æå¼å¸¸ï¼åå ï¼æ²¡ææ£æµå°ä¼ æå¨"); } else { th.setHumidityIn(Double.valueOf(h)); } th.setSerId(ser.getId()); th.setSerId(device.getDeviceId()); th.setThAddr(thAddr + ""); //åæ¾ç¼å ProtocolUtils.addTh2Map(ser.getDeviceId(), th); ProtocolUtils.addTh2Map(device.getDeviceId(), th); log.info("仿¸©ä»æ¹¿è§£æå®æ={}", th); } src/main/java/com/fzzy/protocol/zldz/cmd/CommandBuilder1128.java
¶Ô±ÈÐÂÎļþ @@ -0,0 +1,78 @@ package com.fzzy.protocol.zldz.cmd; import com.fzzy.api.data.ApiCommonDevice; import com.fzzy.api.utils.BytesUtil; import com.fzzy.gateway.data.BaseReqData; import com.fzzy.gateway.entity.GatewayDevice; import com.fzzy.protocol.zldz.data.Message; import com.fzzy.protocol.zldz.data.MessageBody; import com.fzzy.protocol.zldz.server.ZldzServerUtil; /** * 仿¸©ä»æ¹¿å½ä»¤ * * @author Andy */ public class CommandBuilder1128 extends CommandBuilder { private final static CommandBuilder1128 instance = new CommandBuilder1128(); private CommandBuilder1128() { } public static CommandBuilder1128 getInstance() { return instance; } public Message buildMessage(BaseReqData reqData, ApiCommonDevice commonDevice) { Message message = new Message(); message.setBegin(ZldzServerUtil.MSG_START); // è°æ´Message头é¨ä¿¡æ¯ buildHeader(message, reqData, commonDevice); //å å¤´æ ¡éª buildCrc8(message); // åå»ºæ¶æ¯ä½ buildBody(message, reqData); // æ·»å æ ¡éª buildCrc16(message); message.setEnd(ZldzServerUtil.MSG_END); // çææ´ä½å½ä»¤ä¿¡æ¯åBYteä¿¡æ¯ build(message); return message; } public void buildHeader(Message message, BaseReqData reqData, ApiCommonDevice commonDevice) { GatewayDevice device = reqData.getDevice(); String hexCompanyId = BytesUtil.getTargetId(commonDevice.getOrgId(), true); hexCompanyId = BytesUtil.tran_LH(hexCompanyId); message.setSourceId(hexCompanyId); message.setSendId(hexCompanyId); message.setCompanyId(hexCompanyId); message.setTargetId(BytesUtil.tran_LH(BytesUtil.getTargetId(device.getDeviceSn(), true))); message.setMessageType(BytesUtil.tran_LH(ZldzServerUtil.MSG_TYPE_1128)); } public void buildBody(Message message, BaseReqData reqData) { GatewayDevice device = reqData.getDevice(); MessageBody body = new MessageBody(); body.setLength(2); body.setContent(BytesUtil.tran_LH(BytesUtil.getTargetId(device.getThAddr(), true))); message.setBody(body); } } src/main/java/com/fzzy/protocol/zldz/server/HeartbeatProviderImpl.java
@@ -17,7 +17,7 @@ @Override public byte[] provide(IoSession session) { log.debug("========ç³»ç»å¼å§æ§è¡å¿è·³=========={}---{}", session.getCompanyId(), session.getBusinessKey()); log.debug("========ç³»ç»å¼å§æ§è¡å¿è·³=========-{}", session.getBusinessKey()); return null; } src/main/java/com/fzzy/protocol/zldz/server/SessionListener.java
@@ -35,21 +35,25 @@ Constant.updateCacheOnline(session.getAddress(), session.getPort()); /***********************设å¤ä¸çº¿å主å¨è·åé 置信æ¯******************/ try { /***********************设å¤ä¸çº¿å主å¨è·åé 置信æ¯******************/ BaseRequest request = new BaseRequest(); request.setIp(session.getAddress()); request.setPort(session.getPort()); request.setCompanyId(ZldzServerUtil.DEFAULT_COMPANY); // Step1 çæå¨çº¿æµè¯å½ä»¤ Message message = OnLineCommandBuilder.getInstance().buildMessage(request); // åéå½ä»¤ InvokeResult result = session.invoke(message.getByteMsg()); //妿å½ä»¤åé失败ï¼ç´æ¥è¸¢æè¿æ¥ if (!InvokeResult.SUCCESS.getCode().equals(result.getCode())) { session.destroy(); BaseRequest request = new BaseRequest(); request.setIp(session.getAddress()); request.setPort(session.getPort()); request.setCompanyId(ZldzServerUtil.DEFAULT_COMPANY); // Step1 çæå¨çº¿æµè¯å½ä»¤ Message message = OnLineCommandBuilder.getInstance().buildMessage(request); // åéå½ä»¤ InvokeResult result = session.invoke(message.getByteMsg()); //妿å½ä»¤åé失败ï¼ç´æ¥è¸¢æè¿æ¥ if (!InvokeResult.SUCCESS.getCode().equals(result.getCode())) { session.destroy(); } log.info("å¹³å°------->>åæº---设å¤ä¸çº¿ï¼èªå¨åé设å¤é 置信æ¯è¯·æ±å½ä»¤--{}", message); } catch (Exception e) { log.error("å¹³å°------->>åæº---设å¤ä¸çº¿ï¼èªå¨åé设å¤é 置信æ¯è¯·æ±å½ä»¤å¤±è´¥--{}", session.getAddress()); } log.info("å¹³å°------->>åæº---设å¤ä¸çº¿ï¼èªå¨åé设å¤é 置信æ¯è¯·æ±å½ä»¤--{}",message); } /** @@ -69,8 +73,9 @@ if (null == gatewayDeviceService) { gatewayDeviceService = SpringUtil.getBean(GatewayDeviceService.class); } gatewayDeviceService.OfflineByCommonDevice(commonDevice); //踢åºè¿æ¥ session.destroy(); } } src/main/java/com/fzzy/protocol/zldz/service/ZldzGatewayGrainService.java
@@ -92,8 +92,55 @@ @Override public BaseResp syncGrainTh(BaseReqData reqData) { BaseResp resp = new BaseResp(); GatewayDevice device = reqData.getDevice(); //è·åè¿æ¥ä¸ç设å¤ä¿¡æ¯ ApiCommonDevice apiCommonDevice = Constant.getCommonDeviceCache(device.getDeviceSn()); if (null == apiCommonDevice) { resp.setCode(500); resp.setMsg("ç³»ç»æªè·åå°ä¸è¡è¿æ¥è®¾å¤ä¿¡æ¯ï¼æ æ³æ§è¡"); log.error("----------------ç³»ç»æªè·åå°ä¸è¡è¿æ¥è®¾å¤ä¿¡æ¯,æ æ³æ§è¡---------"); return resp; } if (Constant.YN_N.equals(apiCommonDevice.getStatus())) { resp.setCode(500); resp.setMsg("ä¸è¡è®¾å¤ä¸å¨çº¿---æ æ³æ§è¡----" + device.getDeviceName()); log.error("ä¸è¡è®¾å¤ä¸å¨çº¿---æ æ³æ§è¡----" + device.getDeviceName()); return resp; } device.setIp(apiCommonDevice.getIp()); device.setPort(apiCommonDevice.getPort()); reqData.setDevice(device); try { //çææ¸©åº¦å½ä»¤ Message message = CommandBuilder1128.getInstance().getInstance().buildMessage(reqData, apiCommonDevice); log.error("å¹³å°------>>>>æ§å¶æï¼å鿏©æ¹¿åº¦æ£æµå½ä»¤-{}", message); // åéå½ä»¤ InvokeResult result = ZldzServerEngine.push(apiCommonDevice.getIp(), apiCommonDevice.getPort(), message.getByteMsg()); // å°è£ è¿åä¿¡æ¯ if (!InvokeResult.SUCCESS.getCode().equals(result.getCode())) { log.error("å¹³å°------>>>>æ§å¶æï¼å鿏©æ¹¿åº¦æ£æµå½ä»¤-失败{}", result.getMessage()); resp.setCode(500); resp.setMsg("å¹³å°------>>>>æ§å¶æï¼å鿏©æ¹¿åº¦æ£æµå½ä»¤-失败ï¼" + result.getMessage()); } } catch (Exception e) { log.error("æ¸©æ¹¿åº¦æ£æµå¼å¸¸ï¼{}", e); resp.setCode(500); resp.setMsg("å¹³å°------>>>>æ§å¶æï¼å鿏©æ¹¿åº¦æ£æµå½ä»¤ï¼" + e.getMessage()); return resp; } return resp; }