-
Notifications
You must be signed in to change notification settings - Fork 5
/
opcgroups.go
224 lines (198 loc) · 6.13 KB
/
opcgroups.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
package opcda
import (
"errors"
"sync"
"sync/atomic"
"github.com/huskar-t/opcda/com"
)
type OPCGroups struct {
iServer *com.IOPCServer
iCommon *com.IOPCCommon
parent *OPCServer
groupID uint32
defaultActive bool
defaultGroupUpdateRate uint32
defaultDeadband float32
defaultLocaleID uint32
defaultGroupTimeBias int32
groups []*OPCGroup
sync.RWMutex
}
func NewOPCGroups(opcServer *OPCServer) *OPCGroups {
return &OPCGroups{
parent: opcServer,
iServer: opcServer.iServer,
defaultActive: true,
defaultGroupUpdateRate: uint32(1000),
defaultDeadband: float32(0.0),
defaultLocaleID: uint32(0x0400),
defaultGroupTimeBias: int32(0),
iCommon: opcServer.iCommon,
}
}
// GetParent Returns reference to the parent OPCServer object.
func (gs *OPCGroups) GetParent() *OPCServer {
return gs.parent
}
// GetDefaultGroupIsActive get the default active state for OPCGroups created using Groups.Add
func (gs *OPCGroups) GetDefaultGroupIsActive() bool {
return gs.defaultActive
}
// SetDefaultGroupIsActive set the default active state for OPCGroups created using Groups.Add
func (gs *OPCGroups) SetDefaultGroupIsActive(defaultActive bool) {
gs.defaultActive = defaultActive
}
// GetDefaultGroupUpdateRate get the default update rate (in milliseconds) for OPCGroups created using Groups.Add
func (gs *OPCGroups) GetDefaultGroupUpdateRate() uint32 {
return gs.defaultGroupUpdateRate
}
// SetDefaultGroupUpdateRate set the default update rate (in milliseconds) for OPCGroups created using Groups.Add
func (gs *OPCGroups) SetDefaultGroupUpdateRate(defaultGroupUpdateRate uint32) {
gs.defaultGroupUpdateRate = defaultGroupUpdateRate
}
// GetDefaultGroupDeadband get the default deadband for OPCGroups created using Groups.Add
func (gs *OPCGroups) GetDefaultGroupDeadband() float32 {
return gs.defaultDeadband
}
// SetDefaultGroupDeadband set the default deadband for OPCGroups created using Groups.Add
func (gs *OPCGroups) SetDefaultGroupDeadband(defaultDeadband float32) {
gs.defaultDeadband = defaultDeadband
}
// GetDefaultGroupLocaleID get the default locale for OPCGroups created using Groups.Add.
func (gs *OPCGroups) GetDefaultGroupLocaleID() uint32 {
return gs.defaultLocaleID
}
// SetDefaultGroupLocaleID set the default locale for OPCGroups created using Groups.Add.
func (gs *OPCGroups) SetDefaultGroupLocaleID(defaultLocaleID uint32) {
gs.defaultLocaleID = defaultLocaleID
}
// GetDefaultGroupTimeBias get the default time bias for OPCGroups created using Groups.Add.
func (gs *OPCGroups) GetDefaultGroupTimeBias() int32 {
return gs.defaultGroupTimeBias
}
// SetDefaultGroupTimeBias set the default time bias for OPCGroups created using Groups.Add.
func (gs *OPCGroups) SetDefaultGroupTimeBias(defaultGroupTimeBias int32) {
gs.defaultGroupTimeBias = defaultGroupTimeBias
}
// GetCount Required property for collections.
func (gs *OPCGroups) GetCount() int {
gs.RLock()
defer gs.RUnlock()
return len(gs.groups)
}
// Item Returns an OPCGroup by ItemSpecifier. ItemSpecifier is the name or 0-based index into the collection
func (gs *OPCGroups) Item(index int32) (*OPCGroup, error) {
gs.RLock()
defer gs.RUnlock()
if index < 0 || index >= int32(len(gs.groups)) {
return nil, errors.New("index out of range")
}
return gs.groups[index], nil
}
// ItemByName Returns an OPCGroup by name
func (gs *OPCGroups) ItemByName(name string) (*OPCGroup, error) {
gs.RLock()
defer gs.RUnlock()
for _, v := range gs.groups {
if v.groupName == name {
return v, nil
}
}
return nil, errors.New("not found")
}
// Add Creates a new OPCGroup object and adds it to the collections
func (gs *OPCGroups) Add(szName string) (*OPCGroup, error) {
gs.Lock()
defer gs.Unlock()
hClientGroup := atomic.AddUint32(&gs.groupID, 1)
phServerGroup, pRevisedUpdateRate, ppUnk, err := gs.iServer.AddGroup(
szName,
gs.defaultActive,
gs.defaultGroupUpdateRate,
hClientGroup,
&gs.defaultGroupTimeBias,
&gs.defaultDeadband,
gs.defaultLocaleID,
&com.IID_IOPCGroupStateMgt,
)
if err != nil {
return nil, err
}
opcGroup, err := NewOPCGroup(gs, ppUnk, hClientGroup, phServerGroup, szName, pRevisedUpdateRate)
if err != nil {
ppUnk.Release()
return nil, err
}
gs.groups = append(gs.groups, opcGroup)
return opcGroup, nil
}
// GetOPCGroupByName Returns an OPCGroup by name
func (gs *OPCGroups) GetOPCGroupByName(name string) (*OPCGroup, error) {
return gs.ItemByName(name)
}
// GetOPCGroup Returns an OPCGroup by server handle
func (gs *OPCGroups) GetOPCGroup(serverHandle uint32) (*OPCGroup, error) {
gs.RLock()
defer gs.RUnlock()
for _, v := range gs.groups {
if v.serverGroupHandle == serverHandle {
return v, nil
}
}
return nil, errors.New("not found")
}
// Remove Removes an OPCGroup from the collection
func (gs *OPCGroups) Remove(serverHandle uint32) error {
gs.Lock()
defer gs.Unlock()
for i, v := range gs.groups {
if v.serverGroupHandle == serverHandle {
err := gs.doRemove(serverHandle)
if err != nil {
return err
}
v.Release()
gs.groups = append(gs.groups[:i], gs.groups[i+1:]...)
return nil
}
}
return errors.New("not found")
}
func (gs *OPCGroups) doRemove(serverHandle uint32) error {
return gs.iServer.RemoveGroup(serverHandle, true)
}
// RemoveByName Removes an OPCGroup from the collection by name
func (gs *OPCGroups) RemoveByName(name string) error {
gs.Lock()
defer gs.Unlock()
for i, v := range gs.groups {
if v.groupName == name {
err := gs.doRemove(v.GetServerHandle())
if err != nil {
return err
}
v.Release()
gs.groups = append(gs.groups[:i], gs.groups[i+1:]...)
return nil
}
}
return errors.New("not found")
}
// RemoveAll Removes all OPCGroups from the collection
func (gs *OPCGroups) RemoveAll() error {
gs.Lock()
defer gs.Unlock()
for _, v := range gs.groups {
gs.doRemove(v.GetServerHandle())
v.Release()
}
gs.groups = nil
return nil
}
// Release Releases the resources used by the collection and the items it contains.
func (gs *OPCGroups) Release() error {
for _, group := range gs.groups {
group.Release()
}
return nil
}