-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathopcserver.go
510 lines (466 loc) · 13.4 KB
/
opcserver.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
package opcda
import (
"fmt"
"time"
"unsafe"
"github.com/huskar-t/opcda/com"
"golang.org/x/sys/windows/registry"
"golang.org/x/sys/windows"
)
type OPCServer struct {
iServer *com.IOPCServer
iCommon *com.IOPCCommon
iItemProperty *com.IOPCItemProperties
groups *OPCGroups
Name string
Node string
clientName string
location com.CLSCTX
container *com.IConnectionPointContainer
point *com.IConnectionPoint
event *ShutdownEventReceiver
cookie uint32
}
// Connect connect to OPC server
func Connect(progID, node string) (opcServer *OPCServer, err error) {
location := com.CLSCTX_LOCAL_SERVER
if !com.IsLocal(node) {
location = com.CLSCTX_REMOTE_SERVER
}
clsid, err := getClsID(progID, node, location)
if err != nil {
return nil, NewOPCWrapperError("get clsid", err)
}
iUnknownServer, err := com.MakeCOMObjectEx(node, location, clsid, &com.IID_IOPCServer)
if err != nil {
return nil, NewOPCWrapperError("make com object IOPCServer", err)
}
defer func() {
if err != nil {
iUnknownServer.Release()
}
}()
var iUnknownCommon *com.IUnknown
err = iUnknownServer.QueryInterface(&com.IID_IOPCCommon, unsafe.Pointer(&iUnknownCommon))
if err != nil {
return nil, NewOPCWrapperError("server query interface IOPCCommon", err)
}
defer func() {
if err != nil {
iUnknownCommon.Release()
}
}()
var iUnknownItemProperties *com.IUnknown
err = iUnknownServer.QueryInterface(&com.IID_IOPCItemProperties, unsafe.Pointer(&iUnknownItemProperties))
if err != nil {
return nil, NewOPCWrapperError("server query interface IOPCItemProperties", err)
}
defer func() {
if err != nil {
iUnknownItemProperties.Release()
}
}()
server := &com.IOPCServer{IUnknown: iUnknownServer}
common := &com.IOPCCommon{IUnknown: iUnknownCommon}
itemProperties := &com.IOPCItemProperties{IUnknown: iUnknownItemProperties}
opcServer = &OPCServer{
iServer: server,
iCommon: common,
iItemProperty: itemProperties,
Name: progID,
Node: node,
location: location,
}
opcServer.groups = NewOPCGroups(opcServer)
return opcServer, nil
}
func getClsID(progID, node string, location com.CLSCTX) (clsid *windows.GUID, err error) {
if location == com.CLSCTX_LOCAL_SERVER {
id, err := windows.GUIDFromString(progID)
if err != nil {
return nil, NewOPCWrapperError("windows.GUIDFromString", err)
}
return &id, nil
} else {
// try get clsid from server list
clsid, err = getClsIDFromServerList(progID, node, location)
if err != nil {
// try get clsid from windows reg
clsid, err = getClsIDFromReg(progID, node)
if err != nil {
return nil, NewOPCWrapperError("getClsIDFromReg", err)
}
}
return clsid, nil
}
}
func getClsIDFromServerList(progID, node string, location com.CLSCTX) (*windows.GUID, error) {
iCatInfo, err := com.MakeCOMObjectEx(node, location, &com.CLSID_OpcServerList, &com.IID_IOPCServerList2)
if err != nil {
return nil, err
}
defer iCatInfo.Release()
sl := &com.IOPCServerList2{IUnknown: iCatInfo}
clsid, err := sl.CLSIDFromProgID(progID)
if err != nil {
return nil, err
}
return clsid, nil
}
func getClsIDFromReg(progID, node string) (*windows.GUID, error) {
var clsid *windows.GUID
var err error
hKey, err := registry.OpenRemoteKey(node, registry.CLASSES_ROOT)
if err != nil {
return nil, err
}
defer hKey.Close()
hProgIDKey, err := registry.OpenKey(hKey, progID, registry.READ)
if err != nil {
return nil, err
}
defer hProgIDKey.Close()
_, clsid, err = getClsidFromProgIDKey(hProgIDKey)
return clsid, err
}
func getClsidFromProgIDKey(hProgIDKey registry.Key) (string, *windows.GUID, error) {
hClsidKey, err := registry.OpenKey(hProgIDKey, "CLSID", registry.READ)
if err != nil {
return "", nil, err
}
defer hClsidKey.Close()
clsidStr, _, err := hClsidKey.GetStringValue("")
if err != nil {
return "", nil, err
}
clsid, err := windows.GUIDFromString(clsidStr)
return clsidStr, &clsid, err
}
type ServerInfo struct {
ProgID string
ClsStr string
VerIndProgID string
ClsID *windows.GUID
}
// GetOPCServers get OPC servers from node
func GetOPCServers(node string) ([]*ServerInfo, error) {
result, err := getServersFromOpcServerList(node)
if err != nil {
return getServersFromReg(node)
}
return result, nil
}
func getServersFromOpcServerList(node string) ([]*ServerInfo, error) {
location := com.CLSCTX_LOCAL_SERVER
if !com.IsLocal(node) {
location = com.CLSCTX_REMOTE_SERVER
}
iCatInfo, err := com.MakeCOMObjectEx(node, location, &com.CLSID_OpcServerList, &com.IID_IOPCServerList2)
if err != nil {
return nil, NewOPCWrapperError("make com object IOPCServerList2", err)
}
cids := []windows.GUID{IID_CATID_OPCDAServer10, IID_CATID_OPCDAServer20}
defer iCatInfo.Release()
sl := &com.IOPCServerList2{IUnknown: iCatInfo}
iEnum, err := sl.EnumClassesOfCategories(cids, nil)
if err != nil {
return nil, NewOPCWrapperError("enum classes of categories", err)
}
defer iEnum.Release()
var result []*ServerInfo
for {
var classID windows.GUID
var actual uint32
err = iEnum.Next(1, &classID, &actual)
if err != nil {
break
}
server, err := getServer(sl, &classID)
if err != nil {
return nil, NewOPCWrapperError("getServer", err)
}
result = append(result, server)
}
return result, nil
}
func getServersFromReg(node string) ([]*ServerInfo, error) {
var result []*ServerInfo
var err error
hKey, err := registry.OpenRemoteKey(node, registry.CLASSES_ROOT)
if err != nil {
return nil, err
}
defer hKey.Close()
tsKeys, _ := hKey.ReadSubKeyNames(-1)
for _, tsKey := range tsKeys {
info := getServersFromKey(hKey, tsKey)
if info != nil {
result = append(result, info)
}
}
return result, nil
}
func getServersFromKey(hKey registry.Key, progID string) *ServerInfo {
hProgIDKey, err := registry.OpenKey(hKey, progID, registry.READ)
if err != nil {
return nil
}
defer hProgIDKey.Close()
hOPCKey, err := registry.OpenKey(hProgIDKey, "OPC", registry.READ)
if err != nil {
return nil
}
defer hOPCKey.Close()
clsidStr, clsid, err := getClsidFromProgIDKey(hProgIDKey)
if err != nil {
return nil
}
return &ServerInfo{
ProgID: progID,
ClsStr: clsidStr,
VerIndProgID: progID,
ClsID: clsid,
}
}
func getServer(sl *com.IOPCServerList2, classID *windows.GUID) (*ServerInfo, error) {
progID, userType, VerIndProgID, err := sl.GetClassDetails(classID)
if err != nil {
return nil, fmt.Errorf("FAILED to get prog ID from class ID: %w", err)
}
defer func() {
com.CoTaskMemFree(unsafe.Pointer(progID))
com.CoTaskMemFree(unsafe.Pointer(userType))
com.CoTaskMemFree(unsafe.Pointer(VerIndProgID))
}()
clsStr := classID.String()
return &ServerInfo{
ProgID: windows.UTF16PtrToString(progID),
ClsStr: clsStr,
ClsID: classID,
VerIndProgID: windows.UTF16PtrToString(VerIndProgID),
}, nil
}
// GetLocaleID get locale ID
func (s *OPCServer) GetLocaleID() (uint32, error) {
localeID, err := s.iCommon.GetLocaleID()
return localeID, err
}
// GetStartTime Returns the time the server started running
func (s *OPCServer) GetStartTime() (time.Time, error) {
status, err := s.iServer.GetStatus()
if err != nil {
return time.Time{}, err
}
return status.StartTime, nil
}
// GetCurrentTime Returns the current time from the server
func (s *OPCServer) GetCurrentTime() (time.Time, error) {
status, err := s.iServer.GetStatus()
if err != nil {
return time.Time{}, err
}
return status.CurrentTime, nil
}
// GetLastUpdateTime Returns the last update time from the server
func (s *OPCServer) GetLastUpdateTime() (time.Time, error) {
status, err := s.iServer.GetStatus()
if err != nil {
return time.Time{}, err
}
return status.LastUpdateTime, nil
}
// GetMajorVersion Returns the major part of the server version number
func (s *OPCServer) GetMajorVersion() (uint16, error) {
status, err := s.iServer.GetStatus()
if err != nil {
return 0, err
}
return status.MajorVersion, nil
}
// GetMinorVersion Returns the minor part of the server version number
func (s *OPCServer) GetMinorVersion() (uint16, error) {
status, err := s.iServer.GetStatus()
if err != nil {
return 0, err
}
return status.MinorVersion, nil
}
// GetBuildNumber Returns the build number of the server
func (s *OPCServer) GetBuildNumber() (uint16, error) {
status, err := s.iServer.GetStatus()
if err != nil {
return 0, err
}
return status.BuildNumber, nil
}
// GetVendorInfo Returns the vendor information string for the server
func (s *OPCServer) GetVendorInfo() (string, error) {
status, err := s.iServer.GetStatus()
if err != nil {
return "", err
}
return status.VendorInfo, nil
}
// GetServerState Returns the server’s state
func (s *OPCServer) GetServerState() (com.OPCServerState, error) {
status, err := s.iServer.GetStatus()
if err != nil {
return 0, err
}
return status.ServerState, nil
}
// SetLocaleID set locale ID
func (s *OPCServer) SetLocaleID(localeID uint32) error {
return s.iCommon.SetLocaleID(localeID)
}
// GetBandwidth Returns the bandwidth of the server
func (s *OPCServer) GetBandwidth() (uint32, error) {
status, err := s.iServer.GetStatus()
if err != nil {
return 0, err
}
return status.BandWidth, nil
}
// GetOPCGroups get a collection of OPCGroup objects
func (s *OPCServer) GetOPCGroups() *OPCGroups {
return s.groups
}
// GetServerName Returns the server name of the server that the client connected to via Connect().
func (s *OPCServer) GetServerName() string {
return s.Name
}
// GetServerNode Returns the node name of the server that the client connected to via Connect().
func (s *OPCServer) GetServerNode() string {
return s.Node
}
// GetClientName Returns the client name of the client
func (s *OPCServer) GetClientName() string {
return s.clientName
}
// SetClientName Sets the client name of the client
func (s *OPCServer) SetClientName(clientName string) error {
err := s.iCommon.SetClientName(clientName)
if err != nil {
return err
}
s.clientName = clientName
return nil
}
type PropertyDescription struct {
PropertyID int32
Description string
DataType int16
AccessRights int16
}
// CreateBrowser Creates an OPCBrowser object
func (s *OPCServer) CreateBrowser() (*OPCBrowser, error) {
return NewOPCBrowser(s)
}
// GetErrorString Converts an error number to a readable string
func (s *OPCServer) GetErrorString(errorCode int32) (string, error) {
return s.iCommon.GetErrorString(uint32(errorCode))
}
// QueryAvailableLocaleIDs Return the available LocaleIDs for this server/client session
func (s *OPCServer) QueryAvailableLocaleIDs() ([]uint32, error) {
return s.iCommon.QueryAvailableLocaleIDs()
}
// QueryAvailableProperties Return a list of ID codes and Descriptions for the available properties for this ItemID
func (s *OPCServer) QueryAvailableProperties(itemID string) (pPropertyIDs []uint32, ppDescriptions []string, ppvtDataTypes []uint16, err error) {
return s.iItemProperty.QueryAvailableProperties(itemID)
}
// GetItemProperties Return a list of the current data values for the passed ID codes.
func (s *OPCServer) GetItemProperties(itemID string, propertyIDs []uint32) (data []interface{}, errors []error, err error) {
var errs []int32
data, errs, err = s.iItemProperty.GetItemProperties(itemID, propertyIDs)
if err != nil {
return nil, nil, err
}
errors = s.errors(errs)
return data, errors, nil
}
// LookupItemIDs Return a list of ItemIDs (if available) for each of the passed ID codes.
// have not tested because simulator return error
func (s *OPCServer) LookupItemIDs(itemID string, propertyIDs []uint32) ([]string, []error, error) {
ItemIDs, errs, err := s.iItemProperty.LookupItemIDs(itemID, propertyIDs)
if err != nil {
return nil, nil, err
}
errors := s.errors(errs)
return ItemIDs, errors, nil
}
func (s *OPCServer) errors(errs []int32) []error {
errors := make([]error, len(errs))
for i, e := range errs {
if e < 0 {
errStr, _ := s.GetErrorString(e)
errors[i] = &OPCError{
ErrorCode: e,
ErrorMessage: errStr,
}
}
}
return errors
}
// RegisterServerShutDown register server shut down event
func (s *OPCServer) RegisterServerShutDown(ch chan string) error {
if s.event == nil {
var err error
var iUnknownContainer *com.IUnknown
var point *com.IConnectionPoint
var cookie uint32
err = s.iServer.IUnknown.QueryInterface(&com.IID_IConnectionPointContainer, unsafe.Pointer(&iUnknownContainer))
if err != nil {
return NewOPCWrapperError("query interface IConnectionPointContainer", err)
}
defer func() {
if err != nil {
iUnknownContainer.Release()
}
}()
container := &com.IConnectionPointContainer{IUnknown: iUnknownContainer}
point, err = container.FindConnectionPoint(&IID_IOPCShutdown)
if err != nil {
return NewOPCWrapperError("container find connect point", err)
}
defer func() {
if err != nil {
point.Release()
}
}()
event := NewShutdownEventReceiver()
cookie, err = point.Advise((*com.IUnknown)(unsafe.Pointer(event)))
if err != nil {
return NewOPCWrapperError("point advise", err)
}
s.container = container
s.point = point
s.event = event
s.cookie = cookie
}
s.event.AddReceiver(ch)
return nil
}
// Disconnect from OPC server
func (s *OPCServer) Disconnect() error {
var err error
if s.point != nil {
err = s.point.Unadvise(s.cookie)
s.point.Release()
}
if s.container != nil {
s.container.Release()
}
if s.groups != nil {
s.groups.Release()
}
if s.iItemProperty != nil {
s.iItemProperty.Release()
}
if s.iCommon != nil {
s.iCommon.Release()
}
if s.iServer != nil {
s.iServer.Release()
}
return err
}