first commit

This commit is contained in:
Stefan H. 2020-06-20 21:17:02 +02:00
parent 027c8ba063
commit 7262fab9de
58 changed files with 594 additions and 3 deletions

View file

@ -1 +1 @@
0x0000 0x0064

83
logfile.log Normal file
View file

@ -0,0 +1,83 @@
04.10.2019 21:59:02: Connected with result code 0
04.10.2019 22:07:22: Connected with result code 0
04.10.2019 22:07:40: Connected with result code 0
04.10.2019 22:07:42: Received message '1' on topic '/UHB/status/public' with QoS 0
04.10.2019 22:07:42: current Status of space: None
04.10.2019 22:07:47: Received message '1' on topic '/UHB/status/public' with QoS 0
04.10.2019 22:07:48: current Status of space: None
04.10.2019 22:07:51: Received message '0' on topic '/UHB/status/public' with QoS 0
04.10.2019 22:07:51: current Status of space: None
04.10.2019 22:07:51: {
"api": "0.13",
"contact": {
"email": "info@un-hack-bar.de",
"irc": "irc://irc.hackint.net/c3un",
"ml": "discuss@unhb.de",
"twitter": "@un_hack_bar"
},
"ext_ccc": "chaostreff",
"feeds": {
"calendar": {
"type": "ical",
"url": "https://www.un-hack-bar.de/events.ics"
}
},
"issue_report_channels": [
"email"
],
"location": {
"address": "Morgenstrasse 6,59423 Unna, Germany",
"lat": 51.53575,
"lon": 7.69172
},
"logo": "https://www.un-hack-bar.de/wp-content/uploads/2018/06/unhb_klein.png",
"space": "UN-Hack-Bar",
"state": {
"icon": {
"closed": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/xcmwcXGdQqlgkcKxUROTndHL",
"open": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/OXoHKOrWRStZkRDHMdCPyidA"
},
"lastchange": 1570219671.006862,
"open": true
},
"url": "https://un-hack-bar.de"
}
04.10.2019 22:07:56: Received message '1' on topic '/UHB/status/public' with QoS 0
04.10.2019 22:07:56: current Status of space: True
04.10.2019 22:07:56: {
"api": "0.13",
"contact": {
"email": "info@un-hack-bar.de",
"irc": "irc://irc.hackint.net/c3un",
"ml": "discuss@unhb.de",
"twitter": "@un_hack_bar"
},
"ext_ccc": "chaostreff",
"feeds": {
"calendar": {
"type": "ical",
"url": "https://www.un-hack-bar.de/events.ics"
}
},
"issue_report_channels": [
"email"
],
"location": {
"address": "Morgenstrasse 6,59423 Unna, Germany",
"lat": 51.53575,
"lon": 7.69172
},
"logo": "https://www.un-hack-bar.de/wp-content/uploads/2018/06/unhb_klein.png",
"space": "UN-Hack-Bar",
"state": {
"icon": {
"closed": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/xcmwcXGdQqlgkcKxUROTndHL",
"open": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/OXoHKOrWRStZkRDHMdCPyidA"
},
"lastchange": 1570219676.791465,
"open": false
},
"url": "https://un-hack-bar.de"
}
04.10.2019 22:08:17: Received message '1' on topic '/UHB/status/public' with QoS 0
04.10.2019 22:08:17: current Status of space: False

13
matrix_close.py Executable file
View file

@ -0,0 +1,13 @@
#!/usr/bin/python
from matrix_client.client import MatrixClient
from unhb_mqtt_config import *
# 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_closed_json)
matrix_room.send_text("Der Space ist jetzt geschlossen.")
# matrix_room.send_state_event("m.room.avatar", matrix_open_json)
# matrix_room.send_text("Der Space ist jetzt offen.")

122
nohup.out Normal file
View file

