2026-06-30 24681c81c09022f584a57006f2534b5f74723414
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package cn.iocoder.yudao.module.iot.gateway.protocol.modbus.tcpclient.manager;
 
import cn.iocoder.yudao.module.iot.core.biz.dto.IotModbusDeviceConfigRespDTO;
import cn.iocoder.yudao.module.iot.core.biz.dto.IotModbusPointRespDTO;
import cn.iocoder.yudao.module.iot.gateway.protocol.modbus.tcpclient.manager.IotModbusTcpClientPollScheduler.ReadSegment;
import org.junit.jupiter.api.Test;
 
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
 
import static cn.iocoder.yudao.module.iot.gateway.protocol.modbus.common.utils.IotModbusCommonUtils.FC_READ_COILS;
import static cn.iocoder.yudao.module.iot.gateway.protocol.modbus.common.utils.IotModbusCommonUtils.FC_READ_HOLDING_REGISTERS;
import static cn.iocoder.yudao.module.iot.gateway.protocol.modbus.common.utils.IotModbusCommonUtils.FC_READ_INPUT_REGISTERS;
import static org.junit.jupiter.api.Assertions.*;
 
/**
 * {@link IotModbusTcpClientPollScheduler} 的单元测试
 *
 * @author 芋道源码
 */
public class IotModbusTcpClientPollSchedulerTest {
 
    @Test
    public void testBuildReadSegments_mergeContinuousPoints() {
        // 准备参数
        IotModbusPointRespDTO point01 = randomPoint(1L, FC_READ_HOLDING_REGISTERS, 0, 1, 1000);
        IotModbusPointRespDTO point02 = randomPoint(2L, FC_READ_HOLDING_REGISTERS, 1, 2, 1000);
        IotModbusPointRespDTO point03 = randomPoint(3L, FC_READ_HOLDING_REGISTERS, 4, 1, 1000);
        IotModbusDeviceConfigRespDTO config = randomConfig(point01, point02, point03);
 
        // 调用
        List<ReadSegment> segments = IotModbusTcpClientPollScheduler.buildReadSegments(config);
 
        // 断言
        assertEquals(2, segments.size());
        assertEquals(0, segments.get(0).getStartAddress());
        assertEquals(3, segments.get(0).getRegisterCount());
        assertIterableEquals(Arrays.asList(point01, point02), segments.get(0).getPoints());
        assertEquals(4, segments.get(1).getStartAddress());
        assertEquals(1, segments.get(1).getRegisterCount());
        assertIterableEquals(Collections.singletonList(point03), segments.get(1).getPoints());
    }
 
    @Test
    public void testBuildReadSegments_mergeOverlappingPoints() {
        // 准备参数
        IotModbusPointRespDTO point01 = randomPoint(1L, FC_READ_HOLDING_REGISTERS, 0, 2, 1000);
        IotModbusPointRespDTO point02 = randomPoint(2L, FC_READ_HOLDING_REGISTERS, 1, 1, 1000);
        IotModbusDeviceConfigRespDTO config = randomConfig(point01, point02);
 
        // 调用
        List<ReadSegment> segments = IotModbusTcpClientPollScheduler.buildReadSegments(config);
 
        // 断言
        assertEquals(1, segments.size());
        assertEquals(0, segments.get(0).getStartAddress());
        assertEquals(2, segments.get(0).getRegisterCount());
        assertIterableEquals(Arrays.asList(point01, point02), segments.get(0).getPoints());
    }
 
