|
@@ -11,6 +11,7 @@ package me.zhengjie.modules.quartz.task;/**
|
|
|
* Description: iot-ykt
|
|
|
*/
|
|
|
|
|
|
+import cn.hutool.core.util.ObjectUtil;
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
import com.digitalpetri.modbus.codec.Modbus;
|
|
|
import com.digitalpetri.modbus.master.ModbusTcpMaster;
|
|
@@ -27,28 +28,29 @@ import io.netty.buffer.ByteBuf;
|
|
|
import io.netty.util.ReferenceCountUtil;
|
|
|
import lombok.RequiredArgsConstructor;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
-import me.zhengjie.modules.dm.envmonitor.historydata.domain.DmEnvHistoryData;
|
|
|
import me.zhengjie.modules.dm.envmonitor.historymodbusdata.domain.DmEnvHistoryModbusData;
|
|
|
import me.zhengjie.modules.dm.envmonitor.historymodbusdata.service.DmEnvHistoryModbusDataService;
|
|
|
+import me.zhengjie.modules.dm.envmonitor.historywarn.domain.DmEnvHistoryWarn;
|
|
|
+import me.zhengjie.modules.dm.envmonitor.historywarn.service.DmEnvHistoryWarnService;
|
|
|
+import me.zhengjie.modules.dm.envmonitor.historywarnconfig.domain.DmEnvHistoryWarnConfig;
|
|
|
+import me.zhengjie.modules.dm.envmonitor.historywarnconfig.service.DmEnvHistoryWarnConfigService;
|
|
|
import me.zhengjie.modules.dm.modbus.channel.domain.DmModbusChannel;
|
|
|
import me.zhengjie.modules.dm.modbus.data.domain.DmModbusData;
|
|
|
import me.zhengjie.modules.dm.modbus.device.domain.DmModbusDevice;
|
|
|
-import me.zhengjie.modules.dm.modbus.drive.domain.DmModbusDrive;
|
|
|
import me.zhengjie.modules.dm.modbus.drive.service.DmModbusDriveService;
|
|
|
import me.zhengjie.modules.dm.modbus.drive.service.dto.DmModbusDriveDto;
|
|
|
import me.zhengjie.modules.dm.modbus.group.domain.DmModbusGroup;
|
|
|
-import me.zhengjie.modules.system.repository.UserRepository;
|
|
|
import me.zhengjie.utils.StringUtils;
|
|
|
-import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.web.bind.annotation.InitBinder;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.PreDestroy;
|
|
|
+import java.sql.Timestamp;
|
|
|
import java.util.*;
|
|
|
import java.util.concurrent.CompletableFuture;
|
|
|
import java.util.concurrent.ExecutionException;
|
|
|
+import java.util.concurrent.atomic.AtomicReference;
|
|
|
|
|
|
/**
|
|
|
* <功能简要> <br>
|
|
@@ -67,11 +69,15 @@ public class ModbusTask {
|
|
|
|
|
|
private final DmEnvHistoryModbusDataService dmEnvHistoryModbusDataService;
|
|
|
|
|
|
+ private final DmEnvHistoryWarnConfigService dmEnvHistoryWarnConfigService;
|
|
|
+
|
|
|
+ private final DmEnvHistoryWarnService dmEnvHistoryWarnService;
|
|
|
+
|
|
|
@Value("${modbusUrl}")
|
|
|
private String modbusUrl; // modbus默认地址
|
|
|
private final int modbusPort = 502; // modbus默认端口号
|
|
|
private final int modbusSalveId = 1; //modbus默认从机地址
|
|
|
- private static ModbusTcpMaster master;
|
|
|
+ private ModbusTcpMaster master;
|
|
|
|
|
|
/**
|
|
|
* 获取TCP协议的Master
|
|
@@ -89,7 +95,7 @@ public class ModbusTask {
|
|
|
|
|
|
public void gainModbusData() {
|
|
|
try {
|
|
|
- init();
|
|
|
+// init();
|
|
|
/**
|
|
|
* 根据sql查询所有总集合
|
|
|
*/
|
|
@@ -123,7 +129,7 @@ public class ModbusTask {
|
|
|
// DmEnvHistoryData dmEnvHistoryData = new DmEnvHistoryData();
|
|
|
// Set<DmEnvHistoryData> dmEnvHistoryDataSet = new HashSet<>();
|
|
|
//已经确认 空气质量统一用3X(Input Register) 数据类型为short
|
|
|
-
|
|
|
+ AtomicReference<String> F = new AtomicReference<>("");
|
|
|
dmModbusData.forEach(data->{
|
|
|
try {
|
|
|
switch (data.getSoftwareRegisterType()){
|
|
@@ -172,49 +178,138 @@ public class ModbusTask {
|
|
|
//默认使用 3X(Input Register)
|
|
|
log.info("调用读取InputRegisters模拟量数据");
|
|
|
// 读取数据 数据类型为 float Integer.valueOf(data.getSoftwareRegisterAddress())
|
|
|
- Number number = readInputRegisters(Integer.valueOf(data.getSoftwareRegisterAddress()), 4, modbusSalveId);
|
|
|
+ Number number = readInputRegisters(Integer.valueOf(data.getSoftwareRegisterAddress())-1, 4, modbusSalveId);
|
|
|
+// Number number = 321;
|
|
|
//获取点名 例如 1F_CO2
|
|
|
String callTheRoll = data.getCallTheRoll();
|
|
|
log.info("{}寄存器地址:{}",callTheRoll,data.getSoftwareRegisterAddress());
|
|
|
// 获取楼层id
|
|
|
- String F = StringUtils.substringBefore(callTheRoll, "F");
|
|
|
+ F.set(StringUtils.substringBefore(callTheRoll, "F"));
|
|
|
// 获取检测的数据名称
|
|
|
String dataName = StringUtils.substringAfterLast(callTheRoll, "_");
|
|
|
switch (dataName){
|
|
|
case "CHO":
|
|
|
+ DmEnvHistoryWarnConfig dmEnvHistoryWarnConfigCho = dmEnvHistoryWarnConfigService.selectWarnDataByName("CHO");
|
|
|
dmEnvHistoryModbusData.setCho(number.toString());
|
|
|
+ if (!ObjectUtil.isEmpty(dmEnvHistoryWarnConfigCho) && Integer.valueOf(dmEnvHistoryModbusData.getCho())>=Integer.valueOf(dmEnvHistoryWarnConfigCho.getWarnValue())){
|
|
|
+ DmEnvHistoryWarn dmEnvHistoryWarn = new DmEnvHistoryWarn();
|
|
|
+ dmEnvHistoryWarn.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
|
|
+ dmEnvHistoryWarn.setDeviceIdentifier(device.getDeviceId().toString());
|
|
|
+ dmEnvHistoryWarn.setDeviceName(device.getDeviceName());
|
|
|
+ dmEnvHistoryWarn.setWarnIndex(dmEnvHistoryWarnConfigCho.getWarnValue());
|
|
|
+ dmEnvHistoryWarn.setWarnValue(dmEnvHistoryModbusData.getCho());
|
|
|
+ dmEnvHistoryWarn.setWarnContent("甲醛过高");
|
|
|
+ dmEnvHistoryWarnService.create(dmEnvHistoryWarn);
|
|
|
+ }
|
|
|
break;
|
|
|
case "CO2":
|
|
|
+ DmEnvHistoryWarnConfig dmEnvHistoryWarnConfigCo2 = dmEnvHistoryWarnConfigService.selectWarnDataByName("CO2");
|
|
|
dmEnvHistoryModbusData.setCo2(number.toString());
|
|
|
+ if (!ObjectUtil.isEmpty(dmEnvHistoryWarnConfigCo2) && Integer.valueOf(dmEnvHistoryModbusData.getCo2())>=Integer.valueOf(dmEnvHistoryWarnConfigCo2.getWarnValue())){
|
|
|
+ DmEnvHistoryWarn dmEnvHistoryWarn = new DmEnvHistoryWarn();
|
|
|
+ dmEnvHistoryWarn.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
|
|
+ dmEnvHistoryWarn.setDeviceIdentifier(device.getDeviceId().toString());
|
|
|
+ dmEnvHistoryWarn.setDeviceName(device.getDeviceName());
|
|
|
+ dmEnvHistoryWarn.setWarnIndex(dmEnvHistoryWarnConfigCo2.getWarnValue());
|
|
|
+ dmEnvHistoryWarn.setWarnValue(dmEnvHistoryModbusData.getCo2());
|
|
|
+ dmEnvHistoryWarn.setWarnContent("二氧化碳过高");
|
|
|
+ dmEnvHistoryWarnService.create(dmEnvHistoryWarn);
|
|
|
+ }
|
|
|
break;
|
|
|
case "H":
|
|
|
+ DmEnvHistoryWarnConfig dmEnvHistoryWarnConfigH = dmEnvHistoryWarnConfigService.selectWarnDataByName("H");
|
|
|
dmEnvHistoryModbusData.setH(number.toString());
|
|
|
+ if (!ObjectUtil.isEmpty(dmEnvHistoryWarnConfigH) && Integer.valueOf(dmEnvHistoryModbusData.getH())>=Integer.valueOf(dmEnvHistoryWarnConfigH.getWarnValue())){
|
|
|
+ DmEnvHistoryWarn dmEnvHistoryWarn = new DmEnvHistoryWarn();
|
|
|
+ dmEnvHistoryWarn.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
|
|
+ dmEnvHistoryWarn.setDeviceIdentifier(device.getDeviceId().toString());
|
|
|
+ dmEnvHistoryWarn.setDeviceName(device.getDeviceName());
|
|
|
+ dmEnvHistoryWarn.setWarnIndex(dmEnvHistoryWarnConfigH.getWarnValue());
|
|
|
+ dmEnvHistoryWarn.setWarnValue(dmEnvHistoryModbusData.getH());
|
|
|
+ dmEnvHistoryWarn.setWarnContent("湿度过高");
|
|
|
+ dmEnvHistoryWarnService.create(dmEnvHistoryWarn);
|
|
|
+ }
|
|
|
break;
|
|
|
case "PM10":
|
|
|
+ DmEnvHistoryWarnConfig dmEnvHistoryWarnConfigPm10 = dmEnvHistoryWarnConfigService.selectWarnDataByName("PM10");
|
|
|
dmEnvHistoryModbusData.setPm10(number.toString());
|
|
|
+ if (!ObjectUtil.isEmpty(dmEnvHistoryWarnConfigPm10) && Integer.valueOf(dmEnvHistoryModbusData.getPm10())>=Integer.valueOf(dmEnvHistoryWarnConfigPm10.getWarnValue())){
|
|
|
+ DmEnvHistoryWarn dmEnvHistoryWarn = new DmEnvHistoryWarn();
|
|
|
+ dmEnvHistoryWarn.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
|
|
+ dmEnvHistoryWarn.setDeviceIdentifier(device.getDeviceId().toString());
|
|
|
+ dmEnvHistoryWarn.setDeviceName(device.getDeviceName());
|
|
|
+ dmEnvHistoryWarn.setWarnIndex(dmEnvHistoryWarnConfigPm10.getWarnValue());
|
|
|
+ dmEnvHistoryWarn.setWarnValue(dmEnvHistoryModbusData.getPm10());
|
|
|
+ dmEnvHistoryWarn.setWarnContent("pm10过高");
|
|
|
+ dmEnvHistoryWarnService.create(dmEnvHistoryWarn);
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ case "PM25":
|
|
|
+ DmEnvHistoryWarnConfig dmEnvHistoryWarnConfigPm25 = dmEnvHistoryWarnConfigService.selectWarnDataByName("PM25");
|
|
|
+ dmEnvHistoryModbusData.setPm25(number.toString());
|
|
|
+ if (!ObjectUtil.isEmpty(dmEnvHistoryWarnConfigPm25) && Integer.valueOf(dmEnvHistoryModbusData.getPm25())>=Integer.valueOf(dmEnvHistoryWarnConfigPm25.getWarnValue())){
|
|
|
+ DmEnvHistoryWarn dmEnvHistoryWarn = new DmEnvHistoryWarn();
|
|
|
+ dmEnvHistoryWarn.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
|
|
+ dmEnvHistoryWarn.setDeviceIdentifier(device.getDeviceId().toString());
|
|
|
+ dmEnvHistoryWarn.setDeviceName(device.getDeviceName());
|
|
|
+ dmEnvHistoryWarn.setWarnIndex(dmEnvHistoryWarnConfigPm25.getWarnValue());
|
|
|
+ dmEnvHistoryWarn.setWarnValue(dmEnvHistoryModbusData.getPm25());
|
|
|
+ dmEnvHistoryWarn.setWarnContent("pm2.5过高");
|
|
|
+ dmEnvHistoryWarnService.create(dmEnvHistoryWarn);
|
|
|
+ }
|
|
|
break;
|
|
|
case "T":
|
|
|
+ DmEnvHistoryWarnConfig dmEnvHistoryWarnConfigT = dmEnvHistoryWarnConfigService.selectWarnDataByName("T");
|
|
|
dmEnvHistoryModbusData.setT(number.toString());
|
|
|
+ if (!ObjectUtil.isEmpty(dmEnvHistoryWarnConfigT) && Integer.valueOf(dmEnvHistoryModbusData.getT())>=Integer.valueOf(dmEnvHistoryWarnConfigT.getWarnValue())){
|
|
|
+ DmEnvHistoryWarn dmEnvHistoryWarn = new DmEnvHistoryWarn();
|
|
|
+ dmEnvHistoryWarn.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
|
|
+ dmEnvHistoryWarn.setDeviceIdentifier(device.getDeviceId().toString());
|
|
|
+ dmEnvHistoryWarn.setDeviceName(device.getDeviceName());
|
|
|
+ dmEnvHistoryWarn.setWarnIndex(dmEnvHistoryWarnConfigT.getWarnValue());
|
|
|
+ dmEnvHistoryWarn.setWarnValue(dmEnvHistoryModbusData.getT());
|
|
|
+ dmEnvHistoryWarn.setWarnContent("温度过高");
|
|
|
+ dmEnvHistoryWarnService.create(dmEnvHistoryWarn);
|
|
|
+ }
|
|
|
break;
|
|
|
case "VOC":
|
|
|
+ DmEnvHistoryWarnConfig dmEnvHistoryWarnConfigVoc = dmEnvHistoryWarnConfigService.selectWarnDataByName("VOC");
|
|
|
dmEnvHistoryModbusData.setVoc(number.toString());
|
|
|
+ if (!ObjectUtil.isEmpty(dmEnvHistoryWarnConfigVoc) && Integer.valueOf(dmEnvHistoryModbusData.getVoc())>=Integer.valueOf(dmEnvHistoryWarnConfigVoc.getWarnValue())){
|
|
|
+ DmEnvHistoryWarn dmEnvHistoryWarn = new DmEnvHistoryWarn();
|
|
|
+ dmEnvHistoryWarn.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
|
|
+ dmEnvHistoryWarn.setDeviceIdentifier(device.getDeviceId().toString());
|
|
|
+ dmEnvHistoryWarn.setDeviceName(device.getDeviceName());
|
|
|
+ dmEnvHistoryWarn.setWarnIndex(dmEnvHistoryWarnConfigVoc.getWarnValue());
|
|
|
+ dmEnvHistoryWarn.setWarnValue(dmEnvHistoryModbusData.getVoc());
|
|
|
+ dmEnvHistoryWarn.setWarnContent("VOC过高");
|
|
|
+ dmEnvHistoryWarnService.create(dmEnvHistoryWarn);
|
|
|
+ }
|
|
|
break;
|
|
|
}
|
|
|
- //设置关联id
|
|
|
- dmEnvHistoryModbusData.setDataId(Long.valueOf(F));
|
|
|
+
|
|
|
break;
|
|
|
}
|
|
|
- }catch (InterruptedException e) {
|
|
|
- log.error("调用读取InputRegisters模拟量数据,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
- return;
|
|
|
- } catch (ExecutionException e) {
|
|
|
+ }catch (Exception e) {
|
|
|
log.error("调用读取InputRegisters模拟量数据,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
return;
|
|
|
}
|
|
|
+// catch (InterruptedException e) {
|
|
|
+// log.error("调用读取InputRegisters模拟量数据,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
+// return;
|
|
|
+// } catch (ExecutionException e) {
|
|
|
+// log.error("调用读取InputRegisters模拟量数据,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
+// return;
|
|
|
+// }
|
|
|
});
|
|
|
- dmEnvHistoryModbusData.setCreateTime(new Date());
|
|
|
- dmEnvHistoryModbusDataService.create(dmEnvHistoryModbusData);
|
|
|
- log.info("添加的modbus的数据,{}" + JSONObject.toJSONString(dmEnvHistoryModbusData) );
|
|
|
+
|
|
|
+ if (!ObjectUtil.isEmpty(dmEnvHistoryModbusData)){
|
|
|
+ //设置关联id
|
|
|
+ dmEnvHistoryModbusData.setDataId(Long.valueOf(F.get()));
|
|
|
+ dmEnvHistoryModbusData.setCreateTime(new Date());
|
|
|
+ dmEnvHistoryModbusDataService.create(dmEnvHistoryModbusData);
|
|
|
+ log.info("添加的modbus的数据,{}" + JSONObject.toJSONString(dmEnvHistoryModbusData) );
|
|
|
+ }
|
|
|
}
|
|
|
});
|
|
|
}
|
|
@@ -225,10 +320,10 @@ public class ModbusTask {
|
|
|
});
|
|
|
|
|
|
log.info("所有的驱动下的数据,{}" + JSONObject.toJSONString(dmModbusDriveDtos) );
|
|
|
- release();
|
|
|
+// release();
|
|
|
}catch (Exception e){
|
|
|
log.error("modbus获取失败,失败原因:{}",e.getMessage());
|
|
|
- release();
|
|
|
+// release();
|
|
|
return;
|
|
|
}
|
|
|
|
|
@@ -321,7 +416,7 @@ public class ModbusTask {
|
|
|
* @throws ExecutionException
|
|
|
* 异常
|
|
|
*/
|
|
|
- public static Number readHoldingRegisters(int address, int quantity, int unitId)
|
|
|
+ public Number readHoldingRegisters(int address, int quantity, int unitId)
|
|
|
throws InterruptedException, ExecutionException {
|
|
|
Number result = null;
|
|
|
CompletableFuture<ReadHoldingRegistersResponse> future = master
|
|
@@ -350,7 +445,7 @@ public class ModbusTask {
|
|
|
* @throws ExecutionException
|
|
|
* 异常
|
|
|
*/
|
|
|
- public static Number readInputRegisters(int address, int quantity, int unitId)
|
|
|
+ public Number readInputRegisters(int address, int quantity, int unitId)
|
|
|
throws InterruptedException, ExecutionException {
|
|
|
Number result = null;
|
|
|
CompletableFuture<ReadInputRegistersResponse> future = master
|