-
Notifications
You must be signed in to change notification settings - Fork 1
/
builder.go
455 lines (395 loc) · 14.2 KB
/
builder.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
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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
package modbus
import (
"errors"
"fmt"
"github.com/aldas/go-modbus-client/packet"
)
const (
// FieldTypeBit represents single bit out 16 bit register. Use `Field.Bit` (0-15) to indicate which bit is meant.
FieldTypeBit FieldType = 1
// FieldTypeByte represents single byte of 16 bit, 2 byte, single register. Use `Field.FromHighByte` to indicate is high or low byte is meant.
FieldTypeByte FieldType = 2
// FieldTypeUint8 represents uint8 value of 2 byte, single register. Use `Field.FromHighByte` to indicate is high or low byte value is meant.
FieldTypeUint8 FieldType = 3
// FieldTypeInt8 represents int8 value of 2 byte, single register. Use `Field.FromHighByte` to indicate is high or low byte value is meant.
FieldTypeInt8 FieldType = 4
// FieldTypeUint16 represents single register (16 bit) as uint16 value
FieldTypeUint16 FieldType = 5
// FieldTypeInt16 represents single register (16 bit) as int16 value
FieldTypeInt16 FieldType = 6
// FieldTypeUint32 represents 2 registers (32 bit) as uint32 value. Use `Field.ByteOrder` to indicate byte and word order of register data.
FieldTypeUint32 FieldType = 7
// FieldTypeInt32 represents 2 registers (32 bit) as int32 value. Use `Field.ByteOrder` to indicate byte and word order of register data.
FieldTypeInt32 FieldType = 8
// FieldTypeUint64 represents 4 registers (64 bit) as uint64 value. Use `Field.ByteOrder` to indicate byte and word order of register data.
FieldTypeUint64 FieldType = 9
// FieldTypeInt64 represents 4 registers (64 bit) as int64 value. Use `Field.ByteOrder` to indicate byte and word order of register data.
FieldTypeInt64 FieldType = 10
// FieldTypeFloat32 represents 2 registers (32 bit) as float32 value. Use `Field.ByteOrder` to indicate byte and word order of register data.
FieldTypeFloat32 FieldType = 11
// FieldTypeFloat64 represents 4 registers (64 bit) as float64 value. Use `Field.ByteOrder` to indicate byte and word order of register data.
FieldTypeFloat64 FieldType = 12
// FieldTypeString represents N registers as string value. Use `Field.Length` to length of string.
FieldTypeString FieldType = 13
maxFieldTypeValue = uint8(13)
)
// FieldType is enum type for data types that Field can represent
type FieldType uint8
// Fields is slice of Field instances
type Fields []Field
// Field is distinct field be requested and extracted from response
// Tag `mapstructure` allows you to marshal https://github.com/spf13/viper supported configuration format to the Field
type Field struct {
ServerAddress string `json:"server_address" mapstructure:"server_address"` // [network://]host:port
UnitID uint8 `json:"unit_id" mapstructure:"unit_id"`
RegisterAddress uint16 `json:"register_address" mapstructure:"register_address"`
Type FieldType `json:"type" mapstructure:"type"`
Bit uint8 `json:"bit" mapstructure:"bit"`
FromHighByte bool `json:"from_high_byte" mapstructure:"from_high_byte"`
Length uint8 `json:"Length" mapstructure:"Length"`
ByteOrder packet.ByteOrder `json:"byte_order" mapstructure:"byte_order"`
Name string `json:"Name" mapstructure:"Name"`
}
// registerSize returns how many register/words does this field would take in modbus response
func (f *Field) registerSize() uint16 {
switch f.Type {
case FieldTypeFloat64, FieldTypeInt64, FieldTypeUint64:
return 4
case FieldTypeFloat32, FieldTypeInt32, FieldTypeUint32:
return 2
case FieldTypeString:
if f.Length%2 == 0 { // even
return uint16(f.Length) / 2
}
return (uint16(f.Length) / 2) + 1 // odd
default:
return 1
}
}
// Validate checks if Field is values are correctly filled
func (f Field) Validate() error {
if f.ServerAddress == "" {
return errors.New("field server address can not be empty")
}
if f.Type == 0 {
return errors.New("field type must be set")
}
if uint8(f.Type) > maxFieldTypeValue {
return errors.New("field type has invalid value")
}
if f.Bit > 15 {
return errors.New("field bit value must be in range (0-15)")
}
if f.Type == FieldTypeString && f.Length == 0 {
return errors.New("field with type string must have length set")
}
return nil
}
// ExtractFrom extracts field value from given registers data
func (f Field) ExtractFrom(registers *packet.Registers) (interface{}, error) {
switch f.Type {
case FieldTypeBit:
return registers.Bit(f.RegisterAddress, f.Bit)
case FieldTypeByte:
return registers.Byte(f.RegisterAddress, f.FromHighByte)
case FieldTypeUint8:
return registers.Uint8(f.RegisterAddress, f.FromHighByte)
case FieldTypeInt8:
return registers.Int8(f.RegisterAddress, f.FromHighByte)
case FieldTypeUint16:
return registers.Uint16(f.RegisterAddress)
case FieldTypeInt16:
return registers.Int16(f.RegisterAddress)
case FieldTypeUint32:
return registers.Uint32WithByteOrder(f.RegisterAddress, f.ByteOrder)
case FieldTypeInt32:
return registers.Int32WithByteOrder(f.RegisterAddress, f.ByteOrder)
case FieldTypeUint64:
return registers.Uint64WithByteOrder(f.RegisterAddress, f.ByteOrder)
case FieldTypeInt64:
return registers.Int64WithByteOrder(f.RegisterAddress, f.ByteOrder)
case FieldTypeFloat32:
return registers.Float32WithByteOrder(f.RegisterAddress, f.ByteOrder)
case FieldTypeFloat64:
return registers.Float64WithByteOrder(f.RegisterAddress, f.ByteOrder)
case FieldTypeString:
return registers.StringWithByteOrder(f.RegisterAddress, f.Length, f.ByteOrder)
}
return nil, errors.New("extraction failure due unknown field type")
}
// BField is distinct field be requested and extracted from response
type BField struct {
Field
}
// ServerAddress sets modbus server address for Field. Usage `[network://]host:port`
func (f *BField) ServerAddress(serverAddress string) *BField {
f.Field.ServerAddress = serverAddress
return f
}
// UnitID sets unitID for Field
func (f *BField) UnitID(unitID uint8) *BField {
f.Field.UnitID = unitID
return f
}
// ByteOrder sets word and byte order for Field to be used when extracting values from response
func (f *BField) ByteOrder(byteOrder packet.ByteOrder) *BField {
f.Field.ByteOrder = byteOrder
return f
}
// Name sets name/identifier for Field to be used to uniquely identify value when extracting values from response
func (f *BField) Name(name string) *BField {
f.Field.Name = name
return f
}
// 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
serverAddress string // [network://]host:port
unitID uint8
}
// NewRequestBuilder creates new instance of Builder with given defaults.
// Arguments can be left empty and ServerAddress+UnitID provided for each field separately
func NewRequestBuilder(serverAddress string, unitID uint8) *Builder {
return &Builder{
serverAddress: serverAddress,
unitID: unitID,
fields: make(Fields, 0, 5),
}
}
// AddAll adds field into Builder. AddAll does not set ServerAddress and UnitID values.
func (b *Builder) AddAll(fields Fields) *Builder {
b.fields = append(b.fields, fields...)
return b
}
// Add adds field into Builder
func (b *Builder) Add(field *BField) *Builder {
b.fields = append(b.fields, field.Field)
return b
}
// Bit add bit (0-15) field to Builder to be requested and extracted
func (b *Builder) Bit(registerAddress uint16, bit uint8) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeBit,
RegisterAddress: registerAddress,
Bit: bit,
},
}
}
// Byte add byte field to Builder to be requested and extracted
func (b *Builder) Byte(registerAddress uint16, fromHighByte bool) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeByte,
RegisterAddress: registerAddress,
FromHighByte: fromHighByte,
},
}
}
// Uint8 add uint8 field to Builder to be requested and extracted
func (b *Builder) Uint8(registerAddress uint16, fromHighByte bool) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeUint8,
RegisterAddress: registerAddress,
FromHighByte: fromHighByte,
},
}
}
// Int8 add int8 field to Builder to be requested and extracted
func (b *Builder) Int8(registerAddress uint16, fromHighByte bool) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeInt8,
RegisterAddress: registerAddress,
FromHighByte: fromHighByte,
},
}
}
// Uint16 add uint16 field to Builder to be requested and extracted
func (b *Builder) Uint16(registerAddress uint16) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeUint16,
RegisterAddress: registerAddress,
},
}
}
// Int16 add int16 field to Builder to be requested and extracted
func (b *Builder) Int16(registerAddress uint16) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeInt16,
RegisterAddress: registerAddress,
},
}
}
// Uint32 add uint32 field to Builder to be requested and extracted
func (b *Builder) Uint32(registerAddress uint16) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeUint32,
RegisterAddress: registerAddress,
},
}
}
// Int32 add int32 field to Builder to be requested and extracted
func (b *Builder) Int32(registerAddress uint16) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeInt32,
RegisterAddress: registerAddress,
},
}
}
// Uint64 add uint64 field to Builder to be requested and extracted
func (b *Builder) Uint64(registerAddress uint16) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeUint64,
RegisterAddress: registerAddress,
},
}
}
// Int64 add int64 field to Builder to be requested and extracted
func (b *Builder) Int64(registerAddress uint16) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeInt64,
RegisterAddress: registerAddress,
},
}
}
// Float32 add float32 field to Builder to be requested and extracted
func (b *Builder) Float32(registerAddress uint16) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeFloat32,
RegisterAddress: registerAddress,
},
}
}
// Float64 add float64 field to Builder to be requested and extracted
func (b *Builder) Float64(registerAddress uint16) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeFloat64,
RegisterAddress: registerAddress,
},
}
}
// String add string field to Builder to be requested and extracted
func (b *Builder) String(registerAddress uint16, length uint8) *BField {
return &BField{
Field{
ServerAddress: b.serverAddress,
UnitID: b.unitID,
Type: FieldTypeString,
Length: length,
RegisterAddress: registerAddress,
},
}
}
// RegisterRequest helps to connect requested fields to responses
type RegisterRequest 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
// 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)
}
// AsRegisters returns response data as Register to more convenient access
func (r RegisterRequest) 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 interface{}
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 RegisterRequest) ExtractFields(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
}
// ReadHoldingRegistersTCP combines fields into TCP Read Holding Registers (FC3) requests
func (b *Builder) ReadHoldingRegistersTCP() ([]RegisterRequest, error) {
return split(b.fields, "fc3_tcp")
}
// ReadHoldingRegistersRTU combines fields into RTU Read Holding Registers (FC3) requests
func (b *Builder) ReadHoldingRegistersRTU() ([]RegisterRequest, error) {
return split(b.fields, "fc3_rtu")
}
// ReadInputRegistersTCP combines fields into TCP Read Input Registers (FC4) requests
func (b *Builder) ReadInputRegistersTCP() ([]RegisterRequest, error) {
return split(b.fields, "fc4_tcp")
}
// ReadInputRegistersRTU combines fields into RTU Read Input Registers (FC4) requests
func (b *Builder) ReadInputRegistersRTU() ([]RegisterRequest, error) {
return split(b.fields, "fc4_rtu")
}