-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmpc_lirc.py
executable file
·122 lines (109 loc) · 4.17 KB
/
mpc_lirc.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Do not forget to run "mpc update" before using other controls.
import lirc
import logging
import os
import subprocess
import sys
import time
# https://github.com/sdiemer/django-web-utils
from django_web_utils.daemon.base import BaseDaemon
logger = logging.getLogger('mpc_lirc')
USER = 'pi'
BASE_DIR = os.path.dirname(os.path.abspath(os.path.expanduser(__file__)))
class MopidyController(BaseDaemon):
DAEMON_NAME = 'mpc_lirc'
SERVER_DIR = BASE_DIR
LOG_DIR = BASE_DIR
CONF_DIR = BASE_DIR
PID_DIR = BASE_DIR
NEED_DJANGO = False
# DEFAULTS = dict(LOGGING_LEVEL='INFO')
def run(self, *args):
root_dir = os.path.dirname(self.daemon_path)
if root_dir.endswith('/.'):
root_dir = root_dir[:-2]
if root_dir != '':
os.chdir(root_dir)
self.playlist_index = 0
# Play a sound to indicate the daemon is launched
# (usefull when no screen is available)
self.play_sound('online')
self.start_listening()
def execute(self, cmd):
p = subprocess.Popen(cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
out, err = p.communicate()
out = out.decode('utf-8') if out else ''
if err:
if out:
out += '\n'
out += 'Stderr: ' + err.decode('utf-8')
logger.debug('>>> %s', cmd)
logger.debug('code: %s | %s', p.returncode, out)
return p.returncode == 0, out
def play_sound(self, name):
self.execute(['aplay', 'sounds/%s.wav' % name])
def load_playlist(self, index):
self.execute(['/usr/bin/mpc', 'clear'])
loaded, out = self.execute(['/usr/bin/mpc', 'load', 'playlist_%s' % index])
if loaded:
self.execute(['/usr/bin/mpc', 'play'])
self.playlist_index = index
def start_listening(self):
logger.info('Using lircrc.conf: %s', os.path.join(BASE_DIR, 'lircrc.conf'))
lirc.init('mpc_lirc', os.path.join(BASE_DIR, 'lircrc.conf'))
logger.info('Ready to receive key events.')
while True:
try:
received = lirc.nextcode()
except Exception as e:
logger.warning('lirc: Failed to get next code: %s', e)
time.sleep(3)
else:
if received:
self.play_sound('received')
self.key_pressed(received[0])
def key_pressed(self, key):
if key == 'KEY_PLAYPAUSE':
self.execute(['/usr/bin/mpc', 'toggle'])
elif key == 'KEY_VOLUMEUP':
self.execute(['/usr/bin/mpc', 'volume', '+5'])
elif key == 'KEY_VOLUMEDOWN':
self.execute(['/usr/bin/mpc', 'volume', '-5'])
elif key == 'KEY_PREVIOUS':
self.execute(['/usr/bin/mpc', 'prev'])
elif key == 'KEY_NEXT':
self.execute(['/usr/bin/mpc', 'next'])
elif key == 'KEY_F1':
self.execute([os.path.join(BASE_DIR, 'screen.sh'), 'on'])
elif key == 'KEY_F2':
self.execute([os.path.join(BASE_DIR, 'screen.sh'), 'off'])
elif key == 'KEY_HOME':
self.execute(['/usr/bin/mpc', 'shuffle'])
elif key == 'KEY_MODE':
self.play_sound('shutdown')
self.execute(['sudo', 'shutdown', '-h', 'now'])
lirc.deinit()
sys.exit(0)
elif key == 'KEY_CHANNELDOWN':
i = self.playlist_index - 1
if i >= 0:
self.load_playlist(i)
elif key == 'KEY_CHANNELUP':
i = self.playlist_index + 1
if i <= 9:
self.load_playlist(i)
elif key in ('KEY_0', 'KEY_1', 'KEY_2', 'KEY_3', 'KEY_4', 'KEY_5', 'KEY_6', 'KEY_7', 'KEY_8', 'KEY_9'):
self.load_playlist(int(key[4]))
else:
logger.info('Received unbinded key: %s', key)
if __name__ == '__main__':
user = subprocess.getoutput('whoami')
if user != USER:
# switch from root to requested user
print('Using user %s.' % USER)
from django_web_utils.system_utils import run_as
run_as(USER)
daemon = MopidyController(sys.argv)
daemon.start()