Commit 58d3fa08 authored by yogesh.m's avatar yogesh.m

update for reccursion

parent 1c97f698
......@@ -3,8 +3,10 @@ import asyncio
import socket
from time import sleep
import pickle
import json
ENDPOINT = 'opc.tcp://2.2.2.7:53530/OPCUA/SimulationServer'
NAMESPACE = 'http://www.prosysopc.com/OPCUA/SimulationServer/'
NAMESPACE = 'http://www.prosysopc.com/OPCUA/SimulationNodes/'
msgFromClient = "Hello UDP Server"
bytesToSend = str.encode(msgFromClient)
......@@ -12,35 +14,46 @@ serverAddressPort = ("2.2.2.5", 20002)
bufferSize = 1024
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
nm_no = None
async def create_node_hierarchy(client, node_objects):
node_hierarchy={}
for sub_obj in node_objects:
if("ns="+str(nm_no)+";" in str(sub_obj)):
node = client.get_node(sub_obj)
children_nodes = await node.get_children()
identifier = str(children_nodes[0].nodeid.Identifier) if children_nodes else ""
node_class = await sub_obj.read_node_class()
if(identifier not in str(sub_obj)):
child_disp_name= await sub_obj.read_display_name()
node_hierarchy[child_disp_name.Text]=await create_node_hierarchy(client,children_nodes)
node_hierarchy[child_disp_name.Text]["name"]=child_disp_name.Text
if (node_class._name_ == "Variable"):
datatype = await sub_obj.read_data_type_as_variant_type()
node_hierarchy[child_disp_name.Text]["datatype"] = datatype._value_
else:
disp_name = await sub_obj.read_display_name()
node_hierarchy[disp_name.Text] ={}
node_hierarchy[disp_name.Text]["name"]=disp_name.Text
if (node_class._name_ == "Variable"):
datatype = await sub_obj.read_data_type_as_variant_type()
node_hierarchy[disp_name.Text]["datatype"] = datatype._value_
return node_hierarchy
async def get_send_hash():
global nm_no
async with Client(url=ENDPOINT) as client:
root = client.get_root_node()
root_objects = await root.get_children()
nm_no = await client.get_namespace_index("http://www.prosysopc.com/OPCUA/SimulationNodes/")
nm_no=await client.get_namespace_index(NAMESPACE)
root_node = client.get_root_node()
root_objects = await root_node.get_children()
node_objects = root_objects[0]
while True:
sleep(1)
individual_node={}
hierarchy = {}
Objects = await root_objects[0].get_children()
for sub_obj in Objects:
if (sub_obj.nodeid.NamespaceIndex == nm_no):
display_name=await sub_obj.read_display_name()
identifier_type = sub_obj.nodeid.NodeIdType._name_
children = await sub_obj.get_children()
for ch in children:
identifier= ch.nodeid.Identifier
individual_node[identifier]={}
node_name=await ch.read_display_name()
individual_node[identifier]["node_name"] =node_name.Text
datatype=await ch.read_data_type_as_variant_type()
individual_node[identifier]["datatype"] = datatype._value_
hierarchy[display_name.Text] = {}
hierarchy[display_name.Text]["name"]=identifier_type
hierarchy[display_name.Text]["obj"]=pickle.dumps(individual_node)
hierarchy["hash"]=hash(str(hierarchy))
hierarchy=pickle.dumps(hierarchy)
node_objects_children = await node_objects.get_children() # replace with your own node id
node_hierarchy = await create_node_hierarchy(client, node_objects_children)
node_hierarchy["hash"] = hash(str(node_hierarchy))
json_hierarchy = json.dumps(node_hierarchy)
hierarchy=pickle.dumps(json_hierarchy)
UDPClientSocket.sendto(hierarchy, serverAddressPort)
#print(hierarchy)
def send_hash():
asyncio.run(get_send_hash())
\ No newline at end of file
......@@ -27,7 +27,6 @@ class MyHandler(SubHandler):
while True:
[node, value, data] = self._queue.get_nowait()
datatype = type(value)
print(value)
packet = str(datatype) + "&" + str(value) + "&" + str(node.nodeid.NamespaceIndex) + "&" + str(node.nodeid.Identifier)
UDPClientSocket.sendto(str.encode(packet), serverAddressPort)
......
......@@ -461,4 +461,4 @@ class opcua_pack():
self.cs=self.cs+1
elif(datatype=="<class 'int'>"):
sock.sendall(self.write_request(self.SecureChannelId, self.TokenId, self.identifier_string, self.cs, int(ns),int(nodeid),float(val), datatype))
self.cs=self.cs+1
\ No newline at end of file
self.cs = self.cs + 1
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment