-
Notifications
You must be signed in to change notification settings - Fork 4
/
garageoPIner.py
148 lines (121 loc) · 3.8 KB
/
garageoPIner.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
137
138
139
140
141
142
143
144
145
146
147
148
from functools import wraps
from flask import session,request, Response, Flask
from threading import Timer
import RPi.GPIO as GPIO
import time
from datetime import timedelta
import configparser
app = Flask(__name__)
app.secret_key = 'm4bG3YJwarQQXU3F' #Needed for keep session open
GO_USERNAME = 'admin'
GO_PASSWORD = 'garageopiner'
GO_PORT = 80
GO_PIN1 = 4
GO_PIN2 = 17
GPIO.setmode(GPIO.BCM)
timer1 = None #Timer for Door1
timer2 = None # Timer for Door2
def check_auth(username, password):
"""This function is called to check if a username /
password combination is valid.
"""
return username == GO_USERNAME and password == GO_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
@app.route('/toggleIN1')
@requires_auth
def toggleIN1():
try:
stopTimeControlIN1()
togglePin(GO_PIN1)
response = "Relay switched successfull."
except Exception as e:
response = "Error occured: " + str(e)
return response
@app.route('/toggleIN2')
@requires_auth
def toggleIN2():
try:
stopTimeControlIN1()
togglePin(GO_PIN2)
response = "Relay switched successfull."
except Exception as e:
response = "Error occured: " + str(e)
return response ;
@app.route('/timeControlIN1')
@requires_auth
def timeControlIN1():
global timer1
seconds = request.args.get('seconds')
#Keep session open to allow timer to switch relay again
session.permanent = True
app.permanent_session_lifetime = timedelta(seconds=int(seconds)+5)
stopTimeControlIN1()
togglePin(GO_PIN1)
timer1 = Timer(int(seconds),togglePin, [GO_PIN1])
timer1.start()
return "Timer started"
@app.route('/stopTimeControlIN1')
def stopTimeControlIN1():
global timer1
if timer1!=None:
timer1.cancel()
timer1=None
return "Timer canceled"
@app.route('/timeControlIN2')
@requires_auth
def timeControlIN2():
global timer2
seconds = request.args.get('seconds')
#Keep session open to allow timer to switch relay again
session.permanent = True
app.permanent_session_lifetime = timedelta(seconds=int(seconds)+5)
stopTimeControlIN2()
togglePin(GO_PIN2)
timer2 = Timer(int(seconds),togglePin, [GO_PIN2])
timer2.start()
return "Timer started"
@app.route('/stopTimeControlIN2')
@requires_auth
def stopTimeControlIN2():
global timer2
if timer2!=None:
timer2.cancel()
timer2=None
return "Timer canceled"
@app.route('/')
@requires_auth
def deliverWebPage():
return app.send_static_file('index.html')
def togglePin(pin):
GPIO.setup(int(pin), GPIO.OUT)
GPIO.output(int(pin),GPIO.LOW)
time.sleep(0.2);
GPIO.output(int(pin),GPIO.HIGH)
if __name__ == '__main__':
files = ['garageoPIner.config']
config = configparser.RawConfigParser()
dataset = config.read('garageoPIner.config')
if len(dataset) == len(files):
GO_PORT = int(config.get("Settings","port"))
GO_PIN1 = int(config.get("Settings","pin1"))
GO_PIN2 = int(config.get("Settings","pin2"))
GO_USERNAME = config.get("Credentials","username")
GO_PASSWORD = config.get("Credentials","password")
else:
raise ValueError("Failed to open/find configuration file")
GPIO.setup(GO_PIN1,GPIO.IN)
GPIO.setup(GO_PIN2,GPIO.IN)
app.run(host='0.0.0.0',port=GO_PORT,debug=True)