Skip to content

Commit

Permalink
Paho MQTT v2 Callbacks (#102)
Browse files Browse the repository at this point in the history
* Paho MQTT v2 Callbacks

* Notification about old paho version

---------

Co-authored-by: infeeeee <gyetpet@mailbox.org>
  • Loading branch information
richibrics and infeeeee committed Mar 5, 2024
1 parent 249d879 commit 8819964
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
19 changes: 13 additions & 6 deletions IoTuring/Protocols/MQTTClient/MQTTClient.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import sys

from IoTuring.Logger.LogObject import LogObject
from IoTuring.MyApp.App import App
import paho.mqtt.client as MqttClient
import paho.mqtt.publish as publish

try:
import paho.mqtt.enums as mqttEnums
except ModuleNotFoundError:
sys.exit("paho.mqtt.enums not found! Update paho-mqtt package to 2.0.0! Exiting...")


from IoTuring.Protocols.MQTTClient.TopicCallback import TopicCallback

Expand Down Expand Up @@ -42,7 +49,7 @@ def IsConnected(self):
return self.connected

def SetupClient(self) -> None:
self.client = MqttClient.Client(self.name)
self.client = MqttClient.Client(callback_api_version=mqttEnums.CallbackAPIVersion.VERSION2, client_id=self.name)

if self.username != "" and self.password != "":
self.client.username_pw_set(self.username, self.password)
Expand All @@ -61,15 +68,15 @@ def AsyncConnect(self) -> None:

# EVENTS

def Event_OnClientConnect(self, client, userdata, flags, rc) -> None:
if rc == 0: # Connections is OK
def Event_OnClientConnect(self, client, userdata, flags, reason_code, properties)-> None:
if reason_code==0: # Connections is OK
self.Log(self.LOG_INFO, "Connection established")
self.connected = True
self.SubscribeToAllTopics()
else:
self.Log(self.LOG_ERROR, "Connection error")
self.Log(self.LOG_ERROR, "Connection error: code " + str(reason_code))

def Event_OnClientDisconnect(self, client, userdata, rc) -> None:
def Event_OnClientDisconnect(self, client, userdata, flags, reason_code, properties)-> None:
self.Log(self.LOG_ERROR, "Connection lost")
self.connected = False

Expand Down
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ authors = [
{name = "infeeeee", email = "gyetpet@mailbox.org"}
]
maintainers = [
{name = "richibrics", email = "riccardo.briccola.dev@gmail.com"}
{name = "richibrics", email = "riccardo.briccola.dev@gmail.com"},
{name = "infeeeee", email = "gyetpet@mailbox.org"}
]
classifiers = [
"Development Status :: 3 - Alpha",
Expand All @@ -21,7 +22,7 @@ classifiers = [
]

dependencies = [
"paho-mqtt<2.0.0",
"paho-mqtt>=2.0.0",
"psutil",
"PyYAML",
"requests",
Expand Down

0 comments on commit 8819964

Please sign in to comment.