-
Notifications
You must be signed in to change notification settings - Fork 0
/
pidom.py
executable file
·134 lines (106 loc) · 3.17 KB
/
pidom.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
from flask import Flask
from flask_restplus import Resource, Api, reqparse
from flask import request, Response, render_template
from functools import wraps
import gpio
import lightstate
import time
app = Flask(__name__, static_url_path='')
app.config.from_envvar('SETTINGS')
api = Api(app, default="domotica", doc='/doc')
from werkzeug.contrib.fixers import ProxyFix
app.wsgi_app = ProxyFix(app.wsgi_app)
output = gpio.Output()
lightState = lightstate.LightState(app.config['REDIS_HOST'], app.config['REDIS_PORT'], app.config['REDIS_DB'], app.config['REDIS_PASSWORD'])
parser = reqparse.RequestParser()
parser.add_argument('action', type=str, default='up', choices=('up', 'down'))
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == app.config['USER'] and password == app.config['PASSWORD']
def authenticate():
"""Sends a 401 response that enables basic auth"""
return Response(
'Could not verify your access level for that URL.\n'
'You have to login with proper credentials', 401,
{'WWW-Authenticate': 'Basic realm="Login Required"'})
def requires_auth(f):
@wraps(f)
def decorated(*args, **kwargs):
auth = request.authorization
if not auth or not check_auth(auth.username, auth.password):
return authenticate()
return f(*args, **kwargs)
return decorated
### END Basic Auth
@requires_auth
@app.route('/index')
@app.route('/')
def index():
return render_template('index.html')
@api.route('/screen')
class Screen(Resource):
@requires_auth
@api.expect(parser)
def get(self):
args = parser.parse_args()
print args
if args['action'] == "down":
output.pulse(1)
elif args['action'] == 'up':
output.pulse(2)
@api.route('/garage')
class Garage(Resource):
@requires_auth
def get(self):
output.pulse(0,1)
@api.route('/alloff')
class Alloff(Resource):
@requires_auth
def get(self):
output.pulse(3)
@api.route('/outside')
class Outside(Resource):
@requires_auth
def get(self):
output.pulse(4)
lightState.toggle(0)
@api.route('/stairs')
class Stairs(Resource):
@requires_auth
def get(self):
output.pulse(5)
lightState.toggle(1)
@api.route('/frontdoorgroupoff')
class FrontdoorGroupOff(Resource):
@requires_auth
def get(self):
output.pulse(6)
lightState.setState([False, False])
@api.route('/frontdoorgroupon')
class FrontdoorGroupOn(Resource):
@requires_auth
def get(self):
output.pulse(7)
lightState.setState([True, True])
@api.route('/frontdoorgroupstate')
class FrontdoorGroupState(Resource):
@requires_auth
def get(self):
return lightState.getState()
@requires_auth
def post(self):
state = request.get_json()
lightState.setState(state)
# sync: groupoff
output.pulse(6)
# set state outside
if state[0]:
output.pulse(4)
# set state stairs
if state[1]:
output.pulse(5)
return state
if __name__ == '__main__':
app.run(host='::')