-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathapp.py
480 lines (370 loc) · 14.5 KB
/
app.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
#!/usr/bin/env python3
# read README.md for pre-reqs, and customize config.yaml
# to run:
# python3 app.py
import base64
from pathlib import Path
import os
import ssl
import paho.mqtt.client as mqtt
from flask import Flask, render_template, send_from_directory
from flask_socketio import SocketIO
from yaml import safe_load
# determine path to this script
mypath = Path().absolute()
# Load a default image file
default_image_file = mypath / "static" / "img" / "default.png"
print("Using default cover image file {}".format(default_image_file))
default_image_mime_type = "image/png"
default_image_b64_str = ""
with default_image_file.open("rb") as imageFile:
image_octets = imageFile.read()
default_image_b64_str = base64.b64encode(image_octets).decode("utf-8")
# App will die here if config file is missing.
# Read only on startup. If edited, app must be relaunched to see changes
config_file = mypath / "config.yaml"
print("Using config file {}".format(config_file))
with config_file.open() as f:
config = safe_load(f)
# subtrees of the config file
MQTT_CONF = config["mqtt"] # required section
WEBSERVER_CONF = config["web_server"] # required section
WEBUI_CONF = config.get("webui", {}) # if missing, assume defaults
# "base" topic - should match shairport-sync.conf {mqtt.topic}
TOPIC_ROOT = MQTT_CONF["topic"]
print(TOPIC_ROOT)
# this variable will keep the most recent track info pieces sent to socketio
SAVED_INFO = {}
app = Flask(__name__)
app.config["SECRET_KEY"] = WEBSERVER_CONF.get("secret_key", "secret!")
socketio = SocketIO(app)
known_play_metadata_types = {
"songalbum": "songalbum",
"volume": "volume",
"client_ip": "client_ip",
"active_start": "active_start",
"active_end": "active_end",
"play_start": "play_start",
"play_end": "play_end",
"play_flush": "play_flush",
"play_resume": "play_resume",
}
# Other known 'ssnc' include:
# 'PICT': 'show'
known_core_metadata_types = {
"artist": "showArtist",
"album": "showAlbum",
"title": "showTitle",
"genre": "showGenre",
}
def populateTemplateData(config):
"""Use values from config file to form templateData for HTML template.
Set default value if the key is not found in config (second arg in dict.get())
"""
templateData = {}
if config.get("show_player", True):
templateData["showPlayer"] = True
if config.get("show_player_extended", False):
templateData["showPlayerExtended"] = True
if config.get("show_player_shuffle", False):
templateData["showPlayerShuffle"] = True
if config.get("show_player_seeking", False):
templateData["showPlayerSeeking"] = True
if config.get("show_player_stop", False):
templateData["showPlayerStop"] = True
if config.get("show_canvas", False):
templateData["showCanvas"] = True
if config.get("show_update_info", True):
templateData["showUpdateInfo"] = True
if config.get("show_artwork", True):
templateData["showCoverArt"] = True
if config.get("artwork_rounded_corners", False):
templateData["showCoverArtRoundedCorners"] = True
if config.get("show_track_metadata", True):
metadata_types = config.get(
"track_metadata", ["artist", "album", "title"]
) # defaults to these three
for metadata_type in metadata_types:
if metadata_type in known_core_metadata_types:
templateData[known_core_metadata_types[metadata_type]] = True
return templateData
def _form_subtopic_topic(subtopic):
"""Return full topic path given subtopic."""
topic = TOPIC_ROOT + "/" + subtopic
return topic
# Available commands listed in shairport-sync.conf
known_remote_commands = [
"command",
"beginff",
"beginrew",
"mutetoggle",
"nextitem",
"previtem",
"pause",
"playpause",
"play",
"stop",
"playresume",
"shuffle_songs",
"volumedown",
"volumeup",
]
def _generate_remote_command(command):
"""Return MQTT topic and message for a given remote command."""
if command in known_remote_commands:
print(command)
topic = TOPIC_ROOT + "/remote"
msg = command
return topic, msg
else:
raise ValueError("Unknown remote command: {}".format(command))
def on_connect(client, userdata, flags, rc):
"""For when MQTT client receives a CONNACK response from the server.
Adding subscriptions in on_connect() means that they'll be re-subscribed
for lost/re-connections to MQTT server.
"""
# print("Connected with result code {}".format(rc))
subtopic_list = list(known_core_metadata_types.keys())
subtopic_list.extend(list(known_play_metadata_types.keys()))
# if we are not showing cover art, do not subscribe to it
if (populateTemplateData(WEBUI_CONF)).get("showCoverArt"):
subtopic_list.append("cover")
for subtopic in subtopic_list:
topic = _form_subtopic_topic(subtopic)
print("topic", topic, end=" ")
(result, msg_id) = client.subscribe(topic, 0) # QoS==0 should be fine
print(msg_id)
def _guessImageMime(magic):
"""Peeks at leading bytes in binary object to identify image format."""
if magic.startswith(b"\xff\xd8"):
return "image/jpeg"
elif magic.startswith(b"\x89PNG\r\n\x1a\r"):
return "image/png"
else:
return "image/jpg"
def _send_and_store_playing_metadata(metadata_name, message):
"""Forms playing metadata message and sends to browser client using socket.io.
Also saves a copy of sent message (into SAVED_INFO dict), which is used to
resend most-recent event messages in case the browser page is refreshed,
another browser client connects, etc.
Applies a naming convention of prepending string 'playing_' to metadata
name in socketio sent event. Of course the same naming convention is used
on receiving client event.
"""
# print("{} update".format(metadata_name))
msg = {"data": message.payload.decode("utf8")}
emitted_metadata_name = "playing_{}".format(metadata_name)
SAVED_INFO[emitted_metadata_name] = msg
socketio.emit(emitted_metadata_name, msg)
def _send_play_event(metadata_name):
"""Forms play event message and sends to browser client using socket.io."""
print("{}".format(metadata_name))
socketio.emit(metadata_name, metadata_name)
# https://stackoverflow.com/a/1970037
def make_interpolator(left_min, left_max, right_min, right_max):
"""Create factory for an interpolator function."""
# Figure out how 'wide' each range is
leftSpan = left_max - left_min
rightSpan = right_max - right_min
# Compute the scale factor between left and right values
scaleFactor = float(rightSpan) / float(leftSpan)
# create interpolation function using pre-calculated scaleFactor
def interp_fn(value):
return right_min + (value - left_min) * scaleFactor
return interp_fn
# https://github.com/mikebrady/shairport-sync-metadata-reader/blob/master/README.md
# sent as a string "airplay_volume,volume,lowest_volume,highest_volume"
# - airplay_volume is 0.00 down to -30.00, with -144.00 meaning "mute"
volume_scaler = make_interpolator(-30.0, 0, -0.5, 100.0)
def _send_volume_event(metadata_name, message):
"""Forms volume event message and sends to browser client using socket.io."""
print("{}".format(metadata_name))
(airplay_volume, volume, lowest_volume, highest_volume) = message.payload.decode(
"ascii"
).split(",")
volume_as_percent = 0.0
try:
airplay_volume_float = float(airplay_volume)
volume_as_percent = volume_scaler(airplay_volume_float)
except ValueError:
volume_as_percent = 50.0
msg = {"data": int(volume_as_percent)}
socketio.emit(metadata_name, msg)
def on_message(client, userdata, message):
"""Implement callback for when a subscribed-to MQTT message is received."""
if message.topic != _form_subtopic_topic("cover"):
print(message.topic, message.payload)
# Playing track info fields
if message.topic == _form_subtopic_topic("artist"):
_send_and_store_playing_metadata("artist", message)
if message.topic == _form_subtopic_topic("album"):
_send_and_store_playing_metadata("album", message)
if message.topic == _form_subtopic_topic("genre"):
_send_and_store_playing_metadata("genre", message)
if message.topic == _form_subtopic_topic("title"):
_send_and_store_playing_metadata("title", message)
# Player state
if message.topic == _form_subtopic_topic("play_start"):
_send_play_event("play_start")
if message.topic == _form_subtopic_topic("play_end"):
_send_play_event("play_end")
if message.topic == _form_subtopic_topic("play_flush"):
_send_play_event("play_flush")
if message.topic == _form_subtopic_topic("play_resume"):
_send_play_event("play_resume")
# volume
if message.topic == _form_subtopic_topic("volume"):
_send_volume_event("volume", message)
# cover art
if message.topic == _form_subtopic_topic("cover"):
# print("cover update")
if message.payload:
mime_type = _guessImageMime(message.payload)
image_b64_str = base64.b64encode(message.payload).decode("utf-8")
else:
mime_type = default_image_mime_type
image_b64_str = default_image_b64_str
msg = {"data": image_b64_str, "mimetype": mime_type}
SAVED_INFO["cover_art"] = msg
socketio.emit("cover_art", msg)
# Configure MQTT broker connection
mqttc = mqtt.Client()
# register callbacks
mqttc.on_connect = on_connect
mqttc.on_message = on_message
if MQTT_CONF.get("use_tls"):
tls_conf = MQTT_CONF.get("tls")
print("Using TLS config", tls_conf)
# assumes full valid TLS configuration for paho lib
if tls_conf:
mqttc.tls_set(
ca_certs=tls_conf["ca_certs_path"],
certfile=tls_conf["certfile_path"],
keyfile=tls_conf["keyfile_path"],
cert_reqs=ssl.CERT_REQUIRED,
tls_version=ssl.PROTOCOL_TLSv1_2,
ciphers=None,
)
if tls_conf.get("allow_insecure_server_certificate", False):
# from docs: Do not use this function in a real system. Setting value
# to True means there is no point using encryption.
mqttc.tls_insecure_set(True)
if MQTT_CONF.get("username"):
username = MQTT_CONF.get("username")
print("MQTT username:", username)
pw = MQTT_CONF.get("password")
if pw:
mqttc.username_pw_set(username, password=pw)
else:
mqttc.username_pw_set(username)
if MQTT_CONF.get("logger"):
print("Enabling MQTT logging")
mqttc.enable_logger()
# Launch MQTT broker connection
mqtt_host = MQTT_CONF["host"]
mqtt_port = MQTT_CONF["port"]
print("Connecting to broker", mqtt_host, "port", mqtt_port)
mqttc.connect(mqtt_host, port=mqtt_port)
# loop_start run a thread in the background
mqttc.loop_start()
templateData = populateTemplateData(WEBUI_CONF)
# Define Flask server routes
@app.route("/")
def main():
return render_template("main.html", async_mode=socketio.async_mode, **templateData)
@app.route("/favicon.ico")
def favicon():
"""Handle favicon.ico requests."""
return send_from_directory(
os.path.join(app.root_path, "static"),
"img/favicon.ico",
mimetype="image/vnd.microsoft.icon",
)
@socketio.on("myevent")
def handle_my_custom_event(json):
"""Re-send now playing info.
Using this event, typically emitted from browser client, to re-send now
playing info on a page reload, e.g.
"""
# print('received data: ' + str(json))
if json.get("data"):
print("myevent:", json["data"])
for key, msg in SAVED_INFO.items():
# print(key, msg)
print(key,)
socketio.emit(key, msg)
@socketio.on("remote_previtem")
def handle_previtem(json):
print("handle_previtem", str(json))
(topic, msg) = _generate_remote_command("previtem")
mqttc.publish(topic, msg)
@socketio.on("remote_nextitem")
def handle_nextitem(json):
print("handle_nextitem", str(json))
(topic, msg) = _generate_remote_command("nextitem")
mqttc.publish(topic, msg)
# what 'stop' does is not desired; cannot be resumed
@socketio.on("remote_stop")
def handle_stop(json):
print("handle_stop", str(json))
print("WARNING: remote_stop cannot be resumed")
(topic, msg) = _generate_remote_command("stop")
mqttc.publish(topic, msg)
@socketio.on("remote_pause")
def handle_pause(json):
print("handle_pause", str(json))
(topic, msg) = _generate_remote_command("pause")
mqttc.publish(topic, msg)
@socketio.on("remote_playpause")
def handle_playpause(json):
print("handle_playpause", str(json))
(topic, msg) = _generate_remote_command("playpause")
mqttc.publish(topic, msg)
@socketio.on("remote_play")
def handle_play(json):
print("handle_play", str(json))
(topic, msg) = _generate_remote_command("play")
mqttc.publish(topic, msg)
@socketio.on("remote_playresume")
def handle_playresume(json):
print("handle_playresume", str(json))
(topic, msg) = _generate_remote_command("playresume")
mqttc.publish(topic, msg)
@socketio.on("remote_mutetoggle")
def handle_mutetoggle(json):
print("handle_mutetoggle", str(json))
(topic, msg) = _generate_remote_command("mutetoggle")
mqttc.publish(topic, msg)
@socketio.on("remote_volumedown")
def handle_volumedown(json):
print("handle_volumedown", str(json))
(topic, msg) = _generate_remote_command("volumedown")
mqttc.publish(topic, msg)
@socketio.on("remote_volumeup")
def handle_volumeup(json):
print("handle_volumeup", str(json))
(topic, msg) = _generate_remote_command("volumeup")
mqttc.publish(topic, msg)
@socketio.on("remote_beginrew")
def handle_beginrew(json):
print("handle_beginrew", str(json))
(topic, msg) = _generate_remote_command("beginrew")
mqttc.publish(topic, msg)
@socketio.on("remote_beginff")
def handle_beginff(json):
print("handle_beginff", str(json))
(topic, msg) = _generate_remote_command("beginff")
mqttc.publish(topic, msg)
@socketio.on("remote_shuffle_songs")
def handle_shuffle_songs(json):
print("handle_shuffle_songs", str(json))
(topic, msg) = _generate_remote_command("shuffle_songs")
mqttc.publish(topic, msg)
# launch the Flask (+socketio) webserver!
if __name__ == "__main__":
web_host = WEBSERVER_CONF["host"]
web_port = WEBSERVER_CONF["port"]
web_debug = WEBSERVER_CONF["debug"]
print("Starting webserver")
print(" http://{}:{}".format(web_host, web_port))
socketio.run(app, host=web_host, port=web_port, debug=web_debug)