-
Notifications
You must be signed in to change notification settings - Fork 388
/
Tag.go
137 lines (120 loc) · 4.14 KB
/
Tag.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
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package modbus
import (
"context"
"encoding/binary"
"fmt"
"github.com/apache/plc4x/plc4go/spi/options"
"strconv"
apiModel "github.com/apache/plc4x/plc4go/pkg/api/model"
apiValues "github.com/apache/plc4x/plc4go/pkg/api/values"
readWriteModel "github.com/apache/plc4x/plc4go/protocols/modbus/readwrite/model"
spiModel "github.com/apache/plc4x/plc4go/spi/model"
"github.com/apache/plc4x/plc4go/spi/utils"
"github.com/pkg/errors"
)
const (
AddressOffset = 1
)
type modbusTag struct {
apiModel.PlcTag
TagType TagType
Address uint16
Quantity uint16
Datatype readWriteModel.ModbusDataType
}
func NewTag(tagType TagType, address uint16, quantity uint16, datatype readWriteModel.ModbusDataType) apiModel.PlcTag {
return modbusTag{
TagType: tagType,
Address: address - AddressOffset,
Quantity: quantity,
Datatype: datatype,
}
}
func NewModbusPlcTagFromStrings(tagType TagType, addressString string, quantityString string, datatype readWriteModel.ModbusDataType, _options ...options.WithOption) (apiModel.PlcTag, error) {
address, err := strconv.ParseUint(addressString, 10, 16)
if err != nil {
return nil, errors.Errorf("Couldn't parse address string '%s' into an int", addressString)
}
customLogger := options.ExtractCustomLoggerOrDefaultToGlobal(_options...)
if quantityString == "" {
customLogger.Debug().Msg("No quantity supplied, assuming 1")
quantityString = "1"
}
quantity, err := strconv.ParseUint(quantityString, 10, 16)
if err != nil {
customLogger.Warn().Err(err).Str("quantityString", quantityString).Msg("Error during parsing for %s. Falling back to 1")
quantity = 1
}
return NewTag(tagType, uint16(address), uint16(quantity), datatype), nil
}
func (m modbusTag) GetAddressString() string {
return fmt.Sprintf("%dx%05d:%s[%d]", m.TagType, m.Address, m.Datatype.String(), m.Quantity)
}
func (m modbusTag) GetValueType() apiValues.PlcValueType {
if plcValueType, ok := apiValues.PlcValueByName(m.Datatype.String()); !ok {
return apiValues.NULL
} else {
return plcValueType
}
}
func (m modbusTag) GetArrayInfo() []apiModel.ArrayInfo {
if m.Quantity != 1 {
return []apiModel.ArrayInfo{
&spiModel.DefaultArrayInfo{
LowerBound: 0,
UpperBound: uint32(m.Quantity),
},
}
}
return []apiModel.ArrayInfo{}
}
func castToModbusTagFromPlcTag(plcTag apiModel.PlcTag) (modbusTag, error) {
if modbusTagVar, ok := plcTag.(modbusTag); ok {
return modbusTagVar, nil
}
return modbusTag{}, errors.New("couldn't cast to ModbusPlcTag")
}
func (m modbusTag) Serialize() ([]byte, error) {
wb := utils.NewWriteBufferByteBased(utils.WithByteOrderForByteBasedBuffer(binary.BigEndian))
if err := m.SerializeWithWriteBuffer(context.Background(), wb); err != nil {
return nil, err
}
return wb.GetBytes(), nil
}
func (m modbusTag) SerializeWithWriteBuffer(ctx context.Context, writeBuffer utils.WriteBuffer) error {
if err := writeBuffer.PushContext(m.TagType.GetName()); err != nil {
return err
}
if err := writeBuffer.WriteUint16("address", 16, m.Address); err != nil {
return err
}
if err := writeBuffer.WriteUint16("numberOfElements", 16, m.Quantity); err != nil {
return err
}
dataType := m.Datatype.String()
if err := writeBuffer.WriteString("dataType", uint32(len([]rune(dataType))*8), "UTF-8", dataType); err != nil {
return err
}
if err := writeBuffer.PopContext(m.TagType.GetName()); err != nil {
return err
}
return nil
}