From ef8ea2fd5b3ffc28d179015b6f3615b4eeec4f84 Mon Sep 17 00:00:00 2001 From: Shreyan Das <72906700+sdspot2034@users.noreply.github.com> Date: Thu, 19 Jan 2023 12:31:56 +0530 Subject: [PATCH] Create client-for-historian-nodes.py Example of a client reading history data from an OPC Server with historized nodes --- examples/client-for-historian-nodes.py | 40 ++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 examples/client-for-historian-nodes.py diff --git a/examples/client-for-historian-nodes.py b/examples/client-for-historian-nodes.py new file mode 100644 index 00000000..804fe0ce --- /dev/null +++ b/examples/client-for-historian-nodes.py @@ -0,0 +1,40 @@ +import datetime +import sys +from opcua import Client, ua + + +# Connect to the OPC server +url = "opc.tcp://localhost:4840" +client = Client(url) +client.connect() + +# Define the tags to fetch +tags = ["ns=2;i=2"] + +# Define the start and end time for the historical data +end_time = datetime.datetime.utcnow() +start_time = end_time - datetime.timedelta(hours=1) + + +try: + # Fetch the historian data + historian_data = [] + for tag in tags: + node = client.get_node(tag) + data = node.read_raw_history(start_time, end_time, numvalues=0) + historian_data.append((tag, data)) + + # Print the data + for tag, values in historian_data: + print(tag) + for value in values: + print(value) + + +except Exception as e: + print(e) + sys.exit() + +finally: + # Disconnect from the OPC server + client.disconnect()