Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for MQTT Client ID #25

Merged
merged 1 commit into from
May 31, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 16 additions & 1 deletion GUI/Broker.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from PyQt5.QtCore import QSettings, QDir
from PyQt5.QtWidgets import QDialog, QLineEdit, QFormLayout, QPushButton, QGroupBox, QCheckBox
import random
import string

from GUI import SpinBox, HLayout, VLayout

Expand Down Expand Up @@ -33,6 +35,13 @@ def __init__(self, *args, **kwargs):
lfl.addRow("Password", self.password)
gbLogin.setLayout(lfl)

gbClientId = QGroupBox("Client ID [optional]")
cfl = QFormLayout()
self.clientId = QLineEdit()
self.clientId.setText(self.settings.value("client_id", "tdm-" + self.random_generator()))
cfl.addRow("Client ID", self.clientId)
gbClientId.setLayout(cfl)

self.cbConnectStartup = QCheckBox("Connect on startup")
self.cbConnectStartup.setChecked(self.settings.value("connect_on_startup", False, bool))

Expand All @@ -42,7 +51,7 @@ def __init__(self, *args, **kwargs):
hlBtn.addWidgets([btnSave, btnCancel])

vl = VLayout()
vl.addWidgets([gbHost, gbLogin, self.cbConnectStartup])
vl.addWidgets([gbHost, gbLogin, gbClientId, self.cbConnectStartup])
vl.addLayout(hlBtn)

self.setLayout(vl)
Expand All @@ -56,5 +65,11 @@ def accept(self):
self.settings.setValue("username", self.username.text())
self.settings.setValue("password", self.password.text())
self.settings.setValue("connect_on_startup", self.cbConnectStartup.isChecked())
self.settings.setValue("client_id", self.clientId.text())
self.settings.sync()
self.done(QDialog.Accepted)

##################################################################
# utils
def random_generator(self, size=6, chars=string.ascii_uppercase + string.digits):
return ''.join(random.choice(chars) for x in range(size))
51 changes: 42 additions & 9 deletions Util/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,19 @@ class MqttClient(QtCore.QObject):
keepAliveChanged = QtCore.pyqtSignal(int)
cleanSessionChanged = QtCore.pyqtSignal(bool)
protocolVersionChanged = QtCore.pyqtSignal(int)
usernameChanged = QtCore.pyqtSignal(str)
passwordChanged = QtCore.pyqtSignal(str)
clientIdChanged = QtCore.pyqtSignal(str)

messageSignal = QtCore.pyqtSignal(str, str)

def __init__(self, parent=None):
super(MqttClient, self).__init__(parent)

self.m_hostname = ""
self.m_clientId = ""
self.m_username = ""
self.m_password = ""
self.m_port = 1883
self.ssl = False
self.m_keepAlive = 60
Expand All @@ -36,13 +42,6 @@ def __init__(self, parent=None):

self.m_state = MqttClient.Disconnected

self.m_client = mqtt.Client(clean_session=self.m_cleanSession,
protocol=self.protocolVersion)

self.m_client.on_connect = self.on_connect
self.m_client.on_message = self.on_message
self.m_client.on_disconnect = self.on_disconnect


@QtCore.pyqtProperty(int, notify=stateChanged)
def state(self):
Expand Down Expand Up @@ -74,8 +73,35 @@ def port(self, port):
self.m_port = port
self.portChanged.emit(port)

def setAuth(self, username, password):
self.m_client.username_pw_set(username, password)
@QtCore.pyqtProperty(int, notify=usernameChanged)
def username(self):
return self.m_username

@username.setter
def username(self, username):
if self.m_username == username: return
self.m_username = username
self.usernameChanged.emit(username)

@QtCore.pyqtProperty(int, notify=passwordChanged)
def password(self):
return self.m_password

@password.setter
def password(self, password):
if self.m_password == password: return
self.m_password = password
self.passwordChanged.emit(password)

@QtCore.pyqtProperty(int, notify=clientIdChanged)
def clientId(self):
return self.m_clientId

@clientId.setter
def clientId(self, clientId):
if self.m_clientId == clientId: return
self.m_clientId = clientId
self.clientIdChanged.emit(clientId)

@QtCore.pyqtProperty(int, notify=keepAliveChanged)
def keepAlive(self):
Expand Down Expand Up @@ -111,6 +137,13 @@ def protocolVersion(self, protocolVersion):
#################################################################
@QtCore.pyqtSlot()
def connectToHost(self):
self.m_client = mqtt.Client(client_id=self.m_clientId, clean_session=self.m_cleanSession,
protocol=self.protocolVersion)
self.m_client.username_pw_set(self.m_username, self.m_password)

self.m_client.on_connect = self.on_connect
self.m_client.on_message = self.on_message
self.m_client.on_disconnect = self.on_disconnect
if self.m_hostname:
self.connecting.emit()
try:
Expand Down
26 changes: 12 additions & 14 deletions tdm.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,16 +189,7 @@ def toggle_autoupdate(self, state):

def toggle_connect(self, state):
if state and self.mqtt.state == self.mqtt.Disconnected:
self.broker_hostname = self.settings.value('hostname', 'localhost')
self.broker_port = self.settings.value('port', 1883, int)
self.broker_username = self.settings.value('username')
self.broker_password = self.settings.value('password')

self.mqtt.hostname = self.broker_hostname
self.mqtt.port = self.broker_port

if self.broker_username:
self.mqtt.setAuth(self.broker_username, self.broker_password)
self.mqtt_connect_perform()
self.mqtt.connectToHost()
elif not state and self.mqtt.state == self.mqtt.Connected:
self.mqtt_disconnect()
Expand All @@ -211,19 +202,26 @@ def autoupdate(self):
self.mqtt.publish(cmnd+"STATUS", payload=8)

def mqtt_connect(self):
self.mqtt_connect_perform()
if self.mqtt.state == self.mqtt.Disconnected:
self.mqtt.connectToHost()

def mqtt_connect_perform(self):
self.broker_hostname = self.settings.value('hostname', 'localhost')
self.broker_port = self.settings.value('port', 1883, int)
self.broker_username = self.settings.value('username')
self.broker_password = self.settings.value('password')
self.broker_clientId = self.settings.value('client_id')

self.mqtt.hostname = self.broker_hostname
self.mqtt.port = self.broker_port

if self.broker_username:
self.mqtt.setAuth(self.broker_username, self.broker_password)
self.mqtt.username = self.broker_username
self.mqtt.password = self.broker_password
if self.broker_clientId:
self.mqtt.clientId = self.broker_clientId

if self.mqtt.state == self.mqtt.Disconnected:
self.mqtt.connectToHost()

def mqtt_disconnect(self):
self.mqtt.disconnectFromHost()
Expand Down Expand Up @@ -612,4 +610,4 @@ def closeEvent(self, e):
MW = MainWindow()
MW.show()

sys.exit(app.exec_())
sys.exit(app.exec_())