-
Notifications
You must be signed in to change notification settings - Fork 0
/
localwood.py
136 lines (119 loc) · 4.74 KB
/
localwood.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import os
import web
import RPi.GPIO as GPIO
import time
import socket_setup
import logging
logging.basicConfig(format='%(levelname)s: %(message)s', level=logging.INFO)
auth_token = os.environ.get('AUTH_TOKEN')
if not auth_token:
logging.warn("Proceeding without authentication")
if not any(os.environ.get(key) for key in ['SOCKET_1_LABEL', 'SOCKET_2_LABEL', 'SOCKET_3_LABEL', 'SOCKET_4_LABEL']):
raise ValueError("No power sockets enabled, missing envs e.g. SOCKET_1_LABEL")
urls = (
'/', 'homepage',
'/sockets', 'sockets'
)
render = web.template.render('templates/')
app = web.application(urls, globals())
def authenticate_user(params):
if auth_token:
if not hasattr(params, 'token'):
raise web.badrequest('Missing URL param "token"')
if params.token != auth_token:
raise web.badrequest('Invalid auth token')
class homepage:
def GET(self):
authenticate_user(web.input())
return render.homepage(
os.environ.get('PAGE_TITLE', "Localwood Socket Control"),
os.environ.get('PAGE_HEADING', "Socket Control"),
os.environ.get('SOCKET_1_LABEL'),
os.environ.get('SOCKET_2_LABEL'),
os.environ.get('SOCKET_3_LABEL'),
os.environ.get('SOCKET_4_LABEL')
)
class sockets:
def POST(self):
params = web.input()
authenticate_user(params)
if not hasattr(params, 'socket'):
raise web.badrequest('Missing URL param "socket"')
if not hasattr(params, 'state'):
raise web.badrequest('Missing URL param "state"')
if params.socket == 'all' and params.state == 'on':
# See: https://energenie4u.co.uk/res/pdfs/ENER314%20UM.pdf
logging.info("Sending code 1011 all sockets on")
GPIO.output (13, True)
GPIO.output (16, False)
GPIO.output (15, True)
GPIO.output (11, True)
elif params.socket == 'all' and params.state == 'off':
logging.info("Sending code 0011 all sockets off")
GPIO.output (13, False)
GPIO.output (16, False)
GPIO.output (15, True)
GPIO.output (11, True)
elif params.socket == '1' and params.state == 'on':
logging.info("Sending code 1111 socket 1 on")
GPIO.output (13, True)
GPIO.output (16, True)
GPIO.output (15, True)
GPIO.output (11, True)
elif params.socket == '1' and params.state == 'off':
logging.info("Sending code 0111 socket 1 off")
GPIO.output (13, False)
GPIO.output (16, True)
GPIO.output (15, True)
GPIO.output (11, True)
elif params.socket == '2' and params.state == 'on':
logging.info("Sending code 1110 socket 2 on")
GPIO.output (13, True)
GPIO.output (16, True)
GPIO.output (15, True)
GPIO.output (11, False)
elif params.socket == '2' and params.state == 'off':
logging.info("Sending code 0110 socket 2 off")
GPIO.output (13, False)
GPIO.output (16, True)
GPIO.output (15, True)
GPIO.output (11, False)
elif params.socket == '3' and params.state == 'on':
logging.info("Sending code 1101 socket 3 on")
GPIO.output (13, True)
GPIO.output (16, True)
GPIO.output (15, False)
GPIO.output (11, True)
elif params.socket == '3' and params.state == 'off':
logging.info("Sending code 0101 socket 3 off")
GPIO.output (13, False)
GPIO.output (16, True)
GPIO.output (15, False)
GPIO.output (11, True)
elif params.socket == '4' and params.state == 'on':
logging.info("Sending code 1100 socket 4 on")
GPIO.output (13, True)
GPIO.output (16, True)
GPIO.output (15, False)
GPIO.output (11, False)
elif params.socket == '4' and params.state == 'off':
logging.info("Sending code 0100 socket 4 off")
GPIO.output (13, False)
GPIO.output (16, True)
GPIO.output (15, False)
GPIO.output (11, False)
else:
logging.info('Unknown combo socket=%s state=%s', params.socket, params.state)
raise web.badrequest('Socket or state invalid (expects socket: 1-4 state: on/off)')
# let it settle, encoder requires this
time.sleep(0.1)
# Enable the modulator
GPIO.output (22, True)
# keep enabled for a period
time.sleep(1)
# Disable the modulator
GPIO.output (22, False)
return 'Done'
if __name__ == "__main__":
socket_setup.setup()
app.run()