enhance dashboard client

This commit is contained in:
Tobias Brunner 2018-01-19 21:35:17 +01:00
parent 73897bffa2
commit c485de0b93
1 changed files with 30 additions and 19 deletions

View File

@ -17,10 +17,17 @@ MQTT_PASSWORD = os.getenv("MQTT_PASSWORD")
MQTT_BASE_TOPIC = os.getenv("MQTT_BASE_TOPIC", "pylokid")
TMP_DIR = os.getenv("TMP_DIR", "/tmp")
# Initialization
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
LOGGER = logging.getLogger('dashboard')
PIDS = {}
def on_connect(client, userdata, flags, rc):
print("Connected with result code "+str(rc))
LOGGER.info("Connected to MQTT with result code %s", str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
@ -30,34 +37,39 @@ def on_message(client, userdata, msg):
topic_detail = msg.topic.split("/")
f_id = topic_detail[2]
if topic_detail[1] == 'Einsatzausdruck_FW' and topic_detail[3] == 'pdf':
print('Einsatzausdruck for ' + f_id)
LOGGER.info("[%s] New Einsatzausdruck received", f_id)
file_name = TMP_DIR + "/dashboard/" + f_id + ".pdf"
file = open(file_name, "wb")
file.write(msg.payload)
file.close()
# process = subprocess.Popen(["/usr/bin/okular", "--presentation", file_name])
process = subprocess.Popen(
["/usr/bin/xpdf", "-z", "width", "-fullscreen", file_name],
env=dict(os.environ, DISPLAY=":0")
)
PIDS[f_id] = process.pid
if f_id in PIDS:
LOGGER.info(
"[%s] Einsatzausdruck already being displayed with PID %s",
f_id,
str(PIDS[f_id])
)
else:
LOGGER.info("[%s] Displaying Einsatzausdruck with xpdf", f_id)
process = subprocess.Popen(
["/usr/bin/xpdf", "-z", "width", "-fullscreen", file_name],
env=dict(os.environ, DISPLAY=":0")
)
PIDS[f_id] = process.pid
elif topic_detail[1] == 'Einsatzprotokoll':
print('Einsatzprotokoll for ' + f_id)
os.kill(PIDS[f_id], 9)
LOGGER.info("[%s] New Einsatzprotokoll received", f_id)
if f_id in PIDS:
LOGGER.info("[%s] Killing xpdf PID %s", f_id, str(PIDS[f_id]))
os.kill(PIDS[f_id], 9)
PIDS.pop(f_id)
else:
LOGGER.info("[%s] No xpdf PID found", f_id)
else:
print('Unbekannt')
LOGGER.info("[%s] Unknown", topic_detail[1])
def main():
""" main """
# Logging configuration
logging.basicConfig(
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)
logger = logging.getLogger('dashboard')
mqtt_client = mqtt.Client()
mqtt_client.on_connect = on_connect
mqtt_client.on_message = on_message
@ -67,7 +79,6 @@ def main():
mqtt_client.connect(MQTT_SERVER, 8883, 60)
mqtt_client.loop_forever()
if __name__ == '__main__':
try:
main()