Passed
Push — dev ( 97f75d...d1bef8 )
by Olivier
03:40
created

examples.SubHandler.status_change_notification()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
1
import sys
2
sys.path.insert(0, "..")
3
import logging
4
import time
5
6
try:
7
    from IPython import embed
8
except ImportError:
9
    import code
10
11
    def embed():
12
        vars = globals()
13
        vars.update(locals())
14
        shell = code.InteractiveConsole(vars)
15
        shell.interact()
16
17
18
from opcua import Client
19
from opcua import ua
20
21
22
class SubHandler(object):
23
24
    """
25
    Subscription Handler. To receive events from server for a subscription
26
    data_change and event methods are called directly from receiving thread.
27
    Do not do expensive, slow or network operation there. Create another
28
    thread if you need to do such a thing
29
    """
30
31
    def datachange_notification(self, node, val, data):
32
        print("Python: New data change event", node, val)
33
34
    def event_notification(self, event):
35
        print("Python: New event", event)
36
37
    def status_change_notification(self, status):
38
        print ("Python: New status change", status)
39
40
41
if __name__ == "__main__":
42
    logging.basicConfig(level=logging.WARN)
43
    #logger = logging.getLogger("KeepAlive")
44
    #logger.setLevel(logging.DEBUG)
45
46
    #client = Client("opc.tcp://localhost:4840/freeopcua/server/")
47
    client = Client("opc.tcp://admin@localhost:4840/freeopcua/server/") #connect using a user
48
    try:
49
        client.connect()
50
51
        # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
52
        root = client.get_root_node()
53
        print("Root node is: ", root)
54
        objects = client.get_objects_node()
55
        print("Objects node is: ", objects)
56
57
        # Node objects have methods to read and write node attributes as well as browse or populate address space
58
        print("Children of root are: ", root.get_children())
59
60
        # get a specific node knowing its node id
61
        #var = client.get_node(ua.NodeId(1002, 2))
62
        #var = client.get_node("ns=3;i=2002")
63
        #print(var)
64
        #var.get_data_value() # get value of node as a DataValue object
65
        #var.get_value() # get value of node as a python builtin
66
        #var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type
67
        #var.set_value(3.9) # set node value using implicit data type
68
69
        # Now getting a variable node using its browse path
70
        myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
71
        obj = root.get_child(["0:Objects", "2:MyObject"])
72
        print("myvar is: ", myvar)
73
74
        # subscribing to a variable node
75
        handler = SubHandler()
76
        sub = client.create_subscription(500, handler)
77
        handle = sub.subscribe_data_change(myvar)
78
        time.sleep(0.1)
79
80
        # we can also subscribe to events from server
81
        sub.subscribe_events()
82
        # sub.unsubscribe(handle)
83
        # sub.delete()
84
85
        # calling a method on server
86
        res = obj.call_method("2:multiply", 3, "klk")
87
        print("method result is: ", res)
88
89
        print("Children of MyObject are: ", obj.get_children())
90
        print("myvar should be still there")
91
        deletenode = ua.DeleteNodesItem()
92
        deletenode.NodeId = obj.get_child(["2:MyVariable"]).nodeid
93
        deletenode.DeleteTargetReferences = True
94
        results = client.bclient.delete_nodes([deletenode])
95
        results[0].check()
96
        print("Children of MyObject are: ", obj.get_children())
97
        print("myvar should disapear")
98
99
        embed()
100
    finally:
101
        client.disconnect()
102