    @Test
    public void testBuildReadSegments_notMergeDifferentFunctionCodeOrPollInterval() {
        // 准备参数
        IotModbusPointRespDTO point01 = randomPoint(1L, FC_READ_HOLDING_REGISTERS, 0, 1, 1000);
        IotModbusPointRespDTO point02 = randomPoint(2L, FC_READ_INPUT_REGISTERS, 1, 1, 1000);
        IotModbusPointRespDTO point03 = randomPoint(3L, FC_READ_HOLDING_REGISTERS, 1, 1, 2000);
        IotModbusDeviceConfigRespDTO config = randomConfig(point01, point02, point03);
 
        // 调用
        List<ReadSegment> segments = IotModbusTcpClientPollScheduler.buildReadSegments(config);
 
        // 断言
        assertEquals(3, segments.size());
        assertEquals(FC_READ_HOLDING_REGISTERS, segments.get(0).getFunctionCode());
        assertEquals(1000, segments.get(0).getPollInterval());
        assertEquals(FC_READ_HOLDING_REGISTERS, segments.get(1).getFunctionCode());
        assertEquals(2000, segments.get(1).getPollInterval());
        assertEquals(FC_READ_INPUT_REGISTERS, segments.get(2).getFunctionCode());
        assertEquals(1000, segments.get(2).getPollInterval());
    }
 
    @Test
    public void testBuildReadSegments_splitWhenExceedsMaxRegisterCount() {
        // 准备参数
        IotModbusPointRespDTO point01 = randomPoint(1L, FC_READ_HOLDING_REGISTERS, 0, 100, 1000);
        IotModbusPointRespDTO point02 = randomPoint(2L, FC_READ_HOLDING_REGISTERS, 100, 30, 1000);
        IotModbusDeviceConfigRespDTO config = randomConfig(point01, point02);
 
        // 调用
        List<ReadSegment> segments = IotModbusTcpClientPollScheduler.buildReadSegments(config);
 
        // 断言
        assertEquals(2, segments.size());
        assertEquals(0, segments.get(0).getStartAddress());
        assertEquals(100, segments.get(0).getRegisterCount());
        assertEquals(100, segments.get(1).getStartAddress());
        assertEquals(30, segments.get(1).getRegisterCount());
    }
 
    @Test
    public void testExtractPointRawValues() {
        // 准备参数
        IotModbusPointRespDTO point = randomPoint(1L, FC_READ_HOLDING_REGISTERS, 12, 2, 1000);
        ReadSegment segment = new ReadSegment(FC_READ_HOLDING_REGISTERS, 1000, 10, 4, Collections.singletonList(point));
        int[] rawValues = new int[]{100, 200, 300, 400};
 
        // 调用
        int[] pointRawValues = IotModbusTcpClientPollScheduler.extractPointRawValues(rawValues, segment, point);
 
        // 断言
        assertArrayEquals(new int[]{300, 400}, pointRawValues);
    }
 
    @Test
    public void testExtractPointRawValuesForCoils() {
        // 准备参数
        IotModbusPointRespDTO point = randomPoint(1L, FC_READ_COILS, 3, 2, 1000);
        ReadSegment segment = new ReadSegment(FC_READ_COILS, 1000, 0, 5, Collections.singletonList(point));
        int[] rawValues = new int[]{1, 0, 1, 1, 0, 0, 0, 0}; // 线圈响应可能按字节补齐
 
        // 调用
        int[] pointRawValues = IotModbusTcpClientPollScheduler.extractPointRawValues(rawValues, segment, point);
 
        // 断言
        assertArrayEquals(new int[]{1, 0}, pointRawValues);
    }
 
    private static IotModbusDeviceConfigRespDTO randomConfig(IotModbusPointRespDTO... points) {
        IotModbusDeviceConfigRespDTO config = new IotModbusDeviceConfigRespDTO();
        config.setDeviceId(1L);
        config.setPoints(Arrays.asList(points));
        return config;
    }
 
    private static IotModbusPointRespDTO randomPoint(Long id, Integer functionCode, Integer registerAddress,
                                                     Integer registerCount, Integer pollInterval) {
        IotModbusPointRespDTO point = new IotModbusPointRespDTO();
        point.setId(id);
        point.setFunctionCode(functionCode);
        point.setRegisterAddress(registerAddress);
        point.setRegisterCount(registerCount);
        point.setPollInterval(pollInterval);
        return point;
    }
 
}