-
Notifications
You must be signed in to change notification settings - Fork 1
/
splitter.go
228 lines (196 loc) · 5.82 KB
/
splitter.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
package modbus
import (
"fmt"
"github.com/aldas/go-modbus-client/packet"
"sort"
)
type splitToFuncType uint8
const (
splitToFC1TCP splitToFuncType = iota
splitToFC1RTU
splitToFC2TCP
splitToFC2RTU
splitToFC3TCP
splitToFC3RTU
splitToFC4TCP
splitToFC4RTU
)
// split groups (by host:port+UnitID, "optimized" max amount of fields for max quantity) fields into packets
func split(fields []Field, funcType splitToFuncType) ([]BuilderRequest, error) {
onlyCoils := funcType == splitToFC1TCP || funcType == splitToFC1RTU || funcType == splitToFC2TCP || funcType == splitToFC2RTU
connectionGroup, err := groupForSingleConnection(fields, onlyCoils)
if err != nil {
return nil, err
}
batches := batchToRequests(connectionGroup)
result := make([]BuilderRequest, 0, len(batches))
for _, b := range batches {
var req packet.Request
var err error
switch funcType {
case splitToFC1TCP:
req, err = packet.NewReadCoilsRequestTCP(b.UnitID, b.StartAddress, b.Quantity)
case splitToFC1RTU:
req, err = packet.NewReadCoilsRequestRTU(b.UnitID, b.StartAddress, b.Quantity)
case splitToFC2TCP:
req, err = packet.NewReadDiscreteInputsRequestTCP(b.UnitID, b.StartAddress, b.Quantity)
case splitToFC2RTU:
req, err = packet.NewReadDiscreteInputsRequestRTU(b.UnitID, b.StartAddress, b.Quantity)
case splitToFC3TCP:
req, err = packet.NewReadHoldingRegistersRequestTCP(b.UnitID, b.StartAddress, b.Quantity)
case splitToFC3RTU:
req, err = packet.NewReadHoldingRegistersRequestRTU(b.UnitID, b.StartAddress, b.Quantity)
case splitToFC4TCP:
req, err = packet.NewReadInputRegistersRequestTCP(b.UnitID, b.StartAddress, b.Quantity)
case splitToFC4RTU:
req, err = packet.NewReadInputRegistersRequestRTU(b.UnitID, b.StartAddress, b.Quantity)
}
if err != nil {
return nil, err
}
result = append(result, BuilderRequest{
Request: req,
ServerAddress: b.Address,
UnitID: b.UnitID,
StartAddress: b.StartAddress,
Fields: b.fields,
})
}
return result, nil
}
// groupForSingleConnection groups fields into groups what can be requested potentially by same request (same server + unit ID + function)
func groupForSingleConnection(fields []Field, onlyCoils bool) ([]builderSlotGroup, error) {
groups := map[string]builderSlotGroup{}
for _, f := range fields {
if err := f.Validate(); err != nil {
return nil, err
}
// create groups by modbus server Address + unitID + isCoil
isCoil := f.Type == FieldTypeCoil
if onlyCoils && !isCoil {
continue
} else if !onlyCoils && isCoil {
continue
}
gID := fmt.Sprintf("%v_%v_%v", f.ServerAddress, f.UnitID, isCoil)
group, ok := groups[gID]
if !ok {
group = builderSlotGroup{
serverAddress: f.ServerAddress,
unitID: f.UnitID,
isForCoils: isCoil,
slots: make([]builderSlot, 0),
}
groups[gID] = group
}
group.AddField(f)
groups[gID] = group
}
// TODO: as of Go 1.23 this could be shortened to `slices.Collect(maps.Values(groups))`
result := make([]builderSlotGroup, 0, len(groups))
for _, g := range groups {
result = append(result, g)
}
return result, nil
}
func batchToRequests(connectionGroup []builderSlotGroup) []requestBatch {
// Coils are always grouped to separate requests (fc1/fc2) from fields suitable for registers (fc3/fc4)
//
// NB: is batching/grouping algorithm is very naive. It just sorts fields by register and creates N number
// of requests of them by limiting quantity to MaxRegistersInReadResponse. It does not try to optimise long caps
// between fields
// assumes that UnitID is same for all fields within group
var result = make([]requestBatch, 0)
for _, slotGroup := range connectionGroup {
address := slotGroup.serverAddress
unitID := slotGroup.unitID
addressLimit := packet.MaxRegistersInReadResponse
if slotGroup.isForCoils {
addressLimit = packet.MaxCoilsInReadResponse
}
sort.Sort(slotsSorter(slotGroup.slots))
batch := requestBatch{}
isFirstSeen := false
var firstAddress uint16
for _, slot := range slotGroup.slots {
slotAddress := slot.address
if !isFirstSeen {
firstAddress = slotAddress
isFirstSeen = true
batch.StartAddress = firstAddress
batch.Address = address
batch.UnitID = unitID
}
slotEndAddress := slotAddress + slot.size
addressDiff := slotEndAddress - firstAddress
if addressDiff > addressLimit {
result = append(result, batch)
batch = requestBatch{
Address: address,
UnitID: unitID,
StartAddress: slotAddress,
}
firstAddress = slotAddress
addressDiff = slot.size
}
if batch.Quantity < addressDiff {
batch.Quantity = addressDiff
}
batch.fields = append(batch.fields, slot.fields...)
}
result = append(result, batch)
}
return result
}
type builderSlot struct {
address uint16
size uint16
fields Fields
}
type builderSlots []builderSlot
func (bs *builderSlots) IndexOf(address uint16) int {
for i, b := range *bs {
if b.address == address {
return i
}
}
return -1
}
type slotsSorter builderSlots
func (a slotsSorter) Len() int { return len(a) }
func (a slotsSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a slotsSorter) Less(i, j int) bool {
return a[i].address < a[j].address
}
type builderSlotGroup struct {
serverAddress string
unitID uint8
isForCoils bool
slots builderSlots
}
func (g *builderSlotGroup) AddField(f Field) {
registerSize := f.registerSize()
i := g.slots.IndexOf(f.Address)
if i == -1 {
g.slots = append(g.slots, builderSlot{
address: f.Address,
size: registerSize,
fields: Fields{f},
})
return
}
slot := g.slots[i]
slot.fields = append(slot.fields, f)
if registerSize > slot.size {
slot.size = registerSize
}
g.slots[i] = slot
}
type requestBatch struct {
Address string
UnitID uint8
StartAddress uint16
Quantity uint16
IsForCoils bool
fields Fields
}