-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplug.py
More file actions
108 lines (87 loc) · 2.61 KB
/
plug.py
File metadata and controls
108 lines (87 loc) · 2.61 KB
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
from flask import Flask, request, session, redirect, jsonify, render_template, url_for
from gpiozero import LED, Button
import yaml
from functools import partial
import multiprocessing as mp
import ctypes
import hashlib
class PlugFlaskApp(Flask):
def run(self, host='0.0.0.0', port=80, debug=None, load_dotenv=True, **options):
super(PlugFlaskApp, self).run(host=host, port=port, debug=debug, load_dotenv=load_dotenv, **options)
app = PlugFlaskApp(__name__)
app.secret_key = 'pr*WATsfFm9a"M_RJbsMm9j;\hH:=(,^EGftHX/_L</BrWXTbFY&wgQRh]dg2^\!R>djVbe7APC&d(='
sst = None
config = None
def toggle(si):
global sst
global config
sst[si] = not sst[si]
gpio = config['ports'][si]['gpio']
if sst[si]:
gpio.on()
else:
gpio.off()
def setup_app(app):
global config
global sst
with open("config.yaml", 'r') as stream:
try:
config = yaml.safe_load(stream)
except yaml.YAMLError as exc:
print(exc)
quit()
si = 0
for port in config['ports']:
port['gpio'] = LED(port['gpio_actor'])
port['button'] = Button(port['gpio_button'])
port['button'].when_pressed = partial(toggle, si)
si = si + 1
sst = mp.Array(ctypes.c_bool, len(config['ports']))
setup_app(app)
@app.route('/api/v1/login', methods=['POST'])
def api_login():
global config
username = request.form.get('username', None)
password = hashlib.md5(request.form.get('password', None).encode('utf-8')).hexdigest()
print('login user:', username, ' password:', password)
for user in config['users']:
if user['name'] == username and user['password'] == password:
print('user matched: ', user)
session['username'] = user['name']
return 'OK'
return "Unauthorized", 400
@app.route('/api/v1/switch/<int:port>/<int:status>')
def api_switch(port, status):
global sst
global config
if port >= 0 and port < len(config['ports']):
un = session['username'] if 'username' in session else ''
if 'ALL' in config['ports'][port]['permission'] or un in config['ports'][port]['permission']:
sst[port] = (status != 0)
gpio = config['ports'][port]['gpio']
if sst[port]:
gpio.on()
else:
gpio.off()
return 'OK'
@app.route('/api/v1/status')
def api_status():
global sst
lsst = sst[:].copy()
return jsonify(lsst)
@app.route('/login', methods=['GET'])
def login():
return render_template('login.html')
@app.route('/logout', methods=['GET'])
def logout():
session.pop('username', None)
return redirect(url_for('switch'))
@app.route('/switch')
def switch():
global sst
return render_template('switch.html', sst=sst, ports=config['ports'])
@app.route('/')
def index():
return '<a href="/switch">switch</a>'
if __name__ == '__main__':
app.run()