Cosmetic changes
authorEdouard Tisserant <edouard.tisserant@gmail.com>
Mon, 18 Nov 2024 13:55:51 +0100
changeset 4040 ea2db66b185f
parent 4039 9ff455817691
child 4041 af7671a9945a
Cosmetic changes
connectors/ERPC/__init__.py
runtime/ServicePublisher.py
--- a/connectors/ERPC/__init__.py	Mon Nov 18 13:52:40 2024 +0100
+++ b/connectors/ERPC/__init__.py	Mon Nov 18 13:55:51 2024 +0100
@@ -73,6 +73,36 @@
             for idx, force in orders],)
 }
 
+def rpc_wrapper(method_name, confnodesroot):
+    client_method = getattr(BeremizPLCObjectServiceClient, method_name)
+    return_wrapper = ReturnWrappers.get(
+        method_name, 
+        lambda client_method, obj, args_wrapper, *args: client_method(obj, *args_wrapper(*args)))
+    args_wrapper = ArgsWrappers.get(method_name, lambda *x:x)
+
+    def exception_wrapper(self, *args):
+        try:
+            return return_wrapper(client_method, self, args_wrapper, *args)
+        except erpc.transport.ConnectionClosed as e:
+            confnodesroot._SetConnector(None)
+            confnodesroot.logger.write_error(_("Connection lost!\n"))
+        except erpc.codec.CodecError as e:
+            confnodesroot.logger.write_warning(_("ERPC codec error: %s\n") % e)
+        except erpc.client.RequestError as e:
+            confnodesroot.logger.write_error(_("ERPC request error: %s\n") % e)                
+        except MissingCallException as e:
+            confnodesroot.logger.write_warning(_("Remote call not supported: %s\n") % e)
+        except Exception as e:
+            errmess = _("Exception calling remote PLC object fucntion %s:\n") % method_name \
+                        + traceback.format_exc()
+            confnodesroot.logger.write_error(errmess + "\n")
+            confnodesroot._SetConnector(None)
+
+        return self.PLCObjDefaults.get(method_name)
+    return exception_wrapper
+
+
+
 def ERPC_connector_factory(uri, confnodesroot):
     """
     returns the ERPC connector
@@ -108,39 +138,11 @@
             'Malformed URI "%s": %s\n' % (uri, str(e)))
         return None
 
-    def rpc_wrapper(method_name):
-        client_method = getattr(BeremizPLCObjectServiceClient, method_name)
-        return_wrapper = ReturnWrappers.get(
-            method_name, 
-            lambda client_method, obj, args_wrapper, *args: client_method(obj, *args_wrapper(*args)))
-        args_wrapper = ArgsWrappers.get(method_name, lambda *x:x)
-
-        def exception_wrapper(self, *args):
-            try:
-                return return_wrapper(client_method, self, args_wrapper, *args)
-            except erpc.transport.ConnectionClosed as e:
-                confnodesroot._SetConnector(None)
-                confnodesroot.logger.write_error(_("Connection lost!\n"))
-            except erpc.codec.CodecError as e:
-                confnodesroot.logger.write_warning(_("ERPC codec error: %s\n") % e)
-            except erpc.client.RequestError as e:
-                confnodesroot.logger.write_error(_("ERPC request error: %s\n") % e)                
-            except MissingCallException as e:
-                confnodesroot.logger.write_warning(_("Remote call not supported: %s\n") % e)
-            except Exception as e:
-                errmess = _("Exception calling remote PLC object fucntion %s:\n") % method_name \
-                          + traceback.format_exc()
-                confnodesroot.logger.write_error(errmess + "\n")
-                confnodesroot._SetConnector(None)
-
-            return self.PLCObjDefaults.get(method_name)
-        return exception_wrapper
-
 
     PLCObjectERPCProxy = type(
         "PLCObjectERPCProxy",
         (ConnectorBase, BeremizPLCObjectServiceClient),
-        {name: rpc_wrapper(name)
+        {name: rpc_wrapper(name, confnodesroot)
             for name,_func in getmembers(IBeremizPLCObjectService, isfunction)})
 
     try:
--- a/runtime/ServicePublisher.py	Mon Nov 18 13:52:40 2024 +0100
+++ b/runtime/ServicePublisher.py	Mon Nov 18 13:55:51 2024 +0100
@@ -71,12 +71,14 @@
         print("MDNS brodcasted service address :" + ip)
         self.ip_32b = socket.inet_aton(ip)
 
-        self.server.register_service(
-            zeroconf.ServiceInfo(service_type,
-                                 self.service_name,
-                                 self.port,
-                                 addresses=[self.ip_32b],
-                                 properties=self.serviceproperties))
+        self.service_info = zeroconf.ServiceInfo(
+            service_type,
+            self.service_name,
+            self.port,
+            addresses=[self.ip_32b],
+            properties=self.serviceproperties)
+
+        self.server.register_service(self.service_info)
         self.retrytimer = None
 
     def UnRegisterService(self):
@@ -84,12 +86,7 @@
             self.retrytimer.cancel()
 
         if self.server is not None:
-            self.server.unregister_service(
-                zeroconf.ServiceInfo(service_type,
-                                     self.service_name,
-                                     self.ip_32b,
-                                     self.port,
-                                     properties=self.serviceproperties))
+            self.server.unregister_service(self.service_info)
             self.server.close()
             self.server = None