-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
executable file
·86 lines (74 loc) · 2.47 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#!/usr/bin/env python3
import paho.mqtt.client as mqtt
import time
import tornado.ioloop
import tornado.web
PORT = 8888
BASE_URL = 'https://bitlair.nl/state'
MQTT_HOST = 'bitlair.nl'
current_state = False
current_state_change = time.time()
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)
def make_app():
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'
current_state_change = time.time()
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
mqttc.connect(MQTT_HOST)
mqttc.loop_start()
app = make_app()
app.listen(8888)
tornado.ioloop.IOLoop.current().start()