unhb-mqtt/unhb-mqtt.py

142 lines
5.2 KiB
Python
Executable file

#!/usr/bin/python
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()
printers = conn.getPrinters()
# formatiert uns hex in der notation 0x0000
def padhexa(s):
return '0x' + s[2:].zfill(4)
def create_qrcode(newcode, hexid):
qrobj = pyqrcode.create(newcode)
with open('qr.png', 'wb') as f:
qrobj.png(f, scale=10)
bg = Image.open('bg.png')
img = Image.open('qr.png')
width, height = img.size
logo_size = 140
logo = Image.open('unhb.png')
xmin = ymin = int((width / 2) - (logo_size / 2))
xmax = ymax = int((width / 2) + (logo_size / 2))
logo = logo.resize((xmax - xmin, ymax - ymin))
img.paste(logo, (xmin, ymin, xmax, ymax))
fnt = ImageFont.truetype('font.ttf', 28)
d = ImageDraw.Draw(img)
d.text((width / 2 - 55, height - 40), hexid, font=fnt)
bg.paste(img, (205, 0, 615, 410))
outimage = "./qrcodes/" + hexid + ".png"
bg.save(outimage)
print("qrcode erfolgreich generiert: " + newcode)
# druckername hardcoded!
conn.printFile("gk420d", outimage, "Labelausdruck", {})
def get_lastkey():
with open("/home/pi/lastkey.txt", "r") as lastkeyinfile:
last_int = int(lastkeyinfile.read().rstrip(), 16)
return lastkey
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))
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
print("Connected with result code " + str(rc))
# Subscribing in on_connect() means that if we lose the connection and
# reconnect then subscriptions will be renewed.
client.subscribe("/UHB/#")
def spaceapi():
print("funktion spaceapi go!")
json.dumps(space_json)
print(y["api"])
write_file = open("spaceapi.json", "w")
json.dump(space_json, write_file)
print("file written")
# The callback for when a PUBLISH message is received from the server.
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" 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()
print(json.dumps(space_obj, sort_keys=True, indent=4))
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)
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()
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_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))
with open("lastkey.txt", "r") as infile:
last_label_key = int(infile.read().rstrip(), 16)
print("bisher generierte labels: " + str(last_label_key))
print("letzte gedruckte label id: " + padhexa(hex(last_label_key)))
nextkey = last_label_key + 1
print("naechste freie label id: " + padhexa(hex(nextkey)))
for i in range(nextkey, nextkey + new_keys):
hexkey = padhexa(hex(i))
urldings = "https://inv.unhb.de/" + hexkey
create_qrcode(urldings, hexkey)
with open("lastkey.txt", "w") as outfile:
outfile.write(hexkey)
# Prepare MQTT client
client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
client.connect("localhost", 1883, 60)
# Prepare Matrix Client
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.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.
client.loop_forever()