Skip to content

Commit

Permalink
Changes towards pahoV2
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertoAldea committed Feb 15, 2024
1 parent 23440d4 commit 922c3d1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 12 deletions.
2 changes: 1 addition & 1 deletion code/drv_mqtt/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
system_logger_tool>=0.0.3
paho-mqtt>=1.5.0
paho-mqtt>=2.0.0
9 changes: 5 additions & 4 deletions code/drv_mqtt/src/wattrex_driver_mqtt/drv_mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)

Expand All @@ -60,15 +61,15 @@ 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')
n_attempts += 1

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.
"""
Expand Down
13 changes: 6 additions & 7 deletions code/drv_mqtt/tests/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit 922c3d1

Please sign in to comment.