freeOPCUA Python多个订阅

时间:2017-10-05 11:41:20

标签: python-3.x opc-ua

我使用python opcua github的最小示例客户端和服务器,我似乎无法弄清楚如何订阅具有不同间隔的多个变量。我想要做的是让某些值以高频率更新,而其他值则以更低的频率更新。

通过将列表传递给

,我获得了成功
handle = sub.subscribe_data_change(monitoredNodes)

monitoredNodes是nodeID的列表。

然而,如果在subHandler中触发数据更改事件,则列表中的任何变量都会随时更改,并且我使用if语句来确定哪个变量已更改。如果我想订阅1000个变量,那么为每个事件运行100个if语句似乎很麻烦且效率低下。

如果有任何人有这方面的经验,我很乐意在这里如何妥善处理。下面是我稍微修改过的示例客户端的代码。

import sys
sys.path.insert(0, "..")
import re
from IPython import embed


from opcua import Client

def getChildren(node):
 children = extractName(root.get_child(node).get_children_descriptions())
 return children

def extractName(description):
 qualifiedNames = re.findall(r"QualifiedName\(.*?\)", str(description))
 nodeNames = re.findall("\d:[a-z,A-Z_]*", str(qualifiedNames))
 return nodeNames

class SubHandler(object):

def datachange_notification(self, node, val, data):
    pass
    #print("Python: New data change event", node, val)

def event_notification(self, event):
    print("Python: New event", event)


if __name__ == "__main__":

client = Client("opc.tcp://0.0.0.0:4840/freeopcua/server/")
try:
    client.connect()
    # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects
    root = client.get_root_node()
    print("Objects node is: ", root.get_browse_name())

    # Node objects have methods to read and write node attributes as well       as browse or populate address space
    print("Children of root are: ", root.get_children())

    rootNode = extractName(str(root.get_children_descriptions()))
    print(rootNode)
    print('''
    The following nodes are found on root.
    Press enter the corresponding number to go deeper.
    ''')
    path = ['0:Objects']
    children=[]

    while True:
        for node in enumerate(getChildren(path)):
            print(node[0], ": ", node[1])
        print("Enter 99 to exit or 88 to go back to top")
        sel = int(input('Please make a selection\n' ))
        if sel == 99:
            break
        elif sel == 88:
            path = []
            children = []
        elif sel == 11:
            print(path)
            print(root.get_child(path).get_value())
            print(root.get_child(path))
        else:
            if path == []:
                path.append(rootNode[sel])
                #print(path)
                #print(getChildren(path))
            else:
                children = getChildren(path)
                path.append(children[sel])
                #print(getChildren(path))

    # Now getting a variable node using its browse path
    myvar = root.get_child(["0:Objects", "2:MyObject", "2:MyVariable"])
    obj = root.get_child(["0:Objects", "2:MyObject"])
    print("myvar is: ", myvar.get_value())

    # subscribing to a variable node
    handler = SubHandler()
    sub = client.create_subscription(500, handler)
    handle = sub.subscribe_data_change(myvar)

    embed()
finally:
    client.disconnect()

1 个答案:

答案 0 :(得分:0)

好的,对于任何想要做我想做的事情和有问题的人。我认为使用您只想更新特定间隔的变量订阅不是正确的方法。似乎通过订阅变量,您将获得该变量的每个更改。如果您的订阅周期长于其更改的速率,则更改事件将自上次更新后的所有历史更改触发多次。这不是我想要的。

相反,我建议在python的apscheduler模块中使用这样的计时器任务,并定期对变量执行读取。这似乎做我想要的。