def call_method(parent, methodid, *args): """ Call an OPC-UA method. methodid is browse name of child method or the nodeid of method as a NodeId object arguments are variants or python object convertible to variants. which may be of different types returns a list of variants which are output of the method """ if isinstance(methodid, str): methodid = parent.get_child(methodid).nodeid elif isinstance(methodid, Node): methodid = methodid.nodeid arguments = [] for arg in args: if not isinstance(arg, ua.Variant): arg = ua.Variant(arg) arguments.append(arg) result = _call_method(parent.server, parent.nodeid, methodid, arguments) if len(result.OutputArguments) == 0: return None elif len(result.OutputArguments) == 1: return result.OutputArguments[0].Value else: return [var.Value for var in result.OutputArguments]
def set_value(self, value, varianttype=None): """ Set value of a node. Only variables(properties) have values. An exception will be generated for other node types. value argument is either: * a python built-in type, converted to opc-ua optionnaly using the variantype argument. * a ua.Variant, varianttype is then ignored * a ua.DataValue, you then have full control over data send to server """ datavalue = None if isinstance(value, ua.DataValue): datavalue = value elif isinstance(value, ua.Variant): datavalue = ua.DataValue(value) else: datavalue = ua.DataValue(ua.Variant(value, varianttype)) self.set_attribute(ua.AttributeIds.Value, datavalue)
def _vtype_to_argument(vtype): arg = ua.Argument() v = ua.Variant(None, vtype) arg.DataType = _guess_uatype(v) return ua.ExtensionObject.from_object(arg)
def _to_variant(val, vtype=None): if isinstance(val, ua.Variant): return val else: return ua.Variant(val, vtype)
print("Root is", root) print("childs of root are: ", root.get_children()) print("name of root is", root.get_browse_name()) objects = client.get_objects_node() print("childs og objects are: ", objects.get_children()) myfloat = client.get_node("ns=4;s=Float") mydouble = client.get_node("ns=4;s=Double") myint64 = client.get_node("ns=4;s=Int64") myuint64 = client.get_node("ns=4;s=UInt64") myint32 = client.get_node("ns=4;s=Int32") myuint32 = client.get_node("ns=4;s=UInt32") var = client.get_node(ua.NodeId("Random1", 5)) print("var is: ", var) print("value of var is: ", var.get_value()) var.set_value(ua.Variant([23], ua.VariantType.Double)) print("setting float value") myfloat.set_value(ua.Variant(1.234, ua.VariantType.Float)) print("reading float value: ", myfloat.get_value()) handler = SubHandler() sub = client.create_subscription(500, handler) handle = sub.subscribe_data_change(var) device = objects.get_child(["2:MyObjects", "2:MyDevice"]) method = device.get_child("2:MyMethod") result = device.call_method(method, ua.Variant("sin"), ua.Variant(180, ua.VariantType.Double)) print("Mehtod result is: ", result) #embed()
def to_variant(*args): uaargs = [] for arg in args: uaargs.append(ua.Variant(arg)) return uaargs