-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOnAir.py
executable file
·241 lines (196 loc) · 7.91 KB
/
OnAir.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python3
import argparse
import configparser
import os
import platform
import re
import shutil
import socket
import threading
import time
import webbrowser
from pathlib import Path
import paho.mqtt.client as mqtt
import rumps
macos_version = int(platform.mac_ver()[0][:2])
class OnAir(object):
def __init__(self):
self.app = rumps.App("OnAir", "⚪")
self.menuMqtt = rumps.MenuItem("MQTT not connected")
self.app.menu.add(self.menuMqtt)
self.menuToggle = rumps.MenuItem("Turn on", callback=self.on_air)
self.app.menu.add(self.menuToggle)
self.app.menu.add(rumps.MenuItem("About OnAir…", callback=self.open_onair_url))
self.args = self.parse_args()
self.mqtt_client = self.create_mqtt_client()
self.menubar_blinker_active = False
self.camera_state_updater_active = True
def run(self):
threading.Thread(target=self.camera_state_updater, daemon=True).start()
self.log(str(self.args))
self.app.run()
def log(self, msg):
if self.args.debug:
print("%s" % msg)
@staticmethod
def open_onair_url(callback_sender=None):
webbrowser.open_new_tab("https://github.com/henrik242/OnAir")
def on_air(self, callback_sender=None):
self.log("on_air()")
self.mqtt_publish("true")
self.menubar_blinker_active = True
threading.Thread(target=self.menubar_blinker, daemon=True).start()
self.menuToggle.title = "Turn off"
self.menuToggle.set_callback(callback=self.off_air)
self.log("on_air() done")
def off_air(self, callback_sender=None):
self.log("off_air()")
self.mqtt_publish("false")
self.menubar_blinker_active = False
self.menuToggle.title = "Turn on"
self.menuToggle.set_callback(callback=self.on_air)
self.log("off_air() done")
def menubar_blinker(self):
self.log("menubar_blinker()")
green = True
while self.menubar_blinker_active:
self.app.title = "🟢" if green else "⚪️"
time.sleep(1)
green = not green
self.app.title = "⚪"
self.log("menubar_blinker() done")
def mqtt_on_connect(self, client, userdata, flags, rc):
if rc == 0:
self.menuMqtt.title = "MQTT connected"
self.log(self.menuMqtt.title)
else:
self.menuMqtt.title = "MQTT not connected (error=%s, user=%s, host=%s)" % (
self.mqtt_err_code(rc),
self.args.user,
self.args.host,
)
self.log(self.menuMqtt.title)
@staticmethod
def mqtt_err_code(code):
return {
0: "connection successful",
1: "incorrect protocol version",
2: "invalid client identifier",
3: "server unavailable",
4: "bad username or password",
5: "not authorised",
}[code]
def mqtt_on_publish(self, client, obj, msg):
self.log("publish: %s" % str(msg))
@staticmethod
def flatten(obj):
return obj[0] if type(obj) is list else obj
def create_mqtt_client(self):
self.log("create_mqtt_client()")
client = mqtt.Client(protocol=mqtt.MQTTv31)
client.username_pw_set(self.flatten(self.args.user), self.flatten(self.args.password))
client.on_connect = self.mqtt_on_connect
client.on_publish = self.mqtt_on_publish
try:
client.connect(self.flatten(self.args.host), self.flatten(self.args.port))
client.loop_start()
except socket.gaierror as err:
self.log("mqtt_publish() failed: %s" % err)
return client
def mqtt_publish(self, state):
self.log("mqtt_publish()")
self.mqtt_client = self.create_mqtt_client() # Need to recreate client in case network has changed
try:
msg_info = self.mqtt_client.publish(
self.args.topic,
(
"""{
"serv": "out_bin_switch",
"type": "cmd.binary.set",
"val_t": "bool",
"val": %s,
"props": {},
"tags": null
}"""
% state
),
)
self.log("mqtt_publish() done: %s" % msg_info.is_published())
except RuntimeError as err:
self.log("mqtt_publish() failed: %s" % err)
def quit(self):
self.menubar_blinker_active = False
self.camera_state_updater_active = False
rumps.quit_application()
def camera_state_updater(self):
self.log("camera_state_updater()")
predicate = 'subsystem == "com.apple.UVCExtension" and composedMessage contains "Post PowerLog"'
extraopts = ""
searchexpr = "guid:(.+)]"
onitem = "Start"
offitem = "Stop"
if macos_version == 12:
predicate = 'eventMessage contains "Post event kCameraStream"'
extraopts = "--style ndjson"
searchexpr = 'VDCAssistant_Device_GUID\\\\" = \\\\"(.+)\\\\";'
onitem = "= On;"
offitem = "= Off;"
if macos_version >= 13:
predicate = 'eventMessage contains "Cameras changed to"'
extraopts = "--style ndjson"
searchexpr = '"Cameras changed to (\[.*\])",'
onitem = "to [ControlCenter"
offitem = "to []"
log_stream = os.popen("""/usr/bin/log stream %s --predicate '%s'""" % (extraopts, predicate), "r")
cameras = dict()
while self.camera_state_updater_active:
item = log_stream.readline()
self.log("reading '%s'" % item.strip())
if item == "":
self.log("log stream died")
break
match = re.search(searchexpr, item)
if match is not None:
if macos_version < 13:
device = match.group(1)
else:
device = "dummy"
if onitem in item:
cameras[device] = True
elif offitem in item:
cameras[device] = False
else:
self.log("Unknown activity: %s" % item)
self.log(cameras)
if any(cameras.values()):
self.log("Camera %s is on" % device)
self.on_air()
else:
self.log("Camera %s is off" % device)
self.off_air()
self.quit()
@staticmethod
def parse_args():
appconfig = ".onair.ini"
homeconfig = str(Path.home()) + "/.onair.ini"
if not os.path.isfile(homeconfig):
shutil.copy(appconfig, homeconfig)
config = configparser.ConfigParser()
config.read(homeconfig)
user = config.get("DEFAULT", "user", fallback=None)
password = config.get("DEFAULT", "password", fallback=None)
host = config.get("DEFAULT", "host", fallback="futurehome-smarthub.local")
port = config.getint("DEFAULT", "port", fallback=1884)
topic = config.get("DEFAULT", "topic", fallback="pt:j1/mt:cmd/rt:dev/rn:zw/ad:1/sv:out_bin_switch/ad:19_0")
debug = config.getboolean("DEFAULT", "debug", fallback=False)
parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument("--host", nargs=1, help=" ", default=host)
parser.add_argument("--port", nargs=1, help=" ", type=int, default=port)
parser.add_argument("--topic", nargs=1, help=" ", default=topic)
parser.add_argument("--user", nargs=1, default=user)
parser.add_argument("--password", nargs=1, default=password)
parser.add_argument("--debug", action="store_true", help=" ", default=debug)
return parser.parse_args()
if __name__ == "__main__":
app = OnAir()
app.run()