-
Notifications
You must be signed in to change notification settings - Fork 0
/
address.go
204 lines (192 loc) · 4.42 KB
/
address.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
// Copyright 2024 shiyuecamus. All Rights Reserved.
// Use of this source code is governed by an MIT license
// that can be found in the LICENSE file.
package core
import (
"github.com/shiyuecamus/gs7/common"
"regexp"
"strconv"
"strings"
)
func ParseAddress(address string) (requestItem common.RequestItem, err error) {
if address == "" {
err = common.ErrorWithCode(common.ErrAddressEmpty)
return
}
address = strings.ToUpper(address)
address = strings.Replace(address, " ", "", -1)
split := strings.Split(address, ".")
var area common.AreaType
var variableType common.ParamVariableType
var dbNumber, byteAddress, bitAddress int
area, err = parseArea(split)
if err != nil {
return
}
variableType, err = parseVariableType(split)
if err != nil {
return
}
dbNumber, err = parseDbNumber(split)
if err != nil {
return
}
byteAddress, err = parseByteAddress(split)
if err != nil {
return
}
bitAddress, err = parseBitAddress(split, variableType)
if err != nil {
return
}
requestItem = NewStandardRequestItem(area, dbNumber, variableType, byteAddress, bitAddress, 1)
return
}
func parseVariableType(split []string) (variableType common.ParamVariableType, err error) {
switch one := split[0][:1]; one {
case "T":
variableType = common.PvtTimer
return
case "C":
variableType = common.PvtCounter
return
case "D":
if len(split) < 2 {
err = common.ErrorWithCode(common.ErrAddressInvalid)
return
}
return extractVariableType(split[1], true)
default:
return extractVariableType(split[0], false)
}
}
func parseBitAddress(split []string, variableType common.ParamVariableType) (int, error) {
switch one := split[0][:1]; one {
case "D":
if len(split) == 3 && variableType == common.PvtBit {
return extractNumber(split[2])
}
return 0, nil
default:
if len(split) == 2 && variableType == common.PvtBit {
return extractNumber(split[2])
}
return 0, nil
}
}
func parseByteAddress(split []string) (int, error) {
switch one := split[0][:1]; one {
case "D":
if len(split) >= 2 {
return extractNumber(split[1])
}
return 0, common.ErrorWithCode(common.ErrAddressInvalid)
default:
return 0, nil
}
}
func parseDbNumber(split []string) (int, error) {
switch one := split[0][:1]; one {
case "D":
return extractNumber(split[0])
case "V":
return 1, nil
default:
return 0, nil
}
}
func parseArea(split []string) (common.AreaType, error) {
switch one := split[0][:1]; one {
case "I":
return common.AtInputs, nil
case "Q":
return common.AtOutputs, nil
case "M":
return common.AtFlags, nil
case "V", "D":
return common.AtDataBlocks, nil
case "T":
return common.AtTimers, nil
case "C":
return common.AtCounters, nil
default:
return 0, common.ErrorWithCode(common.ErrAddressInvalid)
}
}
func extractVariableType(src string, isDb bool) (variableType common.ParamVariableType, err error) {
re, err := regexp.Compile("\\D")
if err != nil {
return 0, err
}
all := re.FindAllString(src, -1)
t := strings.Join(all, "")
if !isDb {
if len(t) < 2 {
return 0, common.ErrorWithCode(common.ErrAddressInvalid)
}
t = t[1:]
} else if len(t) < 1 {
return 0, common.ErrorWithCode(common.ErrAddressInvalid)
}
switch t {
case "X", "BIT":
variableType = common.PvtBit
return
case "B", "BYTE":
variableType = common.PvtByte
return
case "C", "CHAR":
variableType = common.PvtChar
return
case "DW", "DWORD":
variableType = common.PvtDWord
return
case "W", "WORD":
variableType = common.PvtWord
return
case "DI", "DINT":
variableType = common.PvtDInt
return
case "I", "INT":
variableType = common.PvtInt
return
case "D", "DATE":
variableType = common.PvtDate
return
case "DT", "DATETIME":
variableType = common.PvtDateTime
return
case "DTL", "DATETIMELONG":
variableType = common.PvtDTL
return
case "T", "TIME":
variableType = common.PvtTime
return
case "ST", "STIME":
variableType = common.PvtS5Time
return
case "TOD", "TIMEOFDAY":
variableType = common.PvtTimeOfDay
return
case "R", "REAL":
variableType = common.PvtReal
return
case "S", "STRING":
variableType = common.PvtString
return
case "WS", "WSTRING":
variableType = common.PvtWString
return
default:
err = common.ErrorWithCode(common.ErrAddressInvalid)
}
return
}
func extractNumber(src string) (int, error) {
re, err := regexp.Compile("\\D")
if err != nil {
return 0, err
}
number := re.ReplaceAllString(src, "")
return strconv.Atoi(strings.TrimSpace(number))
}