1+ //go:build windows
12// +build windows
23
34package opc
@@ -16,24 +17,24 @@ func init() {
1617 OleInit ()
1718}
1819
19- //OleInit initializes OLE.
20+ // OleInit initializes OLE.
2021func OleInit () {
2122 ole .CoInitializeEx (0 , 0 )
2223}
2324
24- //OleRelease realeses OLE resources in opcAutomation.
25+ // OleRelease realeses OLE resources in opcAutomation.
2526func OleRelease () {
2627 ole .CoUninitialize ()
2728}
2829
29- //AutomationObject loads the OPC Automation Wrapper and handles to connection to the OPC Server.
30+ // AutomationObject loads the OPC Automation Wrapper and handles to connection to the OPC Server.
3031type AutomationObject struct {
3132 unknown * ole.IUnknown
3233 object * ole.IDispatch
3334}
3435
35- //CreateBrowser returns the OPCBrowser object from the OPCServer.
36- //It only works if there is a successful connection.
36+ // CreateBrowser returns the OPCBrowser object from the OPCServer.
37+ // It only works if there is a successful connection.
3738func (ao * AutomationObject ) CreateBrowser () (* Tree , error ) {
3839 // check if server is running, if not return error
3940 if ! ao .IsConnected () {
@@ -56,7 +57,7 @@ func (ao *AutomationObject) CreateBrowser() (*Tree, error) {
5657 return & root , nil
5758}
5859
59- //buildTree runs through the OPCBrowser and creates a tree with the OPC tags
60+ // buildTree runs through the OPCBrowser and creates a tree with the OPC tags
6061func buildTree (browser * ole.IDispatch , branch * Tree ) {
6162 var count int32
6263
@@ -109,8 +110,8 @@ func buildTree(browser *ole.IDispatch, branch *Tree) {
109110
110111}
111112
112- //Connect establishes a connection to the OPC Server on node.
113- //It returns a reference to AutomationItems and error message.
113+ // Connect establishes a connection to the OPC Server on node.
114+ // It returns a reference to AutomationItems and error message.
114115func (ao * AutomationObject ) Connect (server string , node string ) (* AutomationItems , error ) {
115116
116117 // make sure there is not active connection before trying to connect
@@ -149,7 +150,7 @@ func (ao *AutomationObject) Connect(server string, node string) (*AutomationItem
149150 return NewAutomationItems (addItemObject .ToIDispatch ()), nil
150151}
151152
152- //TryConnect loops over the nodes array and tries to connect to any of the servers.
153+ // TryConnect loops over the nodes array and tries to connect to any of the servers.
153154func (ao * AutomationObject ) TryConnect (server string , nodes []string ) (* AutomationItems , error ) {
154155 var errResult string
155156 for _ , node := range nodes {
@@ -162,7 +163,7 @@ func (ao *AutomationObject) TryConnect(server string, nodes []string) (*Automati
162163 return nil , errors .New ("TryConnect was not successful: " + errResult )
163164}
164165
165- //IsConnected check if the server is properly connected and up and running.
166+ // IsConnected check if the server is properly connected and up and running.
166167func (ao * AutomationObject ) IsConnected () bool {
167168 if ao .object == nil {
168169 return false
@@ -178,7 +179,7 @@ func (ao *AutomationObject) IsConnected() bool {
178179 return true
179180}
180181
181- //GetOPCServers returns a list of Prog ID on the specified node
182+ // GetOPCServers returns a list of Prog ID on the specified node
182183func (ao * AutomationObject ) GetOPCServers (node string ) []string {
183184 progids , err := oleutil .CallMethod (ao .object , "GetOPCServers" , node )
184185 if err != nil {
@@ -195,7 +196,7 @@ func (ao *AutomationObject) GetOPCServers(node string) []string {
195196 return servers_found
196197}
197198
198- //Disconnect checks if connected to server and if so, it calls 'disconnect'
199+ // Disconnect checks if connected to server and if so, it calls 'disconnect'
199200func (ao * AutomationObject ) disconnect () {
200201 if ao .IsConnected () {
201202 _ , err := oleutil .CallMethod (ao .object , "Disconnect" )
@@ -205,7 +206,7 @@ func (ao *AutomationObject) disconnect() {
205206 }
206207}
207208
208- //Close releases the OLE objects in the AutomationObject.
209+ // Close releases the OLE objects in the AutomationObject.
209210func (ao * AutomationObject ) Close () {
210211 if ao .object != nil {
211212 ao .disconnect ()
@@ -216,7 +217,7 @@ func (ao *AutomationObject) Close() {
216217 }
217218}
218219
219- //NewAutomationObject connects to the COM object based on available wrappers.
220+ // NewAutomationObject connects to the COM object based on available wrappers.
220221func NewAutomationObject () * AutomationObject {
221222 // TODO: list should not be hard-coded
222223 wrappers := []string {
@@ -250,14 +251,14 @@ func NewAutomationObject() *AutomationObject {
250251 return & object
251252}
252253
253- //AutomationItems store the OPCItems from OPCGroup and does the bookkeeping
254- //for the individual OPC items. Tags can added, removed, and read.
254+ // AutomationItems store the OPCItems from OPCGroup and does the bookkeeping
255+ // for the individual OPC items. Tags can added, removed, and read.
255256type AutomationItems struct {
256257 addItemObject * ole.IDispatch
257258 items map [string ]* ole.IDispatch
258259}
259260
260- //addSingle adds the tag and returns an error. Client handles are not implemented yet.
261+ // addSingle adds the tag and returns an error. Client handles are not implemented yet.
261262func (ai * AutomationItems ) addSingle (tag string ) error {
262263 clientHandle := int32 (1 )
263264 item , err := oleutil .CallMethod (ai .addItemObject , "AddItem" , tag , clientHandle )
@@ -268,7 +269,7 @@ func (ai *AutomationItems) addSingle(tag string) error {
268269 return nil
269270}
270271
271- //Add accepts a variadic parameters of tags.
272+ // Add accepts a variadic parameters of tags.
272273func (ai * AutomationItems ) Add (tags ... string ) error {
273274 var errResult string
274275 for _ , tag := range tags {
@@ -283,7 +284,7 @@ func (ai *AutomationItems) Add(tags ...string) error {
283284 return errors .New (errResult )
284285}
285286
286- //Remove removes the tag.
287+ // Remove removes the tag.
287288func (ai * AutomationItems ) Remove (tag string ) {
288289 item , ok := ai .items [tag ]
289290 if ok {
@@ -306,7 +307,7 @@ func ensureInt16(q interface{}) int16 {
306307 return 0
307308}
308309
309- //readFromOPC reads from the server and returns an Item and error.
310+ // readFromOPC reads from the server and returns an Item and error.
310311func (ai * AutomationItems ) readFromOpc (opcitem * ole.IDispatch ) (Item , error ) {
311312 v := ole .NewVariant (ole .VT_R4 , 0 )
312313 q := ole .NewVariant (ole .VT_INT , 0 )
@@ -330,7 +331,7 @@ func (ai *AutomationItems) readFromOpc(opcitem *ole.IDispatch) (Item, error) {
330331 }, nil
331332}
332333
333- //writeToOPC writes value to opc tag and return an error
334+ // writeToOPC writes value to opc tag and return an error
334335func (ai * AutomationItems ) writeToOpc (opcitem * ole.IDispatch , value interface {}) error {
335336 _ , err := oleutil .CallMethod (opcitem , "Write" , value )
336337 if err != nil {
@@ -342,7 +343,7 @@ func (ai *AutomationItems) writeToOpc(opcitem *ole.IDispatch, value interface{})
342343 return nil
343344}
344345
345- //Close closes the OLE objects in AutomationItems.
346+ // Close closes the OLE objects in AutomationItems.
346347func (ai * AutomationItems ) Close () {
347348 if ai != nil {
348349 for key , opcitem := range ai .items {
@@ -353,15 +354,15 @@ func (ai *AutomationItems) Close() {
353354 }
354355}
355356
356- //NewAutomationItems returns a new AutomationItems instance.
357+ // NewAutomationItems returns a new AutomationItems instance.
357358func NewAutomationItems (opcitems * ole.IDispatch ) * AutomationItems {
358359 ai := AutomationItems {addItemObject : opcitems , items : make (map [string ]* ole.IDispatch )}
359360 return & ai
360361}
361362
362- //opcRealServer implements the Connection interface.
363- //It has the AutomationObject embedded for connecting to the server
364- //and an AutomationItems to facilitate the OPC items bookkeeping.
363+ // opcRealServer implements the Connection interface.
364+ // It has the AutomationObject embedded for connecting to the server
365+ // and an AutomationItems to facilitate the OPC items bookkeeping.
365366type opcConnectionImpl struct {
366367 * AutomationObject
367368 * AutomationItems
@@ -370,7 +371,7 @@ type opcConnectionImpl struct {
370371 mu sync.Mutex
371372}
372373
373- //ReadItem returns an Item for a specific tag.
374+ // ReadItem returns an Item for a specific tag.
374375func (conn * opcConnectionImpl ) ReadItem (tag string ) Item {
375376 conn .mu .Lock ()
376377 defer conn .mu .Unlock ()
@@ -388,7 +389,7 @@ func (conn *opcConnectionImpl) ReadItem(tag string) Item {
388389 return Item {}
389390}
390391
391- //Write writes a value to the OPC Server.
392+ // Write writes a value to the OPC Server.
392393func (conn * opcConnectionImpl ) Write (tag string , value interface {}) error {
393394 conn .mu .Lock ()
394395 defer conn .mu .Unlock ()
@@ -400,7 +401,7 @@ func (conn *opcConnectionImpl) Write(tag string, value interface{}) error {
400401 return errors .New ("No Write performed" )
401402}
402403
403- //Read returns a map of the values of all added tags.
404+ // Read returns a map of the values of all added tags.
404405func (conn * opcConnectionImpl ) Read () map [string ]Item {
405406 conn .mu .Lock ()
406407 defer conn .mu .Unlock ()
@@ -417,7 +418,7 @@ func (conn *opcConnectionImpl) Read() map[string]Item {
417418 return allTags
418419}
419420
420- //Tags returns the currently active tags
421+ // Tags returns the currently active tags
421422func (conn * opcConnectionImpl ) Tags () []string {
422423 var tags []string
423424 if conn .AutomationItems != nil {
@@ -429,13 +430,13 @@ func (conn *opcConnectionImpl) Tags() []string {
429430
430431}
431432
432- //fix tries to reconnect if connection is lost by creating a new connection
433- //with AutomationObject and creating a new AutomationItems instance.
433+ // fix tries to reconnect if connection is lost by creating a new connection
434+ // with AutomationObject and creating a new AutomationItems instance.
434435func (conn * opcConnectionImpl ) fix () {
435436 var err error
436437 if ! conn .IsConnected () {
438+ tags := conn .Tags ()
437439 for {
438- tags := conn .Tags ()
439440 conn .AutomationItems .Close ()
440441 conn .AutomationItems , err = conn .TryConnect (conn .Server , conn .Nodes )
441442 if err != nil {
@@ -451,7 +452,7 @@ func (conn *opcConnectionImpl) fix() {
451452 }
452453}
453454
454- //Close closes the embedded types.
455+ // Close closes the embedded types.
455456func (conn * opcConnectionImpl ) Close () {
456457 conn .mu .Lock ()
457458 defer conn .mu .Unlock ()
@@ -463,7 +464,7 @@ func (conn *opcConnectionImpl) Close() {
463464 }
464465}
465466
466- //NewConnection establishes a connection to the OpcServer object.
467+ // NewConnection establishes a connection to the OpcServer object.
467468func NewConnection (server string , nodes []string , tags []string ) (Connection , error ) {
468469 object := NewAutomationObject ()
469470 items , err := object .TryConnect (server , nodes )
@@ -484,7 +485,7 @@ func NewConnection(server string, nodes []string, tags []string) (Connection, er
484485 return & conn , nil
485486}
486487
487- //CreateBrowser creates an opc browser representation
488+ // CreateBrowser creates an opc browser representation
488489func CreateBrowser (server string , nodes []string ) (* Tree , error ) {
489490 object := NewAutomationObject ()
490491 defer object .Close ()
0 commit comments