提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.channel.modbus.collector; |
H |
2 |
|
|
3 |
import com.iailab.module.data.common.enums.CommonConstant; |
|
4 |
import com.iailab.module.data.common.enums.DataSourceType; |
|
5 |
import com.iailab.framework.common.util.object.ConvertUtils; |
|
6 |
import com.iailab.module.data.common.utils.TagUtils; |
|
7 |
import com.iailab.module.data.channel.modbus.dto.ChannelModBusDeviceDTO; |
|
8 |
import com.iailab.module.data.channel.modbus.entity.ChannelModBusDeviceEntity; |
|
9 |
import com.iailab.module.data.channel.modbus.service.ChannelModbusDeviceService; |
|
10 |
import com.serotonin.modbus4j.ModbusMaster; |
|
11 |
import com.serotonin.modbus4j.code.DataType; |
|
12 |
import lombok.extern.slf4j.Slf4j; |
|
13 |
import javax.annotation.Resource; |
|
14 |
import org.springframework.stereotype.Component; |
|
15 |
import org.springframework.util.CollectionUtils; |
|
16 |
|
|
17 |
import java.util.HashMap; |
|
18 |
import java.util.List; |
|
19 |
import java.util.Map; |
|
20 |
import java.util.concurrent.ConcurrentHashMap; |
|
21 |
|
|
22 |
/** |
|
23 |
* @author PanZhibao |
|
24 |
* @Description |
|
25 |
* @createTime 2024年05月12日 |
|
26 |
*/ |
|
27 |
@Slf4j |
|
28 |
@Component |
|
29 |
public class ModBusCollector { |
|
30 |
|
|
31 |
@Resource |
|
32 |
private ChannelModbusDeviceService channelModbusDeviceService; |
|
33 |
|
|
34 |
private Map<String, ModbusMaster> clientMap = new ConcurrentHashMap<>(); |
|
35 |
|
|
36 |
private Map<String, ChannelModBusDeviceEntity> deviceMap = new HashMap<>(); |
|
37 |
|
|
38 |
private synchronized ModbusMaster getMaster(String sourceId) throws Exception { |
|
39 |
log.info("=========Modbus============"); |
|
40 |
try { |
|
41 |
if (!clientMap.containsKey(sourceId)) { |
|
42 |
ChannelModBusDeviceEntity entity = channelModbusDeviceService.info(sourceId); |
|
43 |
ChannelModBusDeviceDTO tModbusDTO = ConvertUtils.sourceToTarget(entity, ChannelModBusDeviceDTO.class); |
|
44 |
ModbusMaster modbusMaster = ModbusUtils.getMaster(tModbusDTO); |
|
45 |
if (modbusMaster != null) { |
|
46 |
clientMap.put(sourceId, modbusMaster); |
|
47 |
} |
|
48 |
} |
|
49 |
} catch (Exception ex) { |
|
50 |
log.error("=========Modbus Exception============"); |
|
51 |
log.error("ex.message+" + ex.getMessage()); |
|
52 |
throw new Exception(ex.getMessage()); |
|
53 |
} |
|
54 |
return clientMap.get(sourceId); |
|
55 |
} |
|
56 |
|
|
57 |
public Double getTagValue(String sourceId, String tagNo) { |
|
58 |
Double result = CommonConstant.BAD_VALUE.doubleValue(); |
|
59 |
try { |
|
60 |
ModbusMaster modbusMaster = this.getMaster(sourceId); |
|
61 |
// 获取寄存器类型 |
|
62 |
String type = tagNo.substring(0, 1); |
|
63 |
int slaveId = 1; |
|
64 |
int offset = Integer.parseInt(tagNo.substring(1)) - 1; |
|
65 |
switch (type) { |
|
66 |
case "0": |
|
67 |
// 读线圈寄存器 |
|
68 |
result = ModbusUtils.readCoilStatus(modbusMaster, slaveId, offset) ? 1d : 0; |
|
69 |
break; |
|
70 |
case "1": |
|
71 |
// 读离散输入寄存器 |
|
72 |
result = ModbusUtils.inputStatus(modbusMaster, slaveId, offset) ? 1d : 0; |
|
73 |
break; |
|
74 |
case "3": |
|
75 |
// 读输入寄存器 |
|
76 |
result = ModbusUtils.inputRegister(modbusMaster, slaveId, offset, DataType.TWO_BYTE_INT_SIGNED) |
|
77 |
.doubleValue(); |
|
78 |
break; |
|
79 |
case "4": |
|
80 |
// 读输入寄存器数据 |
|
81 |
result = ModbusUtils.holdingRegister(modbusMaster, slaveId, offset, DataType.TWO_BYTE_INT_SIGNED) |
|
82 |
.doubleValue(); |
|
83 |
break; |
|
84 |
default: |
|
85 |
break; |
|
86 |
} |
|
87 |
return result; |
|
88 |
} catch (Exception ex) { |
|
89 |
log.error("TagNo========" + tagNo); |
|
90 |
ex.printStackTrace(); |
|
91 |
} |
|
92 |
return result; |
|
93 |
} |
|
94 |
|
|
95 |
public void setTagValue(String sourceId, String tagNo, String newValue) { |
|
96 |
try { |
|
97 |
ModbusMaster modbusMaster = this.getMaster(sourceId); |
|
98 |
// 获取寄存器类型 |
|
99 |
String type = tagNo.substring(0, 1); |
|
100 |
int slaveId = 1; |
|
101 |
int offset = Integer.parseInt(tagNo.substring(1)) - 1; |
|
102 |
switch (type) { |
|
103 |
case "0": |
|
104 |
// 写单个线圈寄存器 |
|
105 |
ModbusUtils.writeCoilStatus(modbusMaster, slaveId, offset, Integer.parseInt(newValue) > 0 ); |
|
106 |
break; |
|
107 |
case "4": |
|
108 |
// 写单个保持寄存器 |
|
109 |
ModbusUtils.writeRegister(modbusMaster, slaveId, offset, Integer.parseInt(newValue)); |
|
110 |
break; |
|
111 |
default: |
|
112 |
break; |
|
113 |
} |
|
114 |
} catch (Exception ex) { |
|
115 |
ex.printStackTrace(); |
|
116 |
} |
|
117 |
} |
|
118 |
|
|
119 |
public Map<String, Object> getTagValues(List<String[]> params) { |
|
120 |
if (CollectionUtils.isEmpty(params)) { |
|
121 |
return new HashMap<>(); |
|
122 |
} |
|
123 |
Map<String, Object> result = new HashMap<>(params.size()); |
|
124 |
params.forEach(item -> { |
|
125 |
try { |
|
126 |
Double value = this.getTagValue(item[0], item[1]); |
|
127 |
result.put(TagUtils.genTagId(DataSourceType.ModBus.getCode(), deviceMap.get(item[0]).getName(), item[1]), value); |
|
128 |
} catch (Exception ex) { |
|
129 |
ex.printStackTrace(); |
|
130 |
result.put(TagUtils.genTagId(DataSourceType.ModBus.getCode(), deviceMap.get(item[0]).getName(), item[1]), CommonConstant.BAD_VALUE); |
|
131 |
} |
|
132 |
}); |
|
133 |
return result; |
|
134 |
} |
|
135 |
} |