Mercurial > code > home > repos > homeauto
annotate service/rdf_to_mqtt/rdf_to_mqtt.py @ 778:acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
author | drewp@bigasterisk.com |
---|---|
date | Sat, 08 Aug 2020 13:56:39 -0700 |
parents | df7035db28f1 |
children | e7eb3fc8db54 |
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 |
777 | 8 |
9 import cyclone.web | |
10 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
|
11 from docopt import docopt |
762 | 12 from greplin import scales |
13 from greplin.scales.cyclonehandler import StatsHandler | |
777 | 14 from mqtt_client import MqttClient |
15 from rdflib import Namespace | |
581
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 |
777 | 17 from twisted.internet import reactor |
18 | |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
19 import rdf_over_http |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
20 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
21 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
|
22 |
777 | 23 STATS = scales.collection( |
24 '/root', | |
25 scales.PmfStat('putRequests'), | |
26 scales.PmfStat('statement'), | |
27 scales.PmfStat('mqttPublish'), | |
762 | 28 ) |
29 | |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
30 devs = { |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
31 ROOM['kitchenLight']: { |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
32 'root': 'h801_skylight', |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
33 'hasWhite': True, |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
34 }, |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
35 ROOM['kitchenCounterLight']: { |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
36 'root': 'h801_counter', |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
37 'hasWhite': True, |
460
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
38 }, |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
39 ROOM['livingLampShelf']: { |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
40 '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
|
41 'values': 'binary', |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
42 }, |
764 | 43 ROOM['livingLampMantleEntry']: { |
740 | 44 'root': 'sonoff_1/switch/sonoff_basic_relay/command', |
45 'values': 'binary', | |
46 }, | |
764 | 47 ROOM['livingLampMantleChair']: { |
740 | 48 'root': 'sonoff_2/switch/sonoff_basic_relay/command', |
49 'values': 'binary', | |
50 }, | |
764 | 51 ROOM['livingLampToyShelf']: { |
740 | 52 'root': 'sonoff_3/switch/sonoff_basic_relay/command', |
53 'values': 'binary', | |
54 }, | |
764 | 55 ROOM['livingLampPiano']: { |
740 | 56 'root': 'sonoff_4/switch/sonoff_basic_relay/command', |
57 'values': 'binary', | |
58 }, | |
761 | 59 ROOM['theater']: { |
60 'root': 'theater_blaster/ir_out', | |
61 'values': 'theaterOutputs', | |
62 }, | |
776 | 63 ROOM['bedHeadboard']: { |
64 'root': 'bed/light/headboard/command', | |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
65 'hasWhite': True, |
776 | 66 }, |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
67 # https://github.com/Koenkk/zigbee2mqtt.io/blob/new_api/docs/information/mqtt_topics_and_message_structure.md#general |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
68 ROOM['syl1']: { |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
69 'root': 'zigbee2mqtt/syl1/set', |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
70 'hasBrightness': True, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
71 'defaults': { |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
72 'transition': 0, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
73 } |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
74 }, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
75 ROOM['syl2']: { |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
76 'root': 'zigbee2mqtt/syl2/set', |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
77 'hasBrightness': True, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
78 'defaults': { |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
79 'transition': 0, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
80 } |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
81 }, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
82 ROOM['syl3']: { |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
83 'root': 'zigbee2mqtt/syl3/set', |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
84 'hasBrightness': True, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
85 'defaults': { |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
86 'transition': 0, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
87 } |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
88 }, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
89 ROOM['syl4']: { |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
90 'root': 'zigbee2mqtt/syl4/set', |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
91 'hasBrightness': True, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
92 'defaults': { |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
93 'transition': 0, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
94 } |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
95 }, |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
96 } |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
97 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
98 |
761 | 99 class OutputPage(PrettyErrorHandler, cyclone.web.RequestHandler): |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
100 |
762 | 101 @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
|
102 def put(self): |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
103 for stmt in rdf_over_http.rdfStatementsFromRequest(self.request.arguments, self.request.body, self.request.headers): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
104 self._onStatement(stmt) |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
105 |
762 | 106 @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
|
107 def _onStatement(self, stmt): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
108 log.info(f'incoming statement: {stmt}') |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
109 ignored = True |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 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
|
114 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
|
115 brightness = stmt[2].toPython() |
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 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
|
118 self._publishOnOff(attrs, brightness) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
119 else: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
120 self._publishRgbw(attrs, brightness) |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
121 ignored = False |
761 | 122 if stmt[0:2] == (dev, ROOM['inputSelector']): |
765
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
123 choice = stmt[2].toPython().decode('utf8') |
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
124 self._publish(topic=attrs['root'], message=f'input_{choice}') |
761 | 125 ignored = False |
126 if stmt[0:2] == (dev, ROOM['volumeChange']): | |
127 delta = int(stmt[2].toPython()) | |
128 which = 'up' if delta > 0 else 'down' | |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
129 self._publish(topic=f'theater_blaster/ir_out/volume_{which}', message=json.dumps({'timed': abs(delta)})) |
761 | 130 ignored = False |
776 | 131 if stmt[0:2] == (dev, ROOM['color']): |
132 h = stmt[2].toPython() | |
777 | 133 r, g, b = int(h[1:3], 16), int(h[3:5], 16), int(h[5:7], 16) |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
134 msg = { |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
135 'state': 'ON' if r or g or b else 'OFF', |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
136 'color': { |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
137 'r': r, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
138 'g': g, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
139 'b': b |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
140 }, |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
141 } |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
142 if attrs.get('hasBrightness', False): |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
143 # todo- still not right for sylvania bulbs; they want color x-y. |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
144 # see https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html#zigbee2mqttfriendly_nameset |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
145 msg['brightness'] = max(r, g, b) |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
146 if msg['brightness'] != 0: |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
147 scl = msg['brightness'] / 255 |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
148 for chan in ['r', 'g', 'b']: |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
149 msg['color'][chan] = int(msg['color'][chan] / 255.0 / scl * 255) |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
150 |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
151 if attrs.get('hasWhite', False): |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
152 msg['white_value'] = max(r, g, b) |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
153 msg.update(attrs.get('defaults', {})) |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
154 self._publish(topic=attrs['root'], message=json.dumps(msg)) |
777 | 155 ignored = False |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
156 |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
157 if ignored: |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
158 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
|
159 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
160 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
|
161 msg = 'OFF' |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
162 if brightness > 0: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
163 msg = 'ON' |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
164 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
|
165 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
166 def _publishRgbw(self, attrs, brightness): |
777 | 167 for chan, scale in [('w1', 1), ('r', 1), ('g', .8), ('b', .8)]: |
168 self._publish(topic=f"{attrs['root']}/light/kit_{chan}/command", | |
169 messageJson={ | |
170 'state': 'ON', | |
171 'brightness': int(brightness * 255) | |
172 }) | |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
173 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
174 def _publishFrontScreenText(self, stmt): |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
175 ignored = True |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
176 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
|
177 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
|
178 ignored = False |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
179 self.settings.mqtt.publish(b'frontwindow/%s' % line.encode('ascii'), stmt[2].toPython()) |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
180 return ignored |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
181 |
762 | 182 @STATS.mqttPublish.time() |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
183 def _publish(self, topic: str, messageJson: object = None, message: str = None): |
765
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
184 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
|
185 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
|
186 message = json.dumps(messageJson) |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
187 self.settings.mqtt.publish(topic.encode('ascii'), message.encode('ascii')) |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
188 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
189 |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
190 if __name__ == '__main__': |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
191 arg = docopt(""" |
739 | 192 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
|
193 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
194 -v Verbose |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
195 """) |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
196 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
|
197 |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
198 mqtt = MqttClient(clientId='rdf_to_mqtt', brokerHost='mosquitto-ext.default.svc.cluster.local', brokerPort=1883) |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
199 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
200 port = 10008 |
777 | 201 reactor.listenTCP(port, |
202 cyclone.web.Application([ | |
203 (r"/()", cyclone.web.StaticFileHandler, { | |
204 "path": ".", | |
205 "default_filename": "index.html" | |
206 }), | |
207 (r'/output', OutputPage), | |
208 (r'/stats/(.*)', StatsHandler, { | |
209 'serverName': 'rdf_to_mqtt' | |
210 }), | |
211 ], | |
212 mqtt=mqtt, | |
213 debug=arg['-v']), | |
214 interface='::') | |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
215 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
|
216 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
217 reactor.run() |