Aktuelle Version unhb-mqtt.py

This commit is contained in:
Stefan H. 2019-08-18 16:23:01 +02:00
parent aef0244e63
commit 027c8ba063

28
unhb-mqtt.py Normal file → Executable file
View file

@ -3,9 +3,11 @@ import paho.mqtt.client as mqtt
import json
import time
import pyqrcode
import subprocess
from PIL import Image, ImageDraw, ImageFont
import cups
from matrix_client.client import MatrixClient
from unhb_mqtt_config import *
# Druckerinitialisierung
conn = cups.Connection()
@ -47,20 +49,12 @@ def get_lastkey():
return lastkey
# Spaceapi definitions
space_status = "undefined"
# Test SilSon space_json = """{"api": "0.13","space": "UN-Hack-Bar","logo": "https://www.un-hack-bar.de/wp-content/uploads/2018/06/unhb_klein.png","url": "https://un-hack-bar.de","location": {"address": "Morgenstrasse 6, 59423 Unna, Germany","lon": 7.69172,"lat": 51.53575},"contact": {"email": "info@un-hack-bar.de","irc": "irc://irc.hackint.net/c3un","ml": "chaos-unna@lists.ctdo.de","twitter": "@un_hack_bar"},"state": {"open": null,"lastchange": 23},"feeds":{"calendar":{"type":"ical","url":"https://www.un-hack-bar.de/events.ics"}},"issue_report_channels": ["email"],"ext_ccc": "chaostreff"}"""
space_json = """{"api": "0.13","space": "UN-Hack-Bar","logo": "https://www.un-hack-bar.de/wp-content/uploads/2018/06/unhb_klein.png","url": "https://un-hack-bar.de","location": {"address": "Morgenstrasse 6, 59423 Unna, Germany","lon": 7.69172,"lat": 51.53575},"contact": {"email": "info@un-hack-bar.de","irc": "irc://irc.hackint.net/c3un","ml": "chaos-unna@lists.ctdo.de","twitter": "@un_hack_bar"},"state": {"open": null,"lastchange": 23},"feeds":{"calendar":{"type":"ical","url":"https://www.un-hack-bar.de/events.ics"}},"issue_report_channels": ["email"],"ext_ccc": "chaostreff", "state": { "icon": { "open": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/AwzcZPKZlaFCiDjJGrbmhVqy", "closed": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/rYahEOVyPMKJDQfLSnKLeHmn" } } }"""
space_obj = json.loads(space_json)
with open("spaceapi.json", "w") as write_file:
json.dump(space_obj, write_file, sort_keys=True)
print(json.dumps(space_obj, sort_keys=True, indent=4))
# Matrix-Bot definitions
matrix_bot_user = "DaneelOlivaw" # Bot's username
matrix_bot_passwd = "xxxxxxxx" # Bot's password
matrix_server = "https://matrix.un-hack-bar.de"
matrix_roomid = "!qxbQFatOwXtPBXegqg:matrix.un-hack-bar.de"
matrix_open_json = {"url": "mxc://matrix.un-hack-bar.de/AwzcZPKZlaFCiDjJGrbmhVqy"}
matrix_closed_json = {"url": "mxc://matrix.un-hack-bar.de/rYahEOVyPMKJDQfLSnKLeHmn"}
matrix_unk_json = {"url": "mxc://matrix.un-hack-bar.de/SXmvTOGHLXRsYDCBQncTBhGl"}
# The callback for when the client receives a CONNACK response from the server.
@ -85,8 +79,9 @@ def spaceapi():
def on_message(client, userdata, msg):
# print(msg.topic+" "+str(msg.payload))
print("Received message '" + str(msg.payload) + "' on topic '" + msg.topic + "' with QoS " + str(msg.qos))
print("current Status of space: " + str(space_obj["state"]["open"]))
if msg.topic == '/UHB/status/public':
if msg.payload == "0":
if (msg.payload == "0" and not space_obj["state"]["open"]):
# space_obj["state"] = {u'open': True,u'lastchange': time.time()}
space_obj["state"]["open"] = True
space_obj["state"]["lastchange"] = time.time()
@ -94,7 +89,8 @@ def on_message(client, userdata, msg):
with open("spaceapi.json", "w") as write_file:
json.dump(space_obj, write_file, sort_keys=True)
matrix_room.send_state_event("m.room.avatar", matrix_open_json)
elif msg.payload == "1":
matrix_room.send_text("Der Space ist jetzt offen.")
elif (msg.payload == "1" and space_obj["state"]["open"] ):
# space_obj["state"] = {b'open': False,u'lastchange': time.time()}
space_obj["state"]["open"] = False
space_obj["state"]["lastchange"] = time.time()
@ -102,6 +98,9 @@ def on_message(client, userdata, msg):
json.dump(space_obj, write_file, sort_keys=True)
print(json.dumps(space_obj, sort_keys=True, indent=4))
matrix_room.send_state_event("m.room.avatar", matrix_closed_json)
matrix_room.send_text("Der Space ist jetzt geschlossen.")
p = subprocess.Popen('/home/amdash/fritzPower/allOff.sh')
p.wait()
if msg.topic == '/UHB/label/create':
new_keys = int(msg.payload)
print("anzahl neu zu generierender label: " + str(new_keys))
@ -133,6 +132,7 @@ matrix_client = MatrixClient(matrix_server)
matrix_client.login_with_password_no_sync(matrix_bot_user, matrix_bot_passwd)
matrix_room = matrix_client.join_room(matrix_roomid)
matrix_room.send_state_event("m.room.avatar", matrix_unk_json)
matrix_room.send_text("Ich wurde neu gestartet... kein Plan ob der Raum gerade offen oder geschlossen ist.")
# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.