OPC-UA client: add ApplicationUri parameter in case it differs from server's URI wxPython4
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Tue, 25 Oct 2022 18:51:45 +0200
branchwxPython4
changeset 3643 c6068b674b30
parent 3642 cd3d15e8ef42
child 3652 b5c6bb72bfc9
OPC-UA client: add ApplicationUri parameter in case it differs from server's URI
opc_ua/client.py
opc_ua/opcua_client_maker.py
--- a/opc_ua/client.py	Fri Oct 21 18:33:06 2022 +0200
+++ b/opc_ua/client.py	Tue Oct 25 18:51:45 2022 +0200
@@ -67,6 +67,7 @@
                       </xsd:sequence>
                       <xsd:attribute name="Certificate" type="xsd:string" use="optional" default="certificate.pem"/>
                       <xsd:attribute name="PrivateKey" type="xsd:string" use="optional" default="private_key.pem"/>
+                      <xsd:attribute name="ApplicationUri" type="xsd:string" use="optional"/>
                     </xsd:complexType>
                   </xsd:element>
                   <xsd:element name="UserPassword">
@@ -114,6 +115,9 @@
                 # cryptomaterial is expected to be in project's user provide file directory
                 if name in ["Certificate","PrivateKey"]:
                     value = os.path.join(self.GetCTRoot()._getProjectFilesPath(), value)
+                # ApplicationUri defaults to URI if not set
+                if name == "ApplicationUri" and not value:
+                        value = res["URI"]
                 res[name] = value
 
         return res
--- a/opc_ua/opcua_client_maker.py	Fri Oct 21 18:33:06 2022 +0200
+++ b/opc_ua/opcua_client_maker.py	Tue Oct 25 18:51:45 2022 +0200
@@ -43,7 +43,8 @@
         ("Certificate", "certificate.der"),
         ("PrivateKey", "private_key.pem"),
         ("Policy", "Basic256Sha256"),
-        ("Mode", "SignAndEncrypt")],
+        ("Mode", "SignAndEncrypt"),
+        ("ApplicationUri", "")],
     "UserPassword":[
         ("User", None),
         ("Password", None)]}
@@ -318,6 +319,7 @@
             elif AuthType=="x509":
                 self.client.set_security_string(
                     "{Policy},{Mode},{Certificate},{PrivateKey}".format(**config))
+                self.client.application_uri = config["ApplicationUri"]
 
             self.client.connect()
             self.client.load_type_definitions()  # load definition of server specific structures/extension objects
@@ -572,13 +574,15 @@
     retval = UA_Client_connect(client, uri);
 
 /* Note : Policy is ignored here since open62541 client supports all policies by default */
-#define INIT_x509(Policy, UpperCaseMode, PrivateKey, Certificate)                                  \\
+#define INIT_x509(Policy, UpperCaseMode, PrivateKey, Certificate, ApplicationUri)                  \\
     LogInfo("OPC-UA Init x509 %s,%s,%s,%s", #Policy, #UpperCaseMode, PrivateKey, Certificate);     \\
                                                                                                    \\
     UA_ByteString certificate = loadFile(Certificate);                                             \\
     UA_ByteString privateKey  = loadFile(PrivateKey);                                              \\
                                                                                                    \\
     cc->securityMode = UA_MESSAGESECURITYMODE_##UpperCaseMode;                                     \\
+    UA_String_clear(&cc->clientDescription.applicationUri);                                        \\
+    cc->clientDescription.applicationUri = UA_STRING_ALLOC(ApplicationUri);                        \\
     UA_ClientConfig_setDefaultEncryption(cc, certificate, privateKey, NULL, 0, NULL, 0);           \\
                                                                                                    \\
     retval = UA_Client_connect(client, uri);                                                       \\
@@ -652,8 +656,10 @@
         AuthType = config["AuthType"]
         if AuthType == "x509":
             config["UpperCaseMode"] = config["Mode"].upper()
+            if not config["ApplicationUri"]:
+                config["ApplicationUri"] = config["URI"]
             formatdict["init"] += """
-    INIT_x509({Policy}, {UpperCaseMode}, "{PrivateKey}", "{Certificate}")""".format(**config)
+    INIT_x509({Policy}, {UpperCaseMode}, "{PrivateKey}", "{Certificate}", "{ApplicationUri}")""".format(**config)
         elif AuthType == "UserPassword":
             formatdict["init"] += """
     INIT_UserPassword("{User}", "{Password}")""".format(**config)