-
Notifications
You must be signed in to change notification settings - Fork 0
/
TeletextApp.py
146 lines (123 loc) · 5.6 KB
/
TeletextApp.py
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
TeletextApp.py
MIT License (c) Faure Systems <dev at faure dot systems>
TeletextApp extends GuizeroApp.
"""
from constants import *
import gettext
try:
gettext.find("TeletextApp")
traduction = gettext.translation('TeletextApp', localedir='locale', languages=['fr'])
traduction.install()
except:
_ = gettext.gettext # cool, this hides PyLint warning Undefined name '_'
from GuizeroApp import GuizeroApp
import os, platform, sys, logging
from guizero import Text
from Sound import Sound
class TeletextApp(GuizeroApp):
# __________________________________________________________________
def __init__(self, argv, client, debugging_mqtt=False):
super().__init__(argv, client, debugging_mqtt)
self.logger.info(_("Props started"))
if platform.system() != 'Windows':
if self.logger.level != logging.DEBUG:
self._gui.full_screen = True # exit fullscreen with Esc (so for props without a keyboard)
else:
self._gui.width = 592
self._gui.height = 333
self._gui.bg = 'black'
self._gui.tk.config(cursor="none")
self._texte = Text(self._gui, "") # "" value is translated to "-" for MQTT_DISPLAY_TOPIC
self._texte.height = 1080
self._texte.text_color = 'green'
self._texte.font = "Helvetica"
if platform.system() != 'Windows':
self._texte.size = "90"
else:
self._texte.size = "28"
self._sound = Sound(self._logger)
if platform.system() != 'Windows':
os.system("amixer cset numid=3 1") # audio jack
os.system("amixer set 'PCM' -- -1000")
if self._mqttConnected:
try:
(result, mid) = self._mqttClient.publish(MQTT_DISPLAY_TOPIC, "-", qos=MQTT_DEFAULT_QoS, retain=True)
except Exception as e:
self._logger.error(
"{0} '{1}' on {2}".format(_("MQTT API : failed to call publish() for"), "-", MQTT_DISPLAY_TOPIC))
self._logger.debug(e)
# __________________________________________________________________
def onConnect(self, client, userdata, flags, rc):
# extend as a virtual method
# display message will '-' for black screen
if hasattr(self, '_texte'):
text = self._texte.value
if not text:
text = "-"
try:
(result, mid) = self._mqttClient.publish(MQTT_DISPLAY_TOPIC, text, qos=MQTT_DEFAULT_QoS, retain=True)
except Exception as e:
self._logger.error(
"{0} '{1}' on {2}".format(_("MQTT API : failed to call publish() for"), text, MQTT_DISPLAY_TOPIC))
self._logger.debug(e)
# __________________________________________________________________
def onMessage(self, topic, message):
# extend as a virtual method
print(topic, message)
if message in ["app:startup", "app:quit"]:
super().onMessage(topic, message)
elif message.startswith("display:"):
text = message[8:]
self._texte.value = text
if self._mqttConnected:
try:
(result, mid) = self._mqttClient.publish(MQTT_DISPLAY_TOPIC, text, qos=MQTT_DEFAULT_QoS,
retain=True)
self._logger.info(
"{0} '{1}' (mid={2}) on {3}".format(_("Program sending message"), message, mid,
MQTT_DISPLAY_TOPIC))
except Exception as e:
self._logger.error(
"{0} '{1}' on {2}".format(_("MQTT API : failed to call publish() for"), message,
MQTT_DISPLAY_TOPIC))
self._logger.debug(e)
self.publishMessage(self._mqttOutbox, "DONE " + message)
self.publishDataChanges()
self._sound.play('media/bell.wav')
elif message.startswith("erase"):
self._texte.value = ""
if self._mqttConnected:
try:
(result, mid) = self._mqttClient.publish(MQTT_DISPLAY_TOPIC, "-", qos=MQTT_DEFAULT_QoS, retain=True)
self._logger.info(
"{0} '{1}' (mid={2}) on {3}".format(_("Program sending message"), message, mid,
MQTT_DISPLAY_TOPIC))
except Exception as e:
self._logger.error(
"{0} '{1}' on {2}".format(_("MQTT API : failed to call publish() for"), message,
MQTT_DISPLAY_TOPIC))
self._logger.debug(e)
self.publishMessage(self._mqttOutbox, "DONE " + message)
self.publishDataChanges()
else:
self.publishMessage(self._mqttOutbox, "OMIT " + message)
# __________________________________________________________________
def publishAllData(self):
super().publishAllData()
# __________________________________________________________________
def publishDataChanges(self):
super().publishDataChanges()
# __________________________________________________________________
def quit(self):
self._gui.exit_full_screen()
self._gui.destroy()
try:
self._mqttClient.disconnect()
self._mqttClient.loop_stop()
except:
pass
self.logger.info(_("Props stopped"))
sys.exit(0)