From bf34444f482223d291830c13cb147392298d99ee Mon Sep 17 00:00:00 2001
From: vince <757871790@qq.com>
Date: 星期五, 15 三月 2024 09:39:25 +0800
Subject: [PATCH] 粮情协议优化
---
src/main/java/com/fzzy/protocol/bhzn/v0/analysis/AnalysisService.java | 220 +++++++++++++++++++++++++++++++++++++++++++++---------
1 files changed, 182 insertions(+), 38 deletions(-)
diff --git a/src/main/java/com/fzzy/protocol/bhzn/v0/analysis/AnalysisService.java b/src/main/java/com/fzzy/protocol/bhzn/v0/analysis/AnalysisService.java
index 7e131b3..ea1fca7 100644
--- a/src/main/java/com/fzzy/protocol/bhzn/v0/analysis/AnalysisService.java
+++ b/src/main/java/com/fzzy/protocol/bhzn/v0/analysis/AnalysisService.java
@@ -3,9 +3,13 @@
import com.alibaba.fastjson.JSONObject;
import com.fzzy.api.Constant;
import com.fzzy.api.data.ApiCommonDevice;
+import com.fzzy.api.data.DepotType;
import com.fzzy.api.data.GatewayDeviceType;
import com.fzzy.api.utils.BytesUtil;
import com.fzzy.api.utils.NumberUtil;
+import com.fzzy.api.utils.RedisConst;
+import com.fzzy.api.utils.RedisUtil;
+import com.fzzy.data.ConfigData;
import com.fzzy.gateway.GatewayUtils;
import com.fzzy.gateway.api.GatewayDeviceReportService;
import com.fzzy.gateway.api.GatewayRemoteManager;
@@ -20,8 +24,10 @@
import com.fzzy.protocol.bhzn.v0.data.IoMessage;
import com.fzzy.protocol.bhzn.v0.server.BhznGrainV0ServerEngine;
import com.fzzy.protocol.bhzn.v0.server.BhznGrainV0ServerUtils;
+import com.fzzy.gateway.data.GrainCableData;
import com.fzzy.protocol.data.THDto;
import lombok.extern.slf4j.Slf4j;
+import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
@@ -41,6 +47,10 @@
@Resource
private GatewayRemoteManager gatewayRemoteManager;
+ @Resource
+ private ConfigData configData;
+ @Resource
+ private RedisUtil redisUtil;
/**
* 閽堝鍒嗗寘绮儏鎶ユ枃杩涜灏佽
@@ -97,22 +107,20 @@
*/
private void analysisGrain(IoMessage message) {
try {
-
//鏍规嵁鍒嗘満SN鑾峰彇璁惧閰嶇疆淇℃伅
- GatewayDevice gatewayDevice = GatewayUtils.getCacheByDeviceSn(message.getAddr());
+ GatewayDevice gatewayDevice = BhznGrainV0ServerUtils.contextOrder;
if (null == gatewayDevice) {
replayGrain(message);
log.error("涓绘満-------->>骞冲彴锛岃В鏋愮伯鎯呭け璐ワ紝鏈幏鍙栧埌绯荤粺璁惧閰嶇疆淇℃伅锛�" + message.getAddr());
return;
}
//鏍规嵁鍒嗘満鍦板潃鑾峰彇鍒嗘満淇℃伅
- ApiCommonDevice commonDevice = Constant.getCommonDeviceCache(message.getIp());
- if (commonDevice == null) {
- replayGrain(message);
- log.error("涓绘満-------->>骞冲彴锛岃В鏋愮伯鎯呭け璐ワ紝鏈幏鍙栧埌绯荤粺绮儏涓绘満閰嶇疆锛�" + message.getAddr());
- return;
- }
-
+// ApiCommonDevice commonDevice = Constant.getCommonDeviceCache(message.getIp());
+// if (commonDevice == null) {
+// replayGrain(message);
+// log.error("涓绘満-------->>骞冲彴锛岃В鏋愮伯鎯呭け璐ワ紝鏈幏鍙栧埌绯荤粺绮儏涓绘満閰嶇疆锛�" + message.getAddr());
+// return;
+// }
//鑾峰彇璇锋眰淇℃伅
BaseReqData reqData = ProtocolUtils.getSyncReq(gatewayDevice.getDepotIdSys());
@@ -123,11 +131,7 @@
}
// 鍒ゆ柇鏁版嵁鏈夋病鏈夋敹鍙栧畬鏁�
- String[] attCable = gatewayDevice.getCableRule().split("-");
- int cableZ = Integer.valueOf(attCable[0]);
- int cableY = Integer.valueOf(attCable[1]);
- int cableX = Integer.valueOf(attCable[2]);
- int sumPoint = cableZ * cableY * cableX;
+ GrainCableData cableData = GatewayUtils.getCableData(gatewayDevice);
//鑾峰彇褰撳墠绮儏娓╁害鎶ユ枃
String grainHex = message.getContent().substring(16);
@@ -136,11 +140,11 @@
int curPoint = BytesUtil.hexToInt(BytesUtil.tran_LH(message.getContent().substring(4, 8)));
//濡傛灉褰撳墠鍖呯殑鏁版嵁涓暟澶т簬绛変簬褰撳墠浠撳簱鐨勯厤缃偣浣嶅垯琛ㄧず鍗曞寘杩斿洖
- if (curPoint >= sumPoint) {
+ if (curPoint >= cableData.getSumNum()) {
log.info("鍒嗘満------>>>骞冲彴锛氱伯鎯呮暟鎹崟鍖�=" + grainHex);
//杩斿洖绮儏鎺ユ敹淇℃伅
replayGrain(message);
- analysisGrain2(message, reqData, grainHex);
+ analysisGrain2(message, reqData, grainHex, cableData);
return;
}
@@ -157,13 +161,12 @@
grainHex = oldGrainHex + grainHex;
}
- if (grainHex.length() >= sumPoint * 4) {
+ if (grainHex.length() >= cableData.getSumNum() * 4) {
//杩斿洖绮儏鎺ユ敹淇℃伅
replayGrain(message);
log.info("鍒嗘満------>>>骞冲彴锛氱伯鎯呮暟鎹鍖咃紝瀹屾暣鏁版嵁=" + grainHex);
- analysisGrain2(message, reqData, grainHex);
+ analysisGrain2(message, reqData, grainHex, cableData);
return;
-
} else {
log.info("鍒嗘満------>>>骞冲彴锛氬皢绗竴鍖呮暟鎹瓨鍏ュ唴瀛�=" + grainHex);
contextMapGrain.put(key, grainHex);
@@ -185,20 +188,22 @@
String hexStr = CommandBuild.getMsgGrainReply(message.getAddr());
log.info("骞冲彴--------->>>涓绘満锛岃繑鍥炵伯鎯呮姤鏂囨敹鍒颁俊鎭紝鎶ユ枃={}", hexStr);
- BhznGrainV0ServerEngine.push(message.getIp(), message.getPort(), BytesUtil.hexStrToBytes(hexStr));
+ BhznGrainV0ServerEngine.pushByMin(hexStr);
}
- private void analysisGrain2(IoMessage message, BaseReqData reqData, String grainStr) {
+ /**
+ * 鑾峰彇鍒版墍鏈夌殑绮儏鏁版嵁锛屽紑濮嬭В鏋�
+ *
+ * @param message
+ * @param reqData
+ * @param grainStr
+ */
+ private void analysisGrain2(IoMessage message, BaseReqData reqData, String grainStr, GrainCableData cableData) {
GatewayDevice device = reqData.getDevice();
- // 鑾峰彇瀹屾暣鐨勭伯鎯呭寘淇℃伅
- String[] attCable = device.getCableRule().split("-");
- int cableZ = Integer.valueOf(attCable[0]);
- int cableY = Integer.valueOf(attCable[1]);
- int cableX = Integer.valueOf(attCable[2]);
// 鏍规嵁灞傝鍒楄幏鍙栨寚瀹氶暱搴�
int start = 0;
- int len = 4 * cableZ * cableY * cableX;
+ int len = 4 * cableData.getSumNum();
log.info("鍒嗘満------>>>骞冲彴锛氳繑鍥炵伯鎯呭畬鏁翠俊鎭紝鍒嗘満={}", device.getDeviceName());
@@ -234,25 +239,48 @@
String key = "GRAIN_" + message.getAddr();
contextMapGrain.put(key, null);
- // 灏嗛泦鍚堣В鏋愭垚鍧愭爣鏁版嵁
- addPoint1(temps, reqData, thDto);
+
+
+ if(DepotType.TYPE_03.getCode().equals(device.getDepotType())){
+ //绛掍粨
+ analysisAndPush3(temps, reqData, thDto, cableData);
+ }else if(DepotType.TYPE_02.getCode().equals(device.getDepotType())){
+ analysisAndPush2(temps, reqData, thDto, cableData);
+ }else if(DepotType.TYPE_04.getCode().equals(device.getDepotType())){
+ analysisAndPush2(temps, reqData, thDto, cableData);
+ }else {
+ //骞虫埧浠�
+ analysisAndPush1(temps, reqData, thDto, cableData);
+ }
}
+ /**
+ * 绛掍粨锛岃В鏋愮浜屾锛岃В鏋愬埌鍧愭爣鏁版嵁
+ * @param temps
+ * @param reqData
+ * @param thDto
+ * @param cableData
+ */
+ private void analysisAndPush2(List<Double> temps, BaseReqData reqData, THDto thDto, GrainCableData cableData) {
+ //TODO
+ log.info("-------------------------鏆傛湭瀹炵幇----------------");
+ }
/**
- * 骞虫埧浠擄紝瑙f瀽绗簩姝ワ紝瑙f瀽鍒板潗鏍囨暟鎹�
+ * 娌圭綈浠擄紝瑙f瀽绗簩姝ワ紝瑙f瀽鍒板潗鏍囨暟鎹�
*
* @param temps
* @throws Exception
*/
- private void addPoint1(List<Double> temps, BaseReqData reqData, THDto thDto) {
+ private void analysisAndPush3(List<Double> temps, BaseReqData reqData, THDto thDto, GrainCableData cableData) {
GatewayDevice device = reqData.getDevice();
- String[] attCable = device.getCableRule().split("-");
- int cableZ = Integer.valueOf(attCable[0]);
- int cableY = Integer.valueOf(attCable[1]);
- int cableX = Integer.valueOf(attCable[2]);
+ int cableZ = cableData.getCableZ();
+ int cableY = cableData.getCableY();
+ int cableX = cableData.getCableX();
+
+ int sumNum = temps.size();
//鏁版嵁灏佽
GrainData grain = new GrainData();
@@ -271,8 +299,123 @@
GrainOutPut outPut = new GrainOutPut();
- double max = ReMessageBuilder.MAX_TEMP, min = ReMessageBuilder.MIN_TEMP, sumT = 0.0, sumNum = cableX
- * cableY * cableZ;
+ double max = ReMessageBuilder.MAX_TEMP, min = ReMessageBuilder.MIN_TEMP, sumT = 0.0;
+
+ List<GrainTemp> temperature = new ArrayList<>();
+ //鏍瑰彿
+ int cableNum = 1, position = 0;
+
+ double curTemp;
+ int x = 0, y = 0, z = 0;
+ for (int i = 0; i < sumNum; 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(min + "");
+
+
+ JSONObject properties = new JSONObject();
+ properties.put("data", outPut);
+ properties.put("timestamp", grain.getTimestamp());
+
+ String height = this.getCacheHeight(device);
+ if (StringUtils.isEmpty(height)) height = "0.0";
+ properties.put("liquidHeight", height);
+
+ grain.setProperties(properties);
+
+ //灏佽濂界殑鏁版嵁
+ log.info("---娴呭渾浠撳皝瑁呭畬鎴�----寮�濮嬫墽琛屾帹閫�");
+
+ reqData.setData(JSONObject.toJSONString(grain));
+
+ doPushGrain(reqData);
+ }
+
+ /**
+ * 浠嶳EDIS涓幏鍙栨恫浣嶉珮搴︿俊鎭�
+ *
+ * @param device
+ * @return
+ */
+ private String getCacheHeight(GatewayDevice device) {
+ //缁欏叾浠栬蒋浣跨敤
+ String key = RedisConst.KEY_DEPOT_HEIGHT + ":" + configData.getCompanyId() + "_" + device.getDepotIdSys();
+ return (String) redisUtil.get(key);
+ }
+
+
+
+ /**
+ * 骞虫埧浠擄紝瑙f瀽绗簩姝ワ紝瑙f瀽鍒板潗鏍囨暟鎹�
+ *
+ * @param temps
+ * @throws Exception
+ */
+ private void analysisAndPush1(List<Double> temps, BaseReqData reqData, THDto thDto, GrainCableData cableData) {
+
+ GatewayDevice device = reqData.getDevice();
+
+ int cableZ = cableData.getCableZ();
+ int cableY = cableData.getCableY();
+ int cableX = cableData.getCableX();
+
+ //鏁版嵁灏佽
+ 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 = cableData.getSumNum();
List<GrainTemp> temperature = new ArrayList<>();
//鏍瑰彿
@@ -283,6 +426,7 @@
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);
@@ -418,7 +562,7 @@
} finally {
String hexStr = CommandBuild.getMsgTHReply(message.getAddr());
log.info("骞冲彴--------->>>涓绘満锛岃繑鍥炰粨娓╀粨婀挎敹鍒颁俊鎭紝鎶ユ枃={}", hexStr);
- BhznGrainV0ServerEngine.push(message.getIp(), message.getPort(), BytesUtil.hexStrToBytes(hexStr));
+ BhznGrainV0ServerEngine.pushByMin(hexStr);
}
}
--
Gitblit v1.9.3