diff --git a/lastkey.txt b/lastkey.txt index aa01954..0261f1d 100644 --- a/lastkey.txt +++ b/lastkey.txt @@ -1 +1 @@ -0x0000 +0x0064 \ No newline at end of file diff --git a/logfile.log b/logfile.log new file mode 100644 index 0000000..fc0c9c1 --- /dev/null +++ b/logfile.log @@ -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 diff --git a/matrix_close.py b/matrix_close.py new file mode 100755 index 0000000..ac849b3 --- /dev/null +++ b/matrix_close.py @@ -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.") diff --git a/nohup.out b/nohup.out new file mode 100644 index 0000000..a8891ab --- /dev/null +++ b/nohup.out @@ -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 + 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 diff --git a/qr.png b/qr.png new file mode 100644 index 0000000..67b6534 Binary files /dev/null and b/qr.png differ diff --git a/qrcodes/0x0001.png b/qrcodes/0x0001.png new file mode 100644 index 0000000..42facca Binary files /dev/null and b/qrcodes/0x0001.png differ diff --git a/qrcodes/0x0002.png b/qrcodes/0x0002.png new file mode 100644 index 0000000..f841e96 Binary files /dev/null and b/qrcodes/0x0002.png differ diff --git a/qrcodes/0x0003.png b/qrcodes/0x0003.png new file mode 100644 index 0000000..1678e14 Binary files /dev/null and b/qrcodes/0x0003.png differ diff --git a/qrcodes/0x0004.png b/qrcodes/0x0004.png new file mode 100644 index 0000000..84b23c5 Binary files /dev/null and b/qrcodes/0x0004.png differ diff --git a/qrcodes/0x0005.png b/qrcodes/0x0005.png new file mode 100644 index 0000000..abd26a5 Binary files /dev/null and b/qrcodes/0x0005.png differ diff --git a/qrcodes/0x0006.png b/qrcodes/0x0006.png new file mode 100644 index 0000000..4f0deb0 Binary files /dev/null and b/qrcodes/0x0006.png differ diff --git a/qrcodes/0x0007.png b/qrcodes/0x0007.png new file mode 100644 index 0000000..672c302 Binary files /dev/null and b/qrcodes/0x0007.png differ diff --git a/qrcodes/0x0008.png b/qrcodes/0x0008.png new file mode 100644 index 0000000..f30305f Binary files /dev/null and b/qrcodes/0x0008.png differ diff --git a/qrcodes/0x0009.png b/qrcodes/0x0009.png new file mode 100644 index 0000000..3b65de6 Binary files /dev/null and b/qrcodes/0x0009.png differ diff --git a/qrcodes/0x000a.png b/qrcodes/0x000a.png new file mode 100644 index 0000000..8aff993 Binary files /dev/null and b/qrcodes/0x000a.png differ diff --git a/qrcodes/0x000b.png b/qrcodes/0x000b.png new file mode 100644 index 0000000..b150cae Binary files /dev/null and b/qrcodes/0x000b.png differ diff --git a/qrcodes/0x000c.png b/qrcodes/0x000c.png new file mode 100644 index 0000000..cd79f15 Binary files /dev/null and b/qrcodes/0x000c.png differ diff --git a/qrcodes/0x000d.png b/qrcodes/0x000d.png new file mode 100644 index 0000000..b13a2d4 Binary files /dev/null and b/qrcodes/0x000d.png differ diff --git a/qrcodes/0x000e.png b/qrcodes/0x000e.png new file mode 100644 index 0000000..21c85fb Binary files /dev/null and b/qrcodes/0x000e.png differ diff --git a/qrcodes/0x000f.png b/qrcodes/0x000f.png new file mode 100644 index 0000000..efc29cf Binary files /dev/null and b/qrcodes/0x000f.png differ diff --git a/qrcodes/0x0010.png b/qrcodes/0x0010.png new file mode 100644 index 0000000..70aa009 Binary files /dev/null and b/qrcodes/0x0010.png differ diff --git a/qrcodes/0x0011.png b/qrcodes/0x0011.png new file mode 100644 index 0000000..9d5f8ab Binary files /dev/null and b/qrcodes/0x0011.png differ diff --git a/qrcodes/0x0047.png b/qrcodes/0x0047.png new file mode 100644 index 0000000..6f0f3eb Binary files /dev/null and b/qrcodes/0x0047.png differ diff --git a/qrcodes/0x0048.png b/qrcodes/0x0048.png new file mode 100644 index 0000000..4ce1999 Binary files /dev/null and b/qrcodes/0x0048.png differ diff --git a/qrcodes/0x0049.png b/qrcodes/0x0049.png new file mode 100644 index 0000000..7ff44e2 Binary files /dev/null and b/qrcodes/0x0049.png differ diff --git a/qrcodes/0x004a.png b/qrcodes/0x004a.png new file mode 100644 index 0000000..d22c3e2 Binary files /dev/null and b/qrcodes/0x004a.png differ diff --git a/qrcodes/0x004b.png b/qrcodes/0x004b.png new file mode 100644 index 0000000..a7267f1 Binary files /dev/null and b/qrcodes/0x004b.png differ diff --git a/qrcodes/0x004c.png b/qrcodes/0x004c.png new file mode 100644 index 0000000..f9acaa0 Binary files /dev/null and b/qrcodes/0x004c.png differ diff --git a/qrcodes/0x004d.png b/qrcodes/0x004d.png new file mode 100644 index 0000000..23cc4ce Binary files /dev/null and b/qrcodes/0x004d.png differ diff --git a/qrcodes/0x004e.png b/qrcodes/0x004e.png new file mode 100644 index 0000000..5c1bd40 Binary files /dev/null and b/qrcodes/0x004e.png differ diff --git a/qrcodes/0x004f.png b/qrcodes/0x004f.png new file mode 100644 index 0000000..9bcbe45 Binary files /dev/null and b/qrcodes/0x004f.png differ diff --git a/qrcodes/0x0050.png b/qrcodes/0x0050.png new file mode 100644 index 0000000..4527942 Binary files /dev/null and b/qrcodes/0x0050.png differ diff --git a/qrcodes/0x0051.png b/qrcodes/0x0051.png new file mode 100644 index 0000000..07d67c2 Binary files /dev/null and b/qrcodes/0x0051.png differ diff --git a/qrcodes/0x0052.png b/qrcodes/0x0052.png new file mode 100644 index 0000000..b460f31 Binary files /dev/null and b/qrcodes/0x0052.png differ diff --git a/qrcodes/0x0053.png b/qrcodes/0x0053.png new file mode 100644 index 0000000..9d9ffda Binary files /dev/null and b/qrcodes/0x0053.png differ diff --git a/qrcodes/0x0054.png b/qrcodes/0x0054.png new file mode 100644 index 0000000..1cb7f90 Binary files /dev/null and b/qrcodes/0x0054.png differ diff --git a/qrcodes/0x0055.png b/qrcodes/0x0055.png new file mode 100644 index 0000000..dee1ecc Binary files /dev/null and b/qrcodes/0x0055.png differ diff --git a/qrcodes/0x0056.png b/qrcodes/0x0056.png new file mode 100644 index 0000000..6cdbf61 Binary files /dev/null and b/qrcodes/0x0056.png differ diff --git a/qrcodes/0x0057.png b/qrcodes/0x0057.png new file mode 100644 index 0000000..5f50fed Binary files /dev/null and b/qrcodes/0x0057.png differ diff --git a/qrcodes/0x0058.png b/qrcodes/0x0058.png new file mode 100644 index 0000000..9fc4272 Binary files /dev/null and b/qrcodes/0x0058.png differ diff --git a/qrcodes/0x0059.png b/qrcodes/0x0059.png new file mode 100644 index 0000000..190d24b Binary files /dev/null and b/qrcodes/0x0059.png differ diff --git a/qrcodes/0x005a.png b/qrcodes/0x005a.png new file mode 100644 index 0000000..0817e84 Binary files /dev/null and b/qrcodes/0x005a.png differ diff --git a/qrcodes/0x005b.png b/qrcodes/0x005b.png new file mode 100644 index 0000000..99e705e Binary files /dev/null and b/qrcodes/0x005b.png differ diff --git a/qrcodes/0x005c.png b/qrcodes/0x005c.png new file mode 100644 index 0000000..7789e48 Binary files /dev/null and b/qrcodes/0x005c.png differ diff --git a/qrcodes/0x005d.png b/qrcodes/0x005d.png new file mode 100644 index 0000000..abb0821 Binary files /dev/null and b/qrcodes/0x005d.png differ diff --git a/qrcodes/0x005e.png b/qrcodes/0x005e.png new file mode 100644 index 0000000..b108665 Binary files /dev/null and b/qrcodes/0x005e.png differ diff --git a/qrcodes/0x005f.png b/qrcodes/0x005f.png new file mode 100644 index 0000000..0ce0760 Binary files /dev/null and b/qrcodes/0x005f.png differ diff --git a/qrcodes/0x0060.png b/qrcodes/0x0060.png new file mode 100644 index 0000000..1d4f6da Binary files /dev/null and b/qrcodes/0x0060.png differ diff --git a/qrcodes/0x0061.png b/qrcodes/0x0061.png new file mode 100644 index 0000000..8315da1 Binary files /dev/null and b/qrcodes/0x0061.png differ diff --git a/qrcodes/0x0062.png b/qrcodes/0x0062.png new file mode 100644 index 0000000..29113f7 Binary files /dev/null and b/qrcodes/0x0062.png differ diff --git a/qrcodes/0x0063.png b/qrcodes/0x0063.png new file mode 100644 index 0000000..eebe25a Binary files /dev/null and b/qrcodes/0x0063.png differ diff --git a/qrcodes/0x0064.png b/qrcodes/0x0064.png new file mode 100644 index 0000000..d40e9b1 Binary files /dev/null and b/qrcodes/0x0064.png differ diff --git a/spaceapi.json b/spaceapi.json new file mode 100644 index 0000000..02255c7 --- /dev/null +++ b/spaceapi.json @@ -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"} \ No newline at end of file diff --git a/unhb-mqtt-20190817.py b/unhb-mqtt-20190817.py new file mode 100755 index 0000000..e3b3289 --- /dev/null +++ b/unhb-mqtt-20190817.py @@ -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() diff --git a/unhb-mqtt-dev.py b/unhb-mqtt-dev.py new file mode 100644 index 0000000..0a80add --- /dev/null +++ b/unhb-mqtt-dev.py @@ -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() diff --git a/unhb-mqtt.py b/unhb-mqtt.py index 0186a81..db67041 100755 --- a/unhb-mqtt.py +++ b/unhb-mqtt.py @@ -105,7 +105,7 @@ def on_message(client, userdata, msg): new_keys = int(msg.payload) 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) print("bisher generierte labels: " + str(last_label_key)) @@ -117,7 +117,7 @@ def on_message(client, userdata, msg): hexkey = padhexa(hex(i)) urldings = "https://inv.unhb.de/" + 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) diff --git a/unhb_mqtt_config.py b/unhb_mqtt_config.py new file mode 100644 index 0000000..6ca87fd --- /dev/null +++ b/unhb_mqtt_config.py @@ -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" + } + } + }""" diff --git a/unhb_mqtt_config.pyc b/unhb_mqtt_config.pyc new file mode 100644 index 0000000..d1f4e1a Binary files /dev/null and b/unhb_mqtt_config.pyc differ