-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbuilder.go
More file actions
368 lines (322 loc) · 11.7 KB
/
builder.go
File metadata and controls
368 lines (322 loc) · 11.7 KB
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
package modbus
import (
"bytes"
"errors"
"fmt"
"strconv"
"strings"
"time"
"github.com/aldas/go-modbus-client/packet"
)
const (
protocolAny ProtocolType = 0 // any or unknown protocol
// ProtocolTCP represents Modbus TCP encoding which could be transferred over TCP/UDP connection.
ProtocolTCP ProtocolType = 1
// ProtocolRTU represents Modbus RTU encoding with a Cyclic-Redundant Checksum. Could be transferred
// over TCP/UDP and serial connection.
ProtocolRTU ProtocolType = 2
// protocolASCII represents Modbus ASCII encoding where each data byte is split into the two bytes
// representing the two ASCII characters in the Hexadecimal value
// NOTE: NOT YET IMPLEMENTED
// protocolASCII ProtocolType = 3
)
// ProtocolType represents which Modbus encoding is being used.
type ProtocolType uint8
// UnmarshalJSON converts raw bytes from JSON to Invalid
func (pt *ProtocolType) UnmarshalJSON(raw []byte) error {
if len(raw) < 4 {
return fmt.Errorf("protocol value too short, given: '%s'", raw)
}
t := string(raw)
if strings.EqualFold(t, `"tcp"`) {
*pt = ProtocolTCP
} else if strings.EqualFold(t, `"rtu"`) {
*pt = ProtocolRTU
} else {
return fmt.Errorf("unknown protocol value, given: '%s'", t)
}
return nil
}
// A Duration represents the elapsed time between two instants. This library type extends time.Duration
// with JSON marshalling and unmarshalling support to/from string. i.e. "1s" unmarshalled to `1 * time.Second`
type Duration time.Duration
// MarshalJSON converts Duration to JSON bytes
func (d Duration) MarshalJSON() ([]byte, error) {
buf := bytes.Buffer{}
buf.WriteRune('"')
buf.WriteString(time.Duration(d).String())
buf.WriteRune('"')
return buf.Bytes(), nil
}
// UnmarshalJSON converts raw bytes from JSON to Duration
func (d *Duration) UnmarshalJSON(raw []byte) error {
const maxDur = time.Hour * 24 * 30 * 12
if len(raw) == 0 {
*d = Duration(0)
return nil
}
if raw[0] != '"' {
v, err := strconv.ParseInt(string(raw), 10, 64)
if err != nil {
return fmt.Errorf("could not parse Duration as int, err: %w", err)
}
if v < 0 {
return fmt.Errorf("unmarshalled duration cannot be negative")
}
if v >= int64(maxDur) { // ~1 year max
return fmt.Errorf("unmarshalled duration cannot be greater than 8640 hours (~1 year)")
}
*d = Duration(v)
return nil
}
if len(raw) < 3 {
return fmt.Errorf("duration value too short, given: '%s'", raw)
}
e := len(raw) - 1
if raw[e] != '"' {
return fmt.Errorf("duration value does not end with quote mark, given: '%s'", raw)
}
tmp, err := time.ParseDuration(string(raw[1:e]))
if err != nil {
return fmt.Errorf("could not parse Duration from string, err: %w", err)
}
if tmp < 0 {
return fmt.Errorf("unmarshalled duration cannot be negative")
}
if tmp >= maxDur { // ~1 year max
return fmt.Errorf("unmarshalled duration cannot be greater than 8640 hours (~1 year)")
}
*d = Duration(tmp)
return nil
}
// Builder helps to group extractable field values of different types into modbus requests with minimal amount of separate requests produced
type Builder struct {
fields Fields
config BuilderDefaults
}
// BuilderDefaults holds Builder default values for adding/creating Fields
type BuilderDefaults struct {
// Should be formatted as url.URL scheme `[scheme:][//[userinfo@]host][/]path[?query]`
// Example:
// * `127.0.0.1:502` (library defaults to `tcp` as scheme)
// * `udp://127.0.0.1:502`
// * `/dev/ttyS0?BaudRate=4800`
// * `file:///dev/ttyUSB?BaudRate=4800`
ServerAddress string `json:"server_address" mapstructure:"server_address"`
FunctionCode uint8 `json:"function_code" mapstructure:"function_code"`
// UnitID default value for added field. Note: if non zero is set as default it will overwrite zero on Field.
UnitID uint8 `json:"unit_id" mapstructure:"unit_id"`
Protocol ProtocolType `json:"protocol" mapstructure:"protocol"`
Interval Duration `json:"interval" mapstructure:"interval"`
// Splitter is used to split fields into requests. If nil DefaultSplitter is used.
Splitter Splitter
}
// NewRequestBuilderWithConfig creates new instance of Builder with given defaults.
// Arguments can be left empty and ServerAddress+UnitID provided for each field separately
func NewRequestBuilderWithConfig(config BuilderDefaults) *Builder {
return &Builder{
fields: make(Fields, 0, 5),
config: config,
}
}
// NewRequestBuilder creates new instance of Builder
func NewRequestBuilder(serverAddress string, unitID uint8) *Builder {
return NewRequestBuilderWithConfig(BuilderDefaults{
ServerAddress: serverAddress,
UnitID: unitID,
})
}
// AddAll adds field into Builder. AddAll does not set ServerAddress and UnitID values.
func (b *Builder) AddAll(fields Fields) *Builder {
for _, field := range fields {
b.add(field)
}
return b
}
// AddField adds field into Builder
func (b *Builder) AddField(field Field) *Builder {
return b.add(field)
}
// Add adds field into Builder
func (b *Builder) add(field Field) *Builder {
if field.ServerAddress == "" {
field.ServerAddress = b.config.ServerAddress
}
if field.FunctionCode == 0 {
field.FunctionCode = b.config.FunctionCode
}
if field.UnitID == 0 && b.config.UnitID != 0 {
field.UnitID = b.config.UnitID
}
if field.Protocol == protocolAny {
field.Protocol = b.config.Protocol
}
if field.RequestInterval == 0 {
field.RequestInterval = b.config.Interval
}
b.fields = append(b.fields, field)
return b
}
// BuilderRequest helps to connect requested fields to responses
type BuilderRequest struct {
packet.Request
// ServerAddress is modbus server address where request should be sent
ServerAddress string
// UnitID is unit identifier of modbus slave device
UnitID uint8
// StartAddress is start register address for request
StartAddress uint16
// Quantity is amount of registers/coils to return with request
Quantity uint16
Protocol ProtocolType
RequestInterval time.Duration
// Fields is slice of field use to construct the request and to be extracted from response
Fields Fields
}
// RegistersResponse is marker interface for responses returning register data
type RegistersResponse interface {
packet.Response
AsRegisters(requestStartAddress uint16) (*packet.Registers, error)
}
// CoilsResponse is marker interface for responses returning coil/discrete data
type CoilsResponse interface {
packet.Response
IsCoilSet(startAddress uint16, coilAddress uint16) (bool, error)
}
// AsRegisters returns response data as Register to more convenient access
func (r BuilderRequest) AsRegisters(response RegistersResponse) (*packet.Registers, error) {
return response.AsRegisters(r.StartAddress)
}
// FieldValue is concrete value extracted from register data using field data type and byte order
type FieldValue struct {
Field Field
// Value contains extracted value
// possible types:
// * bool
// * byte
// * []byte
// * uint[8/16/32/64]
// * int[8/16/32/64]
// * float[32/64]
// * string
Value any
// Error contains error that occurred during extracting field from response.
// In case Field.Invalid was set and response data contained it the Error is set to modbus.ErrInvalidValue
Error error
}
// ErrorFieldExtractHadError is returned when ExtractFields could not extract value from Field
var ErrorFieldExtractHadError = errors.New("field extraction had an error. check FieldValue.Error for details")
// ExtractFields extracts Field values from given response. When continueOnExtractionErrors is true and error occurs
// during extraction, this method does not end but continues to extract all Fields and returns ErrorFieldExtractHadError
// at the end. To distinguish errors check FieldValue.Error field.
func (r BuilderRequest) ExtractFields(response packet.Response, continueOnExtractionErrors bool) ([]FieldValue, error) {
switch resp := response.(type) {
case RegistersResponse:
return r.extractRegisterFields(resp, continueOnExtractionErrors)
case CoilsResponse:
return r.extractCoilFields(resp, continueOnExtractionErrors)
}
return nil, errors.New("can not extract fields from unsupported response type")
}
func (r BuilderRequest) extractRegisterFields(response RegistersResponse, continueOnExtractionErrors bool) ([]FieldValue, error) {
regs, err := response.AsRegisters(r.StartAddress)
if err != nil {
return nil, err
}
hadErrors := false
capacity := 0
if continueOnExtractionErrors {
capacity = len(r.Fields)
}
result := make([]FieldValue, 0, capacity)
for _, f := range r.Fields {
vTmp, err := f.ExtractFrom(regs)
if err != nil && !continueOnExtractionErrors {
return nil, fmt.Errorf("field extraction failed. name: %v err: %w", f.Name, err)
}
if !hadErrors && err != nil {
hadErrors = true
}
tmp := FieldValue{
Field: f,
Value: vTmp,
Error: err,
}
result = append(result, tmp)
}
if hadErrors {
return result, ErrorFieldExtractHadError
}
return result, nil
}
func (r BuilderRequest) extractCoilFields(response CoilsResponse, continueOnExtractionErrors bool) ([]FieldValue, error) {
hadErrors := false
capacity := 0
if continueOnExtractionErrors {
capacity = len(r.Fields)
}
result := make([]FieldValue, 0, capacity)
for _, f := range r.Fields {
vTmp, err := response.IsCoilSet(r.StartAddress, f.Address)
if err != nil && !continueOnExtractionErrors {
return nil, fmt.Errorf("field extraction failed. name: %v err: %w", f.Name, err)
}
if !hadErrors && err != nil {
hadErrors = true
}
tmp := FieldValue{
Field: f,
Value: vTmp,
Error: err,
}
result = append(result, tmp)
}
if hadErrors {
return result, ErrorFieldExtractHadError
}
return result, nil
}
// Split combines Fields into requests by their ServerAddress+FunctionCode+UnitID+Protocol+RequestInterval
func (b *Builder) split(functionCode uint8, protocol ProtocolType) ([]BuilderRequest, error) {
s := b.config.Splitter
if s == nil {
s = DefaultSplitter{}
}
return s.Split(b.fields, functionCode, protocol)
}
// Split combines Fields into requests by their ServerAddress+FunctionCode+UnitID+Protocol+RequestInterval
func (b *Builder) Split() ([]BuilderRequest, error) {
return b.split(0, protocolAny)
}
// ReadHoldingRegistersTCP combines Fields into TCP Read Holding Registers (FC3) requests
func (b *Builder) ReadHoldingRegistersTCP() ([]BuilderRequest, error) {
return b.split(packet.FunctionReadHoldingRegisters, ProtocolTCP)
}
// ReadHoldingRegistersRTU combines Fields into RTU Read Holding Registers (FC3) requests
func (b *Builder) ReadHoldingRegistersRTU() ([]BuilderRequest, error) {
return b.split(packet.FunctionReadHoldingRegisters, ProtocolRTU)
}
// ReadInputRegistersTCP combines Fields into TCP Read Input Registers (FC4) requests
func (b *Builder) ReadInputRegistersTCP() ([]BuilderRequest, error) {
return b.split(packet.FunctionReadInputRegisters, ProtocolTCP)
}
// ReadInputRegistersRTU combines Fields into RTU Read Input Registers (FC4) requests
func (b *Builder) ReadInputRegistersRTU() ([]BuilderRequest, error) {
return b.split(packet.FunctionReadInputRegisters, ProtocolRTU)
}
// ReadCoilsTCP combines Fields into TCP Read Coils (FC1) requests
func (b *Builder) ReadCoilsTCP() ([]BuilderRequest, error) {
return b.split(packet.FunctionReadCoils, ProtocolTCP)
}
// ReadCoilsRTU combines Fields into RTU Read Coils (FC1) requests
func (b *Builder) ReadCoilsRTU() ([]BuilderRequest, error) {
return b.split(packet.FunctionReadCoils, ProtocolRTU)
}
// ReadDiscreteInputsTCP combines Fields into TCP Read Discrete Inputs (FC2) requests
func (b *Builder) ReadDiscreteInputsTCP() ([]BuilderRequest, error) {
return b.split(packet.FunctionReadDiscreteInputs, ProtocolTCP)
}
// ReadDiscreteInputsRTU combines Fields into RTU Read Discrete Inputs (FC2) requests
func (b *Builder) ReadDiscreteInputsRTU() ([]BuilderRequest, error) {
return b.split(packet.FunctionReadDiscreteInputs, ProtocolRTU)
}