私はpython opcua githubのための最小限のサンプルクライアントとサーバーを使用しています。異なる間隔で複数の変数をサブスクライブする方法を理解できないようです。私がやりたいことは、特定の値を高い頻度で更新し、他の値をはるかに低い頻度で更新することです。freeOPCUA Python複数のサブスクリプション
私は
handle = sub.subscribe_data_change(monitoredNodes)
monitoredNodesは、ノード識別子のリストであることにリストを渡すことに成功しました。
しかし、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()
更新:私はすべてを倍増させて2つのサブスクリプションを作りました。私は新しいSubHandlerクラス、2番目のハンドラオブジェクト、2番目のサブオブジェクト、2番目のハンドルオブジェクトを作成しました。これは、if文の長いリストで単一のサブスクリプションを行うよりも悪いようです。私は根本的にこれについて間違っていますか? – uprightcarrion