|
@@ -11,6 +11,7 @@ package me.zhengjie.modules.quartz.task;/**
|
|
|
* Description: iot-ykt
|
|
|
*/
|
|
|
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
import com.digitalpetri.modbus.codec.Modbus;
|
|
|
import com.digitalpetri.modbus.master.ModbusTcpMaster;
|
|
|
import com.digitalpetri.modbus.master.ModbusTcpMasterConfig;
|
|
@@ -26,6 +27,9 @@ 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.modbus.channel.domain.DmModbusChannel;
|
|
|
import me.zhengjie.modules.dm.modbus.data.domain.DmModbusData;
|
|
|
import me.zhengjie.modules.dm.modbus.device.domain.DmModbusDevice;
|
|
@@ -34,6 +38,7 @@ 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;
|
|
@@ -41,6 +46,7 @@ import org.springframework.web.bind.annotation.InitBinder;
|
|
|
|
|
|
import javax.annotation.PostConstruct;
|
|
|
import javax.annotation.PreDestroy;
|
|
|
+import java.util.HashSet;
|
|
|
import java.util.List;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Set;
|
|
@@ -61,6 +67,9 @@ import java.util.concurrent.ExecutionException;
|
|
|
public class ModbusTask {
|
|
|
|
|
|
private final DmModbusDriveService dmModbusDriveService;
|
|
|
+
|
|
|
+ private final DmEnvHistoryModbusDataService dmEnvHistoryModbusDataService;
|
|
|
+
|
|
|
@Value("${modbusUrl}")
|
|
|
private String modbusUrl; // modbus默认地址
|
|
|
private final int modbusPort = 502; // modbus默认端口号
|
|
@@ -112,24 +121,27 @@ public class ModbusTask {
|
|
|
*/
|
|
|
List<DmModbusData> dmModbusData = group.getDmModbusData();
|
|
|
if (!dmModbusData.isEmpty()){
|
|
|
+ DmEnvHistoryModbusData dmEnvHistoryModbusData = new DmEnvHistoryModbusData();
|
|
|
+// DmEnvHistoryData dmEnvHistoryData = new DmEnvHistoryData();
|
|
|
+// Set<DmEnvHistoryData> dmEnvHistoryDataSet = new HashSet<>();
|
|
|
//已经确认 空气质量统一用3X(Input Register) 数据类型为short
|
|
|
dmModbusData.forEach(data->{
|
|
|
switch (data.getSoftwareRegisterType()){
|
|
|
- case "3X(Input Register)":
|
|
|
- log.info("调用读取InputRegisters模拟量数据");
|
|
|
- try {
|
|
|
- Number number = readInputRegisters(Integer.valueOf(data.getSoftwareRegisterAddress()), 1, 1);
|
|
|
-
|
|
|
- } catch (InterruptedException e) {
|
|
|
- log.error("调用读取InputRegisters模拟量数据,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
- } catch (ExecutionException e) {
|
|
|
- log.error("调用读取InputRegisters模拟量数据,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
- }
|
|
|
- break;
|
|
|
+// case "3X(Input Register)":
|
|
|
+// log.info("调用读取InputRegisters模拟量数据");
|
|
|
+// try {
|
|
|
+// Number number = readInputRegisters(Integer.valueOf(data.getSoftwareRegisterAddress()), 1, modbusSalveId);
|
|
|
+//
|
|
|
+// } catch (InterruptedException e) {
|
|
|
+// log.error("调用读取InputRegisters模拟量数据,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
+// } catch (ExecutionException e) {
|
|
|
+// log.error("调用读取InputRegisters模拟量数据,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
+// }
|
|
|
+// break;
|
|
|
case "4X(Holding Register)":
|
|
|
log.info("调用读取HoldingRegister数据方法");
|
|
|
try {
|
|
|
- Number number = readHoldingRegisters(Integer.valueOf(data.getSoftwareRegisterAddress()), 1, 1);
|
|
|
+ Number number = readHoldingRegisters(Integer.valueOf(data.getSoftwareRegisterAddress()), 1, modbusSalveId);
|
|
|
} catch (InterruptedException e) {
|
|
|
log.error("调用读取HoldingRegister数据方法,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
} catch (ExecutionException e) {
|
|
@@ -139,7 +151,7 @@ public class ModbusTask {
|
|
|
case "1X(Input Status)":
|
|
|
log.info("调用读取readDiscreteInputs开关量方法");
|
|
|
try {
|
|
|
- Boolean aBoolean = readDiscreteInputs(Integer.valueOf(data.getSoftwareRegisterAddress()), 1, 1);
|
|
|
+ Boolean aBoolean = readDiscreteInputs(Integer.valueOf(data.getSoftwareRegisterAddress()), 1, modbusSalveId);
|
|
|
} catch (InterruptedException e) {
|
|
|
log.error("调用读取HoldingRegister数据方法,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
} catch (ExecutionException e) {
|
|
@@ -149,15 +161,58 @@ public class ModbusTask {
|
|
|
case "0X(Coil Status)":
|
|
|
log.info("调用读取Coils开关量,{readCoils}方法");
|
|
|
try {
|
|
|
- Boolean aBoolean = readCoils(Integer.valueOf(data.getSoftwareRegisterAddress()), 1, 1);
|
|
|
+ Boolean aBoolean = readCoils(Integer.valueOf(data.getSoftwareRegisterAddress()), 1, modbusSalveId);
|
|
|
} catch (InterruptedException e) {
|
|
|
e.printStackTrace();
|
|
|
} catch (ExecutionException e) {
|
|
|
e.printStackTrace();
|
|
|
}
|
|
|
break;
|
|
|
- }
|
|
|
+ default:
|
|
|
+ //默认使用 3X(Input Register)
|
|
|
+ log.info("调用读取InputRegisters模拟量数据");
|
|
|
+ try {
|
|
|
+ // 读取数据 数据类型为 float
|
|
|
+ Number number = readInputRegisters(Integer.valueOf(data.getSoftwareRegisterAddress()), 1, modbusSalveId);
|
|
|
+ //获取点名 例如 1F_CO2
|
|
|
+ String callTheRoll = data.getCallTheRoll();
|
|
|
+ // 获取楼层id
|
|
|
+ String F = StringUtils.substringBefore(callTheRoll, "F");
|
|
|
+ // 获取检测的数据名称
|
|
|
+ String dataName = StringUtils.substringAfterLast(callTheRoll, "_");
|
|
|
+ switch (dataName){
|
|
|
+ case "CHO":
|
|
|
+ dmEnvHistoryModbusData.setCho(number.toString());
|
|
|
+ break;
|
|
|
+ case "CO2":
|
|
|
+ dmEnvHistoryModbusData.setCo2(number.toString());
|
|
|
+ break;
|
|
|
+ case "H":
|
|
|
+ dmEnvHistoryModbusData.setH(number.toString());
|
|
|
+ break;
|
|
|
+ case "PM10":
|
|
|
+ dmEnvHistoryModbusData.setPm10(number.toString());
|
|
|
+ break;
|
|
|
+ case "T":
|
|
|
+ dmEnvHistoryModbusData.setT(number.toString());
|
|
|
+ break;
|
|
|
+ case "VOC":
|
|
|
+ dmEnvHistoryModbusData.setVoc(number.toString());
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
+ //设置关联id
|
|
|
+ dmEnvHistoryModbusData.setDataId(Long.valueOf(F));
|
|
|
+
|
|
|
+ } catch (InterruptedException e) {
|
|
|
+ log.error("调用读取InputRegisters模拟量数据,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
+ } catch (ExecutionException e) {
|
|
|
+ log.error("调用读取InputRegisters模拟量数据,modbus读取数据失败,失败原因:{}",e.getMessage());
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ dmEnvHistoryModbusDataService.create(dmEnvHistoryModbusData);
|
|
|
+ log.info("添加的modbus的数据,{}" + JSONObject.toJSONString(dmEnvHistoryModbusData) );
|
|
|
});
|
|
|
}
|
|
|
});
|
|
@@ -168,9 +223,11 @@ public class ModbusTask {
|
|
|
}
|
|
|
});
|
|
|
|
|
|
- log.info(dmModbusDriveDtos.toString());
|
|
|
+ log.info("所有的驱动下的数据,{}" + JSONObject.toJSONString(dmModbusDriveDtos) );
|
|
|
+ release();
|
|
|
}catch (Exception e){
|
|
|
log.error("modbus获取失败,失败原因:{}",e.getMessage());
|
|
|
+ release();
|
|
|
}
|
|
|
|
|
|
|