Mercurial > code > home > repos > homeauto
annotate service/rdf_to_mqtt/rdf_to_mqtt.py @ 764:b15bc47b97a0
rename living lamps
Ignore-this: 4ccc9c8869cb489d89f5d19da475db69
author | drewp@bigasterisk.com |
---|---|
date | Fri, 27 Mar 2020 23:07:10 -0700 |
parents | 5943cacc8b9b |
children | 82bea37aeb92 |
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 }, | |
740 | 59 #-t theater_blaster/ir_out -m 'input_game' |
60 #-t theater_blaster/ir_out -m 'input_bd' | |
61 #-t theater_blaster/ir_out -m 'input_cbl' | |
62 #-t theater_blaster/ir_out -m 'input_pc' | |
63 #-t theater_blaster/ir_out/volume_up -m '{"times":1}' | |
64 #-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
|
65 } |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
66 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
67 |
761 | 68 class OutputPage(PrettyErrorHandler, cyclone.web.RequestHandler): |
762 | 69 @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
|
70 def put(self): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
71 for stmt in rdf_over_http.rdfStatementsFromRequest( |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
72 self.request.arguments, |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
73 self.request.body, |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
74 self.request.headers): |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
75 self._onStatement(stmt) |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
76 |
762 | 77 @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
|
78 def _onStatement(self, stmt): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
79 log.info(f'incoming statement: {stmt}') |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
80 ignored = True |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
81 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
|
82 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
|
83 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
|
84 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
|
85 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
|
86 brightness = stmt[2].toPython() |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
87 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
88 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
|
89 self._publishOnOff(attrs, brightness) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
90 else: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
91 self._publishRgbw(attrs, brightness) |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
92 ignored = False |
761 | 93 if stmt[0:2] == (dev, ROOM['inputSelector']): |
94 self._publish(topic=attrs['root'], | |
95 message='input_'+str(stmt[2].toPython())) | |
96 ignored = False | |
97 if stmt[0:2] == (dev, ROOM['volumeChange']): | |
98 delta = int(stmt[2].toPython()) | |
99 which = 'up' if delta > 0 else 'down' | |
100 self._publish(topic=f'theater_blaster/ir_out/volume_{which}', | |
101 message=json.dumps({'timed': abs(delta)})) | |
102 ignored = False | |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
103 if ignored: |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
104 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
|
105 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
106 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
|
107 msg = 'OFF' |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
108 if brightness > 0: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
109 msg = 'ON' |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
110 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
|
111 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
112 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
|
113 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
|
114 ('r', 1), |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
115 ('g', .8), |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
116 ('b', .8)]: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
117 self._publish( |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
118 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
|
119 messageJson={ |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
120 'state': 'ON', |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
121 'brightness': int(brightness * 255) |
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 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
124 def _publishFrontScreenText(self, stmt): |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
125 ignored = True |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
126 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
|
127 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
|
128 ignored = False |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
129 self.settings.mqtt.publish( |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
130 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
|
131 stmt[2].toPython()) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
132 return ignored |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
133 |
762 | 134 @STATS.mqttPublish.time() |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
135 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
|
136 message: str=None): |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
137 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
|
138 message = json.dumps(messageJson) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
139 self.settings.mqtt.publish( |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
140 topic.encode('ascii'), |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
141 message.encode('ascii')) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
142 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
143 |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
144 if __name__ == '__main__': |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
145 arg = docopt(""" |
739 | 146 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
|
147 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
148 -v Verbose |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
149 """) |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
150 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
|
151 |
739 | 152 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
|
153 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
154 port = 10008 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
155 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
|
156 (r"/()", cyclone.web.StaticFileHandler, |
7051b8b4766a
build updates. hack in r/g/b and some fixed multipliers
drewp@bigasterisk.com
parents:
392
diff
changeset
|
157 {"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
|
158 (r'/output', OutputPage), |
762 | 159 (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
|
160 ], 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
|
161 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
|
162 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
163 reactor.run() |