From 43650e2301052cd5a2e78fb4cdec78b56eea8f98 Mon Sep 17 00:00:00 2001 From: polyfloyd Date: Mon, 12 May 2025 20:37:08 +0200 Subject: [PATCH 1/5] Add test for schema validation --- server.py | 118 +++++++++++++++++++++++++++---------------------- server_test.py | 17 +++++++ 2 files changed, 81 insertions(+), 54 deletions(-) create mode 100644 server_test.py diff --git a/server.py b/server.py index e910291..c47f654 100755 --- a/server.py +++ b/server.py @@ -6,74 +6,84 @@ import tornado.ioloop import tornado.web -PORT = 8888 -BASE_URL = 'https://bitlair.nl/state' -MQTT_HOST = 'bitlair.nl' +PORT = 8888 +BASE_URL = "https://bitlair.nl/state" +MQTT_HOST = "bitlair.nl" -current_state = False +current_state = False current_state_change = time.time() + +def state(): + global current_state, current_state_change + return { + "api": "0.13", + "space": "Bitlair", + "logo": BASE_URL + "/logo.png", + "url": "https://bitlair.nl/", + "contact": { + "phone": "+31337114666", + "irc": "irc://irc.smurfnet.ch/bitlair", + "twitter": "@bitlair", + "email": "info@bitlair.nl", + "ml": "general@list.bitlair.nl", + }, + "spacefed": { + "spacenet": True, + "spacesaml": True, + "spacephone": False, + }, + "location": { + "address": "Nijverheidsweg-Noord 77, 3812 PK Amersfoort, The Netherlands", + "lat": 52.1697399, + "lon": 5.3561364, + }, + "state": { + "open": current_state, + "lastchange": int(current_state_change), + "icon": { + "open": BASE_URL + "/open.png", + "closed": BASE_URL + "/closed.png", + }, + "mqtt": { + "host": "bitlair.nl", + "port": 1883, + "tls": False, + "topic": "bitlair/state", + "closed": "closed", + "open": "open", + }, + }, + "issue_report_channels": ["twitter"], + } + + class StatejsonHandler(tornado.web.RequestHandler): def get(self): - global current_state, current_state_change - state = { - 'api': '0.13', - 'space': 'Bitlair', - 'logo': BASE_URL+'/logo.png', - 'url': 'https://bitlair.nl/', - 'contact': { - 'phone': '+31337114666', - 'irc': 'irc://irc.smurfnet.ch/bitlair', - 'twitter': '@bitlair', - 'email': 'info@bitlair.nl', - 'ml': 'general@list.bitlair.nl', - }, - 'spacefed': { - 'spacenet': True, - 'spacesaml': True, - 'spacephone': False, - }, - 'location': { - 'address': 'Nijverheidsweg-Noord 77, 3812 PK Amersfoort, The Netherlands', - 'lat': 52.1697399, - 'lon': 5.3561364, - }, - 'state': { - 'open': current_state, - 'lastchange': int(current_state_change), - 'icon': { - 'open': BASE_URL+'/open.png', - 'closed': BASE_URL+'/closed.png', - }, - 'mqtt': { - 'host': 'bitlair.nl', - 'port': 1883, - 'tls': False, - 'topic': 'bitlair/state', - 'closed': 'closed', - 'open': 'open' - }, - }, - 'issue_report_channels': [ 'twitter' ], - } - self.write(state) + self.write(state()) + def make_app(): - return tornado.web.Application([ - (r'/statejson', StatejsonHandler), - ]) + return tornado.web.Application( + [ + (r"/statejson", StatejsonHandler), + ] + ) + def on_message(client, userdata, message): global current_state, current_state_change - if message.topic == 'bitlair/state': - current_state = message.payload == b'open' + if message.topic == "bitlair/state": + current_state = message.payload == b"open" current_state_change = time.time() -def on_connect(client, userdata, flags, rc): - client.subscribe('bitlair/state', qos=2) -if __name__ == '__main__': +def on_connect(client, userdata, flags, rc): + client.subscribe("bitlair/state", qos=2) + + +if __name__ == "__main__": mqttc = mqtt.Client() mqttc.on_message = on_message mqttc.on_connect = on_connect diff --git a/server_test.py b/server_test.py new file mode 100644 index 0000000..fd800d0 --- /dev/null +++ b/server_test.py @@ -0,0 +1,17 @@ +import requests +from jsonschema import validate + + +import server + + +def test_schema_compliance(): + resp = requests.get( + "https://raw.githubusercontent.com/SpaceApi/schema/refs/heads/master/13.json" + ) + resp.raise_for_status() + schema = resp.json() + + spaceapi = server.state() + + validate(instance=spaceapi, schema=schema) From 4a7281aa8d2d36c7a9266a763bdbc12344f8dca3 Mon Sep 17 00:00:00 2001 From: polyfloyd Date: Mon, 12 May 2025 20:38:07 +0200 Subject: [PATCH 2/5] Update gitignore --- .gitignore | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 57312c6..bee8a64 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1 @@ -*! -*.swo -*.swp +__pycache__ From 0629aeef1e3aed12ddc97d72cf887c5a10fe7844 Mon Sep 17 00:00:00 2001 From: polyfloyd Date: Mon, 12 May 2025 20:45:11 +0200 Subject: [PATCH 3/5] Update contact email --- server.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.py b/server.py index c47f654..c087474 100755 --- a/server.py +++ b/server.py @@ -26,7 +26,7 @@ def state(): "phone": "+31337114666", "irc": "irc://irc.smurfnet.ch/bitlair", "twitter": "@bitlair", - "email": "info@bitlair.nl", + "email": "bestuur@bitlair.nl", "ml": "general@list.bitlair.nl", }, "spacefed": { From 14dfb79cfb16d5554e14377cf7306988c4e6ea54 Mon Sep 17 00:00:00 2001 From: polyfloyd Date: Mon, 12 May 2025 20:45:24 +0200 Subject: [PATCH 4/5] Remove phone --- server.py | 1 - 1 file changed, 1 deletion(-) diff --git a/server.py b/server.py index c087474..26da91f 100755 --- a/server.py +++ b/server.py @@ -23,7 +23,6 @@ def state(): "logo": BASE_URL + "/logo.png", "url": "https://bitlair.nl/", "contact": { - "phone": "+31337114666", "irc": "irc://irc.smurfnet.ch/bitlair", "twitter": "@bitlair", "email": "bestuur@bitlair.nl", From 305f0fa99f587d069281b3aa6d864ef3dc21490b Mon Sep 17 00:00:00 2001 From: polyfloyd Date: Mon, 12 May 2025 21:00:07 +0200 Subject: [PATCH 5/5] Update to SpaceAPI schema 15 --- server.py | 38 +++++++++++++++++++++++--------------- server_test.py | 3 +-- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/server.py b/server.py index 26da91f..f080c6c 100755 --- a/server.py +++ b/server.py @@ -1,11 +1,11 @@ #!/usr/bin/env python3 -import paho.mqtt.client as mqtt import time + +import paho.mqtt.client as mqtt import tornado.ioloop import tornado.web - PORT = 8888 BASE_URL = "https://bitlair.nl/state" MQTT_HOST = "bitlair.nl" @@ -18,25 +18,21 @@ current_state_change = time.time() def state(): global current_state, current_state_change return { - "api": "0.13", + "api_compatibility": ["15"], "space": "Bitlair", "logo": BASE_URL + "/logo.png", "url": "https://bitlair.nl/", - "contact": { - "irc": "irc://irc.smurfnet.ch/bitlair", - "twitter": "@bitlair", - "email": "bestuur@bitlair.nl", - "ml": "general@list.bitlair.nl", - }, - "spacefed": { - "spacenet": True, - "spacesaml": True, - "spacephone": False, - }, "location": { "address": "Nijverheidsweg-Noord 77, 3812 PK Amersfoort, The Netherlands", "lat": 52.1697399, "lon": 5.3561364, + "timezone": "Europe/Amsterdam", + "country_code": "NL", + "hint": "Ingang aan de oost-zijde van het pand, blauwe deur", + }, + "spacefed": { + "spacenet": True, + "spacesaml": True, }, "state": { "open": current_state, @@ -45,6 +41,7 @@ def state(): "open": BASE_URL + "/open.png", "closed": BASE_URL + "/closed.png", }, + # mqtt is out-of-spec "mqtt": { "host": "bitlair.nl", "port": 1883, @@ -54,7 +51,18 @@ def state(): "open": "open", }, }, - "issue_report_channels": ["twitter"], + "contact": { + "irc": "irc://irc.smurfnet.ch/bitlair", + "mastodon": "@bitlair@hsnl.social", + "email": "bestuur@bitlair.nl", + "ml": "general@list.bitlair.nl", + }, + "feeds": { + "calendar": { + "type": "ical", + "url": "https://bitlair.nl/events.ics", + }, + }, } diff --git a/server_test.py b/server_test.py index fd800d0..b55c2cc 100644 --- a/server_test.py +++ b/server_test.py @@ -1,13 +1,12 @@ import requests from jsonschema import validate - import server def test_schema_compliance(): resp = requests.get( - "https://raw.githubusercontent.com/SpaceApi/schema/refs/heads/master/13.json" + "https://raw.githubusercontent.com/SpaceApi/schema/refs/heads/master/15.json" ) resp.raise_for_status() schema = resp.json()