package com.fzzy.gateway.hx2023.service; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import com.fzzy.api.data.DepotType; import com.fzzy.api.data.GatewayDeviceType; import com.fzzy.api.data.PushProtocol; import com.fzzy.api.utils.DateUtil; import com.fzzy.api.utils.NumberUtil; import com.fzzy.api.utils.RedisConst; import com.fzzy.api.utils.RedisUtil; import com.fzzy.async.fzzy40.Fzzy40CommonService; import com.fzzy.async.fzzy40.entity.Fz40Grain; import com.fzzy.data.ConfigData; import com.fzzy.gateway.GatewayUtils; import com.fzzy.gateway.api.GatewayDeviceTestService; 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 lombok.extern.slf4j.Slf4j; import org.apache.commons.lang.StringUtils; import org.apache.commons.lang3.RandomUtils; import org.springframework.stereotype.Component; import javax.annotation.Resource; import java.util.*; @Slf4j @Component public class ScGatewayTestServiceImpl implements GatewayDeviceTestService { @Resource private Fzzy40CommonService fzzy40CommonService; @Resource private DeviceReportServiceImpl deviceReportService; @Resource private ConfigData configData; @Resource private RedisUtil redisUtil; @Override public String getProtocol() { return PushProtocol.GATEWAY_SC_2023.getCode(); } @Override public BaseResp testGrain(BaseReqData reqData) { GatewayDevice device = reqData.getDevice(); if (DepotType.TYPE_02.getCode().equals(device.getDepotType())) { return getGrainTest2(reqData, device); } else if (DepotType.TYPE_04.getCode().equals(device.getDepotType())) { return getGrainTest2(reqData, device); } else if (DepotType.TYPE_03.getCode().equals(device.getDepotType())) { return getGrainTest3(reqData, device); } else { return getGrainTest1(reqData, device); } } private BaseResp getGrainTest2(BaseReqData reqData, GatewayDevice device) { BaseResp resp = new BaseResp(); resp.setCode(500); resp.setMsg("筒仓测试暂未实现"); return resp; } private BaseResp getGrainTest3(BaseReqData reqData, GatewayDevice device) { BaseResp resp = new BaseResp(); GrainCableData cableData = GatewayUtils.getCableData(device); int cableZ = cableData.getCableZ(); int cableY = cableData.getCableY(); int sumNum = cableData.getSumNum(); WeatherWebDto weather = WeatherWebDto.contextMap.get("default"); double tMIn = 10, tMax = 15; if (null != weather) { double tOut = Double.valueOf(weather.getTem()); tMIn = tOut - 5; tMax = tOut; } if (tMIn < 10) tMIn = 10; if (tMIn > 20) tMIn = 20; if (tMax < 15) tMax = 15; if (tMax > 25) tMax = 25; //数据封装 GrainData grain = new GrainData(); grain.setMessageId(ScConstant.getMessageId()); grain.setDeviceId(device.getDeviceId()); grain.setTimestamp(System.currentTimeMillis() + ""); grain.setMessageType("REPORT_PROPERTY"); 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(); List temperature = new ArrayList<>(); //根号 int cableNum = 1, position = 0; double curTemp = tMIn; double randomNumber = tMIn; int x = 0, y = 0, z = 0; for (int i = 0; i < sumNum; i++) { randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn; curTemp = NumberUtil.keepPrecision(randomNumber, 1); 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); outPut.setAvgTemperature(NumberUtil.keepPrecision((tMax + tMIn) / 2, 1) + ""); outPut.setMinTemperature(tMax + ""); outPut.setMaxTemperature(tMIn + ""); JSONObject properties = new JSONObject(); properties.put("data", outPut); properties.put("timestamp", grain.getTimestamp()); String height = this.getCacheHeight(device); if (StringUtils.isEmpty(height)) height = "1.0"; properties.put("liquidHeight", height); grain.setProperties(properties); //封装好的数据 log.info("---油罐仓信息封装完成----开始执行推送"); return new BaseResp(JSONObject.toJSONString(grain)); } /** * 从REDIS中获取液位高度信息 * * @param device * @return */ private String getCacheHeight(GatewayDevice device) { //给其他软使用 String key = RedisConst.KEY_DEPOT_HEIGHT + ":" + configData.getCompanyId() + "_" + device.getDepotIdSys(); return (String) redisUtil.get(key); } @Override public BaseResp testGrainKafka(BaseReqData reqData) { Date start = DateUtil.getCurZero(reqData.getDayTime()); Date end = DateUtil.getNextZero(reqData.getDayTime()); return pushByV40(reqData, start, end); } private BaseResp pushByV40(BaseReqData reqData, Date start, Date end) { GatewayDevice device = reqData.getDevice(); 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()); return deviceReportService.grainData2GatewayApiInfo(lastData, device); } @Override public BaseResp testWeight(BaseReqData reqData) { GatewayDevice device = reqData.getDevice(); WebSocketPacket packet = new WebSocketPacket(); WebSocketPacketHeader header = new WebSocketPacketHeader(); header.setDeviceName(device.getDeviceName()); header.setProductId(device.getProductId()); packet.setHeaders(header); packet.setMessageType(ScConstant.MESSAGE_TYPE_REPORT_PROPERTY); packet.setDeviceId(device.getDeviceId()); //设置信息主体 WeightInfo weightInfo = new WeightInfo(); weightInfo.setGrossWeight(reqData.getWeight()); weightInfo.setNetWeight(reqData.getWeight()); weightInfo.setNetWeight(reqData.getWeight()); weightInfo.setWeightUnit("KG"); JSONObject jsonObject = new JSONObject(); jsonObject.put("weightInfo", JSON.toJSONString(weightInfo)); packet.setProperties(jsonObject); packet.setTimestamp(System.currentTimeMillis()); return new BaseResp(JSONObject.toJSONString(packet)); } @Override public BaseResp testLpr(BaseReqData reqData) { GatewayDevice device = reqData.getDevice(); WebSocketPacket packet = new WebSocketPacket(); WebSocketPacketHeader header = new WebSocketPacketHeader(); header.setDeviceName(reqData.getDeviceName()); header.setProductId(reqData.getProductId()); packet.setHeaders(header); packet.setMessageType(ScConstant.MESSAGE_TYPE_REPORT_PROPERTY); packet.setDeviceId(reqData.getDeviceId()); packet.setMessageId(System.currentTimeMillis() + ""); //设置信息主体 LprData lpr = new LprData(); lpr.setDeviceId(reqData.getDeviceId()); lpr.setCarNumber(reqData.getCarNumber()); JSONObject jsonObject = new JSONObject(); jsonObject.put("carNumber", reqData.getCarNumber()); jsonObject.put("position", device.getPosition()); packet.setProperties(jsonObject); packet.setTimestamp(System.currentTimeMillis()); return new BaseResp(JSONObject.toJSONString(packet)); } private BaseResp getGrainTest1(BaseReqData reqData, GatewayDevice device) { String[] cableRule = device.getCableRule().split("-"); int cableZ = Integer.valueOf(cableRule[0]); int cableY = Integer.valueOf(cableRule[1]); int cableX = Integer.valueOf(cableRule[2]); int sumNum = cableZ * cableY * cableX; WeatherWebDto weather = WeatherWebDto.contextMap.get("default"); double tMIn = 10, tMax = 15; if (null != weather) { double tOut = Double.valueOf(weather.getTem()); tMIn = tOut - 5; tMax = tOut; } if (tMIn < 10) tMIn = 10; if (tMIn > 20) tMIn = 20; if (tMax < 15) tMax = 15; if (tMax > 25) tMax = 25; //数据封装 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(NumberUtil.keepPrecision((tMax + tMIn) / 2, 1) + ""); outPut.setMinTemperature(tMax + ""); outPut.setMaxTemperature(tMIn + ""); List temperature = new ArrayList<>(); //根号 int cableNum = 1, position = 0; double curTemp = tMIn; double randomNumber = tMIn; int x = 0, y = 0, z = 0; for (int i = 0; i < sumNum; i++) { log.info("i=:" + i); if (i % cableZ == 0) { randomNumber = RandomUtils.nextDouble(tMIn, tMax + 1.5); log.info("第1层温度:" + randomNumber); } else if (i % cableZ == 1) { randomNumber = RandomUtils.nextDouble(tMIn - 1, tMax - 0); log.info("第2层温度:" + randomNumber); } else if (i % cableZ == 2) { randomNumber = RandomUtils.nextDouble(tMIn - 2, tMax - 1.5); log.info("第3层温度:" + randomNumber); } else if (i % cableZ == 3) { randomNumber = RandomUtils.nextDouble(tMIn - 3, tMax - 3); log.info("第4层温度:" + randomNumber); } else if (i % cableZ == 4) { randomNumber = RandomUtils.nextDouble(tMIn - 4, tMax - 4.5); log.info("第5层温度:" + randomNumber); } // randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn; curTemp = NumberUtil.keepPrecision(randomNumber, 1); 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(weather.getTem() != null ? weather.getTem() + "" : "", weather.getHumidity() != null ? weather.getHumidity() + "" : "", "1")); outPut.setTemperatureAndhumidity(ths); grain.setOutput(JSONObject.toJSONString(outPut)); GatewayDevice gatewayDeviceWeather = GatewayUtils.getCacheByDeviceTypeOne(GatewayDeviceType.TYPE_09.getCode()); //气象信息 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)); return new BaseResp(JSONObject.toJSONString(grain)); } }