-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjukebox.py
More file actions
114 lines (91 loc) · 3.42 KB
/
jukebox.py
File metadata and controls
114 lines (91 loc) · 3.42 KB
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
#!/usr/bin/python3
import logging
import os
import sys
import yaml
from mpd import MPDClient
from pymemcache.client import base
if sys.version_info >= (3, 8):
from systemd.journal import JournalHandler
journald_handler = JournalHandler()
else:
from systemd.journal import JournaldLogHandler
journald_handler = JournaldLogHandler()
log = logging.getLogger(__name__)
log.addHandler(journald_handler)
log.setLevel(logging.DEBUG)
def load_yaml_config(filename):
with open(filename, 'r') as file:
config = yaml.safe_load(file)
return config
class Jukebox:
FILE_CONFIG_CARDS = "configuration/cards.yaml"
FILE_CONFIG_JUKEBOX = "configuration/jukebox.yaml"
def __init__(self):
self.config = load_yaml_config(self.FILE_CONFIG_JUKEBOX)
self.cards = load_yaml_config(self.FILE_CONFIG_CARDS)
memcache_host = self.config['memcache'].get('host', '127.0.0.1' )
memcache_port = self.config['memcache'].get('port', 11211 )
self.memc = base.Client((memcache_host, memcache_port))
mpd_host = self.config['mpd'].get('host', 'localhost' )
mpd_port = self.config['mpd'].get('port', 6600 )
self.mpdc = MPDClient()
self.mpdc.connect(mpd_host, mpd_port)
self.card_volume_up = self.config['controlcards']['volume_up']
self.card_volume_down = self.config['controlcards']['volume_up']
self.card_pause = self.config['controlcards']['pause']
self.card_clear = self.config['controlcards']['clear']
self.card_shutdown = self.config['controlcards']['shutdown']
def handle_card(self, card):
log.debug("Handle card: '%s'.", card)
last_card = self.find_last_card()
if self.card_pause == card or last_card == card:
self.play_pause()
elif self.card_volume_up == card :
self.volume(10)
elif self.card_volume_down == card:
self.volume(-10)
elif self.card_clear == card:
self.clear()
elif self.card_shutdown == card:
self.clear()
self.shutdown()
else:
self.play(card)
self.mpdc.disconnect()
def play(self, card):
log.debug("Play card: '%s'.", card)
if card in self.cards:
self.mpdc.clear()
command = "mpc insert " + self.cards[card]
os.system(command)
self.mpdc.play()
self.memc.set('last_card', card)
else:
log.error("Card '%s' not found.", card)
def play_pause(self):
log.debug("Play or pause.")
if self.mpdc.status()["state"] == "stop":
self.mpdc.play()
else:
self.mpdc.pause()
def clear(self):
log.debug("Stop and clear all.")
self.mpdc.stop()
self.mpdc.clear()
self.mpdc.setvol(self.config['mpd']['startvolume'])
self.memc.set('last_card', '')
def volume(self, vol_change):
log.debug("Change volume: '%s'.", vol_change)
self.mpdc.volume(vol_change)
def shutdown(self):
log.debug("Shutdown now.")
os.system("sudo shutdown now")
def find_last_card(self):
optional_last_card = self.memc.get('last_card')
if optional_last_card is not None:
last_card = optional_last_card.decode("utf-8")
else:
last_card = ""
log.debug("Find last card: '%s'.", last_card)
return last_card