-
Hello. I'm working on the task to transfer dynamically sensor-data via opc ua. These values are important which I receive from the machine with a
Definition of struct process_data_struct = await new_struct(server, idx, "ProcessDataStruct", [
new_struct_field("Timestamp", ua.VariantType.DateTime, timestamp),
new_struct_field("StatusMessage", ua.VariantType.String),
new_struct_field("Error", ua.VariantType.Boolean),
new_struct_field("CurrentValue", ua.VariantType.Float)
]) Check whether node is already exists if not create it. Is that about right to check whether the node already exists? try:
# check whether node is already created
sensor_node_id = f"ns=2;s={sensorID}" # find node
sensor_node = await self.client.get_node(sensor_node_id)
except:
# create new node
newObject = await myobj.add_variable(idx, bname=sensorID, datatype=process_data_struct)
# TODO: put data into ´process_data_struct´ |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
# defining struct (see 1. code in question post)
await server.load_data_type_definitions() # very important to enable the struct idk
# create struct
dataScruct = ua.ProcessDataStruct()
# put mock/initial-data in struct
dataScruct.Timestamp = datetime.now()
dataScruct.CurrentValue = 55.5
dataScruct.Error = False
dataScruct.StatusMessage = ''
# add variable
sensor65 = await myobj.add_variable(idx, "Sensor65", ua.Variant(dataScruct, ua.VariantType.ExtensionObject))
# start server
async with server:
self.logger.info("Server: server started!")
while True:
# putting actual data in the struct
dataScruct = ua.ProcessDataStruct()
dataScruct.Timestamp = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
dataScruct.CurrentValue = value
dataScruct.Error = int(error)
dataScruct.StatusMessage = status_message
if sensorID == 65:
await sensor65.set_value(dataScruct) # set data |
Beta Was this translation helpful? Give feedback.
check whether node exists: there is a very helpfull stackoverflow answer:
https://stackoverflow.com/a/78086111/11322034
about the struct I found a solution that works: