Commit 49ee43c7 authored by yogesh.m's avatar yogesh.m

update

parent f3d65857
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project version="4"> <project version="4">
<component name="RunManager"> <component name="ChangeListManager">
<list default="true" id="e733891a-c063-4f94-92d6-4559e921107b" name="Default Changelist" comment="" />
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
<option name="HIGHLIGHT_NON_ACTIVE_CHANGELIST" value="false" />
<option name="LAST_RESOLUTION" value="IGNORE" />
</component>
<component name="Git.Settings">
<option name="RECENT_GIT_ROOT_PATH" value="$PROJECT_DIR$" />
</component>
<component name="ProjectId" id="2MBBW17AOFLvAzWG4Yg12wAh3jz" />
<component name="ProjectViewState">
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent">
<property name="RunOnceActivity.OpenProjectViewOnStart" value="true" />
<property name="RunOnceActivity.ShowReadmeOnStart" value="true" />
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
</component>
<component name="RecentsManager">
<key name="MoveFile.RECENT_KEYS">
<recent name="D:\opcua-cloning" />
</key>
</component>
<component name="RunManager" selected="Python.opcua-client">
<configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true"> <configuration name="main" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
<module name="opcua-cloning" /> <module name="opcua-cloning" />
<option name="INTERPRETER_OPTIONS" value="" /> <option name="INTERPRETER_OPTIONS" value="" />
...@@ -22,5 +47,42 @@ ...@@ -22,5 +47,42 @@
<option name="INPUT_FILE" value="" /> <option name="INPUT_FILE" value="" />
<method v="2" /> <method v="2" />
</configuration> </configuration>
<configuration name="opcua-client" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="opcua-cloning" />
<option name="INTERPRETER_OPTIONS" value="" />
<option name="PARENT_ENVS" value="true" />
<envs>
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/opcua-client.py" />
<option name="PARAMETERS" value="" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
<option name="REDIRECT_INPUT" value="false" />
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<recent_temporary>
<list>
<item itemvalue="Python.opcua-client" />
</list>
</recent_temporary>
</component>
<component name="SpellCheckerSettings" RuntimeDictionaries="0" Folders="0" CustomDictionaries="0" DefaultDictionary="application-level" UseSingleDictionary="true" transferred="true" />
<component name="TaskManager">
<task active="true" id="Default" summary="Default task">
<changelist id="e733891a-c063-4f94-92d6-4559e921107b" name="Default Changelist" comment="" />
<created>1677231122232</created>
<option name="number" value="Default" />
<option name="presentableId" value="Default" />
<updated>1677231122232</updated>
</task>
<servers />
</component> </component>
</project> </project>
\ No newline at end of file
from opcua import Client import asyncio
import socket
url = "opc.tcp://klblrlp1437.corp.knowledgelens.com:53530/OPCUA/SimulationServer"
client = Client(url) from asyncua import Client, Node
client.connect() from asyncua.common.subscription import DataChangeNotif, SubHandler
root = client.get_root_node() msgFromClient = "Hello UDP Server"
objects = root.get_children() bytesToSend = str.encode(msgFromClient)
for obj in objects: serverAddressPort = ("2.2.2.5", 20001)
print(f"Object: {obj.get_browse_name()}") bufferSize = 1024
UDPClientSocket = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
nodes = obj.get_children()
for node in nodes: ENDPOINT = 'opc.tcp://2.2.2.7:53530/OPCUA/SimulationServer'
node_id = node.nodeid.Identifier NAMESPACE = 'http://www.prosysopc.com/OPCUA/SimulationServer/'
identifier = node.get_browse_name().Name
if(node_id==str('85/0:Simulation')): class MyHandler(SubHandler):
object_node = client.get_node(node) def __init__(self):
subnodes=node.get_children() self._queue = asyncio.Queue()
for snode in subnodes:
value = client.get_node(snode).get_value() def datachange_notification(self, node: Node, value, data: DataChangeNotif) -> None:
print(value) self._queue.put_nowait([node, value, data])
client.disconnect()
async def process(self) -> None:
try:
while True:
# Get data in a queue.
[node, value, data] = self._queue.get_nowait()
path = await node.get_path(as_string=True)
datatype = type(value)
packet = str(datatype) + "&" + str(value) + "&" + 'ns=' + str(node.nodeid.NamespaceIndex) + ";i=" + str(node.nodeid.Identifier)
UDPClientSocket.sendto(str.encode(packet), serverAddressPort)
except asyncio.QueueEmpty:
pass
async def get_nodes(client):
subnodes = None
root = client.get_root_node()
objects = await root.get_children()
for obj in objects:
nodes = await obj.get_children()
for node in nodes:
node_id = node.nodeid.Identifier
if (node_id == str('85/0:Simulation')):
subnodes = await node.get_children()
return subnodes
async def main() -> None:
async with Client(url=ENDPOINT) as client:
# Get a variable node.
node = await get_nodes(client)
# Subscribe data change.
handler = MyHandler()
subscription = await client.create_subscription(period=0, handler=handler)
await subscription.subscribe_data_change(node)
# Process data change every 100ms
while True:
await handler.process()
await asyncio.sleep(0.1)
if __name__ == '__main__':
asyncio.run(main())
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