-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPluginApplet.py
More file actions
75 lines (61 loc) · 2.71 KB
/
Copy pathPluginApplet.py
File metadata and controls
75 lines (61 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PluginApplet.py
MIT License (c) Faure Systems <dev at faure dot systems>
PluginApplet application extends MqttApplet.
"""
from constants import *
from MqttApplet import MqttApplet
from PluginDialog import PluginDialog
from PyQt5.QtCore import pyqtSignal, pyqtSlot
class PluginApplet(MqttApplet):
propsMessageReceived = pyqtSignal(str)
# __________________________________________________________________
def __init__(self, argv, client, debugging_mqtt=False):
super().__init__(argv, client, debugging_mqtt)
self.setApplicationDisplayName(APPDISPLAYNAME)
# on_message per topic callbacks
try:
mqtt_sub_props = self._definitions['mqtt-sub-props']
self._mqttClient.message_callback_add(mqtt_sub_props, self.mqttOnMessageFromProps)
except Exception as e:
self._logger.error(self.tr("Plugin sub topic definition is missing"))
self._logger.debug(e)
# on_message default callback
self._mqttClient.on_message = self.mqttOnMessage
self._PluginDialog = PluginDialog(self.tr("Plugin"), './room.png', self._logger)
self._PluginDialog.aboutToClose.connect(self.exitOnClose)
self.propsMessageReceived.connect(self._PluginDialog.onPropsMessage)
self._PluginDialog.show()
# __________________________________________________________________
@pyqtSlot()
def exitOnClose(self):
self._logger.info(self.tr("exitOnClose "))
self.quit()
# __________________________________________________________________
def mqttOnMessage(self, client, userdata, msg):
message = None
try:
message = msg.payload.decode(encoding="utf-8", errors="strict")
except:
pass
if message:
self._logger.info(self.tr("Message received : '") + message + self.tr("' in ") + msg.topic)
##self.messageReceived.emit(msg.topic, message)
else:
self._logger.warning("{0} {1}".format(self.tr("MQTT message decoding failed on"), msg.topic))
# __________________________________________________________________
def mqttOnMessageFromProps(self, client, userdata, msg):
message = None
try:
message = msg.payload.decode(encoding="utf-8", errors="strict")
except:
pass
if message:
self._logger.info(
self.tr("Message received from Plugin props : '") + message + self.tr("' in ") + msg.topic)
self.propsMessageReceived.emit(message)
else:
self._logger.warning(
"{0} {1}".format(self.tr("Decoding MQTT message from Plugin props failed on"), msg.topic))