Mercurial > code > home > repos > homeauto
annotate service/rdf_to_mqtt/rdf_to_mqtt.py @ 1703:80b01d548b9c
add new devs, factor out to devs.py
author | drewp@bigasterisk.com |
---|---|
date | Sat, 23 Oct 2021 13:14:45 -0700 |
parents | ac1ae0c21bb0 |
children | 3f4b447d65f5 |
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 |
1703 | 18 from devs import devs |
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 |
761 | 31 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
|
32 |
762 | 33 @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
|
34 def put(self): |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
35 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
|
36 self._onStatement(stmt) |
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
37 |
762 | 38 @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
|
39 def _onStatement(self, stmt): |
694
925bc4137c93
extract rdfStatementsFromRequest for sharing with other tools
drewp@bigasterisk.com
parents:
597
diff
changeset
|
40 log.info(f'incoming statement: {stmt}') |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
41 ignored = True |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
42 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
|
43 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
|
44 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
|
45 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
|
46 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
|
47 brightness = stmt[2].toPython() |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
48 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
49 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
|
50 self._publishOnOff(attrs, brightness) |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
51 else: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
52 self._publishRgbw(attrs, brightness) |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
53 ignored = False |
761 | 54 if stmt[0:2] == (dev, ROOM['inputSelector']): |
765
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
55 choice = stmt[2].toPython().decode('utf8') |
82bea37aeb92
fix theater input selector string assembling
drewp@bigasterisk.com
parents:
764
diff
changeset
|
56 self._publish(topic=attrs['root'], message=f'input_{choice}') |
761 | 57 ignored = False |
58 if stmt[0:2] == (dev, ROOM['volumeChange']): | |
59 delta = int(stmt[2].toPython()) | |
60 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
|
61 self._publish(topic=f'theater_blaster/ir_out/volume_{which}', message=json.dumps({'timed': abs(delta)})) |
761 | 62 ignored = False |
776 | 63 if stmt[0:2] == (dev, ROOM['color']): |
64 h = stmt[2].toPython() | |
789 | 65 msg = {} |
66 if h.endswith(b'K'): # accept "0.7*2200K" (brightness 0.7) | |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
67 # see https://www.zigbee2mqtt.io/information/mqtt_topics_and_message_structure.html#zigbee2mqttfriendly_nameset |
789 | 68 bright, kelvin = map(float, h[:-1].split(b'*')) |
1685 | 69 msg['state'] = 'ON' |
789 | 70 msg["color_temp"] = round(1000000 / kelvin, 2) |
71 msg['brightness'] = int(bright * 255) # 1..20 look about the same | |
72 else: | |
73 r, g, b = int(h[1:3], 16), int(h[3:5], 16), int(h[5:7], 16) | |
74 msg = { | |
75 'state': 'ON' if r or g or b else 'OFF', | |
76 'color': { | |
77 'r': r, | |
78 'g': g, | |
79 'b': b | |
80 }, | |
1685 | 81 'brightness': max(r, g, b), |
789 | 82 } |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
83 |
789 | 84 if attrs.get('hasWhite', False): |
85 msg['white_value'] = max(r, g, b) | |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
86 msg.update(attrs.get('defaults', {})) |
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
87 self._publish(topic=attrs['root'], message=json.dumps(msg)) |
777 | 88 ignored = False |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
89 |
392
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
90 if ignored: |
79d041273e26
mqtt has two devices now. various older cleanups.
drewp@bigasterisk.com
parents:
378
diff
changeset
|
91 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
|
92 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
93 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
|
94 msg = 'OFF' |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
95 if brightness > 0: |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
96 msg = 'ON' |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
97 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
|
98 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
99 def _publishRgbw(self, attrs, brightness): |
777 | 100 for chan, scale in [('w1', 1), ('r', 1), ('g', .8), ('b', .8)]: |
1702 | 101 self._publish(topic=f"{attrs['root']}/light/kit_{chan}/command", messageJson={'state': 'ON', 'brightness': int(brightness * 255)}) |
696
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
102 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
103 def _publishFrontScreenText(self, stmt): |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
104 ignored = True |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
105 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
|
106 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
|
107 ignored = False |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
108 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
|
109 return ignored |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
110 |
762 | 111 @STATS.mqttPublish.time() |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
112 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
|
113 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
|
114 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
|
115 message = json.dumps(messageJson) |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
116 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
|
117 |
c52b172c0824
add publish to ON/OFF messages. split up the main statement handler
drewp@bigasterisk.com
parents:
694
diff
changeset
|
118 |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
119 if __name__ == '__main__': |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
120 arg = docopt(""" |
739 | 121 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
|
122 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
123 -v Verbose |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
124 """) |
581
30022797642e
mqtt_graph_bridge to new build rules and to py3
drewp@bigasterisk.com
parents:
460
diff
changeset
|
125 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
|
126 |
778
acf58b83022f
rdf_to_mqtt sylvania bulbs and code cleanup and k8s updates
drewp@bigasterisk.com
parents:
777
diff
changeset
|
127 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
|
128 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
129 port = 10008 |
777 | 130 reactor.listenTCP(port, |
131 cyclone.web.Application([ | |
132 (r"/()", cyclone.web.StaticFileHandler, { | |
133 "path": ".", | |
134 "default_filename": "index.html" | |
135 }), | |
136 (r'/output', OutputPage), | |
137 (r'/stats/(.*)', StatsHandler, { | |
138 'serverName': 'rdf_to_mqtt' | |
139 }), | |
140 ], | |
141 mqtt=mqtt, | |
142 debug=arg['-v']), | |
143 interface='::') | |
373
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
144 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
|
145 |
2158e7ad19b1
receive oneshot updates from reasoning; emit commands on MQTT to control H801 wifi dimmer
drewp@bigasterisk.com
parents:
diff
changeset
|
146 reactor.run() |