@ -0,0 +1,122 @@
{
"api": "0.13",
"contact": {
"email": "info@un-hack-bar.de",
"irc": "irc://irc.hackint.net/c3un",
"ml": "discuss@unhb.de",
"twitter": "@un_hack_bar"
},
"ext_ccc": "chaostreff",
"feeds": {
"calendar": {
"type": "ical",
"url": "https://www.un-hack-bar.de/events.ics"
}
},
"issue_report_channels": [
"email"
],
"location": {
"address": "Morgenstrasse 6,59423 Unna, Germany",
"lat": 51.53575,
"lon": 7.69172
},
"logo": "https://www.un-hack-bar.de/wp-content/uploads/2018/06/unhb_klein.png",
"space": "UN-Hack-Bar",
"state": {
"icon": {
"closed": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/xcmwcXGdQqlgkcKxUROTndHL",
"open": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/OXoHKOrWRStZkRDHMdCPyidA"
},
"lastchange": 23,
"open": null
},
"url": "https://un-hack-bar.de"
}
Connected with result code 0
Received message '1' on topic '/UHB/status/public' with QoS 0
current Status of space: None
Received message '0' on topic '/UHB/status/public' with QoS 0
current Status of space: None
{
"api": "0.13",
"contact": {
"email": "info@un-hack-bar.de",
"irc": "irc://irc.hackint.net/c3un",
"ml": "discuss@unhb.de",
"twitter": "@un_hack_bar"
},
"ext_ccc": "chaostreff",
"feeds": {
"calendar": {
"type": "ical",
"url": "https://www.un-hack-bar.de/events.ics"
}
},
"issue_report_channels": [
"email"
],
"location": {
"address": "Morgenstrasse 6,59423 Unna, Germany",
"lat": 51.53575,
"lon": 7.69172
},
"logo": "https://www.un-hack-bar.de/wp-content/uploads/2018/06/unhb_klein.png",
"space": "UN-Hack-Bar",
"state": {
"icon": {
"closed": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/xcmwcXGdQqlgkcKxUROTndHL",
"open": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/OXoHKOrWRStZkRDHMdCPyidA"
},
"lastchange": 1570218206.001311,
"open": true
},
"url": "https://un-hack-bar.de"
}
Received message '1' on topic '/UHB/status/public' with QoS 0
current Status of space: True
{
"api": "0.13",
"contact": {
"email": "info@un-hack-bar.de",
"irc": "irc://irc.hackint.net/c3un",
"ml": "discuss@unhb.de",
"twitter": "@un_hack_bar"
},
"ext_ccc": "chaostreff",
"feeds": {
"calendar": {
"type": "ical",
"url": "https://www.un-hack-bar.de/events.ics"
}
},
"issue_report_channels": [
"email"
],
"location": {
"address": "Morgenstrasse 6,59423 Unna, Germany",
"lat": 51.53575,
"lon": 7.69172
},
"logo": "https://www.un-hack-bar.de/wp-content/uploads/2018/06/unhb_klein.png",
"space": "UN-Hack-Bar",
"state": {
"icon": {
"closed": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/xcmwcXGdQqlgkcKxUROTndHL",
"open": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/OXoHKOrWRStZkRDHMdCPyidA"
},
"lastchange": 1570218210.332163,
"open": false
},
"url": "https://un-hack-bar.de"
}
Received message '1' on topic '/UHB/status/public' with QoS 0
current Status of space: False
Traceback (most recent call last):
File "/home/pi/unhb-mqtt/unhb-mqtt.py", line 141, in <module>
client.loop_forever()
File "/home/pi/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1578, in loop_forever
rc = self.loop(timeout, max_packets)
File "/home/pi/.local/lib/python2.7/site-packages/paho/mqtt/client.py", line 1057, in loop
socklist = select.select(rlist, wlist, [], timeout)
KeyboardInterrupt

BIN
qr.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 592 B

BIN
qrcodes/0x0001.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
qrcodes/0x0002.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
qrcodes/0x0003.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0004.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0005.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
qrcodes/0x0006.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
qrcodes/0x0007.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
qrcodes/0x0008.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0009.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x000a.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x000b.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x000c.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x000d.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x000e.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x000f.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.3 KiB

BIN
qrcodes/0x0010.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0011.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0047.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0048.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0049.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x004a.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x004b.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x004c.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x004d.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x004e.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x004f.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0050.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0051.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0052.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0053.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0054.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0055.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0056.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0057.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0058.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0059.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x005a.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x005b.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x005c.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x005d.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x005e.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x005f.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0060.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0061.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0062.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0063.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

BIN
qrcodes/0x0064.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.4 KiB

1
spaceapi.json Normal file
View file

@ -0,0 +1 @@
{"api": "0.13", "contact": {"email": "info@un-hack-bar.de", "irc": "irc://irc.hackint.net/c3un", "ml": "discuss@unhb.de", "twitter": "@un_hack_bar"}, "ext_ccc": "chaostreff", "feeds": {"calendar": {"type": "ical", "url": "https://www.un-hack-bar.de/events.ics"}}, "issue_report_channels": ["email"], "location": {"address": "Morgenstrasse 6,59423 Unna, Germany", "lat": 51.53575, "lon": 7.69172}, "logo": "https://www.un-hack-bar.de/wp-content/uploads/2018/06/unhb_klein.png", "space": "UN-Hack-Bar", "state": {"icon": {"closed": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/xcmwcXGdQqlgkcKxUROTndHL", "open": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/OXoHKOrWRStZkRDHMdCPyidA"}, "lastchange": 23, "open": null}, "url": "https://un-hack-bar.de"}

150
unhb-mqtt-20190817.py Executable file
View file

@ -0,0 +1,150 @@
#!/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
# 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
# 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/OXoHKOrWRStZkRDHMdCPyidA", "closed": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/xcmwcXGdQqlgkcKxUROTndHL" } } }"""
space_obj = json.loads(space_json)
# Matrix-Bot definitions
matrix_bot_user = "DaneelOlivaw" # Bot's username
matrix_bot_passwd = "StefanTest123" # 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"}
matrix_open_json = {"url": "mxc://matrix.un-hack-bar.de/OXoHKOrWRStZkRDHMdCPyidA"}
matrix_closed_json = {"url": "mxc://matrix.un-hack-bar.de/xcmwcXGdQqlgkcKxUROTndHL"}
matrix_unk_json = {"url": "mxc://matrix.un-hack-bar.de/BpBpjtypqJcBawnmpkAOZHrp"}
# 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))
if msg.topic == '/UHB/status/public':
if msg.payload == "0":
# 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":
# 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()

172
unhb-mqtt-dev.py Normal file
View file

@ -0,0 +1,172 @@
#!/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 *
import sys
# path and name of the log file
logfile = '/home/pi/unhb-mqtt/logfile.log'
# function to save log messages to specified log file
def log(msg):
# open the specified log file
file = open(logfile,"a")
# write log message with timestamp to log file
file.write("%s: %s\n" % (time.strftime("%d.%m.%Y %H:%M:%S"), msg))
# close log file
file.close
# 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))
log("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!")
log("funktion spaceapi go!")
json.dumps(space_json)
log(json.dumps(space_json))
print(y["api"])
log(y["api"])
write_file = open("spaceapi.json", "w")
json.dump(space_json, write_file)
print("spaceapi file written")
log("spaceapi 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))
log("Received message '" + str(msg.payload) + "' on topic '" + msg.topic + "' with QoS " + str(msg.qos))
print("current Status of space: " + str(space_obj["state"]["open"]))
log("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))
log((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))
log(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))
log("anzahl neu zu generierender label: " + str(new_keys))
with open("/home/pi/unhb-mqtt/lastkey.txt", "r") as infile:
last_label_key = int(infile.read().rstrip(), 16)
print("bisher generierte labels: " + str(last_label_key))
log("bisher generierte labels: " + str(last_label_key))
print("letzte gedruckte label id: " + padhexa(hex(last_label_key)))
log("letzte gedruckte label id: " + padhexa(hex(last_label_key)))
nextkey = last_label_key + 1
print("naechste freie label id: " + padhexa(hex(nextkey)))
log("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("/home/pi/unhb-mqtt/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()

View file

@ -105,7 +105,7 @@ def on_message(client, userdata, msg):
new_keys = int(msg.payload) new_keys = int(msg.payload)
print("anzahl neu zu generierender label: " + str(new_keys)) print("anzahl neu zu generierender label: " + str(new_keys))
with open("lastkey.txt", "r") as infile: with open("/home/pi/unhb-mqtt/lastkey.txt", "r") as infile:
last_label_key = int(infile.read().rstrip(), 16) last_label_key = int(infile.read().rstrip(), 16)
print("bisher generierte labels: " + str(last_label_key)) print("bisher generierte labels: " + str(last_label_key))
@ -117,7 +117,7 @@ def on_message(client, userdata, msg):
hexkey = padhexa(hex(i)) hexkey = padhexa(hex(i))
urldings = "https://inv.unhb.de/" + hexkey urldings = "https://inv.unhb.de/" + hexkey
create_qrcode(urldings, hexkey) create_qrcode(urldings, hexkey)
with open("lastkey.txt", "w") as outfile: with open("/home/pi/unhb-mqtt/lastkey.txt", "w") as outfile:
outfile.write(hexkey) outfile.write(hexkey)

50
unhb_mqtt_config.py Normal file
View file

@ -0,0 +1,50 @@
matrix_bot_user = "DaneelOlivaw" # Bot's username
matrix_bot_passwd = "StefanTest123" # 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"}
matrix_open_json = {"url": "mxc://matrix.un-hack-bar.de/OXoHKOrWRStZkRDHMdCPyidA"}
matrix_closed_json = {"url": "mxc://matrix.un-hack-bar.de/xcmwcXGdQqlgkcKxUROTndHL"}
matrix_unk_json = {"url": "mxc://matrix.un-hack-bar.de/BpBpjtypqJcBawnmpkAOZHrp"}
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": "discuss@unhb.de",
"twitter": "@un_hack_bar"},
"feeds":
{"calendar":
{"type":"ical",
"url":"https://www.un-hack-bar.de/events.ics"}
},
"issue_report_channels":["email"],
"ext_ccc": "chaostreff",
"state":
{ "open": null,
"lastchange": 23,
"icon":
{ "open": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/OXoHKOrWRStZkRDHMdCPyidA",
"closed": "https://matrix.un-hack-bar.de/_matrix/media/v1/download/matrix.un-hack-bar.de/xcmwcXGdQqlgkcKxUROTndHL"
}
}
}"""

BIN
unhb_mqtt_config.pyc Normal file

Binary file not shown.