diff --git a/code/drv_mqtt/requirements.txt b/code/drv_mqtt/requirements.txt index 75fddbe..bee5a1f 100644 --- a/code/drv_mqtt/requirements.txt +++ b/code/drv_mqtt/requirements.txt @@ -1,2 +1,2 @@ system_logger_tool>=0.0.3 -paho-mqtt>=1.5.0 \ No newline at end of file +paho-mqtt>=2.0.0 \ No newline at end of file diff --git a/code/drv_mqtt/src/wattrex_driver_mqtt/drv_mqtt.py b/code/drv_mqtt/src/wattrex_driver_mqtt/drv_mqtt.py index 3198047..cca1c2e 100644 --- a/code/drv_mqtt/src/wattrex_driver_mqtt/drv_mqtt.py +++ b/code/drv_mqtt/src/wattrex_driver_mqtt/drv_mqtt.py @@ -8,7 +8,7 @@ ####################### GENERIC IMPORTS ####################### ####################### THIRD PARTY IMPORTS ####################### -from paho.mqtt.client import Client, MQTTv311, MQTTMessage +from paho.mqtt.client import Client, MQTTv311, MQTTMessage, CallbackAPIVersion from paho.mqtt.properties import Properties from paho.mqtt.packettypes import PacketTypes @@ -46,7 +46,8 @@ class DrvMqttDriverC: def __init__(self, error_callback, cred_path : str) -> None: #Connection success callback cred = sys_conf_read_config_params(filename=cred_path, section='mqtt') - self.__client = Client(protocol=MQTTv311, transport="tcp", reconnect_on_failure=True) + self.__client = Client(callback_api_version= CallbackAPIVersion.VERSION2, + protocol=MQTTv311, transport="tcp", reconnect_on_failure=True) self.__client.username_pw_set(cred['user'], cred['password']) self.__client.enable_logger(log) @@ -60,7 +61,7 @@ def __init__(self, error_callback, cred_path : str) -> None: n_attempts = 0 self.__client.loop(timeout=0.5) while not self.__client.is_connected(): - self.__client.reconnect_delay_set(min_delay=0.5, max_delay=30) + self.__client.reconnect_delay_set(min_delay=1, max_delay=30) self.__client.loop(timeout=0.5) if n_attempts > 10: raise DrvMqttBrokerErrorC('Error connecting to mqtt broker') @@ -68,7 +69,7 @@ def __init__(self, error_callback, cred_path : str) -> None: self.__subs_topics = {} - def on_connect(self, client, userdata, flags, error_code): #pylint: disable=unused-argument + def on_connect(self, client, userdata, flags, error_code, properties): #pylint: disable=unused-argument """ Callback function for successful connection to the broker. """ diff --git a/code/drv_mqtt/tests/example.py b/code/drv_mqtt/tests/example.py index b5d2b7d..41f7624 100644 --- a/code/drv_mqtt/tests/example.py +++ b/code/drv_mqtt/tests/example.py @@ -11,13 +11,12 @@ ####################### THIRD PARTY IMPORTS ####################### ####################### SYSTEM ABSTRACTION IMPORTS ####################### -from system_logger_tool import sys_log_logger_get_module_logger -if __name__ == '__main__': - from system_logger_tool import SysLogLoggerC - cycler_logger = SysLogLoggerC() -log = sys_log_logger_get_module_logger(__name__) +from system_logger_tool import sys_log_logger_get_module_logger, SysLogLoggerC, Logger +cycler_logger = SysLogLoggerC(file_log_levels='code/log_config.yaml') +log: Logger = sys_log_logger_get_module_logger(__name__) -from ..src.wattrex_driver_mqtt.drv_mqtt import DrvMqttDriverC #pylint: disable= relative-beyond-top-level +sys.path.append(os.getcwd()+'/code/drv_mqtt/') +from src.wattrex_driver_mqtt.drv_mqtt import DrvMqttDriverC #pylint: disable= relative-beyond-top-level #### Example for bfr #### def error(data): @@ -48,7 +47,7 @@ def main(): """This function is called when the driver is started. It is called when the event loop is started . """ - driver = DrvMqttDriverC(error) + driver = DrvMqttDriverC(error, "code/creds.yaml") try: pwr_ref = 'ctrl/pwr_ref' mode_topic = 'ctrl/mode'