package com.fzzy.protocol.youxian1.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.cmd.ReMessageBuilder;
|
import com.fzzy.protocol.data.THDto;
|
import com.fzzy.protocol.youxian0.ServiceUtils;
|
import com.fzzy.protocol.youxian0.data.GrainRoot;
|
import lombok.extern.slf4j.Slf4j;
|
import org.springframework.stereotype.Component;
|
|
import javax.annotation.Resource;
|
import javax.persistence.criteria.CriteriaBuilder;
|
import java.net.InetAddress;
|
import java.util.ArrayList;
|
import java.util.HashMap;
|
import java.util.List;
|
import java.util.Map;
|
|
/**
|
* 游仙分库,返回报文解析
|
*/
|
@Slf4j
|
@Component(AnalysisService.BEAN_ID)
|
public class AnalysisService {
|
|
public static final String BEAN_ID = "youxian1.analysisService";
|
|
@Resource
|
private GatewayRemoteManager gatewayRemoteManager;
|
|
/**
|
* 用于存放返回的仓温仓湿信息
|
*/
|
public static Map<String, THDto> contextMapTH = new HashMap<>();
|
|
|
/**
|
* 协议解析
|
* <p>
|
* 80 80 80 80 80 90 41 --90代表分机编码
|
* 39 39 39 35 39 39 39 39 39 39 39 39 --四组湿度
|
* B4 30 30 31 30 30 B4 30 30 B4 30 30 --四组温度
|
* ---以下为粮温,3个值转换为一个点
|
* 31 30 35 31 37 32 32 31 34 32 30 33 31 30 38 31 36 34 32 32 35 32 31 38 31 30 34 31 36 36 32 32 33 32 31 34 31 30 32 31 35 39 32 31 37 32 30 34 31 30 37 31 33 37 32 32 34 32 31 37 31 31 30
|
*
|
* @param address
|
* @param port
|
* @param strMsg
|
*/
|
public void analysis(InetAddress address, int port, String strMsg) {
|
|
//分机ID
|
int start = 5 * 2;
|
int end = start + 1 * 2;
|
String deviceSn = strMsg.substring(start, end);
|
// deviceSn = 调整转换规则 ---TODO
|
|
GatewayDevice device = GatewayUtils.getCacheByDeviceSn(deviceSn);
|
|
|
//解析温湿度
|
this.analysisGrainTh(device, strMsg);
|
|
|
//解析粮情
|
this.analysisGrainStep1(device, strMsg);
|
|
}
|
|
private void analysisGrainTh(GatewayDevice device, String strMsg) {
|
//湿度信息,四组,获取有效的一组数据
|
int start = 7 * 2;
|
int end = start + 12 * 2;
|
String hHex = strMsg.substring(start, end);
|
|
THDto thDto = new THDto();
|
double h = getGrainTemp(hHex.substring(0, 6));
|
if (h < 99.9) {
|
thDto.setHumidityIn(h);
|
}
|
h = getGrainTemp(hHex.substring(6, 12));
|
if (h < 99.9) {
|
thDto.setHumidityIn(h);
|
}
|
h = getGrainTemp(hHex.substring(12, 18));
|
if (h < 99.9) {
|
thDto.setHumidityIn(h);
|
}
|
h = getGrainTemp(hHex.substring(18, 24));
|
if (h < 99.9) {
|
thDto.setHumidityIn(h);
|
}
|
|
//湿度信息
|
start = 19 * 2;
|
end = start + 12 * 2;
|
String tHex = strMsg.substring(start, end);
|
double t = getGrainTemp(tHex.substring(0, 6));
|
if (t > -40.0) {
|
thDto.setTempIn(t);
|
}
|
t = getGrainTemp(tHex.substring(6, 12));
|
if (t > -40.0) {
|
thDto.setTempIn(t);
|
}
|
t = getGrainTemp(tHex.substring(12, 18));
|
if (t > -40.0) {
|
thDto.setTempIn(t);
|
}
|
t = getGrainTemp(tHex.substring(18, 24));
|
if (t > -40.0) {
|
thDto.setTempIn(t);
|
}
|
|
this.add2Map(device.getDeviceSn(), thDto);
|
}
|
|
|
/**
|
* 协议解析
|
* <p>
|
* 80 80 80 80 80 90 41 --90代表分机编码
|
* 39 39 39 35 39 39 39 39 39 39 39 39 --四组湿度
|
* B4 30 30 31 30 30 B4 30 30 B4 30 30 --四组温度
|
* ---以下为粮温,3个值转换为一个点
|
* 31 30 35 31 37 32 32 31 34 32 30 33 31 30 38 31 36 34 32 32 35 32 31 38 31 30 34 31 36 36 32 32 33 32 31 34 31 30 32 31 35 39 32 31 37 32 30 34 31 30 37 31 33 37 32 32 34 32 31 37 31 31 30
|
*
|
* @param device
|
* @Param strMsg
|
*/
|
private void analysisGrainStep1(GatewayDevice device, String strMsg) {
|
|
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);
|
|
//获取请求信息
|
BaseReqData reqData = ProtocolUtils.getSyncReq(device.getDepotIdSys());
|
if (null == reqData) {
|
log.error("---------没有获取到请求信息,不执行解析------{}", device.getDeviceName());
|
return;
|
}
|
|
//只保留粮情信息
|
int start = 31 * 2;
|
strMsg = strMsg.substring(start);
|
int num = cableZ * cableY * cableX;
|
String tempStr = "";
|
List<Double> points = new ArrayList<>();
|
double point = 0;
|
for (int i = 0; i < num; i++) {
|
tempStr = strMsg.substring(i * 3 * 2, i * 3 * 2 + 3 * 2);
|
point = this.getGrainTemp(tempStr);
|
|
//处理无效的数据
|
if (point > 50) {
|
point = -100.0;
|
}
|
|
points.add(point);
|
}
|
|
|
analysisGrainStep2(reqData, cableZ, cableY, cableX, points);
|
}
|
|
|
/**
|
* 数据转换
|
*
|
* @param reqData
|
* @param cableZ
|
* @param cableY
|
* @param cableX
|
* @param points
|
*/
|
private void analysisGrainStep2(BaseReqData reqData, int cableZ, int cableY, int cableX, List<Double> points) {
|
|
GatewayDevice device = reqData.getDevice();
|
//数据封装
|
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 < points.size(); 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 > max && curTemp < 40) {
|
max = curTemp;
|
}
|
if (curTemp < min && curTemp > 3) {
|
min = curTemp;
|
}
|
if(curTemp > 3 && curTemp < 40){
|
sumT += curTemp;
|
sumNum++;
|
}
|
}
|
|
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 + "");
|
List<GrainTH> ths = new ArrayList<>();
|
|
|
//获取温湿度
|
THDto thDto = this.getTH(device.getDeviceSn());
|
|
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);
|
}
|
|
|
/**
|
* 计算粮情温度 3组温度
|
*
|
* @param tempHex
|
* @return
|
*/
|
public static double getGrainTemp(String tempHex) {
|
String strAscii = "";
|
strAscii += toAscii(tempHex.substring(0, 2));
|
strAscii += toAscii(tempHex.substring(2, 4));
|
strAscii += toAscii(tempHex.substring(4, 6));
|
|
Integer value = Integer.valueOf(strAscii);
|
|
return value / 10.0;
|
}
|
|
private void add2Map(String deviceSn, THDto thDto) {
|
contextMapTH.put("TH_" + deviceSn, thDto);
|
}
|
|
private THDto getTH(String deviceSn) {
|
return contextMapTH.get("TH_" + deviceSn);
|
}
|
|
|
public static void main(String[] args) {
|
String strMsg = "31 30 35 31 37 32 32 31 34 32 30 33 31 30 38 31 36 34 32 32 35 32 31 38 31 30 34 31 36 36 32 32 33 32 31 34 31 30 32 31 35 39 32 31 37 32 30 34 31 30 37 31 33 37 32 32 34 32 31 37 31 31 30";
|
strMsg = strMsg.replaceAll(" ", "");
|
System.out.println("---" + strMsg);
|
int num = 20;
|
String tempStr = "";
|
double tem;
|
for (int i = 0; i < num; i++) {
|
tempStr = strMsg.substring(i * 3 * 2, i * 3 * 2 + 3 * 2);
|
System.out.println("---" + tempStr);
|
tem = getGrainTemp(tempStr);
|
System.out.println("-tem--" + tem);
|
}
|
}
|
|
public static char toAscii(String hexString) {
|
int decimal = Integer.parseInt(hexString, 16);
|
return (char) decimal;
|
}
|
|
}
|