From 1346ece340481c2489dbc4ada7ddd73971991d44 Mon Sep 17 00:00:00 2001
From: vince <757871790@qq.com>
Date: 星期四, 19 九月 2024 08:49:23 +0800
Subject: [PATCH] 优化测温协议
---
src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java | 158 ++++++++++++++++++++++++++++++++++++++++++++++++----
1 files changed, 144 insertions(+), 14 deletions(-)
diff --git a/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java
index 99ffdd7..b28a874 100644
--- a/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java
+++ b/src/main/java/com/fzzy/protocol/zldz/analysis/AnalysisGrain.java
@@ -1,11 +1,22 @@
package com.fzzy.protocol.zldz.analysis;
+import com.alibaba.fastjson.JSONObject;
+import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.ContextUtil;
+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.io.request.BaseRequest;
import com.fzzy.protocol.ProtocolUtils;
+import com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder;
+import com.fzzy.protocol.data.THDto;
import com.fzzy.protocol.zldz.data.ReMessage;
import com.fzzy.protocol.zldz.service.ZldzGatewayGrainService;
import lombok.extern.slf4j.Slf4j;
@@ -14,6 +25,7 @@
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
+import javax.persistence.criteria.CriteriaBuilder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -32,6 +44,10 @@
@Resource
private ZldzGatewayGrainService zldzGatewayGrainService;
+
+ @Resource
+ private GatewayRemoteManager gatewayRemoteManager;
+
public static Map<String, Map<String, String>> contextMap = new HashMap<>();
@@ -248,8 +264,6 @@
// 澶囩敤鍊�
if (tempValue == ERROR_CHECK_TAG) {
tempValue = ProtocolUtils.ERROR_TEMP;
-
-
//楠岃瘉鏄笉鏄敟褰粨琛ュ伩鍊�
curLay = (i % layMax) + 1;
curRoot = (i / layMax) + 1;
@@ -294,16 +308,10 @@
/**
* 骞虫柟浠撶殑瑙f瀽锛岃В鏋愰渶瑕佽�冭檻褰撳墠鏄惁鍚敤鐨勪竴鍒嗘満澶氫粨
*
- * @param depotConf
- * @param msg
- * @param ser
- * @param exeRequest
* @param packetMap
* @param sumPackets
*/
- private void analysisStep1(ReMessage msg,
- GatewayDevice device, BaseReqData reqData,
- Map<String, String> packetMap, int sumPackets) {
+ private void analysisStep1(ReMessage msg, GatewayDevice device, BaseReqData reqData, Map<String, String> packetMap, int sumPackets) {
// 鑾峰彇瀹屾暣鐨勭伯鎯呭寘淇℃伅
String strPoints = "";
for (int i = 1; i <= sumPackets; i++) {
@@ -316,7 +324,7 @@
int cableX = Integer.valueOf(attCable[2]);
// 鏍规嵁灞傝鍒楄幏鍙栨寚瀹氶暱搴�
- int start = 4 * (device.getCableStart() - 1) * cableZ * cableY;
+ int start = 4 * (Integer.valueOf(device.getCableStart()) - 1) * cableZ * cableY;
int len = 4 * cableZ * cableY * cableX;
log.info("鍒嗘満------>>>骞冲彴锛氳繑鍥炵伯鎯呭畬鏁翠俊鎭紝鍒嗘満={}", device.getDeviceName());
@@ -346,7 +354,7 @@
// 灏嗛泦鍚堣В鏋愭垚鍧愭爣鏁版嵁
- addPoint1(temps, msg, device, reqData);
+ addPoint1(temps, device, reqData, cableX, cableY, cableZ);
}
private String buildCurKey(ReMessage msg, int curPacket) {
@@ -363,11 +371,133 @@
* @param temps
* @throws Exception
*/
- private void addPoint1(List<Double> temps, ReMessage msg,
- GatewayDevice device, BaseReqData reqData) {
- //TODO
+ private void addPoint1(List<Double> temps, GatewayDevice device, BaseReqData reqData, int cableX, int cableY, int cableZ) {
+
+ //鏁版嵁灏佽
+ 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<GrainTemp> temperature = new ArrayList<>();
+ //鏍瑰彿
+ int cableNum = 1, position = 0;
+
+ double curTemp;
+ int x = 0, y = 0, z = 0;
+ for (int i = 0; i < temps.size(); i++) {
+ curTemp = temps.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 + ""));
+
+ //姹傛渶澶ф渶灏忓��
+ if (curTemp < -900) {
+ sumNum--;
+ } else {
+ 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(max + "");
+ List<GrainTH> ths = new ArrayList<>();
+
+
+ THDto thDto = ProtocolUtils.getCacheTh(device.getDeviceId());
+ if (null == thDto) thDto = new THDto();
+
+ ths.add(new GrainTH(thDto.getTempIn() != null ? thDto.getTempIn() + "" : "", thDto.getHumidityIn() != null ? thDto.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));
+
+ 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);
+ }
+
+
/**
* 鍦嗙瓛浠撹В鏋愭楠�
*
--
Gitblit v1.9.3