package com.fzzy.protocol.wujia.analysis; import com.alibaba.fastjson.JSONObject; 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 lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.net.InetAddress; import java.util.ArrayList; import java.util.List; @Slf4j @Component(AnalysisService.BEAN_ID) public class AnalysisService { public static final String BEAN_ID = "wujia.analysisService"; @Resource private GatewayRemoteManager gatewayRemoteManager; /** * 粮情接线 * * @param address * @param port * @param strMsg */ public void analysis(InetAddress address, int port, String strMsg) { if (strMsg.startsWith("FE")) { //DO NOTHING } else { this.analysisGrainStep1(address, strMsg); } } /** * 粮情解析 * X1 data1L data1H data2L data2H data3L data3H………datanL datanH DATA1H DATA1L DATA2H DATA2L AA BB CC * X1---发送的序号 AA BB CC----结束标志 * DATA1H DATA1L----仓内湿度 * DATA2H DATA2L----仓内温度 * data1L data1H-----18B20返回温度 * 注意:粮温返回数据高位在后,低位在前,转换为十进制后乘以0.0625,得到实际温度值 * 仓温湿度高位在前,低位在后,转换为十进制后乘以0.1得到实际温湿度值 * 02 * 89 00 7A 00 E2 00 8C 00 7D 00 E9 00 99 00 82 00 EC 00 87 00 87 00 ED 00 83 00 77 00 EF 00 92 00 7D 00 08 01 87 00 77 00 03 01 7F 00 7A 00 F6 00 * 9B 00 5D 00 D1 00 * 84 00 65 00 D4 00 8D 00 6D 00 D6 00 89 00 6E 00 DD 00 87 00 61 00 CA 00 81 00 6B 00 DA 00 81 00 68 00 E2 00 86 00 69 00 D0 00 * 87 00 6D 00 E4 00 88 00 71 00 07 01 8E 00 76 00 13 01 82 00 75 00 EB 00 86 00 6D 00 DB 00 83 00 71 00 F1 00 87 00 7B 00 03 01 79 00 7D 00 F7 00 * 02 C1 00 6D AA BB CC * @param strMsg */ private void analysisGrainStep1(InetAddress address, String strMsg) { strMsg = strMsg.substring(0,strMsg.length()-6); log.info("完整的数据体:"+strMsg); int start = 0; int len = 1 * 2; String tag = strMsg.substring(start, start + 2); int depotIdSys = BytesUtil.hexToInt(tag); BaseReqData reqData = ProtocolUtils.getSyncReq(depotIdSys + ""); if (null == reqData) { log.error("---------没有获取到请求信息,不执行解析------{}", address); return; } //仓内湿度 start = 1 * 2; len = 2 * 2; tag = strMsg.substring(strMsg.length()-8,strMsg.length() -4); double hIn = BytesUtil.hexToInt(tag) * 0.1; //仓内温度 start = 3 * 2; len = 2 * 2; tag = strMsg.substring(strMsg.length()-4,strMsg.length() ); double tIn = BytesUtil.hexToInt(tag) * 0.1; GatewayDevice device = reqData.getDevice(); String[] attCable = device.getCableRule().split("-"); int cableZ = Integer.valueOf(attCable[0]); int cableY = Integer.valueOf(attCable[1]); int cableX = Integer.valueOf(attCable[2]); log.info("z={},x={},y={}", cableZ, cableX, cableY); int sumNum = cableZ * cableY * cableX; //粮温信息 start = 2; len = 2 * 2; List points = new ArrayList<>(); double temp = 0.0; for (int i = 0; i < sumNum; i++) { start = i * 2 * 2 +2; tag = strMsg.substring(start, start + len); temp = BytesUtil.hexToInt(BytesUtil.tran_LH(tag)) * 0.0625; temp = NumberUtil.keepPrecision(temp, 1); points.add(temp); } //执行封装解析 analysisGrainStep2(reqData, cableZ, cableY, cableX, points, hIn, tIn); } private void analysisGrainStep2(BaseReqData reqData, int cableZ, int cableY, int cableX, List points, double hIn, double tIn) { 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 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 ths = new ArrayList<>(); ths.add(new GrainTH(tIn + "", hIn + "", "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); } }