Browse Source

1、大屏接口bug修改;

2、电表,modbus协议解析bug修复;
dev
mh 4 weeks ago
parent
commit
2977912e0d
  1. 2
      user-service/src/main/java/com/mh/user/mapper/MeterManageMapper.java
  2. 8
      user-service/src/main/java/com/mh/user/netty/EchoServerHandler.java
  3. 2
      user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java
  4. 7
      user-service/src/main/java/com/mh/user/service/impl/ScreenServiceImpl.java
  5. 8
      user-service/src/main/java/com/mh/user/strategy/EleProtocolStrategy.java
  6. 30
      user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java

2
user-service/src/main/java/com/mh/user/mapper/MeterManageMapper.java

@ -19,7 +19,7 @@ public interface MeterManageMapper extends BaseMapper<MeterManageEntity> {
@Select("select * from meter_manage where mt_code=#{deviceAddr} " + @Select("select * from meter_manage where mt_code=#{deviceAddr} " +
" and mt_type=#{deviceType} " + " and mt_type=#{deviceType} " +
" and project_id=#{projectId} " + " and project_id=#{projectId} " +
" and register_addr = #{registerAddr} ") " and register_addr = #{registerAddr}")
MeterManageEntity selectDevice(@Param("deviceAddr") String deviceAddr, MeterManageEntity selectDevice(@Param("deviceAddr") String deviceAddr,
@Param("deviceType") String deviceType, @Param("deviceType") String deviceType,
@Param("projectId") String projectId, @Param("projectId") String projectId,

8
user-service/src/main/java/com/mh/user/netty/EchoServerHandler.java

@ -111,12 +111,11 @@ public class EchoServerHandler extends ChannelInboundHandlerAdapter {
receiveStr = receiveStr.toUpperCase();//返回值全部变成大写 receiveStr = receiveStr.toUpperCase();//返回值全部变成大写
log.info("channelReadComplete接收到的数据{}, 长度: ===> {}", receiveStr, receiveStr.length()); log.info("channelReadComplete接收到的数据{}, 长度: ===> {}", receiveStr, receiveStr.length());
//心跳包处理 //心跳包处理
// if ((receiveStr.length() == 8) && receiveStr.startsWith("24")) { if ((receiveStr.length() == 8) && receiveStr.startsWith("24")) {
if ((receiveStr.length() == 8) && receiveStr.startsWith("C0A801FE")) { // if ((receiveStr.length() == 8) && receiveStr.startsWith("C0A801FE")) {
log.info("接收到心跳包 ===> {}", receiveStr); log.info("接收到心跳包 ===> {}", receiveStr);
idleCount = 1; idleCount = 1;
port = receiveStr.substring(4, 8);//心跳包包含网关端口(自己定义返回心跳包) port = receiveStr.substring(4, 8);//心跳包包含网关端口(自己定义返回心跳包)
port = "6001";
// 清空receiveStr // 清空receiveStr
receiveStr = ""; receiveStr = "";
// 更新对应的网关在线情况 // 更新对应的网关在线情况
@ -390,7 +389,8 @@ public class EchoServerHandler extends ChannelInboundHandlerAdapter {
// 异常捕捉 // 异常捕捉
@Override @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
log.info("异常捕捉,执行ctx.close()......{}", cause.getCause()); cause.getCause().printStackTrace();
log.info("异常捕捉,执行ctx.close" + cause.getCause());
ctx.close(); // 关闭该Channel ctx.close(); // 关闭该Channel
} }

2
user-service/src/main/java/com/mh/user/service/impl/MeterManageServiceImpl.java

@ -43,7 +43,7 @@ public class MeterManageServiceImpl implements MeterManageService {
if (!StringUtils.isBlank(projectId)) { if (!StringUtils.isBlank(projectId)) {
queryWrapper.eq("project_id", projectId); queryWrapper.eq("project_id", projectId);
} }
queryWrapper.eq("is_use", 0); queryWrapper.eq("is_use", 1);
return meterManageMapper.selectList(queryWrapper.orderByDesc("create_time")); return meterManageMapper.selectList(queryWrapper.orderByDesc("create_time"));
} }

7
user-service/src/main/java/com/mh/user/service/impl/ScreenServiceImpl.java

@ -65,8 +65,8 @@ public class ScreenServiceImpl implements ScreenService {
String projectId = screenRequestParamDTO.getProjectId(); String projectId = screenRequestParamDTO.getProjectId();
String startTime = screenRequestParamDTO.getStartTime(); String startTime = screenRequestParamDTO.getStartTime();
String endTime = screenRequestParamDTO.getEndTime(); String endTime = screenRequestParamDTO.getEndTime();
String lastStartTime = DateUtil.yoyDate(startTime + " 00:00:00").substring(0, 10); String lastStartTime = DateUtil.yoyDate(startTime + "-01 00:00:00").substring(0, 10);
String lastEndTime = DateUtil.yoyDate(endTime+ " 23:59:59").substring(0, 10); String lastEndTime = DateUtil.yoyDate(endTime+ "-31 23:59:59").substring(0, 10);
List<Map<String, Object>> monthData = screenMapper.savingYoyEnergyData(projectId, startTime, endTime, lastStartTime, lastEndTime); List<Map<String, Object>> monthData = screenMapper.savingYoyEnergyData(projectId, startTime, endTime, lastStartTime, lastEndTime);
resultJson.add(monthData); resultJson.add(monthData);
break; break;
@ -104,6 +104,9 @@ public class ScreenServiceImpl implements ScreenService {
JSONObject data = new JSONObject(); JSONObject data = new JSONObject();
float[] ll = new float[2]; float[] ll = new float[2];
data.put("name", map.get("project_name")); data.put("name", map.get("project_name"));
if (map.get("longitude") == null || map.get("latitude") == null) {
continue;
}
ll[0] = Float.parseFloat(map.get("longitude").toString()); ll[0] = Float.parseFloat(map.get("longitude").toString());
ll[1] = Float.parseFloat(map.get("latitude").toString()); ll[1] = Float.parseFloat(map.get("latitude").toString());
data.put("value", ll); data.put("value", ll);

8
user-service/src/main/java/com/mh/user/strategy/EleProtocolStrategy.java

@ -99,7 +99,7 @@ public class EleProtocolStrategy implements ProtocolStrategy {
@Override @Override
public String analysisReceiveData(DeviceCodeParamEntity deviceCodeParamEntity, String receiveData) { public String analysisReceiveData(DeviceCodeParamEntity deviceCodeParamEntity, String receiveData) {
log.info("电表97/07规约协议:工厂解析报文"); log.info("电表97/07规约协议:工厂解析报文,接收到的报文<=={}",receiveData);
String data = ""; String data = "";
if (receiveData.length() == 34 || receiveData.length() == 36 || receiveData.length() == 40 || receiveData.length() == 44 || receiveData.length() == 50) { if (receiveData.length() == 34 || receiveData.length() == 36 || receiveData.length() == 40 || receiveData.length() == 44 || receiveData.length() == 50) {
String str1 = receiveData.substring(0, 8); String str1 = receiveData.substring(0, 8);
@ -118,9 +118,10 @@ public class EleProtocolStrategy implements ProtocolStrategy {
log.info("电表报文检验失败: {}", receiveData); log.info("电表报文检验失败: {}", receiveData);
} }
} }
if (!StringUtils.isBlank(data)) { if (StringUtils.isBlank(data) || Constant.FAIL.equals(data)) {
data = String.valueOf(Double.valueOf(data)); //00010.76,去除读数前面带0的情况 return Constant.FAIL;
} }
data = String.valueOf(Double.valueOf(data)); //00010.76,去除读数前面带0的情况
// 解析入库 // 解析入库
analysisMeterOrder485(data, deviceCodeParamEntity); analysisMeterOrder485(data, deviceCodeParamEntity);
return data; return data;
@ -130,6 +131,7 @@ public class EleProtocolStrategy implements ProtocolStrategy {
StringBuilder stringBuilder = new StringBuilder(); StringBuilder stringBuilder = new StringBuilder();
// 累计总读数据:FE FE FE FE 68 69 04 00 70 01 08 68 81 06 43 C3 A5 33 33 33 81 16 // 累计总读数据:FE FE FE FE 68 69 04 00 70 01 08 68 81 06 43 C3 A5 33 33 33 81 16
// 瞬时有功功率:FE FE FE FE 68 69 04 00 70 01 08 68 81 05 63 E9 33 33 33 21 16 // 瞬时有功功率:FE FE FE FE 68 69 04 00 70 01 08 68 81 05 63 E9 33 33 33 21 16
// FE FE FE FE 68 98 01 50 04 00 00 68 C1 01 35 B4 16
// 判断属于哪个数据标识 // 判断属于哪个数据标识
String resultData = Constant.FAIL; String resultData = Constant.FAIL;
if (receiveData.length() > 24) { if (receiveData.length() > 24) {

30
user-service/src/main/java/com/mh/user/strategy/ModbusProtocolStrategy.java

@ -61,8 +61,8 @@ public class ModbusProtocolStrategy implements ProtocolStrategy {
try { try {
// 地址(1) + 功能码(1) + 寄存器地址(2) + 数据域(2) + crc校验 // 地址(1) + 功能码(1) + 寄存器地址(2) + 数据域(2) + crc校验
str = ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(meterManageEntity.getMtCode()), 2) // 设备地址 str = ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(meterManageEntity.getMtCode()), 2) // 设备地址
+ ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(meterManageEntity.getFuncCode()), 2) // 功能码 + ExchangeStringUtil.addZeroForNum(meterManageEntity.getFuncCode(), 2) // 功能码
+ ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(meterManageEntity.getRegisterAddr()), 4) // 寄存器地址 + ExchangeStringUtil.addZeroForNum(meterManageEntity.getRegisterAddr(), 4) // 寄存器地址
+ ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(String.valueOf(meterManageEntity.getRegisterSize())), 4); // 读取寄存器个数 + ExchangeStringUtil.addZeroForNum(ExchangeStringUtil.decToHex(String.valueOf(meterManageEntity.getRegisterSize())), 4); // 读取寄存器个数
// 循环冗余校验 // 循环冗余校验
String checkWord = ExchangeStringUtil.getStrCRC16(str); //CRC16校验 String checkWord = ExchangeStringUtil.getStrCRC16(str); //CRC16校验
@ -77,37 +77,54 @@ public class ModbusProtocolStrategy implements ProtocolStrategy {
@Override @Override
public String analysisReceiveData(DeviceCodeParamEntity deviceCodeParamEntity, String receiveData) { public String analysisReceiveData(DeviceCodeParamEntity deviceCodeParamEntity, String receiveData) {
log.info("modbus标准协议:策略解析报文"); log.info("modbus标准协议:策略解析报文");
String sValue = null;
try {
String checkStr = receiveData.substring(0, receiveData.length() - 4);//检验报文 String checkStr = receiveData.substring(0, receiveData.length() - 4);//检验报文
String checkWord = ExchangeStringUtil.getStrCRC16(checkStr);//生成校验码 String checkWord = ExchangeStringUtil.getStrCRC16(checkStr);//生成校验码
String sValue = "0"; sValue = "0";
String rtData = Constant.FAIL; String rtData = Constant.FAIL;
if (!checkWord.equalsIgnoreCase(receiveData.substring(receiveData.length() - 4))) { if (!checkWord.equalsIgnoreCase(receiveData.substring(receiveData.length() - 4))) {
log.info("Modbus报文检验失败: {}", receiveData); log.info("Modbus报文检验失败: {}", receiveData);
return rtData; return rtData;
} }
// 开始解析: 地址+功能码+数据长度+数据域 // 开始解析: 地址+功能码+数据长度+数据域
// 03 01 01 01 91 F0
// 03 01 01 01 91 F0
// 截取数据长度 // 截取数据长度
String dataLength = receiveData.substring(4, 6); String dataLength = receiveData.substring(4, 6);
int dataLengthInt = Integer.parseInt(dataLength, 16); int dataLengthInt = Integer.parseInt(dataLength, 16);
// 截取数据域 // 截取数据域
String data = receiveData.substring(6, 6 + dataLengthInt * 2); String data = receiveData;
if ("2".equals(deviceCodeParamEntity.getDeviceType())) {
data = data.substring(data.length() - 8, data.length() - 6)
+ data.substring(data.length() - 6, data.length() - 4)
+ data.substring(data.length() - 12, data.length() - 10)
+ data.substring(data.length() - 10, data.length() - 8);
} else {
data = receiveData.substring(6, 6 + dataLengthInt * 2);
}
// 判断 // 判断
switch (deviceCodeParamEntity.getDataType()) { switch (deviceCodeParamEntity.getDataType()) {
case 0: case 0:
case 1: case 1:
// 16进制转十进制类型 // 16进制转十进制类型
sValue = ExchangeStringUtil.hexToDec(data); sValue = ExchangeStringUtil.hexToDec(data);
if (deviceCodeParamEntity.getDigit() == 0) {
sValue = String.valueOf(Long.parseLong(sValue));
} else {
// 保留位数 // 保留位数
sValue = (new BigDecimal(sValue)).divide(new BigDecimal(String.valueOf(deviceCodeParamEntity.getDigit() * 10)), 2, RoundingMode.HALF_UP).toString(); sValue = (new BigDecimal(sValue)).divide(new BigDecimal(String.valueOf(deviceCodeParamEntity.getDigit() * 10)), 2, RoundingMode.HALF_UP).toString();
}
break; break;
case 2: case 2:
// 十六进制字符串转IEEE754浮点型 // 十六进制字符串转IEEE754浮点型
sValue = String.valueOf(ExchangeStringUtil.hexToSingle(data)); sValue = String.valueOf(Math.abs(ExchangeStringUtil.hexToSingle(data)));
break; break;
default: default:
break; break;
} }
log.info("解析数据==>表号:{},寄存器地址:{},值:{}", deviceCodeParamEntity.getDeviceAddr(), deviceCodeParamEntity.getRegisterAddr(), sValue); log.info("解析数据==>表号:{},寄存器地址:{},值:{}", deviceCodeParamEntity.getDeviceAddr(), deviceCodeParamEntity.getRegisterAddr(), sValue);
// 入库数据 // 入库数据
// 冷量表 // 冷量表
if ("2".equals(deviceCodeParamEntity.getDeviceType())) { if ("2".equals(deviceCodeParamEntity.getDeviceType())) {
@ -115,6 +132,9 @@ public class ModbusProtocolStrategy implements ProtocolStrategy {
} else if ("0".equals(deviceCodeParamEntity.getDeviceType())) { } else if ("0".equals(deviceCodeParamEntity.getDeviceType())) {
analysisChillerOrder485(sValue, deviceCodeParamEntity); analysisChillerOrder485(sValue, deviceCodeParamEntity);
} }
} catch (NumberFormatException e) {
log.error("modbus标准协议:策略解析报文异常", e);
}
return sValue; return sValue;
} }

Loading…
Cancel
Save