package com.fzzy.protocol.fzzy.analysis; import com.alibaba.fastjson.JSONObject; import com.fzzy.api.data.ApiCommonDevice; import com.fzzy.api.data.GatewayDeviceType; 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.BaseResp; 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.fzzy.builder.SimpleCommandBuilder; import com.fzzy.protocol.fzzy.cmd.BaseRemoteImpl; import com.fzzy.protocol.fzzy.data.ReMessage; import com.fzzy.protocol.fzzy.data.SendMessage; import com.fzzy.protocol.fzzy.resp.Response2102; import com.fzzy.protocol.fzzy.server.ServerUtils; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.ArrayList; import java.util.List; /** * 粮情解析 * * @author Andy */ @Slf4j @Component(AnalysisGrain.BEAN_ID) public class AnalysisGrain extends BaseRemoteImpl { @Resource private GatewayRemoteManager gatewayRemoteManager; public static final String BEAN_ID = "fzzy.analysisGrain"; public static int ERROR_CHECK_TAG = -1000;//粮温,气体,虫害,故障值 public static int ERROR_CHECK_TAG2 = -2000;//传感器故障或者不在线 public void analysis2101(ReMessage reMessage, ApiCommonDevice ser) { //DO NOTHING } /** * 粮情返回信息解析 * * @param reMessage * @param ser */ public void analysis2102(ReMessage reMessage, ApiCommonDevice ser) { try { //返回控制柜信息 returnMsg(ser); Response2102 response = JSONObject.parseObject(reMessage.getContent(), Response2102.class); //粮情解析 log.info("控制柜--->平台,控制柜返回粮情检测结果--{}", response); //粮情封装和处理 if (StringUtils.isEmpty(response.getLayerPerCircle())) { buildBizInfo1(response); } else { buildBizInfo2(response); } } catch (Exception e) { log.error("控制柜--->平台粮情信息异常-{}", e); } } private void buildBizInfo2(Response2102 response) { //TODO 筒仓的封装解析待处理 } //信息调整封装 private void buildBizInfo1(Response2102 response) { //获取请求信息 BaseReqData reqData = ProtocolUtils.getSyncReq(response.getHouseId()); log.info("--粮情封装解析,获取到请求命令参数----"); if (null == reqData) { log.error("--粮情封装解析,未获取到请求相关参数----"); return; } ProtocolUtils.delSyncReq(response.getHouseId()); log.info(reqData.toString()); int cableZ = Integer.valueOf(response.getLay()); int cableY = Integer.valueOf(response.getRow()); int cableX = Integer.valueOf(response.getCol()); 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(); outPut.setAvgTemperature(response.getTAvg() + ""); outPut.setMinTemperature(response.getTMin() + ""); outPut.setMaxTemperature(response.getTMax() + ""); List temperature = new ArrayList<>(); //根号 int cableNum = 1, position = 0; double curTemp; String[] attr = response.getPoints().split(","); int x = 0, y = 0, z = 0; for (int i = 0; i < attr.length; i++) { curTemp = Double.valueOf(attr[i]); if (curTemp < -900) { curTemp = ERROR_CHECK_TAG; } //数据优化 if (curTemp > 40) { curTemp = response.getTAvg(); } 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 + "")); } outPut.setTemperature(temperature); List ths = new ArrayList<>(); ths.add(new GrainTH(response.getTIn()!=null?response.getTIn()+"":"",response.getHIn()!=null?response.getHIn()+"":"","1")); outPut.setTemperatureAndhumidity(ths); //grain.setOutput(JSONObject.toJSONString(outPut)); grain.setOutput(com.alibaba.fastjson2.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().replaceAll("%","")); 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,grain); } private void doPushGrain(BaseReqData reqData,GrainData grainData) { GatewayDeviceReportService reportService = gatewayRemoteManager.getDeviceReportService(reqData.getDevice().getPushProtocol()); if (null == reportService) { log.error("------------粮情推送失败,系统不存在当前协议执行类----{}", reqData.getDevice().getDeviceName()); return; } reportService.reportGrainData(reqData); reqData.setData(reportService.grainData2GatewayApiInfoKafka(grainData,reqData.getDevice()).getData()); reportService.reportGrainDataByKafka(reqData); } private void returnMsg(ApiCommonDevice ser) { //先回复控制柜 SimpleCommandBuilder commandBuilder = SimpleCommandBuilder.getInstance(); SendMessage message = commandBuilder.buildMessage(ser, ServerUtils.FUNCTION_2102); send(message); } }