From 69e8acc5dd1f760eb60e914472c151bfa8126a52 Mon Sep 17 00:00:00 2001
From: vince <757871790@qq.com>
Date: 星期三, 03 七月 2024 17:08:20 +0800
Subject: [PATCH] 修改测温

---
 src/main/java/com/fzzy/gateway/hx2023/service/ScGatewayTestServiceImpl.java |  333 ++++++++++++++++++++++++++++---------------------------
 1 files changed, 169 insertions(+), 164 deletions(-)

diff --git a/src/main/java/com/fzzy/gateway/hx2023/service/ScGatewayTestServiceImpl.java b/src/main/java/com/fzzy/gateway/hx2023/service/ScGatewayTestServiceImpl.java
index 3bd840c..2181f31 100644
--- a/src/main/java/com/fzzy/gateway/hx2023/service/ScGatewayTestServiceImpl.java
+++ b/src/main/java/com/fzzy/gateway/hx2023/service/ScGatewayTestServiceImpl.java
@@ -1,29 +1,33 @@
 package com.fzzy.gateway.hx2023.service;
 
-import com.alibaba.fastjson2.JSONObject;
+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.lang.time.DateFormatUtils;
+import org.apache.commons.lang3.RandomUtils;
 import org.springframework.stereotype.Component;
 
 import javax.annotation.Resource;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
 
 @Slf4j
 @Component
@@ -31,6 +35,14 @@
 
     @Resource
     private Fzzy40CommonService fzzy40CommonService;
+
+    @Resource
+    private DeviceReportServiceImpl deviceReportService;
+
+    @Resource
+    private ConfigData configData;
+    @Resource
+    private RedisUtil redisUtil;
 
     @Override
     public String getProtocol() {
@@ -42,14 +54,120 @@
 
         GatewayDevice device = reqData.getDevice();
 
-        if (StringUtils.isEmpty(device.getCableCir())) {
+        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("娌℃湁鍖归厤鍒拌鍒�");
+        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<GrainTemp> 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));
+    }
+
+    /**
+     * 浠嶳EDIS涓幏鍙栨恫浣嶉珮搴︿俊鎭�
+     *
+     * @param device
+     * @return
+     */
+    private String getCacheHeight(GatewayDevice device) {
+        //缁欏叾浠栬蒋浣跨敤
+        String key = RedisConst.KEY_DEPOT_HEIGHT + ":" + configData.getCompanyId() + "_" + device.getDepotIdSys();
+        return (String) redisUtil.get(key);
     }
 
     @Override
@@ -62,145 +180,25 @@
     }
 
     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_50, "鏈厤缃郴缁熺浉鍏充粨搴撶紪鐮侊紝鏃犳硶鎵ц褰撳墠鎿嶄綔");
+            return new BaseResp(BaseResp.CODE_500, "鏈厤缃郴缁熺浉鍏充粨搴撶紪鐮侊紝鏃犳硶鎵ц褰撳墠鎿嶄綔");
         }
 
         List<Fz40Grain> listGrain = fzzy40CommonService.listGrain(depotIdSys, start, end);
         if (null == listGrain || listGrain.isEmpty()) {
-            log.error("---------璁惧---{}--鏈悓姝ュ埌绮儏淇℃伅锛岃纭褰撳墠鏉′欢涓嬫槸鍚︽湁鏁版嵁", device.getDeviceName());
-            return new BaseResp(BaseResp.CODE_50, "鏈悓姝ュ埌绮儏淇℃伅锛岃纭褰撳墠鏉′欢涓嬫槸鍚︽湁鏁版嵁");
+            log.error("---------璁惧-{}-绯荤粺浠撳簱缂栫爜-{}-鏈悓姝ュ埌绮儏淇℃伅锛岃纭褰撳墠鏉′欢涓嬫槸鍚︽湁鏁版嵁", device.getDeviceName(), device.getDepotIdSys());
+            return new BaseResp(BaseResp.CODE_500, "鏈悓姝ュ埌绮儏淇℃伅锛岃纭褰撳墠鏉′欢涓嬫槸鍚︽湁鏁版嵁");
         }
 
-        //鑾峰彇鏈�鍚庝竴鏉$伯鎯呬綔涓哄綋鍓嶆暟鎹�
-        Fz40Grain lastData = listGrain.get(listGrain.size() - 1);
-        KafaGrainData pushData = this.lastData2PushData(lastData, device);
-
-        return new BaseResp(JSONObject.toJSONString(pushData));
+        //鑾峰彇绗竴鏉℃暟鎹墽琛屾帹閫�
+        Fz40Grain lastData = listGrain.get(0);
+        log.info(lastData.toString());
+        return deviceReportService.grainData2GatewayApiInfo(lastData, device);
     }
