diff --git a/config.py b/config.py index f0267d83f50872e7df020ddddc91888e807fa410..d64119207cffdcfcccda18ba2d6c3f6b05857c1a 100644 --- a/config.py +++ b/config.py @@ -1,9 +1,13 @@ import logging +from utils import get_platform from paho.mqtt.client import MQTTv31 +_, on_pi = get_platform() ohmpi_id = '0001' -mqtt_broker = 'localhost' +# mqtt_broker = 'localhost' +mqtt_broker = 'localhost' if on_pi else 'mg3d-dev.umons.ac.be' + logging_suffix = '' # OhmPi configuration OHMPI_CONFIG = { @@ -25,6 +29,7 @@ OHMPI_CONFIG = { # Execution logging configuration EXEC_LOGGING_CONFIG = { 'logging_level': logging.INFO, + 'log_file_logging_level' : logging.DEBUG, 'logging_to_console': True, 'file_name': f'exec{logging_suffix}.log', 'max_bytes': 262144, diff --git a/logging_setup.py b/logging_setup.py index 4c8162a4f6aeb6e578fd38d0fa811549d9e764df..e0ced5e8f2d1ba14a9c6e89be15f8cd602187eb9 100644 --- a/logging_setup.py +++ b/logging_setup.py @@ -42,7 +42,7 @@ def setup_loggers(mqtt=True): exec_formatter.datefmt = '%Y-%m-%d %H:%M:%S UTC' exec_handler.setFormatter(exec_formatter) exec_logger.addHandler(exec_handler) - exec_logger.setLevel(EXEC_LOGGING_CONFIG['logging_level']) + exec_logger.setLevel(EXEC_LOGGING_CONFIG['log_file_logging_level']) if logging_to_console: console_exec_handler = logging.StreamHandler(sys.stdout) diff --git a/ohmpi.py b/ohmpi.py index ff36d078efe9a467b1246e6a41c8b47d2cd3ee0e..367c97f3c2d0cb50a759b1ab5d67373b8c34fe05 100644 --- a/ohmpi.py +++ b/ohmpi.py @@ -8,7 +8,7 @@ Olivier KAUFMANN (UMONS), Arnaud WATELET (UMONS) and Guillaume BLANCHY (FNRS/ULi """ import os -import io +from utils import get_platform import json import warnings from copy import deepcopy @@ -64,13 +64,14 @@ class OhmPi(object): mqtt: bool, defaut: True if True publish on mqtt topics while logging, otherwise use other loggers only on_pi: bool,None default: None - if None, the platform on which the class is instanciated is determined to set on_pi to either True or False + if None, the platform on which the class is instantiated is determined to set on_pi to either True or False. + if False the behaviour of an ohmpi will be partially emulated and return random data. idps: if true uses the DPS """ if on_pi is None: - _, on_pi = OhmPi._get_platform() + _, on_pi = get_platform() self._sequence = sequence self.use_mux = use_mux @@ -162,7 +163,7 @@ class OhmPi(object): else: self.exec_logger.warning(f'Failed to connect to control broker. Return code : {rc}') - client = mqtt_client.Client("ohmpi_{OHMPI_CONFIG['id']}_listener", clean_session=False) + client = mqtt_client.Client(f"ohmpi_{OHMPI_CONFIG['id']}_listener", clean_session=False) client.username_pw_set(MQTT_CONTROL_CONFIG['auth'].get('username'), MQTT_CONTROL_CONFIG['auth']['password']) client.on_connect = on_connect @@ -197,8 +198,8 @@ class OhmPi(object): if dic['cmd_id'] != self.cmd_id: self.cmd_id = dic['cmd_id'] self.exec_logger.debug(f'Received command {command}') - payload = json.dumps({'cmd_id': dic['cmd_id'], 'reply': 'ok'}) - publish.single(payload=payload, **publisher_config) + # payload = json.dumps({'cmd_id': dic['cmd_id'], 'reply': 'ok'}) + # publish.single(payload=payload, **publisher_config) self._process_commands(command) self.controller.on_message = on_message @@ -301,26 +302,6 @@ class OhmPi(object): return output - @staticmethod - def _get_platform(): - """Gets platform name and checks if it is a raspberry pi - - Returns - ------- - str, bool - name of the platform on which the code is running, boolean that is true if the platform is a raspberry pi""" - - platform = 'unknown' - on_pi = False - try: - with io.open('/sys/firmware/devicetree/base/model', 'r') as f: - platform = f.read().lower() - if 'raspberry pi' in platform: - on_pi = True - except FileNotFoundError: - pass - return platform, on_pi - def read_quad(self, filename): warnings.warn('This function is deprecated. Use load_sequence instead.', DeprecationWarning) self.load_sequence(filename) @@ -1028,13 +1009,13 @@ class OhmPi(object): print(f'decoded message: {decoded_message}') cmd_id = decoded_message.pop('cmd_id', None) cmd = decoded_message.pop('cmd', None) - args = decoded_message.pop('args', '[]') + args = decoded_message.pop('args', '') if len(args) == 0: args = f'["{args}"]' args = json.loads(args) - kwargs = decoded_message.pop('kwargs', '{}') + kwargs = decoded_message.pop('kwargs', '') if len(kwargs) == 0: - kwargs= '{}' + kwargs = '"{}"' kwargs = json.loads(kwargs) self.exec_logger.debug(f'Calling method {cmd}({args}, {kwargs})') status = False @@ -1222,7 +1203,8 @@ print(colored(r' ________________________________' + '\n' + r'\ \_/ / | | || | | || | _| |_' + '\n' + r' \___/\_| |_/\_| |_/\_| \___/ ', 'red')) print('Version:', VERSION) -platform, on_pi = OhmPi._get_platform() # noqa +platform, on_pi = get_platform() + if on_pi: print(colored(f'\u2611 Running on {platform} platform', 'green')) # TODO: check model for compatible platforms (exclude Raspberry Pi versions that are not supported...) diff --git a/utils.py b/utils.py new file mode 100644 index 0000000000000000000000000000000000000000..3e7bc184cbaf314231120ba9e627f8a6bcde2e24 --- /dev/null +++ b/utils.py @@ -0,0 +1,21 @@ +import io + + +def get_platform(): + """Gets platform name and checks if it is a raspberry pi + + Returns + ------- + str, bool + name of the platform on which the code is running, boolean that is true if the platform is a raspberry pi""" + + platform = 'unknown' + on_pi = False + try: + with io.open('/sys/firmware/devicetree/base/model', 'r') as f: + platform = f.read().lower() + if 'raspberry pi' in platform: + on_pi = True + except FileNotFoundError: + pass + return platform, on_pi \ No newline at end of file