Commit 624a3d06 authored by yogesh.m's avatar yogesh.m

update

parent 7fef5d5a
...@@ -5,15 +5,6 @@ from time import sleep ...@@ -5,15 +5,6 @@ from time import sleep
import pickle import pickle
import json import json
ENDPOINT = 'opc.tcp://2.2.2.7:53530/OPCUA/SimulationServer'
NAMESPACE = 'http://www.prosysopc.com/OPCUA/SimulationNodes/'
msgFromClient = "Hello UDP Server"
bytesToSend = str.encode(msgFromClient)
serverAddressPort = ("2.2.2.5", 20002)
bufferSize = 1024
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
nm_no = None nm_no = None
async def create_node_hierarchy(client, node_objects): async def create_node_hierarchy(client, node_objects):
...@@ -44,8 +35,11 @@ async def create_node_hierarchy(client, node_objects): ...@@ -44,8 +35,11 @@ async def create_node_hierarchy(client, node_objects):
node_hierarchy[identifier_name]["datatype"] = datatype._value_ node_hierarchy[identifier_name]["datatype"] = datatype._value_
return node_hierarchy return node_hierarchy
async def get_send_hash(): async def get_send_hash(ENDPOINT,NAMESPACE,server_hash_udp_ip,server_hash_udp_port):
global nm_no global nm_no
serverAddressPort = (server_hash_udp_ip, server_hash_udp_port)
bufferSize = 1024
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
async with Client(url=ENDPOINT) as client: async with Client(url=ENDPOINT) as client:
nm_no=await client.get_namespace_index(NAMESPACE) nm_no=await client.get_namespace_index(NAMESPACE)
root_node = client.get_root_node() root_node = client.get_root_node()
...@@ -55,9 +49,10 @@ async def get_send_hash(): ...@@ -55,9 +49,10 @@ async def get_send_hash():
node_objects_children = await node_objects.get_children() # replace with your own node id 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 = await create_node_hierarchy(client, node_objects_children)
node_hierarchy["hash"] = hash(str(node_hierarchy)) node_hierarchy["hash"] = hash(str(node_hierarchy))
node_hierarchy["namespace_idx"]=nm_no
json_hierarchy = json.dumps(node_hierarchy) json_hierarchy = json.dumps(node_hierarchy)
hierarchy=pickle.dumps(json_hierarchy) hierarchy=pickle.dumps(json_hierarchy)
UDPClientSocket.sendto(hierarchy, serverAddressPort) UDPClientSocket.sendto(hierarchy, serverAddressPort)
def send_hash(): def send_hash(Endpoint,Namespace,server_hash_udp_ip,server_hash_udp_port):
asyncio.run(get_send_hash()) asyncio.run(get_send_hash(Endpoint,Namespace,server_hash_udp_ip,server_hash_udp_port))
\ No newline at end of file \ No newline at end of file
Configuration:
endpoint_url = "opc.tcp://2.2.2.5:53531/myopc/free"
namespace_server ="http://klopc.com"
udp_hash_receiver_ip="2.2.2.5"
udp_hash_receiver_port=20002
udp_ip = "2.2.2.5"
udp_port = 20001
\ No newline at end of file
configuration:
server_hash_udp_ip: "2.2.2.5"
server_hash_udp_port: 20002
server_udp_ip: 2.2.2.5
server_udp_port: 20001
endpoint: opc.tcp://2.2.2.7:53530/OPCUA/SimulationServer
namespace: http://www.prosysopc.com/OPCUA/SimulationNodes/
\ No newline at end of file
...@@ -4,15 +4,21 @@ import threading ...@@ -4,15 +4,21 @@ import threading
import hash_sender import hash_sender
from asyncua import Client, Node from asyncua import Client, Node
from asyncua.common.subscription import DataChangeNotif, SubHandler from asyncua.common.subscription import DataChangeNotif, SubHandler
import yaml
yamlfile=open("opc_ua_transmitter_config.yaml")
data = yaml.load(yamlfile, Loader=yaml.FullLoader)
server_hash_udp_ip = data["configuration"]["server_hash_udp_ip"]
server_hash_udp_port = data["configuration"]["server_hash_udp_port"]
server_udp_ip = data["configuration"]["server_udp_ip"]
server_udp_port = int(data["configuration"]["server_udp_port"])
serverAddressPort = (server_udp_ip,server_udp_port)
ENDPOINT = data["configuration"]["endpoint"]
NAMESPACE = data["configuration"]["namespace"]
msgFromClient = "Hello UDP Server"
bytesToSend = str.encode(msgFromClient)
serverAddressPort = ("2.2.2.5", 20001)
bufferSize = 1024 bufferSize = 1024
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
nm_no = None
ENDPOINT = 'opc.tcp://2.2.2.7:53530/OPCUA/SimulationServer'
NAMESPACE = 'http://www.prosysopc.com/OPCUA/SimulationServer/'
class MyHandler(SubHandler): class MyHandler(SubHandler):
def __init__(self): def __init__(self):
...@@ -37,7 +43,7 @@ class MyHandler(SubHandler): ...@@ -37,7 +43,7 @@ class MyHandler(SubHandler):
variables=[] variables=[]
async def get_nodes(client,node_objects): async def get_nodes(client,node_objects):
for sub_obj in node_objects: for sub_obj in node_objects:
if ("ns=3;" in str(sub_obj)): if ("ns="+nm_no+";" in str(sub_obj)):
node = client.get_node(sub_obj) node = client.get_node(sub_obj)
children_nodes = await node.get_children() children_nodes = await node.get_children()
identifier = str(children_nodes[0].nodeid.Identifier) if children_nodes else "" identifier = str(children_nodes[0].nodeid.Identifier) if children_nodes else ""
...@@ -53,19 +59,17 @@ async def get_nodes(client,node_objects): ...@@ -53,19 +59,17 @@ async def get_nodes(client,node_objects):
async def main() -> None: async def main() -> None:
global nm_no
async with Client(url=ENDPOINT) as client: async with Client(url=ENDPOINT) as client:
# Get a variable node. nm_no = str(await client.get_namespace_index(NAMESPACE))
object_root_node = client.get_objects_node() object_root_node = client.get_objects_node()
objects = await object_root_node.get_children() objects = await object_root_node.get_children()
node = await get_nodes(client,objects) node = await get_nodes(client,objects)
# Subscribe data change. t1=threading.Thread(target=hash_sender.send_hash,args=(ENDPOINT,NAMESPACE,server_hash_udp_ip,server_hash_udp_port,))
t1=threading.Thread(target=hash_sender.send_hash)
t1.start() t1.start()
handler = MyHandler() handler = MyHandler()
subscription = await client.create_subscription(period=0, handler=handler) subscription = await client.create_subscription(period=0, handler=handler)
await subscription.subscribe_data_change(node) await subscription.subscribe_data_change(node)
# Process data change every 100ms
while True: while True:
await handler.process() await handler.process()
await asyncio.sleep(0.01) await asyncio.sleep(0.01)
......
...@@ -3,16 +3,18 @@ from time import sleep ...@@ -3,16 +3,18 @@ from time import sleep
from opcua_subscriber.opcua_subscribe import opcua_pack from opcua_subscriber.opcua_subscribe import opcua_pack
from asyncua_server import st from asyncua_server import st
import threading import threading
Endpoint_Url = "opc.tcp://2.2.2.5:53531/myopc/free" import yaml
Namespace_Server ="http://klopc.com"
udp_hash_receiver_ip="2.2.2.5" yamlfile=open("opc_ua_transmitter_config.yaml")
udp_hash_receiver_port=20002 data = yaml.load(yamlfile, Loader=yaml.FullLoader)
Endpoint_Url = data["configuration"]["endpoint_url"]
Namespace_Server = data["configuration"]["namespace_server"]
udp_hash_receiver_ip=data["configuration"]["udp_hash_receiver_ip"]
udp_hash_receiver_port=data["configuration"]["udp_hash_receiver_port"]
localIP = data["configuration"]["udp_ip"]
localPort = data["configuration"]["udp_port"]
localIP = "2.2.2.5"
localPort = 20001
bufferSize = 1024 bufferSize = 1024
UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM) UDPServerSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
UDPServerSocket.bind((localIP, localPort)) UDPServerSocket.bind((localIP, localPort))
......
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