-
-    /**
-     * 灏嗙伯鎯呮暟鎹浆鎹负鎺ㄩ�侀渶瑕佺殑鏁版嵁鏍煎紡
-     *
-     * @param lastData
-     * @return
-     */
-    private KafaGrainData lastData2PushData(Fz40Grain lastData, GatewayDevice device) {
-        KafaGrainData result = new KafaGrainData();
-
-        result.setMessageId(ScConstant.getMessageId());
-        result.setMessgeId(result.getMessageId());
-        result.setDeviceID(device.getDeviceId());
-        result.setAvgTemperature(lastData.getTempAve() + "");
-        result.setMinTemperature(lastData.getTempMin() + "");
-        result.setMaxTemperature(lastData.getTempMax() + "");
-        result.setCollectTime(DateFormatUtils.format(lastData.getReceiveDate(), "yyyy-MM-dd HH:mm:ss"));
-
-        //灞�-琛�-鍒�
-        String[] attrCable = lastData.getCable().split("-");
-
-
-        if (StringUtils.isNotEmpty(lastData.getCableCir())) {
-            return lastData2PushData2(lastData, device);
-        }
-
-        //灞傝鍒�
-        int cableZ = Integer.valueOf(attrCable[0]);
-        int cableY = Integer.valueOf(attrCable[1]);
-        int cableX = Integer.valueOf(attrCable[2]);
-
-        //娓╁害闆嗗悎
-        String[] attr = lastData.getPoints().split(",");
-
-        //鏍瑰彿
-        int cableNum = 1, position = 0;
-
-        String curTemp;
-        List<KafkaGrainDataDetail1> temperature = new ArrayList<>();
-
-        int x = 0, y = 0, z = 0;
-        for (int i = 0; i < attr.length; i++) {
-
-            position = i;
-
-            z = i % cableZ + 1;
-            x = i / (cableZ * cableY);
-            y = x * (cableZ * cableY);
-            y = (i - y) / cableZ;
-
-            // 鍊掕浆X杞�
-            x = cableX - 1 - x;
-
-            //鏍瑰彿
-            cableNum = (i / cableZ) + 1;
-
-            curTemp = attr[i];
-
-
-            //鍒ゆ柇鏈�澶�
-            if (curTemp.equals(result.getMaxTemperature())) {
-                result.setMaxX(x + "");
-                result.setMaxY(y + "");
-                result.setMaxZ(position + "");
-            }
-
-
-            //鍒ゆ柇鏈�灏�
-            if (curTemp.equals(result.getMinTemperature())) {
-                result.setMinX(x + "");
-                result.setMinY(y + "");
-                result.setMinZ(position + "");
-            }
-
-            temperature.add(new KafkaGrainDataDetail1(cableNum + "", z + "", curTemp, position + "", x + "", y + ""));
-        }
-
-        //绮俯淇℃伅
-
-        JSONObject trhInfo = new JSONObject();
-        // TRHInfo trhInfo = new TRHInfo();
-        trhInfo.put("temperature", temperature);
-
-
-        //浠撴俯搴︿俊鎭�
-        KafkaGrainTH grainTH = new KafkaGrainTH();
-        grainTH.setHumidity(lastData.getHumidityIn() + "");
-        grainTH.setTemperature(lastData.getTempIn() + "");
-        grainTH.setAirHumidity(lastData.getHumidityOut() + "");
-        grainTH.setAirTemperature(lastData.getTempOut() + "");
-
-        List<KafkaGrainTH> temperatureAndhumidity = new ArrayList<>();
-        temperatureAndhumidity.add(grainTH);
-
-        trhInfo.put("temperatureAndhumidity", temperatureAndhumidity);
-        //trhInfo.put("temperatureAndhumidity",grainTH);
-
-        JSONObject params = new JSONObject();
-        params.put("TRHInfo", trhInfo);
-
-        result.setParams(params);
-
-        return result;
-    }
-
-    /**
-     * 閽堝绛掍粨  TODO -----
-     *
-     * @param lastData
-     * @param device
-     * @return
-     */
-    private KafaGrainData lastData2PushData2(Fz40Grain lastData, GatewayDevice device) {
-
-        return null;
-    }
-
 
     @Override
     public BaseResp testWeight(BaseReqData reqData) {
@@ -221,8 +219,9 @@
         weightInfo.setNetWeight(reqData.getWeight());
         weightInfo.setNetWeight(reqData.getWeight());
         weightInfo.setWeightUnit("KG");
+
         JSONObject jsonObject = new JSONObject();
-        jsonObject.put("weightInfo", JSONObject.toJSONString(weightInfo));
+        jsonObject.put("weightInfo", JSON.toJSONString(weightInfo));
 
         packet.setProperties(jsonObject);
 
@@ -234,6 +233,8 @@
     @Override
     public BaseResp testLpr(BaseReqData reqData) {
 
+
+        GatewayDevice device = reqData.getDevice();
 
         WebSocketPacket packet = new WebSocketPacket();
         WebSocketPacketHeader header = new WebSocketPacketHeader();
@@ -250,7 +251,7 @@
         lpr.setCarNumber(reqData.getCarNumber());
         JSONObject jsonObject = new JSONObject();
         jsonObject.put("carNumber", reqData.getCarNumber());
-        jsonObject.put("position", "big");
+        jsonObject.put("position", device.getPosition());
         packet.setProperties(jsonObject);
 
         packet.setTimestamp(System.currentTimeMillis());
@@ -269,15 +270,16 @@
 
 
         WeatherWebDto weather = WeatherWebDto.contextMap.get("default");
-        double tMIn = 20, tMax = 25;
+        double tMIn = 10, tMax = 15;
         if (null != weather) {
             double tOut = Double.valueOf(weather.getTem());
-            tMIn = tOut - 4;
-            if (tMIn < 4) tMIn = 5;
-            tMax = tOut + 0;
-            if (tMax < 15) tMax = 15;
+            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();
@@ -289,7 +291,7 @@
         headers.setDeviceName(device.getDeviceName());
         headers.setProductId(device.getProductId());
         headers.setOrgId(device.getOrgId());
-        headers.setMsgId(ScConstant.getMessageId());
+        headers.setMsgId(reqData.getMessageId());
         grain.setHeaders(headers);
 
         GrainOutPut outPut = new GrainOutPut();
@@ -307,24 +309,22 @@
         double randomNumber = tMIn;
         int x = 0, y = 0, z = 0;
         for (int i = 0; i < sumNum; i++) {
-            if ((i) % Integer.valueOf(cableRule[0]) == 0) {
-                randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn;
-
-            } else if ((i) % Integer.valueOf(cableRule[0]) == 1) {
-                randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn;
-
-            } else if ((i) % Integer.valueOf(cableRule[0]) == 2) {
-                randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn;
-
-            } else if ((i) % Integer.valueOf(cableRule[0]) == 3) {
-                randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn;
-
-            } else if ((i) % Integer.valueOf(cableRule[0]) == 4) {
-                randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn;
-
-            } else {
-                randomNumber = Math.random() * (tMax - tMIn + 1) + tMIn;
-
+            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);
@@ -341,8 +341,12 @@
         }
 
         outPut.setTemperature(temperature);
+        List<GrainTH> ths = new ArrayList<>();
 
-        grain.setOutput(outPut);
+        ths.add(new GrainTH(weather.getTem() != null ? weather.getTem() + "" : "", weather.getHumidity() != null ? weather.getHumidity().replaceAll("%","") + "" : "", "1"));
+        outPut.setTemperatureAndhumidity(ths);
+
+        grain.setOutput(JSONObject.toJSONString(outPut));
 
 
         GatewayDevice gatewayDeviceWeather = GatewayUtils.getCacheByDeviceTypeOne(GatewayDeviceType.TYPE_09.getCode());
@@ -350,6 +354,7 @@
         //姘旇薄淇℃伅
         GrainWeather weatherStation = new GrainWeather();
         weatherStation.setMessageId(ScConstant.getMessageId());
+        weatherStation.setMessgeId(weatherStation.getMessageId());
 
         if (null != gatewayDeviceWeather) {
             weatherStation.setId(gatewayDeviceWeather.getDeviceId());
@@ -357,7 +362,7 @@
             weatherStation.setId(device.getDeviceId());
         }
         weatherStation.setAirPressure(weather.getPressure());
-        weatherStation.setHumidity(weather.getHumidity());
+        weatherStation.setHumidity(weather.getHumidity().replaceAll("%",""));
         weatherStation.setPm(weather.getAir_pm25());
         weatherStation.setRadiation("0");
         weatherStation.setRainfallAmount(weather.getWea());
@@ -365,7 +370,7 @@
         weatherStation.setWindDirection(weather.getWin());
         weatherStation.setWindPower(weather.getWin_meter());
         weatherStation.setWindSpeed(weather.getWin_speed());
-        grain.setWeatherStation(weatherStation);
+        grain.setWeatherStation(JSONObject.toJSONString(weatherStation));
 
         return new BaseResp(JSONObject.toJSONString(grain));
     }

--
Gitblit v1.9.3