MqttMonitor/MqttMonitorWidget.py

changeset 4
991db89bda9c
parent 3
82845c0fd550
child 5
7162c838cfc9
equal deleted inserted replaced
3:82845c0fd550 4:991db89bda9c
46 46
47 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) 47 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
48 self.brokerComboBox.addItems( 48 self.brokerComboBox.addItems(
49 self.__plugin.getPreferences("RecentBrokers")) 49 self.__plugin.getPreferences("RecentBrokers"))
50 self.brokerStatusLabel.hide() 50 self.brokerStatusLabel.hide()
51
52 self.subscribeButton.setIcon(UI.PixmapCache.getIcon("plus.png"))
53 self.subscribeButton.setEnabled(False)
54 self.unsubscribeButton.setIcon(UI.PixmapCache.getIcon("minus.png"))
55
56 self.__subscribedTopics = []
57 self.__topicQueue = {}
58 self.__updateUnsubscribeTopicComboBox()
51 59
52 self.__client = MqttClient() 60 self.__client = MqttClient()
53 61
54 # connect the MQTT client signals 62 # connect the MQTT client signals
55 self.__client.onConnect.connect(self.__brokerConnected) 63 self.__client.onConnect.connect(self.__brokerConnected)
78 86
79 msg = mqttConnackMessage(rc) 87 msg = mqttConnackMessage(rc)
80 self.__flashBrokerStatusLabel(msg) 88 self.__flashBrokerStatusLabel(msg)
81 89
82 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png")) 90 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircDisconnect.png"))
91
92 self.subscribeGroup.setEnabled(True)
93 self.unsubscribeGroup.setEnabled(True)
83 94
84 @pyqtSlot(int) 95 @pyqtSlot(int)
85 def __brokerDisconnected(self, rc): 96 def __brokerDisconnected(self, rc):
86 """ 97 """
87 Private slot to handle a disconnection from the broker. 98 Private slot to handle a disconnection from the broker.
88 99
89 @param rc MQTT error result code 100 @param rc MQTT error result code
90 @type int 101 @type int
91 """ 102 """
92 self.__connectedToBroker = False 103 self.__connectedToBroker = False
104
105 # ensure, the client loop is stopped
106 self.__client.stopLoop()
93 107
94 if rc != 0: 108 if rc != 0:
95 msg = mqttErrorMessage(rc) 109 msg = mqttErrorMessage(rc)
96 else: 110 else:
97 msg = self.tr("Connection to Broker shut down cleanly.") 111 msg = self.tr("Connection to Broker shut down cleanly.")
98 self.__flashBrokerStatusLabel(msg) 112 self.__flashBrokerStatusLabel(msg)
99 113
100 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png")) 114 self.connectButton.setIcon(UI.PixmapCache.getIcon("ircConnect.png"))
101 115
102 self.__client.stopLoop() 116 self.__subscribedTopics = []
117 self.__topicQueue = {}
118 self.__updateUnsubscribeTopicComboBox()
119
120 self.subscribeGroup.setEnabled(False)
121 self.unsubscribeGroup.setEnabled(False)
103 122
104 @pyqtSlot(str, bytes, int, bool) 123 @pyqtSlot(str, bytes, int, bool)
105 def __messageReceived(self, topic, payload, qos, retain): 124 def __messageReceived(self, topic, payload, qos, retain):
106 """ 125 """
107 Private slot to handle the receipt of a message. 126 Private slot to handle the receipt of a message.
135 @param mid ID of the subscribe request 154 @param mid ID of the subscribe request
136 @type int 155 @type int
137 @param grantedQos tuple of granted quality of service 156 @param grantedQos tuple of granted quality of service
138 @type tuple of int 157 @type tuple of int
139 """ 158 """
140 pass 159 if mid in self.__topicQueue:
160 topic = self.__topicQueue.pop(mid)
161 self.__subscribedTopics.append(topic)
162 self.subscribeTopicEdit.clear()
163
164 self.__updateUnsubscribeTopicComboBox()
141 165
142 @pyqtSlot(int) 166 @pyqtSlot(int)
143 def __topicUnsubscribed(self, mid): 167 def __topicUnsubscribed(self, mid):
144 """ 168 """
145 Private slot to handle being unsubcribed from a topic. 169 Private slot to handle being unsubcribed from a topic.
146 170
147 @param mid ID of the unsubscribe request 171 @param mid ID of the unsubscribe request
148 @type int 172 @type int
149 """ 173 """
150 pass 174 if mid in self.__topicQueue:
175 topic = self.__topicQueue.pop(mid)
176 try:
177 self.__subscribedTopics.remove(topic)
178 self.__updateUnsubscribeTopicComboBox()
179 except ValueError:
180 # ignore it
181 pass
151 182
152 ####################################################################### 183 #######################################################################
153 ## Slots handling UI interactions 184 ## Slots handling UI interactions
154 ####################################################################### 185 #######################################################################
155 186
186 """ 217 """
187 if self.__connectedToBroker: 218 if self.__connectedToBroker:
188 self.__client.disconnectFromServer() 219 self.__client.disconnectFromServer()
189 else: 220 else:
190 host = self.brokerComboBox.currentText() 221 host = self.brokerComboBox.currentText()
191 self.__addBrokerToRecent(host) 222 if host:
192 self.__client.connectToServer(host) # use standard port at 1883 223 self.__addBrokerToRecent(host)
224 self.__client.connectToServer(host)
225 # use standard port at 1883
226
227 @pyqtSlot(str)
228 def on_subscribeTopicEdit_textChanged(self, topic):
229 """
230 Private slot to handle a change of the entered topic.
231
232 @param topic entered topic text
233 @type str
234 """
235 self.subscribeButton.setEnabled(bool(topic))
236
237 @pyqtSlot()
238 def on_subscribeButton_clicked(self):
239 """
240 Private slot to subscribe to the entered topic.
241 """
242 topic = self.subscribeTopicEdit.text()
243 qos = self.subscribeQosSpinBox.value()
244 if topic:
245 self.__topicQueue[
246 self.__client.subscribe(topic, qos)[1]] = topic
247
248 @pyqtSlot(str)
249 def on_unsubscribeTopicComboBox_currentIndexChanged(self, topic):
250 """
251 Private slot to handle the selection of a topic to unsubscribe from.
252
253 @param topic topic text
254 @type str
255 """
256 self.unsubscribeButton.setEnabled(bool(topic))
257
258 @pyqtSlot()
259 def on_unsubscribeButton_clicked(self):
260 """
261 Private slot to unsubscribe from the selected topic.
262 """
263 topic = self.unsubscribeTopicComboBox.currentText()
264 if topic:
265 self.__topicQueue[
266 self.__client.unsubscribe(topic)[1]] = topic
193 267
194 ####################################################################### 268 #######################################################################
195 ## Utility methods 269 ## Utility methods
196 ####################################################################### 270 #######################################################################
197 271
209 brokerList.insert(0, host) 283 brokerList.insert(0, host)
210 self.__plugin.setPreferences("RecentBrokers", brokerList) 284 self.__plugin.setPreferences("RecentBrokers", brokerList)
211 285
212 self.brokerComboBox.clear() 286 self.brokerComboBox.clear()
213 self.brokerComboBox.addItems(brokerList) 287 self.brokerComboBox.addItems(brokerList)
288
289 def __updateUnsubscribeTopicComboBox(self):
290 """
291 Private method to update the unsubcribe topic combo box.
292 """
293 self.unsubscribeTopicComboBox.clear()
294 self.unsubscribeTopicComboBox.addItems(sorted(self.__subscribedTopics))
295 self.unsubscribeButton.setEnabled(len(self.__subscribedTopics) > 0)

eric ide

mercurial