From c8ff96af8647474b1d03f5f374bb18eb59f65987 Mon Sep 17 00:00:00 2001 From: jiazx0107@163.com <jiazx0107@163.com> Date: 星期日, 24 十二月 2023 12:30:09 +0800 Subject: [PATCH] 游仙协议解析-2 --- src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java | 267 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 263 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java b/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java index 0b225f2..5e1d840 100644 --- a/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java +++ b/src/main/java/com/fzzy/protocol/youxian0/analysis/AnalysisService.java @@ -1,22 +1,52 @@ package com.fzzy.protocol.youxian0.analysis; +import com.alibaba.fastjson.JSONObject; import com.fzzy.api.Constant; +import com.fzzy.api.data.GatewayDeviceType; import com.fzzy.api.utils.BytesUtil; +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.protocol.ProtocolUtils; +import com.fzzy.protocol.bhzn.cmd.ReMessageBuilder; +import com.fzzy.protocol.data.THDto; import com.fzzy.protocol.youxian0.ServiceUtils; +import com.fzzy.protocol.youxian0.data.GrainRoot; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; +import javax.annotation.Resource; import java.net.InetAddress; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; /** * 娓镐粰涓诲簱锛岃繑鍥炴姤鏂囪В鏋� */ +@Slf4j @Component(AnalysisService.BEAN_ID) public class AnalysisService { public static final String BEAN_ID = "youxian0.analysisService"; + + @Resource + private GatewayRemoteManager gatewayRemoteManager; + + + private static Map<String, GrainRoot> contextGrainRoot = new HashMap<>(); + + /** + * 鐢ㄤ簬瀛樻斁杩斿洖鐨勪粨娓╀粨婀夸俊鎭� + */ + public static Map<String, THDto> contextMapTH = new HashMap<>(); /** @@ -50,10 +80,28 @@ //绮儏杩斿洖 if (ServiceUtils.FUNCTION_66.equalsIgnoreCase(funId)) { - this.analysisGrain(device, msgId, strMsg); + this.analysisGrainStep1(device, msgId, strMsg); + } + + //娓╂箍搴﹁繑鍥� + if (ServiceUtils.FUNCTION_68.equalsIgnoreCase(funId)) { + this.analysisGrainTh(device, strMsg); } } + + private void analysisGrainTh(GatewayDevice device, String strMsg) { + THDto th = new THDto(); + + //TODO----->>> 寰呰В鏋愯皟鏁达紝鍏堢敤澶栭儴姘旇薄淇℃伅 + //绯荤粺姘旇薄绔欎俊鎭� + WeatherWebDto weather = WeatherWebDto.contextMap.get("default"); + th.setTempIn(Double.valueOf(weather.getTem()) - 1); + th.setHumidityIn(Double.valueOf(weather.getHumidity()) - 1); + + this.add2ThMap(device.getDepotIdSys(), th); + } + /** * 绮儏瑙f瀽 @@ -64,7 +112,14 @@ * @param device * @param msgId 鍛戒护ID */ - private void analysisGrain(GatewayDevice device, int msgId, String strMsg) { + private void analysisGrainStep1(GatewayDevice device, int msgId, String strMsg) { + + //鑾峰彇璇锋眰淇℃伅 + BaseReqData reqData = ProtocolUtils.getSyncReq(device.getDepotIdSys()); + if (null == reqData) { + log.error("---------娌℃湁鑾峰彇鍒拌姹備俊鎭紝涓嶆墽琛岃В鏋�------{}", device.getDeviceName()); + return; + } //鍙繚鐣欑伯鎯呬俊鎭� int start = 22 * 2; @@ -80,17 +135,221 @@ key = kyeNumBin.substring(4); int numValue = BytesUtil.hexToInt(BytesUtil.bin2Hex(key)); - //02 A4 BB BA BA B4 start = 2 * 2; String tempHex; + GrainRoot grainRoot = new GrainRoot(); + grainRoot.setKey(buildGrainRootKey(device.getDeviceSn(), msgId)); + grainRoot.setNum(msgId); + double point = 0; for (int i = 0; i < numValue; i++) { start = start + i * 2; tempHex = strMsg.substring(start, start + 2); //瀹為檯娓╁害锛濆瘑閽�*瀵嗛挜*37(婧㈠嚭涓烘棤绗﹀彿瀛楄妭)鍐嶅紓鎴栧姞瀵嗗悗鐨勬俯搴�/2銆� - + point = this.getGrainTemp(keyValue, tempHex); + log.debug("--------瑙f瀽鍚庣殑娓╁害鐐�----{}---{}", tempHex, point); + grainRoot.getPoints().add(point); } + this.add2GrainMap(grainRoot); + + //鍒ゆ柇鏄笉鏄渶鍚庝竴鍖呮暟鎹紝濡傛灉鏄渶鍚庝竴鍖呮墽琛岃В鏋� + String[] attCable = device.getCableRule().split("-"); + int cableZ = Integer.valueOf(attCable[0]); + int cableY = Integer.valueOf(attCable[1]); + int cableX = Integer.valueOf(attCable[2]); + if (grainRoot.getNum() == cableX) { + analysisGrainStep2(reqData, cableZ, cableY, cableX); + } + } + + /** + * 灏佽绮儏鐐逛綅 + * + * @param reqData + * @param cableZ + * @param cableY + * @param cableX + */ + private void analysisGrainStep2(BaseReqData reqData, int cableZ, int cableY, int cableX) { + + List<Double> points = new ArrayList<>(); + GrainRoot root; + for (int i = 1; i <= cableX; i++) { + root = this.getGrainRoot(buildGrainRootKey(reqData.getDevice().getDeviceSn(), i)); + + if (null == root || null == root.getPoints()) { + log.error("-----------瑙f瀽鑾峰彇鎵�鏈夌伯鎯呮娴嬬偣澶辫触锛屽彇娑堟墽琛�---------{}", reqData.getDevice().getDeviceName()); + return; + } + points.addAll(root.getPoints()); + } + + //鎵ц灏佽瑙f瀽 + analysisGrainStep3(reqData, cableZ, cableY, cableX, points); + } + + private void analysisGrainStep3(BaseReqData reqData, int cableZ, int cableY, int cableX, List<Double> points) { + + GatewayDevice device = reqData.getDevice(); + //鏁版嵁灏佽 + 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 < points.size(); i++) { + curTemp = points.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 + "")); + + 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 = this.getGrainTh(device.getDepotIdSys()); + + 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); + } + + private void add2GrainMap(GrainRoot grainRoot) { + contextGrainRoot.put(grainRoot.getKey(), grainRoot); + } + + private GrainRoot getGrainRoot(String key) { + return contextGrainRoot.get(key); + } + + private String buildGrainRootKey(String deviceSn, int num) { + return deviceSn + "_" + num; + } + + /** + * 璁$畻绮儏娓╁害 + * 瀹為檯娓╁害锛濆瘑閽�*瀵嗛挜*37(婧㈠嚭涓烘棤绗﹀彿瀛楄妭)鍐嶅紓鎴栧姞瀵嗗悗鐨勬俯搴�/2銆� + * 涓句緥璇存槑锛氱涓変釜瀛楄妭BB瀹為檯娓╁害鏄�5*5*37=039D鍙�9D^(0xBB)锛�38/2=19锛� + * + * @param keyValue + * @param tempHex + * @return + */ + private double getGrainTemp(int keyValue, String tempHex) { + int value = keyValue * keyValue * 37; + String valueHex = BytesUtil.intToHexStr(value); + + valueHex = valueHex.substring(2); + + int num1 = BytesUtil.hexToInt(valueHex); + int num2 = BytesUtil.hexToInt(tempHex); + + return (num1 ^ num2) / 2.0; + } + + private void add2ThMap(String depotIdSys, THDto th) { + String key = "TH_" + depotIdSys; + contextMapTH.put(key, th); + } + + private THDto getGrainTh(String depotIdSys) { + String key = "TH_" + depotIdSys; + return contextMapTH.get(key); } } -- Gitblit v1.9.3