-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcolor_server.py
executable file
·145 lines (127 loc) · 5.62 KB
/
color_server.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
# -*- coding: utf-8 -*-
import os
import cherrypy
import subprocess
import signal
import re
import logging
logging.basicConfig(level=logging.DEBUG)
_LOG = logging.getLogger(__name__)
class ColorServer():
def __init__(self):
self._theprocess = None
self._processArgs = None
def _popen(self):
if self._theprocess:
_LOG.debug("PID: %s" % self._theprocess.pid)
self._theprocess.send_signal(signal.SIGINT)
self._theprocess.wait()
self._theprocess = None
_LOG.debug("Killed the Process.")
if self._processArgs:
_LOG.debug("Starting a new Process.")
self._theprocess = subprocess.Popen(self._processArgs)
_LOG.debug("PID: %s" % self._theprocess.pid)
@cherrypy.expose
def index(self, error=None):
html = """<html>
<head>
<title>Color Controller</title>
<link href="/static/css/style.css" rel="stylesheet">
<meta id="viewport" name="viewport" content= "width=device-width, initial-scale=1, maximum-scale=1, user-scalable=0" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
</head>
<body class="outside">
<div class="controller">
<div><h3>LED Demos</h3>
<form style='display: inline-block; padding: 5px;' method="get" action="all">
<button type="submit" class="btn btn-default" id="all">All</button>
</form>
<form style='display: inline-block; padding: 5px;' method="get" action="wipe">
<button type="submit" class="btn btn-default" id="wipe">Wipe</button>
</form>
<form style='display: inline-block; padding: 5px;' method="get" action="theater">
<button type="submit" class="btn btn-default" id="theater">Theater</button>
</form>
<form style='display: inline-block; padding: 5px;' method="get" action="rainbow">
<button type="submit" class="btn btn-default" id="rainbow">Rainbow</button>
</form></div>
<div><h3>Audio Sync</h3>
<form style='display: inline-block; padding: 5px;' method="get" action="audioSync">
<input type="radio" name="toSync" value="hass"> Hass
<input type="radio" name="toSync" value="led"> Led
<input type="radio" name="toSync" value="both"> Both
<button type="submit" class="btn btn-default" id="audioSync">Audio Sync</button>
</form></div>
<div><h3>Home Assistant</h3>"""
if error == "entity":
html += """<BR><font color=red>Invalid Entity!</font><BR>"""
html += """
<form style='display: inline-block; padding: 5px;' method="get" action="hassEntity">
<input type="text" name="entity" placeholder="light.living_room">
<button type="submit" class="btn btn-default" id="hassEntity">Hass Entity</button>
</form></div>
<div><h3>STOP</h3>
<form style='display: inline-block; padding: 5px;' method="get" action="turnOff">
<button type="submit" class="btn btn-default" id="turnOff">Stop Function</button>
</form></div>
</div>
</body>
</html>"""
return html
@cherrypy.expose
def all(self):
_LOG.info("All Demos")
self._processArgs = ["python3", "color_script.py", "-a"]
self._popen()
raise cherrypy.HTTPRedirect("/index")
@cherrypy.expose
def wipe(self):
_LOG.info("Wipe Demo")
self._processArgs = ["python3", "color_script.py", "-w"]
self._popen()
raise cherrypy.HTTPRedirect("/index")
@cherrypy.expose
def theater(self):
_LOG.info("Theater Chase Demo")
self._processArgs = ["python3", "color_script.py", "-t"]
self._popen()
raise cherrypy.HTTPRedirect("/index")
@cherrypy.expose
def rainbow(self):
_LOG.info("Rainbow Demo")
self._processArgs = ["python3", "color_script.py", "-r"]
self._popen()
raise cherrypy.HTTPRedirect("/index")
@cherrypy.expose
def audioSync(self, toSync):
_LOG.info("Audio Sync")
self._processArgs = ["python3", "color_script.py", "-x=" + toSync]
self._popen()
raise cherrypy.HTTPRedirect("/index")
@cherrypy.expose
def hassEntity(self, entity):
_LOG.info("Hass Entity")
rex = re.compile("^[a-z_]+.[a-z_0-9]+$")
if rex.match(entity):
_LOG.debug("True")
self._processArgs = ["python3", "color_script.py", "-e " + entity]
self._popen()
raise cherrypy.HTTPRedirect("/index")
else:
raise cherrypy.HTTPRedirect("/index?error=entity")
@cherrypy.expose
def turnOff(self):
_LOG.info("Turn Off")
self._processArgs = ["python3", "color_script.py", "-s"]
self._popen()
raise cherrypy.HTTPRedirect("/index")
if __name__ == "__main__":
conf = {
"/": {"tools.sessions.on": True, "tools.staticdir.root": os.path.abspath(os.getcwd())},
"/static": {"tools.staticdir.on": True, "tools.staticdir.dir": "./public"},
}
if os.geteuid() == 0:
cherrypy.process.plugins.DropPrivileges(cherrypy.engine, uid=0, gid=0).subscribe()
cherrypy.config.update({"server.socket_host": "0.0.0.0"})
cherrypy.quickstart(ColorServer(), "/", conf)