-
Notifications
You must be signed in to change notification settings - Fork 0
/
siren.py
49 lines (36 loc) · 1.1 KB
/
siren.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
# -*- coding: utf-8 -*-
import json
import logging
import sys
import time
import web
from gpiozero import DigitalOutputDevice
from config import LEVEL
web.config.debug = False
urls = (
'/light', 'Light'
)
# supervisord -c /etc/supervisord.conf
# sudo supervisorctl -c /etc/supervisord.conf reload
class Light:
def POST(self):
web_data = '{}'
if web.data():
web_data = web.data().decode('utf-8')
data = json.loads(web_data)
if not 'duration' in data or not 'actor' in data:
return web.badrequest()
turn_on_the_light(data['duration'] or 1, data['actor'])
return web.nocontent()
def turn_on_the_light(duration, actor):
logging.info('New light request by {} for {} seconds'.format(actor, duration))
relay = DigitalOutputDevice(17)
relay.on()
time.sleep(float(duration))
relay.off()
relay.close()
if __name__ == "__main__":
log_format = '%(asctime)s - %(levelname)s - %(message)s'
logging.basicConfig(stream=sys.stdout, level=LEVEL, format=log_format)
app = web.application(urls, globals())
app.run()