forked from gopcua/opcua
-
Notifications
You must be signed in to change notification settings - Fork 0
/
datetime.go
65 lines (55 loc) · 1.8 KB
/
datetime.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
// Copyright 2018-2020 opcua authors. All rights reserved.
// Use of this source code is governed by a MIT-style license that can be
// found in the LICENSE file.
package main
import (
"context"
"flag"
"fmt"
"log"
"github.com/barisvelioglu/opcua"
"github.com/barisvelioglu/opcua/debug"
"github.com/barisvelioglu/opcua/ua"
)
func main() {
endpoint := flag.String("endpoint", "opc.tcp://localhost:4840", "OPC UA Endpoint URL")
policy := flag.String("policy", "", "Security policy: None, Basic128Rsa15, Basic256, Basic256Sha256. Default: auto")
mode := flag.String("mode", "", "Security mode: None, Sign, SignAndEncrypt. Default: auto")
certFile := flag.String("cert", "", "Path to cert.pem. Required for security mode/policy != None")
keyFile := flag.String("key", "", "Path to private key.pem. Required for security mode/policy != None")
flag.BoolVar(&debug.Enable, "debug", false, "enable debug logging")
flag.Parse()
log.SetFlags(0)
ctx := context.Background()
endpoints, err := opcua.GetEndpoints(ctx, *endpoint)
if err != nil {
log.Fatal(err)
}
ep := opcua.SelectEndpoint(endpoints, *policy, ua.MessageSecurityModeFromString(*mode))
if ep == nil {
log.Fatal("Failed to find suitable endpoint")
}
fmt.Println("*", ep.SecurityPolicyURI, ep.SecurityMode)
opts := []opcua.Option{
opcua.SecurityPolicy(*policy),
opcua.SecurityModeString(*mode),
opcua.CertificateFile(*certFile),
opcua.PrivateKeyFile(*keyFile),
opcua.AuthAnonymous(),
opcua.SecurityFromEndpoint(ep, ua.UserTokenTypeAnonymous),
}
c := opcua.NewClient(ep.EndpointURL, opts...)
if err := c.Connect(ctx); err != nil {
log.Fatal(err)
}
defer c.Close()
v, err := c.Node(ua.NewNumericNodeID(0, 2258)).Value()
switch {
case err != nil:
log.Fatal(err)
case v == nil:
log.Print("v == nil")
default:
log.Print(v.Value())
}
}