MqttMonitor/MqttClient.py

changeset 3
82845c0fd550
parent 2
d439c5109829
child 9
f75a385e9127
equal deleted inserted replaced
2:d439c5109829 3:82845c0fd550
172 @rtype mqtt.MQTTMessageInfo 172 @rtype mqtt.MQTTMessageInfo
173 """ 173 """
174 return self.__mqttClient.publish(topic, payload=payload, qos=qos, 174 return self.__mqttClient.publish(topic, payload=payload, qos=qos,
175 retain=retain) 175 retain=retain)
176 176
177
177 def mqttConnackMessage(connackCode): 178 def mqttConnackMessage(connackCode):
178 """ 179 """
179 Public method to get the string associated with a CONNACK result. 180 Public method to get the string associated with a CONNACK result.
181
182 @param connackCode result code of the connection request
183 @type int
184 @return textual representation for the result code
185 @rtype str
180 """ 186 """
181 if connackCode == mqtt.CONNACK_ACCEPTED: 187 if connackCode == mqtt.CONNACK_ACCEPTED:
182 return QCoreApplication.translate( 188 return QCoreApplication.translate(
183 "MqttConnackMessage", 189 "MqttConnackMessage",
184 "Connection Accepted.") 190 "Connection Accepted.")
185 elif connackCode == mqtt.CONNACK_REFUSED_PROTOCOL_VERSION: 191 elif connackCode == mqtt.CONNACK_REFUSED_PROTOCOL_VERSION:
186 return QCoreApplication.translate( 192 return QCoreApplication.translate(
187 "MqttConnackMessage", 193 "MqttConnackMessage",
188 "Connection Refused: unacceptable protocol version.") 194 "Connection Refused: unacceptable protocol version.")
189 elif connackCode == mqtt.CONNACK_REFUSED_IDENTIFIER_REJECTED: 195 elif connackCode == mqtt.CONNACK_REFUSED_IDENTIFIER_REJECTED:
190 return QCoreApplication.translate( 196 return QCoreApplication.translate(
191 "MqttConnackMessage", 197 "MqttConnackMessage",
192 "Connection Refused: identifier rejected.") 198 "Connection Refused: identifier rejected.")
193 elif connackCode == mqtt.CONNACK_REFUSED_SERVER_UNAVAILABLE: 199 elif connackCode == mqtt.CONNACK_REFUSED_SERVER_UNAVAILABLE:
194 return QCoreApplication.translate( 200 return QCoreApplication.translate(
195 "MqttConnackMessage", 201 "MqttConnackMessage",
196 "Connection Refused: broker unavailable.") 202 "Connection Refused: broker unavailable.")
197 elif connackCode == mqtt.CONNACK_REFUSED_BAD_USERNAME_PASSWORD: 203 elif connackCode == mqtt.CONNACK_REFUSED_BAD_USERNAME_PASSWORD:
198 return QCoreApplication.translate( 204 return QCoreApplication.translate(
199 "MqttConnackMessage", 205 "MqttConnackMessage",
200 "Connection Refused: bad user name or password.") 206 "Connection Refused: bad user name or password.")
201 elif connackCode == mqtt.CONNACK_REFUSED_NOT_AUTHORIZED: 207 elif connackCode == mqtt.CONNACK_REFUSED_NOT_AUTHORIZED:
202 return QCoreApplication.translate( 208 return QCoreApplication.translate(
203 "MqttConnackMessage", 209 "MqttConnackMessage",
204 "Connection Refused: not authorised.") 210 "Connection Refused: not authorised.")
205 else: 211 else:
206 return QCoreApplication.translate( 212 return QCoreApplication.translate(
207 "MqttConnackMessage", 213 "MqttConnackMessage",
208 "Connection Refused: unknown reason.") 214 "Connection Refused: unknown reason.")
215
209 216
210 def mqttErrorMessage(self, mqttErrno): 217 def mqttErrorMessage(self, mqttErrno):
211 """ 218 """
212 Public method to get the error string associated with an MQTT error 219 Public method to get the error string associated with an MQTT error
213 number. 220 number.
221
222 @param mqttErrno result code of a MQTT request
223 @type int
224 @return textual representation of the result code
225 @rtype str
214 """ 226 """
215 if mqttErrno == mqtt.MQTT_ERR_SUCCESS: 227 if mqttErrno == mqtt.MQTT_ERR_SUCCESS:
216 return QCoreApplication.translate( 228 return QCoreApplication.translate(
217 "MqttErrorMessage", 229 "MqttErrorMessage",
218 "No error.") 230 "No error.")
219 elif mqttErrno == mqtt.MQTT_ERR_NOMEM: 231 elif mqttErrno == mqtt.MQTT_ERR_NOMEM:
220 return QCoreApplication.translate( 232 return QCoreApplication.translate(
221 "MqttErrorMessage", 233 "MqttErrorMessage",
222 "Out of memory.") 234 "Out of memory.")
223 elif mqttErrno == mqtt.MQTT_ERR_PROTOCOL: 235 elif mqttErrno == mqtt.MQTT_ERR_PROTOCOL:
224 return QCoreApplication.translate( 236 return QCoreApplication.translate(
225 "MqttErrorMessage", 237 "MqttErrorMessage",
226 "A network protocol error occurred when communicating with" 238 "A network protocol error occurred when communicating with"
227 " the broker.") 239 " the broker.")
228 elif mqttErrno == mqtt.MQTT_ERR_INVAL: 240 elif mqttErrno == mqtt.MQTT_ERR_INVAL:
229 return QCoreApplication.translate( 241 return QCoreApplication.translate(
230 "MqttErrorMessage", 242 "MqttErrorMessage",
231 "Invalid function arguments provided.") 243 "Invalid function arguments provided.")
232 elif mqttErrno == mqtt.MQTT_ERR_NO_CONN: 244 elif mqttErrno == mqtt.MQTT_ERR_NO_CONN:
233 return QCoreApplication.translate( 245 return QCoreApplication.translate(
234 "MqttErrorMessage", 246 "MqttErrorMessage",
235 "The client is not currently connected.") 247 "The client is not currently connected.")
236 elif mqttErrno == mqtt.MQTT_ERR_CONN_REFUSED: 248 elif mqttErrno == mqtt.MQTT_ERR_CONN_REFUSED:
237 return QCoreApplication.translate( 249 return QCoreApplication.translate(
238 "MqttErrorMessage", 250 "MqttErrorMessage",
239 "The connection was refused.") 251 "The connection was refused.")
240 elif mqttErrno == mqtt.MQTT_ERR_NOT_FOUND: 252 elif mqttErrno == mqtt.MQTT_ERR_NOT_FOUND:
241 return QCoreApplication.translate( 253 return QCoreApplication.translate(
242 "MqttErrorMessage", 254 "MqttErrorMessage",
243 "Message not found (internal error).") 255 "Message not found (internal error).")
244 elif mqttErrno == mqtt.MQTT_ERR_CONN_LOST: 256 elif mqttErrno == mqtt.MQTT_ERR_CONN_LOST:
245 return QCoreApplication.translate( 257 return QCoreApplication.translate(
246 "MqttErrorMessage", 258 "MqttErrorMessage",
247 "The connection was lost.") 259 "The connection was lost.")
248 elif mqttErrno == mqtt.MQTT_ERR_TLS: 260 elif mqttErrno == mqtt.MQTT_ERR_TLS:
249 return QCoreApplication.translate( 261 return QCoreApplication.translate(
250 "MqttErrorMessage", 262 "MqttErrorMessage",
251 "A TLS error occurred.") 263 "A TLS error occurred.")
252 elif mqttErrno == mqtt.MQTT_ERR_PAYLOAD_SIZE: 264 elif mqttErrno == mqtt.MQTT_ERR_PAYLOAD_SIZE:
253 return QCoreApplication.translate( 265 return QCoreApplication.translate(
254 "MqttErrorMessage", 266 "MqttErrorMessage",
255 "Payload too large.") 267 "Payload too large.")
256 elif mqttErrno == mqtt.MQTT_ERR_NOT_SUPPORTED: 268 elif mqttErrno == mqtt.MQTT_ERR_NOT_SUPPORTED:
257 return QCoreApplication.translate( 269 return QCoreApplication.translate(
258 "MqttErrorMessage", 270 "MqttErrorMessage",
259 "This feature is not supported.") 271 "This feature is not supported.")
260 elif mqttErrno == mqtt.MQTT_ERR_AUTH: 272 elif mqttErrno == mqtt.MQTT_ERR_AUTH:
261 return QCoreApplication.translate( 273 return QCoreApplication.translate(
262 "MqttErrorMessage", 274 "MqttErrorMessage",
263 "Authorisation failed.") 275 "Authorisation failed.")
264 elif mqttErrno == mqtt.MQTT_ERR_ACL_DENIED: 276 elif mqttErrno == mqtt.MQTT_ERR_ACL_DENIED:
265 return QCoreApplication.translate( 277 return QCoreApplication.translate(
266 "MqttErrorMessage", 278 "MqttErrorMessage",
267 "Access denied by ACL.") 279 "Access denied by ACL.")
268 elif mqttErrno == mqtt.MQTT_ERR_UNKNOWN: 280 elif mqttErrno == mqtt.MQTT_ERR_UNKNOWN:
269 return QCoreApplication.translate( 281 return QCoreApplication.translate(
270 "MqttErrorMessage", 282 "MqttErrorMessage",
271 "Unknown error.") 283 "Unknown error.")
272 elif mqttErrno == mqtt.MQTT_ERR_ERRNO: 284 elif mqttErrno == mqtt.MQTT_ERR_ERRNO:
273 return QCoreApplication.translate( 285 return QCoreApplication.translate(
274 "MqttErrorMessage", 286 "MqttErrorMessage",
275 "Error defined by errno.") 287 "Error defined by errno.")
276 else: 288 else:
277 return QCoreApplication.translate( 289 return QCoreApplication.translate(
278 "MqttErrorMessage", 290 "MqttErrorMessage",
279 "Unknown error.") 291 "Unknown error.")

eric ide

mercurial