diff --git a/devices/clients/opcua_client.py b/devices/clients/opcua_client.py index 7572bde14801fc1aefd5492b1b0ee5974f22aa4b..ac66f470613194101c41784adea6889003cc1cfb 100644 --- a/devices/clients/opcua_client.py +++ b/devices/clients/opcua_client.py @@ -227,18 +227,35 @@ class ProtocolAttribute: try: self.node.set_data_value(opcua.ua.uatypes.Variant(value=value, varianttype=self.ua_type)) + raise TypeError except (TypeError, opcua.ua.uaerrors.BadTypeMismatch) as e: # A type conversion went wrong or there is a type mismatch. # # This is either the conversion us -> opcua in our client, or client -> server. # Report all types involved to allow assessment of the location of the error. if type(value) == list: - our_type = "list(%s) x (%d)" % (type(value[0]).__name__ if value else "", len(value)) + our_type = "list({dtype}) x ({dimensions})".format( + dtype=(type(value[0]).__name__ if value else ""), + dimensions=len(value)) else: - our_type = "%s" % type(value) + our_type = "{dtype}".format( + dtype=type(value)) is_scalar = (self.dim_x + self.dim_y) == 1 - expected_server_type = "%s %s" % (self.ua_type, "(scalar)" if is_scalar else "x (%d, %d)" % (self.dim_x, self.dim_y)) - actual_server_type = "%s x %s" % (self.node.get_data_type_as_variant_type(), self.node.get_array_dimensions() or "???") - raise TypeError("Cannot write value to OPC-UA attribute '%s': tried to convert data type %s to expected server type %s, server reports type %s" % (self.node.get_display_name().to_string(), our_type, expected_server_type, actual_server_type)) from e + if is_scalar: + expected_server_type = "{dtype} (scalar)".format( + dtype=self.ua_type) + else: + expected_server_type = "{dtype} x ({dim_x}, {dim_y})".format( + dtype=self.ua_type, + dim_x=self.dim_x, + dim_y=self.dim_y) + + actual_server_type = "{dtype} {dimensions}".format( + dtype=self.node.get_data_type_as_variant_type(), + dimensions=(self.node.get_array_dimensions() or "???")) + + attribute_name = self.node.get_display_name().to_string() + + raise TypeError(f"Cannot write value to OPC-UA attribute '{attribute_name}': tried to convert data type {our_type} to expected server type {expected_server_type}, server reports type {actual_server_type}") from e