MqttMonitor/MqttMonitorWidget.py

changeset 10
7e0e921dc7ea
parent 8
95b56dcfa09b
child 11
90d3ebed4cc0
equal deleted inserted replaced
9:f75a385e9127 10:7e0e921dc7ea
17 import os 17 import os
18 import collections 18 import collections
19 19
20 from PyQt5.QtCore import pyqtSlot, QTimer 20 from PyQt5.QtCore import pyqtSlot, QTimer
21 from PyQt5.QtGui import QTextCursor 21 from PyQt5.QtGui import QTextCursor
22 from PyQt5.QtWidgets import QWidget 22 from PyQt5.QtWidgets import QWidget, QDialog
23 23
24 from E5Gui import E5MessageBox 24 from E5Gui import E5MessageBox
25 25
26 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget 26 from .Ui_MqttMonitorWidget import Ui_MqttMonitorWidget
27 27
60 os.path.join("MqttMonitor", "icons", "mqtt48.png"))) 60 os.path.join("MqttMonitor", "icons", "mqtt48.png")))
61 61
62 self.brokerWidget.setCurrentIndex(0) 62 self.brokerWidget.setCurrentIndex(0)
63 63
64 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) 64 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
65 self.brokerComboBox.addItems( 65 self.brokerConnectionOptionsButton.setIcon(UI.PixmapCache.getIcon(
66 self.__plugin.getPreferences("RecentBrokers")) 66 os.path.join("MqttMonitor", "icons", "connectionOptions.png")))
67 self.__populateBrokerComboBoxes()
67 self.brokerStatusLabel.hide() 68 self.brokerStatusLabel.hide()
68 69
69 self.subscribeButton.setIcon(UI.PixmapCache.getIcon("plus.png")) 70 self.subscribeButton.setIcon(UI.PixmapCache.getIcon("plus.png"))
70 self.subscribeButton.setEnabled(False) 71 self.subscribeButton.setEnabled(False)
71 self.unsubscribeButton.setIcon(UI.PixmapCache.getIcon("minus.png")) 72 self.unsubscribeButton.setIcon(UI.PixmapCache.getIcon("minus.png"))
75 self.__updateUnsubscribeTopicComboBox() 76 self.__updateUnsubscribeTopicComboBox()
76 77
77 self.__publishedTopics = [] 78 self.__publishedTopics = []
78 self.__updatePublishTopicComboBox() 79 self.__updatePublishTopicComboBox()
79 self.publishButton.setEnabled(False) 80 self.publishButton.setEnabled(False)
81
82 self.__connectionOptions = None
80 83
81 prefix = MqttMonitorWidget.BrokerStatusTopicPrefix 84 prefix = MqttMonitorWidget.BrokerStatusTopicPrefix
82 self.__statusLabelMapping = { 85 self.__statusLabelMapping = {
83 # broker 86 # broker
84 prefix + "version": self.versionLabel, 87 prefix + "version": self.versionLabel,
146 @param rc CONNACK result code 149 @param rc CONNACK result code
147 @type int 150 @type int
148 """ 151 """
149 if rc == 0: 152 if rc == 0:
150 self.__connectedToBroker = True 153 self.__connectedToBroker = True
154 self.__connectionOptions = None
151 155
152 msg = mqttConnackMessage(rc) 156 msg = mqttConnackMessage(rc)
153 self.__flashBrokerStatusLabel(msg) 157 self.__flashBrokerStatusLabel(msg)
154 158
155 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png")) 159 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png"))
289 self.connectButton.setEnabled(False) 293 self.connectButton.setEnabled(False)
290 else: 294 else:
291 self.connectButton.setEnabled(True) 295 self.connectButton.setEnabled(True)
292 296
293 @pyqtSlot() 297 @pyqtSlot()
298 def on_brokerConnectionOptionsButton_clicked(self):
299 """
300 Private slot to show a dialog to modify connection options.
301 """
302 from .MqttConnectionOptionsDialog import MqttConnectionOptionsDialog
303 dlg = MqttConnectionOptionsDialog(
304 self.__client, self.__connectionOptions, parent=self)
305 if dlg.exec_() == QDialog.Accepted:
306 self.__connectionOptions = dlg.getConnectionOptions()
307
308 @pyqtSlot()
294 def on_connectButton_clicked(self): 309 def on_connectButton_clicked(self):
295 """ 310 """
296 Private slot to handle a connect or disconnect request. 311 Private slot to handle a connect or disconnect request.
297 """ 312 """
298 if self.__connectedToBroker: 313 if self.__connectedToBroker:
299 self.__client.disconnectFromServer() 314 self.__client.disconnectFromServer()
300 else: 315 else:
301 host = self.brokerComboBox.currentText() 316 host = self.brokerComboBox.currentText()
317 port = self.brokerPortComboBox.currentText().strip()
318 try:
319 port = int(port)
320 except ValueError:
321 # use standard port at 1883
322 port = 1883
302 if host: 323 if host:
303 self.__addBrokerToRecent(host) 324 self.__addBrokerToRecent(host, port)
304 self.__client.connectToServer(host) 325 self.__client.connectToServer(host, port=port)
305 # use standard port at 1883
306 326
307 @pyqtSlot(str) 327 @pyqtSlot(str)
308 def on_subscribeTopicEdit_textChanged(self, topic): 328 def on_subscribeTopicEdit_textChanged(self, topic):
309 """ 329 """
310 Private slot to handle a change of the entered topic. 330 Private slot to handle a change of the entered topic.
369 """ 389 """
370 topic = self.publishTopicComboBox.currentText() 390 topic = self.publishTopicComboBox.currentText()
371 qos = self.publishQosSpinBox.value() 391 qos = self.publishQosSpinBox.value()
372 retain = self.publishRetainCheckBox.isChecked() 392 retain = self.publishRetainCheckBox.isChecked()
373 payloadStr = self.publishPayloadEdit.toPlainText() 393 payloadStr = self.publishPayloadEdit.toPlainText()
394 if not payloadStr:
395 # use empty string together with the retain flag to clean
396 # a retained message by sending None instead
397 payloadStr = None
374 398
375 msgInfo = self.__client.publish(topic, payloadStr, qos, retain) 399 msgInfo = self.__client.publish(topic, payloadStr, qos, retain)
376 if msgInfo.rc == 0: 400 if msgInfo.rc == 0:
377 if topic not in self.__publishedTopics: 401 if topic not in self.__publishedTopics:
378 self.__publishedTopics.append(topic) 402 self.__publishedTopics.append(topic)
406 430
407 ####################################################################### 431 #######################################################################
408 ## Utility methods 432 ## Utility methods
409 ####################################################################### 433 #######################################################################
410 434
411 def __addBrokerToRecent(self, host): 435 def __addBrokerToRecent(self, host, port):
412 """ 436 """
413 Private method to add a host name to the list of recently connected 437 Private method to add a host name to the list of recently connected
414 brokers. 438 brokers.
415 439
416 @param host host name of broker 440 @param host host name of broker
417 @type str 441 @type str
418 """ 442 @param port port number of the connection
419 brokerList = self.__plugin.getPreferences("RecentBrokers") 443 @type int
420 if host in brokerList: 444 """
421 brokerList.remove(host) 445 brokerList = self.__plugin.getPreferences("RecentBrokersWithPort")
422 brokerList.insert(0, host) 446 hostAndPort = [host, port]
423 self.__plugin.setPreferences("RecentBrokers", brokerList) 447 if hostAndPort in brokerList:
424 448 brokerList.remove(hostAndPort)
449 brokerList.insert(0, hostAndPort)
450 self.__plugin.setPreferences("RecentBrokersWithPort", brokerList)
451
452 self.__populateBrokerComboBoxes()
453
454 def __populateBrokerComboBoxes(self):
455 """
456 Private method to populate the broker name and port combo boxes.
457 """
458 brokerList = self.__plugin.getPreferences("RecentBrokersWithPort")
459
460 # step 1: clear combo boxes
425 self.brokerComboBox.clear() 461 self.brokerComboBox.clear()
426 self.brokerComboBox.addItems(brokerList) 462 self.brokerPortComboBox.clear()
463
464 # step 2a: populate the broker name list
465 self.brokerComboBox.addItems([b[0].strip() for b in brokerList])
466
467 # step 2b: populate the broker ports list
468 if brokerList:
469 currentPort = brokerList[0][1]
470 else:
471 currentPort = 1883
472 currentPortStr = "{0:5}".format(currentPort)
473 portsSet = set([b[1] for b in brokerList])
474 portsSet.update([1883, 8883])
475 self.brokerPortComboBox.addItems(
476 sorted(["{0:5}".format(p) for p in portsSet]))
477 index = self.brokerPortComboBox.findText(currentPortStr)
478 self.brokerPortComboBox.setCurrentIndex(index)
427 479
428 def __updateUnsubscribeTopicComboBox(self): 480 def __updateUnsubscribeTopicComboBox(self):
429 """ 481 """
430 Private method to update the unsubcribe topic combo box. 482 Private method to update the unsubcribe topic combo box.
431 """ 483 """

eric ide

mercurial