From 318609b7308c65ec929f62dc0411689971ac6892 Mon Sep 17 00:00:00 2001
From: su530201 <olivier.kaufmann@umons.ac.be>
Date: Tue, 10 Oct 2023 23:47:59 +0200
Subject: [PATCH] Improve code consistency

---
 .../abstract_hardware_components.py           |  2 +-
 ohmpi/hardware_components/mb_2023_0_X.py      |  7 +--
 ohmpi/hardware_components/mb_2024_0_2.py      |  2 +
 ohmpi/hardware_components/mux_2023_0_X.py     |  7 ++-
 ohmpi/hardware_components/mux_2024_0_X.py     |  5 +-
 ohmpi/hardware_components/pwr_dps5005.py      | 50 +++++++++----------
 6 files changed, 41 insertions(+), 32 deletions(-)

diff --git a/ohmpi/hardware_components/abstract_hardware_components.py b/ohmpi/hardware_components/abstract_hardware_components.py
index a9c3e212..f2e61dbb 100644
--- a/ohmpi/hardware_components/abstract_hardware_components.py
+++ b/ohmpi/hardware_components/abstract_hardware_components.py
@@ -48,7 +48,7 @@ class PwrAbstract(ABC):
             self.soh_logger = create_stdout_logger('soh_mux')
         self.voltage_adjustable = kwargs.pop('voltage_adjustable', False)
         self._voltage = np.nan
-        self._current_adjustable = kwargs.pop('current_adjustable', False)
+        self.current_adjustable = kwargs.pop('current_adjustable', False)
         self._current = np.nan
         self._state = 'off'
         self._current_min = kwargs.pop('current_min', 0.)
diff --git a/ohmpi/hardware_components/mb_2023_0_X.py b/ohmpi/hardware_components/mb_2023_0_X.py
index c985301a..dd0cc6c0 100644
--- a/ohmpi/hardware_components/mb_2023_0_X.py
+++ b/ohmpi/hardware_components/mb_2023_0_X.py
@@ -66,9 +66,10 @@ def _ads_1115_gain_auto(channel):  # Make it a class method ?
 
 class Tx(TxAbstract):
     def __init__(self, **kwargs):
-        if kwargs['model'] == os.path.basename(__file__).rstrip('.py'):
+        if 'model' not in kwargs.keys():
             for key in SPECS['tx'].keys():
                 kwargs = enforce_specs(kwargs, SPECS['tx'], key)
+            self.exec_logger.event(f'{self.model}\ttx_init\tstart\t{datetime.datetime.utcnow()}')
             subclass_init = False
         else:
             subclass_init = True
@@ -211,13 +212,13 @@ class Tx(TxAbstract):
 
 class Rx(RxAbstract):
     def __init__(self, **kwargs):
-        if kwargs['model'] == os.path.basename(__file__).rstrip('.py'):
+        if 'model' not in kwargs.keys():
             for key in SPECS['rx'].keys():
                 kwargs = enforce_specs(kwargs, SPECS['rx'], key)
+            self.exec_logger.event(f'{self.model}\trx_init\tstart\t{datetime.datetime.utcnow()}')
             subclass_init = False
         else:
             subclass_init = True
-        kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
         super().__init__(**kwargs)
         assert isinstance(self.connection, I2C)
 
diff --git a/ohmpi/hardware_components/mb_2024_0_2.py b/ohmpi/hardware_components/mb_2024_0_2.py
index a4b65db3..a7560390 100644
--- a/ohmpi/hardware_components/mb_2024_0_2.py
+++ b/ohmpi/hardware_components/mb_2024_0_2.py
@@ -69,6 +69,7 @@ class Tx(Tx_mb_2023):
         if 'model' not in kwargs.keys():
             for key in SPECS['tx'].keys():
                 kwargs = enforce_specs(kwargs, SPECS['tx'], key)
+            self.exec_logger.event(f'{self.model}\ttx_init\tstart\t{datetime.datetime.utcnow()}')
             subclass_init = False
         else:
             subclass_init = True
