forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
126 lines (111 loc) · 2.95 KB
/
client.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
package opcua
import (
"context"
"fmt"
"time"
"github.com/gopcua/opcua/ua"
"github.com/gopcua/opcua/uacp"
"github.com/gopcua/opcua/uasc"
)
// Client is a high-level client for an OPC/UA server.
// It establishes a secure channel and a session.
type Client struct {
Addr string
config *uasc.Config
sechan *uasc.SecureChannel
session *uasc.Session
}
func NewClient(addr string, cfg *uasc.Config) *Client {
return &Client{Addr: addr, config: cfg}
}
// Open connects to the server and establishes a secure channel
// and a session.
func (c *Client) Open() error {
ctx := context.Background()
conn, err := uacp.Dial(ctx, c.Addr)
if err != nil {
return err
}
sechan := uasc.NewSecureChannel(conn, nil)
if err := sechan.Open(); err != nil {
conn.Close()
return err
}
sechan.EndpointURL = c.Addr
c.sechan = sechan
// todo(fs): this should probably be configurable.
sessionCfg := uasc.NewClientSessionConfig(
[]string{"en-US"},
&ua.AnonymousIdentityToken{PolicyID: "open62541-anonymous-policy"},
)
session := uasc.NewSession(sechan, sessionCfg)
if err := session.Open(); err != nil {
sechan.Close()
return err
}
c.session = session
return nil
}
// Close closes the session, the secure channel and the network
// connection to the server.
func (c *Client) Close() error {
if c.session != nil {
c.session.Close()
}
return c.sechan.Close()
}
// Node returns a node object which accesses its attributes
// through this client connection.
func (c *Client) Node(id *ua.NodeID) *Node {
return &Node{ID: id, c: c}
}
// Read executes a synchronous read request.
func (c *Client) Read(req *ua.ReadRequest) (*ua.ReadResponse, error) {
var res *ua.ReadResponse
err := c.sechan.Send(req, func(v interface{}) error {
r, ok := v.(*ua.ReadResponse)
if !ok {
return fmt.Errorf("invalid response: %T", v)
}
res = r
return nil
})
return res, err
}
// Browse executes a synchronous browse request.
func (c *Client) Browse(req *ua.BrowseRequest) (*ua.BrowseResponse, error) {
var res *ua.BrowseResponse
err := c.sechan.Send(req, func(v interface{}) error {
r, ok := v.(*ua.BrowseResponse)
if !ok {
return fmt.Errorf("invalid response: %T", v)
}
res = r
return nil
})
return res, err
}
// todo(fs): this is not done yet since we need to be able to register
// todo(fs): monitored items.
type Subscription struct {
res *ua.CreateSubscriptionResponse
}
// todo(fs): return subscription object with channel
func (c *Client) Subscribe(intv time.Duration) (*Subscription, error) {
req := &ua.CreateSubscriptionRequest{
RequestedPublishingInterval: float64(intv / time.Millisecond),
RequestedLifetimeCount: 60,
RequestedMaxKeepAliveCount: 20,
PublishingEnabled: true,
}
var res *ua.CreateSubscriptionResponse
err := c.sechan.Send(req, func(v interface{}) error {
r, ok := v.(*ua.CreateSubscriptionResponse)
if !ok {
return fmt.Errorf("invalid response: %T", v)
}
res = r
return nil
})
return &Subscription{res}, err
}