package com.fzzy.protocol.fzzydatabase; import com.alibaba.fastjson.JSONObject; import com.fzzy.api.data.GatewayDeviceProtocol; import com.fzzy.api.data.GatewayDeviceType; import com.fzzy.api.utils.BytesUtil; import com.fzzy.api.utils.DateUtil; import com.fzzy.api.utils.NumberUtil; import com.fzzy.async.fzzy40.Fzzy40CommonService; import com.fzzy.data.ConfigData; import com.fzzy.gateway.GatewayUtils; import com.fzzy.gateway.api.GatewayDeviceReportService; import com.fzzy.gateway.api.GatewayRemoteManager; import com.fzzy.gateway.api.GatewaySyncGranService; import com.fzzy.gateway.data.BaseReqData; import com.fzzy.gateway.data.BaseResp; import com.fzzy.gateway.data.GrainCableData; 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.gateway.service.GatewayConfService; import com.fzzy.gateway.service.GatewayDeviceTestPR; import com.fzzy.protocol.ProtocolUtils; import com.fzzy.protocol.bhzn.cmd.ReMessageBuilder; import com.fzzy.protocol.data.THDto; import com.fzzy.protocol.youxian1.client.ClientEngine; import com.ld.io.api.InvokeResult; import io.netty.channel.Channel; import com.fzzy.async.fzzy40.entity.Fz40Grain; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.ArrayList; import java.util.Date; import java.util.List; /** * 游仙分库协议-粮情采集入口 */ @Slf4j @Component public class FzzyDatabaseGatewayGrainService implements GatewaySyncGranService { @Resource private GatewayConfService confService; @Resource private GatewayRemoteManager gatewayRemoteManager; @Resource private ConfigData configData; @Resource private Fzzy40CommonService fzzy40CommonService; @Resource private GatewayDeviceTestPR gatewayDeviceTestPR; @Override public String getGrainProtocol() { return GatewayDeviceProtocol.GRAIN_FZZY_DATABASE.getCode(); } /** * @param reqData * @return */ @Override public synchronized BaseResp syncGrain(BaseReqData reqData) { BaseResp resp = new BaseResp(); GatewayDevice device = reqData.getDevice(); if (null == device) { resp.setCode(500); resp.setMsg("系统未获取到下行连接设备信息,无法执行"); log.error("----------------系统未获取到下行连接设备信息,无法执行---------"); return resp; } try { //从数据库查询最新的数据,通过mqtt推送上去 //reqData = new BaseReqData(); reqData.setProductId(device.getProductId()); reqData.setDeviceName(device.getDeviceName()); reqData.setDeviceId(device.getDeviceId()); reqData.setDayTime(new Date()); reqData.setDevice(device); Date start = DateUtil.getCurZero(reqData.getDayTime()); Date end = DateUtil.getNextZero(reqData.getDayTime()); //获取到数据 String depotIdSys = device.getDepotIdSys(); if (StringUtils.isEmpty(depotIdSys)) { log.error("--------设备--{}-未配置系统相关仓库编码,无法执行当前操作", device.getDeviceName()); return new BaseResp(BaseResp.CODE_500, "未配置系统相关仓库编码,无法执行当前操作"); } List listGrain = fzzy40CommonService.listGrain(depotIdSys, start, end); if (null == listGrain || listGrain.isEmpty()) { log.error("---------设备-{}-系统仓库编码-{}-未同步到粮情信息,请确认当前条件下是否有数据", device.getDeviceName(), device.getDepotIdSys()); return new BaseResp(BaseResp.CODE_500, "未同步到粮情信息,请确认当前条件下是否有数据"); } //获取第一条数据执行推送 Fz40Grain lastData = listGrain.get(0); log.info(lastData.toString()); lastData.setReceiveDate(new Date()); GrainData grain = fz40Grain2GrainData(reqData, lastData, device); //推送 Thread.sleep(3000); doPushGrain(reqData,grain); } catch (Exception e) { log.error("粮情检测异常:{}", e); resp.setCode(500); resp.setMsg("平台------>>>>控制柜:发送粮情检测命令:" + e.getMessage()); return resp; } return resp; } private GrainData fz40Grain2GrainData(BaseReqData reqData ,Fz40Grain grainData, GatewayDevice device) { GrainCableData cableData = GatewayUtils.getCableData(device); GrainData grain = new GrainData(); //层行列 int cableZ = cableData.getCableZ(); int cableY = cableData.getCableY(); int cableX = cableData.getCableX(); //温度集合 String[] attr = grainData.getPoints().split(","); 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 = 0; List temperature = new ArrayList<>(); //根号 int cableNum = 1, position = 0; double curTemp; int x = 0, y = 0, z = 0; for (int i = 0; i < attr.length; i++) { curTemp = Double.valueOf(attr[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 > max && curTemp < 40) { max = curTemp; } if (curTemp < min && curTemp > 3) { min = curTemp; } if(curTemp > 3 && curTemp < 40){ sumT += curTemp; sumNum++; } } if (sumNum == 0) { sumNum = 1; log.warn("---当前粮情采集异常--"); } //过滤比较用的最大最小值 if (max == ReMessageBuilder.MAX_TEMP) { max = 0.0; } if (min == ReMessageBuilder.MIN_TEMP) { min = 0.0; } //遍历温度点位,异常温度点使用平均温 for( GrainTemp temp : temperature){ Double curTemp2 = Double.valueOf(temp.getTemperature()); if(!(curTemp2 > 3 && curTemp2 < 40)){ temp.setTemperature(outPut.getAvgTemperature()); } } outPut.setTemperature(temperature); outPut.setAvgTemperature(NumberUtil.keepPrecision((sumT / sumNum), 1) + ""); outPut.setMinTemperature(min + ""); outPut.setMaxTemperature(max + ""); List ths = new ArrayList<>(); //获取温湿度 ths.add(new GrainTH(grainData.getTempIn() != null ? grainData.getTempIn() + "" : "", grainData.getHumidityIn() != null ? grainData.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().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)); return 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); } @Override public BaseResp syncGrainTh(BaseReqData reqData) { return new BaseResp(); } @Override public BaseResp syncConf(BaseReqData reqData) { return new BaseResp(); } @Override public BaseResp writeConf(BaseReqData reqData) { return new BaseResp(); } @Override public BaseResp initCable(BaseReqData reqData) { return new BaseResp(); } @Override public BaseResp disconnect(BaseReqData reqData) { return new BaseResp(); } @Override public BaseResp transparent(BaseReqData reqData) { return new BaseResp(); } }