-
Notifications
You must be signed in to change notification settings - Fork 14
/
web-power.py
executable file
·104 lines (86 loc) · 3.41 KB
/
web-power.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
#!/usr/bin/env python3
# Web remote for the Energenie Pi remote
# see http://www.penguintutor.com/
# Copyright Stewart Watkiss 2014-2023
# web-power is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this software. If not, see <http://www.gnu.org/licenses/>
from gpiozero import Energenie
import bottle
from bottle import route, request, response, template, static_file
sockets = [None]
sockets.append(Energenie(1))
sockets.append(Energenie(2))
sockets.append(Energenie(3))
sockets.append(Energenie(4))
# allow on all ip addresses
HOST = ''
# port 80 - standard web port (assumes no other web server installed)
# If using apache or another browser then change this to a different value
PORT = 80
# Template is the type of display
# - default is generic remote control 4x2 + all
# - image is 4 images each with a background image and on / off buttons
custom_template = "default"
# Theme is a folder that can be used for custom images
custom_theme = "default"
# Customization links - useful when embedding (eg. PiQuarium)
# link to home / previous
home_link = "http://127.0.0.1"
home_title = "Home"
# Folder where this is installed and the index.html file is located
# The index.html file is exposed to the webserver as well as any files in a subdirectory called public (ie. /home/pi/pi-power/public)
DOCUMENT_ROOT = '/home/pi/pi-power'
# Create the bottle web server
app = bottle.Bottle()
bottle.TEMPLATE_PATH.insert(0,'/home/pi/pi-power/views/')
# public files
# *** WARNING ANYTHING STORED IN THE PUBLIC FOLDER WILL BE AVAILABLE TO DOWNLOAD BY ANYONE CONNECTED TO THE SAME NETWORK ***
@app.route ('/public/<filename:path>')
def server_public (filename):
return static_file (filename, root=DOCUMENT_ROOT+"/public")
# Handle switch on request
@app.route ('/switchon')
def switchon():
socket = int(request.query.socket)
# If single socket requested
if (socket > 0 and socket <= 4) :
#print 'Switching on {}'.format(socket)
sockets[socket].on()
return 'Requested switch on {}'.format(socket)
# If all sockets requested
elif (socket == 0) :
for i in range (1, 5):
sockets[i].on()
return 'Requested switch on ALL'
else :
return 'Invalid request'
@app.route ('/switchoff')
def switchoff():
socket = int(request.query.socket)
# If single socket requested
if (socket > 0 and socket <= 4) :
#print 'Switching off {}'.format(socket)
sockets[socket].off()
return 'Requested switch off {}'.format(socket)
# If all sockets requested
elif (socket == 0) :
for i in range (1, 5):
sockets[i].off()
return 'Requested switch off ALL'
else:
return 'Invalid request'
# Serve up the default index.html page
@app.route ('/')
def server_home ():
return template (custom_template, theme=custom_theme, homelink=home_link, hometitle=home_title)
app.run(host=HOST, port=PORT)