MqttMonitor/MqttMonitorWidget.py

branch
eric7
changeset 103
5fe4f179975f
parent 102
70b8858199f5
child 104
9a4c9b7f078c
equal deleted inserted replaced
102:70b8858199f5 103:5fe4f179975f
72 self.__messagesFormat = self.messagesEdit.currentCharFormat() 72 self.__messagesFormat = self.messagesEdit.currentCharFormat()
73 self.__messagesTopicFormat = self.messagesEdit.currentCharFormat() 73 self.__messagesTopicFormat = self.messagesEdit.currentCharFormat()
74 self.__messagesTopicFormat.setFontWeight(QFont.Weight.Bold) 74 self.__messagesTopicFormat.setFontWeight(QFont.Weight.Bold)
75 self.__messagesQosFormat = self.messagesEdit.currentCharFormat() 75 self.__messagesQosFormat = self.messagesEdit.currentCharFormat()
76 self.__messagesQosFormat.setFontItalic(True) 76 self.__messagesQosFormat.setFontItalic(True)
77 self.__messagesSubheaderFormat = self.messagesEdit.currentCharFormat()
78 self.__messagesSubheaderFormat.setFontUnderline(True)
77 79
78 self.__propertiesFormat = self.propertiesEdit.currentCharFormat() 80 self.__propertiesFormat = self.propertiesEdit.currentCharFormat()
79 self.__propertiesTopicFormat = self.propertiesEdit.currentCharFormat() 81 self.__propertiesTopicFormat = self.propertiesEdit.currentCharFormat()
80 self.__propertiesTopicFormat.setFontWeight(QFont.Weight.Bold) 82 self.__propertiesTopicFormat.setFontWeight(QFont.Weight.Bold)
81 self.__propertiesNameFormat = self.propertiesEdit.currentCharFormat() 83 self.__propertiesNameFormat = self.propertiesEdit.currentCharFormat()
157 self.__updateUnsubscribeTopicComboBox() 159 self.__updateUnsubscribeTopicComboBox()
158 160
159 self.__publishedTopics = [] 161 self.__publishedTopics = []
160 self.__updatePublishTopicComboBox() 162 self.__updatePublishTopicComboBox()
161 self.publishButton.setEnabled(False) 163 self.publishButton.setEnabled(False)
164 self.publishPropertiesButton.setIcon(
165 UI.PixmapCache.getIcon("listSelection"))
166 self.publishPropertiesButton.setEnabled(False)
167 self.publishPropertiesButton.setVisible(False)
162 168
163 self.__connectionOptions = None 169 self.__connectionOptions = None
164 170
165 prefix = MqttMonitorWidget.BrokerStatusTopicPrefix 171 prefix = MqttMonitorWidget.BrokerStatusTopicPrefix
166 self.__statusLabelMapping = { 172 self.__statusLabelMapping = {
317 self.unsubscribeGroup.setEnabled(True) 323 self.unsubscribeGroup.setEnabled(True)
318 self.unsubscribePropertiesButton.setVisible( 324 self.unsubscribePropertiesButton.setVisible(
319 self.__client.getProtocol() == MqttProtocols.MQTTv5) 325 self.__client.getProtocol() == MqttProtocols.MQTTv5)
320 self.publishGroup.setEnabled(True) 326 self.publishGroup.setEnabled(True)
321 self.brokerStatusButton.setEnabled(True) 327 self.brokerStatusButton.setEnabled(True)
328 self.publishPropertiesButton.setVisible(
329 self.__client.getProtocol() == MqttProtocols.MQTTv5)
322 330
323 self.__statusLoadValues.clear() 331 self.__statusLoadValues.clear()
324 self.__clearBrokerStatusLabels() 332 self.__clearBrokerStatusLabels()
325 self.__setBrokerStatusSubscribed(False) 333 self.__setBrokerStatusSubscribed(False)
326 else: 334 else:
374 self.subscribeGroup.setEnabled(False) 382 self.subscribeGroup.setEnabled(False)
375 self.subscribePropertiesButton.setVisible(False) 383 self.subscribePropertiesButton.setVisible(False)
376 self.unsubscribeGroup.setEnabled(False) 384 self.unsubscribeGroup.setEnabled(False)
377 self.unsubscribePropertiesButton.setVisible(False) 385 self.unsubscribePropertiesButton.setVisible(False)
378 self.publishGroup.setEnabled(False) 386 self.publishGroup.setEnabled(False)
387 self.publishPropertiesButton.setVisible(False)
379 self.brokerStatusButton.setEnabled(False) 388 self.brokerStatusButton.setEnabled(False)
380 389
381 self.__statusLoadValues.clear() 390 self.__statusLoadValues.clear()
382 391
383 @pyqtSlot(int, str) 392 @pyqtSlot(int, str)
464 MQTT v3.1.1). 473 MQTT v3.1.1).
465 474
466 @param mid ID of the subscribe request 475 @param mid ID of the subscribe request
467 @type int 476 @type int
468 """ 477 """
478 # TODO: remember the successfully subscribed topic
479 # TODO: max. number of recent topics as a config item
469 if mid in self.__topicQueue: 480 if mid in self.__topicQueue:
470 topic = self.__topicQueue.pop(mid) 481 topic = self.__topicQueue.pop(mid)
471 self.__subscribedTopics.append(topic) 482 self.__subscribedTopics.append(topic)
472 self.subscribeTopicEdit.clear() 483 self.subscribeTopicEdit.clear()
473 484
721 ) 732 )
722 result, mid = self.__client.unsubscribe( 733 result, mid = self.__client.unsubscribe(
723 topic, properties=properties) 734 topic, properties=properties)
724 self.__topicQueue[mid] = topic 735 self.__topicQueue[mid] = topic
725 736
737 @pyqtSlot()
738 def on_publishPropertiesButton_clicked(self):
739 """
740 Private slot to edit the publish user properties.
741 """
742 topic = self.publishTopicComboBox.currentText()
743 self.__editProperties(
744 "publish",
745 self.tr("PUBLISH: User Properties for '{0}'").format(topic),
746 topic
747 )
748
726 @pyqtSlot(str) 749 @pyqtSlot(str)
727 def on_publishTopicComboBox_editTextChanged(self, topic): 750 def on_publishTopicComboBox_editTextChanged(self, topic):
728 """ 751 """
729 Private slot to handle changes of the publish topic name. 752 Private slot to handle changes of the publish topic name.
730 753
731 @param topic topic text 754 @param topic topic text
732 @type str 755 @type str
733 """ 756 """
734 self.publishButton.setEnabled(bool(topic)) 757 self.publishButton.setEnabled(bool(topic))
758 self.publishPropertiesButton.setEnabled(bool(topic))
735 759
736 @pyqtSlot() 760 @pyqtSlot()
737 def on_publishButton_clicked(self): 761 def on_publishButton_clicked(self):
738 """ 762 """
739 Private slot to publish the entered message. 763 Private slot to publish the entered message.
763 payloadStr = self.publishPayloadEdit.toPlainText() 787 payloadStr = self.publishPayloadEdit.toPlainText()
764 if not payloadStr: 788 if not payloadStr:
765 # use empty string together with the retain flag to clean 789 # use empty string together with the retain flag to clean
766 # a retained message by sending None instead 790 # a retained message by sending None instead
767 payloadStr = None 791 payloadStr = None
768 792 properties = (
769 msgInfo = self.__client.publish(topic, payloadStr, qos, retain) 793 self.__plugin.getPreferences("PublishProperties").get(topic, [])
794 if self.__client.getProtocol() == MqttProtocols.MQTTv5 else
795 None
796 )
797
798 msgInfo = self.__client.publish(
799 topic, payload=payloadStr, qos=qos, retain=retain,
800 properties=properties)
770 if msgInfo.rc == 0: 801 if msgInfo.rc == 0:
771 if topic not in self.__publishedTopics: 802 if topic not in self.__publishedTopics:
772 self.__publishedTopics.append(topic) 803 self.__publishedTopics.append(topic)
773 self.__updatePublishTopicComboBox(resetTopic=False) 804 self.__updatePublishTopicComboBox(resetTopic=False)
774 if self.clearPublishCheckBox.isChecked(): 805 if self.clearPublishCheckBox.isChecked():
778 def on_publishClearRetainedButton_clicked(self): 809 def on_publishClearRetainedButton_clicked(self):
779 """ 810 """
780 Private slot to clear the retained messages for the topic. 811 Private slot to clear the retained messages for the topic.
781 """ 812 """
782 topic = self.publishTopicComboBox.currentText() 813 topic = self.publishTopicComboBox.currentText()
783 814 properties = (
784 msgInfo = self.__client.publish(topic, payload=None, retain=True) 815 self.__plugin.getPreferences("PublishProperties").get(topic, [])
816 if self.__client.getProtocol() == MqttProtocols.MQTTv5 else
817 None
818 )
819
820 msgInfo = self.__client.publish(
821 topic, payload=None, retain=True, properties=properties)
785 if msgInfo.rc == 0: 822 if msgInfo.rc == 0:
786 if topic not in self.__publishedTopics: 823 if topic not in self.__publishedTopics:
787 self.__publishedTopics.append(topic) 824 self.__publishedTopics.append(topic)
788 self.__updatePublishTopicComboBox(resetTopic=False) 825 self.__updatePublishTopicComboBox(resetTopic=False)
789 826
986 hostAndPort = [host, port] 1023 hostAndPort = [host, port]
987 if hostAndPort in brokerList: 1024 if hostAndPort in brokerList:
988 brokerList.remove(hostAndPort) 1025 brokerList.remove(hostAndPort)
989 brokerList.insert(0, hostAndPort) 1026 brokerList.insert(0, hostAndPort)
990 # limit to most recently used 20 entries 1027 # limit to most recently used 20 entries
1028 # TODO: make the amount of recent brokers a config item
991 brokerList = brokerList[:20] 1029 brokerList = brokerList[:20]
992 self.__plugin.setPreferences("RecentBrokersWithPort", brokerList) 1030 self.__plugin.setPreferences("RecentBrokersWithPort", brokerList)
993 1031
994 self.__populateBrokerComboBoxes() 1032 self.__populateBrokerComboBoxes()
995 1033
1110 if retain: 1148 if retain:
1111 self.messagesEdit.setCurrentCharFormat(self.__messagesQosFormat) 1149 self.messagesEdit.setCurrentCharFormat(self.__messagesQosFormat)
1112 self.messagesEdit.insertPlainText(self.tr("Retained Message\n")) 1150 self.messagesEdit.insertPlainText(self.tr("Retained Message\n"))
1113 1151
1114 if properties: 1152 if properties:
1115 self.messagesEdit.setCurrentCharFormat(self.__messagesTopicFormat) 1153 self.messagesEdit.setCurrentCharFormat(
1154 self.__messagesSubheaderFormat)
1116 self.messagesEdit.insertPlainText(self.tr("Properties:\n")) 1155 self.messagesEdit.insertPlainText(self.tr("Properties:\n"))
1117 self.messagesEdit.setCurrentCharFormat(self.__messagesFormat) 1156 self.messagesEdit.setCurrentCharFormat(self.__messagesFormat)
1118 for name, value in sorted(properties.items): 1157 for name, value in sorted(properties.items()):
1119 self.messagesEdit.insertPlainText( 1158 self.messagesEdit.insertPlainText(
1120 self.tr("{0}: {1}\n", "property name, property value") 1159 self.tr("{0}: {1}\n", "property name, property value")
1121 .format(name, value) 1160 .format(name, value)
1122 ) 1161 )
1123 1162
1163 self.messagesEdit.setCurrentCharFormat(self.__messagesSubheaderFormat)
1164 self.messagesEdit.insertPlainText(self.tr("Message:\n"))
1124 payloadStr = str(payload, encoding="utf-8", errors="replace") 1165 payloadStr = str(payload, encoding="utf-8", errors="replace")
1166 payloadStr = Utilities.filterAnsiSequences(payloadStr)
1125 self.messagesEdit.setCurrentCharFormat(self.__messagesFormat) 1167 self.messagesEdit.setCurrentCharFormat(self.__messagesFormat)
1126 self.messagesEdit.insertPlainText( 1168 if payloadStr:
1127 Utilities.filterAnsiSequences(payloadStr)) 1169 self.messagesEdit.insertPlainText(payloadStr)
1170 else:
1171 self.messagesEdit.insertPlainText(self.tr("<empty>"))
1128 1172
1129 if self.followMessagesCheckBox.isChecked(): 1173 if self.followMessagesCheckBox.isChecked():
1130 self.messagesEdit.ensureCursorVisible() 1174 self.messagesEdit.ensureCursorVisible()
1131 else: 1175 else:
1132 self.messagesEdit.verticalScrollBar().setValue(scrollbarValue) 1176 self.messagesEdit.verticalScrollBar().setValue(scrollbarValue)
1271 profileName = self.profileComboBox.currentText() 1315 profileName = self.profileComboBox.currentText()
1272 if profileName: 1316 if profileName:
1273 self.__plugin.setPreferences("MostRecentProfile", profileName) 1317 self.__plugin.setPreferences("MostRecentProfile", profileName)
1274 1318
1275 profilesDict = self.__plugin.getPreferences("BrokerProfiles") 1319 profilesDict = self.__plugin.getPreferences("BrokerProfiles")
1276 connectionProfile = copy.copy(profilesDict[profileName]) 1320 connectionProfile = copy.deepcopy(profilesDict[profileName])
1277 host = connectionProfile["BrokerAddress"] 1321 host = connectionProfile["BrokerAddress"]
1278 port = connectionProfile["BrokerPort"] 1322 port = connectionProfile["BrokerPort"]
1279 try: 1323 try:
1280 protocol = connectionProfile["Protocol"] 1324 protocol = connectionProfile["Protocol"]
1281 except KeyError: 1325 except KeyError:
1346 @param header header to be shown in the edit dialog 1390 @param header header to be shown in the edit dialog
1347 @type str 1391 @type str
1348 @param key key to retrieve the right properties 1392 @param key key to retrieve the right properties
1349 @type str 1393 @type str
1350 """ 1394 """
1351 from .MqttUserPropertiesEditor import MqttUserPropertiesEditor 1395 from .MqttUserPropertiesEditor import MqttUserPropertiesEditorDialog
1352 1396
1353 preferencesKey = "{0}Properties".format(propertiesType.capitalize()) 1397 preferencesKey = "{0}Properties".format(propertiesType.capitalize())
1354 properties = self.__plugin.getPreferences(preferencesKey) 1398 properties = self.__plugin.getPreferences(preferencesKey)
1355 dlg = MqttUserPropertiesEditor(header, properties.get(key, []), self) 1399 dlg = MqttUserPropertiesEditorDialog(
1400 header, properties.get(key, []), self)
1356 if dlg.exec() == QDialog.DialogCode.Accepted: 1401 if dlg.exec() == QDialog.DialogCode.Accepted:
1357 properties[key] = dlg.getProperties() 1402 properties[key] = dlg.getProperties()
1358 self.__plugin.setPreferences(preferencesKey, properties) 1403 self.__plugin.setPreferences(preferencesKey, properties)

eric ide

mercurial