Compare commits
No commits in common. "305f0fa99f587d069281b3aa6d864ef3dc21490b" and "9cc8b0fa402a78323c26914e173d86da75fd4a93" have entirely different histories.
305f0fa99f
...
9cc8b0fa40
3 changed files with 58 additions and 89 deletions
4
.gitignore
vendored
4
.gitignore
vendored
|
@ -1 +1,3 @@
|
||||||
__pycache__
|
*!
|
||||||
|
*.swo
|
||||||
|
*.swp
|
||||||
|
|
127
server.py
127
server.py
|
@ -1,96 +1,79 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
import time
|
|
||||||
|
|
||||||
import paho.mqtt.client as mqtt
|
import paho.mqtt.client as mqtt
|
||||||
|
import time
|
||||||
import tornado.ioloop
|
import tornado.ioloop
|
||||||
import tornado.web
|
import tornado.web
|
||||||
|
|
||||||
PORT = 8888
|
|
||||||
BASE_URL = "https://bitlair.nl/state"
|
PORT = 8888
|
||||||
MQTT_HOST = "bitlair.nl"
|
BASE_URL = 'https://bitlair.nl/state'
|
||||||
|
MQTT_HOST = 'bitlair.nl'
|
||||||
|
|
||||||
|
|
||||||
current_state = False
|
current_state = False
|
||||||
current_state_change = time.time()
|
current_state_change = time.time()
|
||||||
|
|
||||||
|
|
||||||
def state():
|
|
||||||
global current_state, current_state_change
|
|
||||||
return {
|
|
||||||
"api_compatibility": ["15"],
|
|
||||||
"space": "Bitlair",
|
|
||||||
"logo": BASE_URL + "/logo.png",
|
|
||||||
"url": "https://bitlair.nl/",
|
|
||||||
"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,
|
|
||||||
"lastchange": int(current_state_change),
|
|
||||||
"icon": {
|
|
||||||
"open": BASE_URL + "/open.png",
|
|
||||||
"closed": BASE_URL + "/closed.png",
|
|
||||||
},
|
|
||||||
# mqtt is out-of-spec
|
|
||||||
"mqtt": {
|
|
||||||
"host": "bitlair.nl",
|
|
||||||
"port": 1883,
|
|
||||||
"tls": False,
|
|
||||||
"topic": "bitlair/state",
|
|
||||||
"closed": "closed",
|
|
||||||
"open": "open",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
"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",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
class StatejsonHandler(tornado.web.RequestHandler):
|
class StatejsonHandler(tornado.web.RequestHandler):
|
||||||
def get(self):
|
def get(self):
|
||||||
self.write(state())
|
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)
|
||||||
|
|
||||||
def make_app():
|
def make_app():
|
||||||
return tornado.web.Application(
|
return tornado.web.Application([
|
||||||
[
|
(r'/statejson', StatejsonHandler),
|
||||||
(r"/statejson", StatejsonHandler),
|
])
|
||||||
]
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
def on_message(client, userdata, message):
|
def on_message(client, userdata, message):
|
||||||
global current_state, current_state_change
|
global current_state, current_state_change
|
||||||
if message.topic == "bitlair/state":
|
if message.topic == 'bitlair/state':
|
||||||
current_state = message.payload == b"open"
|
current_state = message.payload == b'open'
|
||||||
current_state_change = time.time()
|
current_state_change = time.time()
|
||||||
|
|
||||||
|
|
||||||
def on_connect(client, userdata, flags, rc):
|
def on_connect(client, userdata, flags, rc):
|
||||||
client.subscribe("bitlair/state", qos=2)
|
client.subscribe('bitlair/state', qos=2)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
if __name__ == "__main__":
|
|
||||||
mqttc = mqtt.Client()
|
mqttc = mqtt.Client()
|
||||||
mqttc.on_message = on_message
|
mqttc.on_message = on_message
|
||||||
mqttc.on_connect = on_connect
|
mqttc.on_connect = on_connect
|
||||||
|
|
|
@ -1,16 +0,0 @@
|
||||||
import requests
|
|
||||||
from jsonschema import validate
|
|
||||||
|
|
||||||
import server
|
|
||||||
|
|
||||||
|
|
||||||
def test_schema_compliance():
|
|
||||||
resp = requests.get(
|
|
||||||
"https://raw.githubusercontent.com/SpaceApi/schema/refs/heads/master/15.json"
|
|
||||||
)
|
|
||||||
resp.raise_for_status()
|
|
||||||
schema = resp.json()
|
|
||||||
|
|
||||||
spaceapi = server.state()
|
|
||||||
|
|
||||||
validate(instance=spaceapi, schema=schema)
|
|
Loading…
Add table
Add a link
Reference in a new issue