package com.fzzy.protocol.xsyg.analysis;
|
|
import com.alibaba.fastjson.JSONObject;
|
import com.fzzy.api.data.GatewayDeviceType;
|
import com.fzzy.api.utils.BytesUtil;
|
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.protocol.ProtocolUtils;
|
import com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.commons.lang3.StringUtils;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import java.net.InetAddress;
|
import java.util.ArrayList;
|
import java.util.List;
|
|
@Slf4j
|
@Component(AnalysisService.BEAN_ID)
|
public class AnalysisService {
|
|
public static final String BEAN_ID = "xsyg.analysisService";
|
|
|
@Resource
|
private GatewayRemoteManager gatewayRemoteManager;
|
|
|
/**
|
* 粮情接线
|
*
|
* @param address
|
* @param port
|
* @param strMsg
|
*/
|
public void analysis(InetAddress address, int port, String strMsg) {
|
|
this.analysisGrainStep1(address, strMsg);
|
}
|
|
/**
|
*byte(i) 01 7C 01 67 01 66 01 81 。。。 02 89 AA BB CC
|
* byte(i)--------包头
|
* AA BB CC-----包尾
|
* 蓝色数据每两个字节表示一个测温点的值(转换为十进制后*0.0625).共有cwcs[1]*2个字节。
|
* 02 89 两个字节表示该油罐的空高值(高位在前)单位毫米。(FF FF或00 00表示故障,请在软件上做出处理,把该空高值赋值成传感器安装高度,表示该罐是空罐)。用H值减去该数据,得到油位高度。
|
*
|
* @param strMsg
|
*/
|
private void analysisGrainStep1(InetAddress address, String strMsg) {
|
strMsg = strMsg.substring(0,strMsg.length()-6);
|
log.info("完整的数据体:"+strMsg);
|
int start = 0;
|
int len = 1 * 2;
|
String tag = strMsg.substring(start, start + 2);
|
int depotIdSys = BytesUtil.hexToInt(tag);
|
|
BaseReqData reqData = ProtocolUtils.getSyncReq(depotIdSys + "");
|
|
if (null == reqData) {
|
log.error("---------没有获取到请求信息,不执行解析------{}", address);
|
return;
|
}
|
|
//仓内湿度
|
|
|
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]);
|
log.info("z={},x={},y={}", cableZ, cableX, cableY);
|
|
int sumNum = cableZ * cableY * cableX;
|
//粮温信息
|
start = 2;
|
len = 2 * 2;
|
List<Double> points = new ArrayList<>();
|
double temp = 0.0;
|
for (int i = 0; i < sumNum; i++) {
|
start = i * 2 * 2 +2;
|
tag = strMsg.substring(start, start + len);
|
if("FFFF".equals(tag)){
|
points.add(-100.00);
|
continue;
|
}
|
temp = BytesUtil.hexToInt(tag) * 0.0625;
|
temp = NumberUtil.keepPrecision(temp, 1);
|
points.add(temp);
|
}
|
double height = 0.0;
|
tag = strMsg.substring(strMsg.length()-4);
|
height = NumberUtil.keepPrecision((double) (BytesUtil.hexToInt(tag) /1000), 2);
|
|
//执行封装解析
|
analysisGrainStep2(reqData, cableZ, cableY, cableX, points, -100, -100,height);
|
}
|
|
private void analysisGrainStep2(BaseReqData reqData, int cableZ, int cableY, int cableX, List<Double> points, double hIn, double tIn,double height) {
|
GatewayDevice device = reqData.getDevice();
|
int sumNum = points.size();
|
|
//数据封装
|
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 = com.fzzy.protocol.bhzn.v0.cmd.ReMessageBuilder.MAX_TEMP, min = com.fzzy.protocol.bhzn.v0.cmd.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 = points.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 == com.fzzy.protocol.bhzn.v0.cmd.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 + "");
|
|
|
JSONObject properties = new JSONObject();
|
|
properties.put("timestamp", grain.getTimestamp());
|
outPut.setDetectTime( grain.getTimestamp());
|
if (StringUtils.isEmpty(height+"")) height = 0.0;
|
properties.put("liquidHeight", height);
|
|
outPut.setLiquidHeight(height+"");
|
grain.setOutput(JSONObject.toJSONString(outPut));
|
properties.put("output", outPut);
|
grain.setProperties(properties);
|
|
//封装好的数据
|
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);
|
}
|
}
|