-
Notifications
You must be signed in to change notification settings - Fork 5
/
opcitems.go
287 lines (261 loc) · 7.72 KB
/
opcitems.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
package opcda
import (
"errors"
"sync"
"sync/atomic"
"github.com/huskar-t/opcda/com"
"golang.org/x/sys/windows"
)
type OPCItems struct {
itemMgt *com.IOPCItemMgt
iCommon *com.IOPCCommon
parent *OPCGroup
itemID uint32
defaultRequestedDataType com.VT
defaultAccessPath string
defaultActive bool
items []*OPCItem
sync.RWMutex
}
func NewOPCItems(
parent *OPCGroup,
itemMgt *com.IOPCItemMgt,
iCommon *com.IOPCCommon,
) *OPCItems {
return &OPCItems{
parent: parent,
itemMgt: itemMgt,
defaultRequestedDataType: com.VT_EMPTY,
defaultAccessPath: "",
defaultActive: true,
iCommon: iCommon,
}
}
// GetParent Returns reference to the parent OPCGroup object
func (is *OPCItems) GetParent() *OPCGroup {
return is.parent
}
// GetDefaultRequestedDataType get the requested data type that will be used in calls to Add
func (is *OPCItems) GetDefaultRequestedDataType() com.VT {
return is.defaultRequestedDataType
}
// SetDefaultRequestedDataType set the requested data type that will be used in calls to Add
func (is *OPCItems) SetDefaultRequestedDataType(defaultRequestedDataType com.VT) {
is.defaultRequestedDataType = defaultRequestedDataType
}
// GetDefaultAccessPath get the default AccessPath that will be used in calls to Add
func (is *OPCItems) GetDefaultAccessPath() string {
return is.defaultAccessPath
}
// SetDefaultAccessPath set the default AccessPath that will be used in calls to Add
func (is *OPCItems) SetDefaultAccessPath(defaultAccessPath string) {
is.Lock()
defer is.Unlock()
is.defaultAccessPath = defaultAccessPath
}
// GetDefaultActive get the default active state for OPCItems created using Items.Add
func (is *OPCItems) GetDefaultActive() bool {
return is.defaultActive
}
// SetDefaultActive set the default active state for OPCItems created using Items.Add
func (is *OPCItems) SetDefaultActive(defaultActive bool) {
is.defaultActive = defaultActive
}
// GetCount get the number of items in the collection
func (is *OPCItems) GetCount() int {
return len(is.items)
}
// Item get the item by index
func (is *OPCItems) Item(index int32) (*OPCItem, error) {
is.RLock()
defer is.RUnlock()
if index < 0 || index >= int32(len(is.items)) {
return nil, errors.New("index out of range")
}
return is.items[index], nil
}
// ItemByName get the item by name
func (is *OPCItems) ItemByName(name string) (*OPCItem, error) {
is.RLock()
defer is.RUnlock()
for _, v := range is.items {
if v.tag == name {
return v, nil
}
}
return nil, errors.New("not found")
}
// GetOPCItem returns the OPCItem by serverHandle
func (is *OPCItems) GetOPCItem(serverHandle uint32) (*OPCItem, error) {
is.RLock()
defer is.RUnlock()
for _, v := range is.items {
if v.serverHandle == serverHandle {
return v, nil
}
}
return nil, errors.New("not found")
}
// AddItem adds an item to the group.
func (is *OPCItems) AddItem(tag string) (*OPCItem, error) {
items, errs, err := is.AddItems([]string{tag})
if err != nil {
return nil, err
}
if errs[0] != nil {
return nil, errs[0]
}
return items[0], nil
}
// AddItems adds items to the group.
func (is *OPCItems) AddItems(tags []string) ([]*OPCItem, []error, error) {
is.Lock()
defer is.Unlock()
accessPath := is.defaultAccessPath
active := is.defaultActive
dt := is.defaultRequestedDataType
items := is.createDefinitions(tags, accessPath, active, dt)
results, errs, err := is.itemMgt.AddItems(items)
if err != nil {
return nil, nil, err
}
var resultErrors = make([]error, len(tags))
var opcItems = make([]*OPCItem, len(tags))
for j := 0; j < len(tags); j++ {
if errs[j] < 0 {
resultErrors[j] = is.getError(errs[j])
} else {
item := NewOPCItem(is, tags[j], results[j], items[j].HClient, accessPath, active)
opcItems[j] = item
is.items = append(is.items, item)
}
}
return opcItems, resultErrors, nil
}
// Remove Removes an OPCItem
func (is *OPCItems) Remove(serverHandles []uint32) {
is.Lock()
defer is.Unlock()
for _, v := range serverHandles {
for j, w := range is.items {
if w.serverHandle == v {
w.Release()
w.itemMgt.RemoveItems([]uint32{v})
is.items = append(is.items[:j], is.items[j+1:]...)
break
}
}
}
}
// Validate Determines if one or more OPCItems could be successfully created via the Add method (but does not add them).
func (is *OPCItems) Validate(tags []string, requestedDataTypes *[]com.VT, accessPaths *[]string) ([]error, error) {
var definitions []com.TagOPCITEMDEF
for i, v := range tags {
cHandle := atomic.AddUint32(&is.itemID, 1)
item := com.TagOPCITEMDEF{
SzAccessPath: windows.StringToUTF16Ptr(""),
SzItemID: windows.StringToUTF16Ptr(v),
BActive: com.BoolToComBOOL(false),
HClient: cHandle,
DwBlobSize: 0,
PBlob: nil,
VtRequested: uint16(is.defaultRequestedDataType),
}
if requestedDataTypes != nil {
item.VtRequested = uint16((*requestedDataTypes)[i])
}
if accessPaths != nil {
item.SzAccessPath = windows.StringToUTF16Ptr((*accessPaths)[i])
}
definitions = append(definitions, item)
}
_, errs, err := is.itemMgt.ValidateItems(definitions, false)
if err != nil {
return nil, err
}
var resultErrors = make([]error, len(errs))
for j := 0; j < len(errs); j++ {
if errs[j] < 0 {
resultErrors[j] = is.getError(errs[j])
}
}
return resultErrors, nil
}
// SetActive Allows Activation and deactivation of individual OPCItem’s in the OPCItems Collection
func (is *OPCItems) SetActive(serverHandles []uint32, active bool) []error {
resultErrors := make([]error, len(serverHandles))
for i, handle := range serverHandles {
item, err := is.GetOPCItem(handle)
if err != nil {
resultErrors[i] = err
continue
}
err = item.SetIsActive(active)
if err != nil {
resultErrors[i] = err
}
}
return resultErrors
}
// SetClientHandles Changes the client handles or one or more Items in a Group.
func (is *OPCItems) SetClientHandles(serverHandles []uint32, clientHandles []uint32) []error {
resultErrors := make([]error, len(serverHandles))
for i, handle := range serverHandles {
item, err := is.GetOPCItem(handle)
if err != nil {
resultErrors[i] = err
continue
}
err = item.SetClientHandle(clientHandles[i])
if err != nil {
resultErrors[i] = err
}
}
return resultErrors
}
// SetDataTypes Changes the requested data type for one or more Items
func (is *OPCItems) SetDataTypes(serverHandles []uint32, requestedDataTypes []com.VT) []error {
resultErrors := make([]error, len(serverHandles))
for i, handle := range serverHandles {
item, err := is.GetOPCItem(handle)
if err != nil {
resultErrors[i] = err
continue
}
err = item.SetRequestedDataType(requestedDataTypes[i])
if err != nil {
resultErrors[i] = err
}
}
return resultErrors
}
// Release Releases the OPCItems collection and all associated resources.
func (is *OPCItems) Release() {
for _, item := range is.items {
item.Release()
}
is.itemMgt.Release()
}
func (is *OPCItems) createDefinitions(tags []string, accessPath string, active bool, requestedDataType com.VT) []com.TagOPCITEMDEF {
var definitions []com.TagOPCITEMDEF
for _, v := range tags {
cHandle := atomic.AddUint32(&is.itemID, 1)
definitions = append(definitions, com.TagOPCITEMDEF{
SzAccessPath: windows.StringToUTF16Ptr(accessPath),
SzItemID: windows.StringToUTF16Ptr(v),
BActive: com.BoolToComBOOL(active),
HClient: cHandle,
DwBlobSize: 0,
PBlob: nil,
VtRequested: uint16(requestedDataType),
})
}
return definitions
}
func (is *OPCItems) getError(errorCode int32) error {
errStr, _ := is.iCommon.GetErrorString(uint32(errorCode))
return &OPCError{
ErrorCode: errorCode,
ErrorMessage: errStr,
}
}