Mercurial > code > home > repos > homeauto
comparison service/colplay/colplay.py @ 1280:87d7a14073a2
last version of colplay, which i hope won't need a custom service soon
Ignore-this: dd6799d5eba9854538e56c791e9f836a
darcs-hash:25d151e3431e1d6c2237c203feeb9ab1f017ecfa
author | drewp <drewp@bigasterisk.com> |
---|---|
date | Sat, 20 Apr 2019 23:56:03 -0700 |
parents | 2ff06e28eef0 |
children |
comparison
equal
deleted
inserted
replaced
1279:b4004739640e | 1280:87d7a14073a2 |
---|---|
13 import cyclone.web | 13 import cyclone.web |
14 from dateutil.tz import tzlocal | 14 from dateutil.tz import tzlocal |
15 from cyclone.httpclient import fetch | 15 from cyclone.httpclient import fetch |
16 from webcolors import rgb_to_hex | 16 from webcolors import rgb_to_hex |
17 from influxdb import InfluxDBClient | 17 from influxdb import InfluxDBClient |
18 from rdflib import Namespace, Graph | |
19 from rdflib.parser import StringInputSource | |
20 | |
21 ROOM = Namespace('http://projects.bigasterisk.com/room/') | |
18 | 22 |
19 influx = InfluxDBClient('bang', 9060, 'root', 'root', 'main') | 23 influx = InfluxDBClient('bang', 9060, 'root', 'root', 'main') |
20 | 24 |
21 def currentAudio(location='brace'): | 25 def currentAudio(location='frontbed'): |
22 t = time.time() | 26 t = time.time() |
23 row = list(influx.query("""SELECT mean(value) FROM audioLevel WHERE "location" = '%s' AND time > %ds""" % (location, t - 30)))[0][0] | 27 row = list(influx.query("""SELECT mean(value) FROM audioLevel WHERE "location" = '%s' AND time > %ds""" % (location, t - 30)))[0][0] |
24 log.debug("query took %.03fms", 1000 * (time.time() - t)) | 28 log.debug("query took %.03fms", 1000 * (time.time() - t)) |
25 base = {'brace': .020, | 29 base = {'frontbed': .015, |
26 'living': .03, | 30 'living': .03, |
27 }[location] | 31 }[location] |
28 high = { | 32 high = { |
29 'brace': .1, | 33 'frontbed': .40, |
30 'living': .3, | 34 'living': .3, |
31 }[location] | 35 }[location] |
32 return max(0.0, min(1.0, (row['mean'] - base) / high)) | 36 return max(0.0, min(1.0, (row['mean'] - base) / high)) |
33 | 37 |
34 | 38 |
137 now = datetime.now(tzlocal()) | 141 now = datetime.now(tzlocal()) |
138 hr = now.hour + now.minute / 60 + now.second / 3600 | 142 hr = now.hour + now.minute / 60 + now.second / 3600 |
139 x = int(((hr - 12) % 24) * pxPerHour) % 2400 | 143 x = int(((hr - 12) % 24) * pxPerHour) % 2400 |
140 log.debug("x = %s", x) | 144 log.debug("x = %s", x) |
141 | 145 |
142 audioLevel = currentAudio('brace') | 146 audioLevel = currentAudio('frontbed') |
143 log.debug('level = %s', audioLevel) | 147 log.debug('level = %s', audioLevel) |
144 for i, (name, ypos) in enumerate(sorted(lightYPos.items())): | 148 for i, (name, ypos) in enumerate(sorted(lightYPos.items())): |
145 | 149 |
146 if i / len(lightYPos) < audioLevel: | 150 if i / len(lightYPos) < audioLevel: |
147 setColor(name, (500, 0, 0)) | 151 setColor(name, (500, 0, 0)) |
156 except Exception: | 160 except Exception: |
157 self.lastError = traceback.format_exc() | 161 self.lastError = traceback.format_exc() |
158 log.error(self.lastError) | 162 log.error(self.lastError) |
159 self.lastErrorTime = time.time() | 163 self.lastErrorTime = time.time() |
160 | 164 |
165 | |
166 | |
167 class OneShot(cyclone.web.RequestHandler): | |
168 def post(self): | |
169 g = Graph() | |
170 g.parse(StringInputSource(self.request.body), format={ | |
171 'text/n3': 'n3', | |
172 }[self.request.headers['content-type']]) | |
173 | |
174 for anim in g.subjects(ROOM['playback'], ROOM['start']): | |
175 startAnim(anim) | |
161 | 176 |
162 class IndexHandler(cyclone.web.RequestHandler): | 177 class IndexHandler(cyclone.web.RequestHandler): |
163 def get(self): | 178 def get(self): |
164 ls = self.settings.lightState | 179 ls = self.settings.lightState |
165 now = time.time() | 180 now = time.time() |
174 lightState = LightState() | 189 lightState = LightState() |
175 task.LoopingCall(lightState.step).start(3)#3600 / pxPerHour) | 190 task.LoopingCall(lightState.step).start(3)#3600 / pxPerHour) |
176 log.info("listening http on 9051") | 191 log.info("listening http on 9051") |
177 reactor.listenTCP(9051, cyclone.web.Application([ | 192 reactor.listenTCP(9051, cyclone.web.Application([ |
178 (r'/', IndexHandler), | 193 (r'/', IndexHandler), |
194 (r'/oneShot', OneShot), | |
179 ], lightState=lightState)) | 195 ], lightState=lightState)) |
180 reactor.run() | 196 reactor.run() |