def connect(self, uri): self.disconnect() logger.info("Connecting to %s with parameters %s, %s, %s, %s", uri, self.security_mode, self.security_policy, self.user_certificate_path, self.user_private_key_path) self.client = Client(uri) self.client.application_uri = self.application_uri self.client.description = "FreeOpcUa Client GUI" # Set user identity token if self.user_private_key_path: self.client.load_private_key(self.user_private_key_path) if self.user_certificate_path: self.client.load_client_certificate(self.user_certificate_path) # Set security mode and security policy if self.security_mode is not None and self.security_policy is not None: self.client.set_security( getattr(crypto.security_policies, 'SecurityPolicy' + self.security_policy), self.application_certificate_path, self.application_private_key_path, mode=getattr(ua.MessageSecurityMode, self.security_mode) ) self.client.connect() self._connected = True self.client.load_data_type_definitions() self.client.load_enums() self.client.load_type_definitions() self.save_security_settings(uri)
def get_endpoints(uri): client = Client(uri, timeout=2) edps = client.connect_and_get_server_endpoints() for i, ep in enumerate(edps, start=1): logger.info('Endpoint %s:', i) for (n, v) in endpoint_to_strings(ep): logger.info(' %s: %s', n, v) logger.info('') return edps
def connect(self, uri): self.disconnect() logger.info("Connecting to %s with parameters %s, %s, %s, %s", uri, self.security_mode, self.security_policy, self.certificate_path, self.private_key_path) self.client = Client(uri) if self.security_mode is not None and self.security_policy is not None: self.client.set_security( getattr(crypto.security_policies, 'SecurityPolicy' + self.security_policy), self.certificate_path, self.private_key_path, mode=getattr(ua.MessageSecurityMode, self.security_mode) ) self.client.connect() self._connected = True self.client.load_enums() self.client.load_type_definitions() self.client.load_data_type_definitions() self.save_security_settings(uri)
def start_server(self, endpoint): self._server = UAServer() self._server.endpoint = 48400 # enpoint not supported yet #self._server.endpoint = endpoint self._server.start() #self._server.set_server_name("OpcUa Modeler Server") time.sleep(0.2) self._client = Client(endpoint) self._client.connect() self.nodes = self._client.nodes self.get_node = self._client.get_node self.get_namespace_array = self._client.get_namespace_array # now remove freeopcua namespace, not necessary when modeling and # ensures correct idx for exported nodesets ns_node = self._client.get_node( ua.NodeId(ua.ObjectIds.Server_NamespaceArray)) nss = ns_node.read_value()
def datachange_notification(self, node, val, data): print("Python: New data change event", node, val) def event_notification(self, event): print("Python: New event", event) if __name__ == "__main__": #from IPython import embed logging.basicConfig(level=logging.DEBUG) #client = Client("opc.tcp://*****:*****@localhost:53530/OPCUA/SimulationServer/") #client.set_security_string("Basic256Sha256,SignAndEncrypt,certificate-example.der,private-key-example.pem") try: start_thread_loop() client = Client("opc.tcp://localhost:53530/OPCUA/SimulationServer/") client.connect() root = client.nodes.root print("Root is", root) print("childs of root are: ", root.get_children()) print("name of root is", root.read_browse_name()) objects = client.nodes.objects 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))
def client_no_tloop(server): with Client("opc.tcp://admin@localhost:8840/freeopcua/server") as c: yield c
def client(tloop, server): c = Client("opc.tcp://admin@localhost:8840/freeopcua/server", tloop=tloop) with c: yield c
from asyncua.sync import Client from asyncua import ua import asyncio import time # Get Temperature with Client("opc.tcp://169.254.1.70:4840/freeopcua/server/") as client: print( client.get_node( 'ns=4;s=MAIN.IOPT100.stat.arrAI[0].lrValueUser').get_value()) # Initialize the lamp with Client("opc.tcp://169.254.1.70:4840/freeopcua/server/") as client: print(client.get_node('ns=4;s=MAIN.Lamp001.stat.sState').get_value()) print(client.get_node('ns=4;s=MAIN.Lamp001.stat.sStatus').get_value()) print(client.get_node('ns=4;s=MAIN.Lamp001.stat.sSubstate').get_value()) parent = client.get_node('ns=4;s=MAIN.Lamp001') method = parent.get_child("4:RPC_Init") # Show the needed input arguments inputs = method.get_child("0:InputArguments").get_value() arguments = [] res = parent.call_method(method, *arguments) # Wait for 1 second time.sleep(1) # Show status print(client.get_node('ns=4;s=MAIN.Lamp001.stat.sState').get_value()) print(client.get_node('ns=4;s=MAIN.Lamp001.stat.sStatus').get_value()) print(client.get_node('ns=4;s=MAIN.Lamp001.stat.sSubstate').get_value()) # Enable the lamp with Client("opc.tcp://169.254.1.70:4840/freeopcua/server/") as client:
import sys sys.path.insert(0, "../..") from asyncua.sync import Client, start_thread_loop, stop_thread_loop if __name__ == "__main__": start_thread_loop() client = Client("opc.tcp://*****:*****@localhost:4840/freeopcua/server/") #connect using a user try: client.connect() # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects # Node objects have methods to read and write node attributes as well as browse or populate address space print("Children of root are: ", client.nodes.root.get_children()) # get a specific node knowing its node id #var = client.get_node(ua.NodeId(1002, 2)) #var = client.get_node("ns=3;i=2002") #print(var) #var.get_data_value() # get value of node as a DataValue object #var.get_value() # get value of node as a python builtin #var.set_value(ua.Variant([23], ua.VariantType.Int64)) #set node value using explicit data type #var.set_value(3.9) # set node value using implicit data type # Now getting a variable node using its browse path myvar = client.nodes.root.get_child( ["0:Objects", "2:MyObject", "2:MyVariable"]) obj = client.nodes.root.get_child(["0:Objects", "2:MyObject"])
thread if you need to do such a thing """ def datachange_notification(self, node, val, data): print("Python: New data change event", node, val) def event_notification(self, event): print("Python: New event", event) if __name__ == "__main__": logging.basicConfig(level=logging.WARN) #logger = logging.getLogger("KeepAlive") #logger.setLevel(logging.DEBUG) with ThreadLoop() as tloop: with Client("opc.tcp://*****:*****@localhost:4840/freeopcua/server/") #connect using a user client.load_type_definitions( ) # load definition of server specific structures/extension objects # Client has a few methods to get proxy to UA nodes that should always be in address space such as Root or Objects print("Objects node is: ", client.nodes.objects) # Node objects have methods to read and write node attributes as well as browse or populate address space print("Children of root are: ", client.nodes.root.get_children()) # get a specific node knowing its node id #var = client.get_node(ua.NodeId(1002, 2)) #var = client.get_node("ns=3;i=2002") #print(var) #var.read_data_value() # get value of node as a DataValue object
def client(tloop, server): c = Client("opc.tcp://localhost:8840/freeopcua/server") c.connect() yield c c.disconnect()