OPC-UA: better handling and reporting of server browser's connection errors. wxPython4
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Thu, 10 Nov 2022 19:31:59 +0100
branchwxPython4
changeset 3667 49e6b73de505
parent 3666 bf3b5f58c832
child 3668 142c268124ab
OPC-UA: better handling and reporting of server browser's connection errors.
opc_ua/opcua_client_maker.py
--- a/opc_ua/opcua_client_maker.py	Thu Nov 10 19:31:09 2022 +0100
+++ b/opc_ua/opcua_client_maker.py	Thu Nov 10 19:31:59 2022 +0100
@@ -288,28 +288,9 @@
     def OnConnectButton(self, event):
         if self.connect_button.GetValue():
             
-            self.tree_panel = wx.Panel(self)
-            self.tree_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
-            self.tree_sizer.AddGrowableCol(0)
-            self.tree_sizer.AddGrowableRow(0)
-
-            self.tree = gizmos.TreeListCtrl(self.tree_panel, -1, style=0, agwStyle=
-                                            gizmos.TR_DEFAULT_STYLE
-                                            | gizmos.TR_MULTIPLE
-                                            | gizmos.TR_FULL_ROW_HIGHLIGHT
-                                       )
-
-            prepare_image_list()
-            self.tree.SetImageList(il)
-
-            for idx,(colname, width) in enumerate(zip(treecolnames, treecolwidths)):
-                self.tree.AddColumn(colname)
-                self.tree.SetColumnWidth(idx, width)
-
-            self.tree.SetMainColumn(0)
-
             config = self.config_getter()
             self.client = Client(config["URI"])
+            self.log("OPCUA browser: connecting to {}\n".format(config["URI"]))
             
             AuthType = config["AuthType"]
             if AuthType=="UserPasword":
@@ -319,7 +300,34 @@
                 self.client.set_security_string(
                     "{Policy},{Mode},{Certificate},{PrivateKey}".format(**config))
 
-            self.client.connect()
+            try :
+                self.client.connect()
+            except Exception as e:
+                self.log("OPCUA browser: "+str(e)+"\n")
+                self.client = None
+                self.connect_button.SetValue(False)
+                return
+
+            self.tree_panel = wx.Panel(self)
+            self.tree_sizer = wx.FlexGridSizer(cols=1, hgap=0, rows=2, vgap=0)
+            self.tree_sizer.AddGrowableCol(0)
+            self.tree_sizer.AddGrowableRow(0)
+
+            self.tree = gizmos.TreeListCtrl(self.tree_panel, -1, style=0, agwStyle=
+                                            gizmos.TR_DEFAULT_STYLE
+                                            | gizmos.TR_MULTIPLE
+                                            | gizmos.TR_FULL_ROW_HIGHLIGHT
+                                       )
+
+            prepare_image_list()
+            self.tree.SetImageList(il)
+
+            for idx,(colname, width) in enumerate(zip(treecolnames, treecolwidths)):
+                self.tree.AddColumn(colname)
+                self.tree.SetColumnWidth(idx, width)
+
+            self.tree.SetMainColumn(0)
+
             self.client.load_type_definitions()  # load definition of server specific structures/extension objects
             rootnode = self.client.get_root_node()