@@ -96,6 +97,7 @@ class Rx(Rx_mb_2023):
         if 'model' not in kwargs.keys():
             for key in SPECS['rx'].keys():
                 kwargs = enforce_specs(kwargs, SPECS['rx'], key)
+            self.exec_logger.event(f'{self.model}\trx_init\tstart\t{datetime.datetime.utcnow()}')
             subclass_init = False
         else:
             subclass_init = True
diff --git a/ohmpi/hardware_components/mux_2023_0_X.py b/ohmpi/hardware_components/mux_2023_0_X.py
index 7e380ee8..9dc808f1 100644
--- a/ohmpi/hardware_components/mux_2023_0_X.py
+++ b/ohmpi/hardware_components/mux_2023_0_X.py
@@ -1,5 +1,6 @@
 import os
 import numpy as np
+import datetime
 from ohmpi.hardware_components import MuxAbstract
 import adafruit_tca9548a  # noqa
 from adafruit_mcp230xx.mcp23017 import MCP23017  # noqa
@@ -9,6 +10,7 @@ from ohmpi.utils import enforce_specs
 
 # hardware characteristics and limitations
 SPECS = {'model': {'default': os.path.basename(__file__).rstrip('.py')},
+         'id': {'default': 'mux_??'},
          'voltage_max': {'default': 50.},
          'current_max': {'default': 3.},
          'activation_delay': {'default': 0.01},
@@ -57,9 +59,10 @@ inner_cabling = {'1_role': {(1, 'X'): {'MCP': 0, 'MCP_GPIO': 0}, (2, 'X'): {'MCP
 
 class Mux(MuxAbstract):
     def __init__(self, **kwargs):
-        if kwargs['model'] == os.path.basename(__file__).rstrip('.py'):
+        if 'model' not in kwargs.keys():
             for key in SPECS.keys():
                 kwargs = enforce_specs(kwargs, SPECS, key)
+            self.exec_logger.event(f'{self.model}{self.board_id}\tmux_init\tstart\t{datetime.datetime.utcnow()}')
             subclass_init = False
         else:
             subclass_init = True
@@ -82,6 +85,8 @@ class Mux(MuxAbstract):
         if self.addresses is None:
             self._get_addresses()
         self.exec_logger.debug(f'{self.board_id} addresses: {self.addresses}')
+        if not subclass_init:
+            self.exec_logger.event(f'{self.model}_{self.board_id}\tmux_init\tend\t{datetime.datetime.utcnow()}')
 
     def _get_addresses(self):
         """ Converts inner cabling addressing into (electrodes, role) addressing """
diff --git a/ohmpi/hardware_components/mux_2024_0_X.py b/ohmpi/hardware_components/mux_2024_0_X.py
index db82f198..2172272b 100644
--- a/ohmpi/hardware_components/mux_2024_0_X.py
+++ b/ohmpi/hardware_components/mux_2024_0_X.py
@@ -10,7 +10,7 @@ from ohmpi.utils import enforce_specs
 
 # hardware characteristics and limitations
 SPECS = {'model': {'default': os.path.basename(__file__).rstrip('.py')},
-         'id' : {'default': 'mux_??'},
+         'id': {'default': 'mux_??'},
          'voltage_max': {'default': 50.},
          'current_max': {'default': 3.},
          'activation_delay': {'default': 0.01},
@@ -58,9 +58,10 @@ inner_cabling = {'4_roles': {(1, 'X'): {'MCP': 0, 'MCP_GPIO': 0}, (1, 'Y'): {'MC
 
 class Mux(MuxAbstract):
     def __init__(self, **kwargs):
-        if kwargs['model'] == os.path.basename(__file__).rstrip('.py'):
+        if 'model' not in kwargs.keys():
             for key in SPECS.keys():
                 kwargs = enforce_specs(kwargs, SPECS, key)
+            self.exec_logger.event(f'{self.model}{self.board_id}\tmux_init\tstart\t{datetime.datetime.utcnow()}')
             subclass_init = False
         else:
             subclass_init = True
diff --git a/ohmpi/hardware_components/pwr_dps5005.py b/ohmpi/hardware_components/pwr_dps5005.py
index 163456d9..50a92e43 100644
--- a/ohmpi/hardware_components/pwr_dps5005.py
+++ b/ohmpi/hardware_components/pwr_dps5005.py
@@ -1,38 +1,39 @@
 from ohmpi.hardware_components.abstract_hardware_components import PwrAbstract
-from ohmpi.config import HARDWARE_CONFIG
-import importlib
+import datetime
 import numpy as np
-import minimalmodbus  # noqa
+#import minimalmodbus  # noqa
 import os
+from ohmpi.utils import enforce_specs
 
-CTL_CONFIG = HARDWARE_CONFIG['ctl']
-ctl_name = HARDWARE_CONFIG['ctl'].pop('board_name', 'raspberry_pi')
-ctl_connection = HARDWARE_CONFIG['ctl'].pop('connection', 'modbus')
-ctl_module = importlib.import_module(f'ohmpi.hardware_components.{ctl_name}')
-CTL_CONFIG['baudrate'] = CTL_CONFIG.pop('baudrate', 9600)
-CTL_CONFIG['bitesize'] = CTL_CONFIG.pop('bitesize', 8)
-CTL_CONFIG['timeout'] = CTL_CONFIG.pop('timeout', 1)
-CTL_CONFIG['debug'] = CTL_CONFIG.pop('debug', False)
-CTL_CONFIG['parity'] = CTL_CONFIG.pop('parity', 'N')
-CTL_CONFIG['mode'] = CTL_CONFIG.pop('mode', minimalmodbus.MODE_RTU)
-CTL_CONFIG['port'] = CTL_CONFIG.pop('port', '/dev/ttyUSB0')
-CTL_CONFIG['slave_address'] = CTL_CONFIG.pop('slave_address', 1)
+# hardware characteristics and limitations
+SPECS = {'model': {'default': os.path.basename(__file__).rstrip('.py')},
+         'voltage': {'default': 12., 'max': 50., 'min': 0.},
+         'current_max': {'default': 100.},
+         }
 
 
 class Pwr(PwrAbstract):
     def __init__(self, **kwargs):
-        kwargs.update({'board_name': os.path.basename(__file__).rstrip('.py')})
-        voltage = kwargs.pop('voltage', 12.)
+        if 'model' not in kwargs.keys():
+            for key in SPECS.keys():
+                kwargs = enforce_specs(kwargs, SPECS, key)
+            subclass_init = False
+        else:
+            subclass_init = True
+        if not subclass_init:
+            self.exec_logger.event(f'{self.model}\tpwr_init\tstart\t{datetime.datetime.utcnow()}')
         super().__init__(**kwargs)
         # if a controller is passed in kwargs, it will be instantiated
-        if self.ctl is None:
-            self.ctl = ctl_module.Ctl(**CTL_CONFIG)
-        self.connection = self.ctl.interfaces[kwargs.pop('connection', ctl_connection)]
+        #if self.ctl is None:
+        #    self.ctl = ctl_module.Ctl(**CTL_CONFIG)
+        #self.connection = self.ctl.interfaces[kwargs.pop('connection', ctl_connection)]
+        self._voltage = kwargs['voltage']
+        self._current_max = kwargs['current_max']
         self.voltage_adjustable = True
-        self._voltage = voltage
-        self._current_adjustable = False
+        self.current_adjustable = False
         self._current = np.nan
-        self._current_max = kwargs.pop('current_max', 100.)
+        if not subclass_init:
+            self.exec_logger.event(f'{self.model}\tpwr_init\tend\t{datetime.datetime.utcnow()}')
 
     @property
     def current(self):
@@ -61,6 +62,5 @@ class Pwr(PwrAbstract):
     def battery_voltage(self):
         self.connection.read_register(0x05, 2)
 
-    @property
-    def current_max(self,value):
+    def current_max(self, value):
         self.connection.write_register(0x0001, value * 10, 0)
-- 
GitLab