Mercurial > code > home > repos > homeauto
annotate service/rdf_to_mqtt/rdf_to_mqtt.py @ 776:8fa420250799
add headboard
author | drewp@bigasterisk.com |
---|---|
date | Sun, 28 Jun 2020 14:30:52 -0700 |
parents | 82bea37aeb92 |
children | df7035db28f1 |
rev | line source |
---|---|
597
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
1 """ |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
2 We get output statements that are like light9's deviceAttrs (:dev1 :color "#ff0000"), |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
3 convert those to outputAttrs (:dev1 :red 255; :green 0; :blue 0) and post them to mqtt. |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
4 |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
5 This is like light9/bin/collector. |
e1ee6661329a
adjust kitchen PWM freqs. add comments and proposed contents of n3 configs
drewp@bigasterisk.com
parents:
586
diff
changeset
|
6 """ |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
7 import json |
739 | 8 from mqtt_client import MqttClient |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
9 from docopt import docopt |
762 | 10 from rdflib import Namespace |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
11 from twisted.internet import reactor |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
12 import cyclone.web |
762 | 13 from greplin import scales |
14 from greplin.scales.cyclonehandler import StatsHandler | |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
15 |
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
16 from standardservice.logsetup import log, verboseLogging |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
17 import rdf_over_http |
761 | 18 from cycloneerr import PrettyErrorHandler |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
19 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
20 ROOM = Namespace('http://projects.bigasterisk.com/room/') |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
21 |
762 | 22 STATS = scales.collection('/root', |
23 scales.PmfStat('putRequests'), | |
24 scales.PmfStat('statement'), | |
25 scales.PmfStat('mqttPublish'), | |
26 ) | |
27 | |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
28 devs = { |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
29 ROOM['kitchenLight']: { |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
30 'root': 'h801_skylight', |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
31 }, |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
32 ROOM['kitchenCounterLight']: { |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
33 'root': 'h801_counter', |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
34 }, |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
35 ROOM['livingLampShelf']: { |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
36 'root': 'sonoff_0/switch/sonoff_basic_relay/command', |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
37 'values': 'binary', |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
38 }, |
764 | 39 ROOM['livingLampMantleEntry']: { |
740 | 40 'root': 'sonoff_1/switch/sonoff_basic_relay/command', |
41 'values': 'binary', | |
42 }, | |
764 | 43 ROOM['livingLampMantleChair']: { |
740 | 44 'root': 'sonoff_2/switch/sonoff_basic_relay/command', |
45 'values': 'binary', | |
46 }, | |
764 | 47 ROOM['livingLampToyShelf']: { |
740 | 48 'root': 'sonoff_3/switch/sonoff_basic_relay/command', |
49 'values': 'binary', | |
50 }, | |
764 | 51 ROOM['livingLampPiano']: { |
740 | 52 'root': 'sonoff_4/switch/sonoff_basic_relay/command', |
53 'values': 'binary', | |
54 }, | |
761 | 55 ROOM['theater']: { |
56 'root': 'theater_blaster/ir_out', | |
57 'values': 'theaterOutputs', | |
58 }, | |
776 | 59 ROOM['bedHeadboard']: { |
60 'root': 'bed/light/headboard/command', | |
61 }, | |
740 | 62 #-t theater_blaster/ir_out -m 'input_game' |
63 #-t theater_blaster/ir_out -m 'input_bd' | |
64 #-t theater_blaster/ir_out -m 'input_cbl' | |
65 #-t theater_blaster/ir_out -m 'input_pc' | |
66 #-t theater_blaster/ir_out/volume_up -m '{"times":1}' | |
67 #-t theater_blaster/ir_out/volume_down -m '{"times":1}' | |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
68 } |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
69 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
70 |
761 | 71 class OutputPage(PrettyErrorHandler, cyclone.web.RequestHandler): |
762 | 72 @STATS.putRequests.time() |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
73 def put(self): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
74 for stmt in rdf_over_http.rdfStatementsFromRequest( |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
75 self.request.arguments, |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
76 self.request.body, |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
77 self.request.headers): |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
78 self._onStatement(stmt) |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
79 |
762 | 80 @STATS.statement.time() |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
81 def _onStatement(self, stmt): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
82 log.info(f'incoming statement: {stmt}') |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
83 ignored = True |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
84 for dev, attrs in devs.items(): |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
85 if stmt[0] == ROOM['frontWindow']: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
86 ignored = ignored and self._publishFrontScreenText(stmt) |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
87 if stmt[0:2] == (dev, ROOM['brightness']): |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
88 log.info(f'brightness request: {stmt}') |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
89 brightness = stmt[2].toPython() |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
90 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
91 if attrs.get('values', '') == 'binary': |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
92 self._publishOnOff(attrs, brightness) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
93 else: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
94 self._publishRgbw(attrs, brightness) |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
95 ignored = False |
761 | 96 if stmt[0:2] == (dev, ROOM['inputSelector']): |
765
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
97 choice = stmt[2].toPython().decode('utf8') |
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
98 self._publish(topic=attrs['root'], message=f'input_{choice}') |
761 | 99 ignored = False |
100 if stmt[0:2] == (dev, ROOM['volumeChange']): | |
101 delta = int(stmt[2].toPython()) | |
102 which = 'up' if delta > 0 else 'down' | |
103 self._publish(topic=f'theater_blaster/ir_out/volume_{which}', | |
104 message=json.dumps({'timed': abs(delta)})) | |
105 ignored = False | |
776 | 106 if stmt[0:2] == (dev, ROOM['color']): |
107 h = stmt[2].toPython() | |
108 r,g,b = int(h[1:3], 16), int(h[3:5], 16), int(h[5:7], 16) | |
109 self._publish(topic=attrs['root'], | |
110 message=json.dumps({'state': 'ON' if r or g or b else 'OFF', | |
111 'color': {'r': r, 'g': g, 'b': b}, | |
112 'white_value': max(r, g, b)})) | |
113 ignored = false | |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
114 if ignored: |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
115 log.warn("ignoring %s", stmt) |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
116 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
117 def _publishOnOff(self, attrs, brightness): |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
118 msg = 'OFF' |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
119 if brightness > 0: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
120 msg = 'ON' |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
121 self._publish(topic=attrs['root'], message=msg) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
122 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
123 def _publishRgbw(self, attrs, brightness): |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
124 for chan, scale in [('w1', 1), |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
125 ('r', 1), |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
126 ('g', .8), |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
127 ('b', .8)]: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
128 self._publish( |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
129 topic=f"{attrs['root']}/light/kit_{chan}/command", |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
130 messageJson={ |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
131 'state': 'ON', |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
132 'brightness': int(brightness * 255) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
133 }) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
134 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
135 def _publishFrontScreenText(self, stmt): |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
136 ignored = True |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
137 for line in ['line1', 'line2', 'line3', 'line4']: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
138 if stmt[1] == ROOM[line]: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
139 ignored = False |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
140 self.settings.mqtt.publish( |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
141 b'frontwindow/%s' % line.encode('ascii'), |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
142 stmt[2].toPython()) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
143 return ignored |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
144 |
762 | 145 @STATS.mqttPublish.time() |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
146 def _publish(self, topic: str, messageJson: object=None, |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
147 message: str=None): |
765
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
148 log.debug(f'mqtt.publish {topic} {message} {messageJson}') |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
149 if messageJson is not None: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
150 message = json.dumps(messageJson) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
151 self.settings.mqtt.publish( |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
152 topic.encode('ascii'), |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
153 message.encode('ascii')) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
154 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
155 |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
156 if __name__ == '__main__': |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
157 arg = docopt(""" |
739 | 158 Usage: rdf_to_mqtt.py [options] |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
159 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
160 -v Verbose |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
161 """) |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
162 verboseLogging(arg['-v']) |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
163 |
739 | 164 mqtt = MqttClient(clientId='rdf_to_mqtt', brokerPort=1883) |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
165 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
166 port = 10008 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
167 reactor.listenTCP(port, cyclone.web.Application([ |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
168 (r"/()", cyclone.web.StaticFileHandler, |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
169 {"path": ".", "default_filename": "index.html"}), |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
170 (r'/output', OutputPage), |
762 | 171 (r'/stats/(.*)', StatsHandler, {'serverName': 'rdf_to_mqtt'}), |
722
a93fbf0d0daa
dep updates; graph url renames; and other build updates
drewp@bigasterisk.com
parents:
696
diff
changeset
|
172 ], mqtt=mqtt, debug=arg['-v']), interface='::') |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
173 log.warn('serving on %s', port) |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
174 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
175 reactor.run() |