This commit is contained in:
2017-10-11 01:13:07 +02:00
parent 732996ce62
commit b46a20f6f9
2 changed files with 42 additions and 12 deletions
+18 -7
View File
@@ -9,7 +9,7 @@ import os
import requests
import urllib
logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(levelname)s - %(message)s')
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s')
logging.info('Initializing')
# Configuration
@@ -20,7 +20,7 @@ SOUND_SCAN_FAIL = "file:///home/pi/sounds/fail.mp3"
SOUND_PLAYBACK_ERROR = "file:///home/pi/sounds/error.mp3"
QR_SCANNER_TIMEOUT = 3
MOPIDY_URL = 'http://127.0.0.1:6680/mopidy/rpc'
MOPIDY_RPC_URL = 'http://127.0.0.1:6680/mopidy/rpc'
MUSIC_EXTENSIONS = ('.aac', '.ac3', '.aiff', '.amr', '.au', '.flac', '.m4a', '.mid', '.mka', '.mp3', '.ogg', '.ra', '.voc', '.wav', '.wma')
# photo sensor on PIN 17
@@ -60,7 +60,7 @@ def call(method, params=None):
else:
json = '{"jsonrpc": "2.0", "id": 1, "method": "' + method + '" , "params": ' + params + '}'
response = requests.post(MOPIDY_URL, data=json)
response = requests.post(MOPIDY_RPC_URL, data=json)
logging.debug(response.text)
return response
@@ -70,7 +70,7 @@ def add_uri(filename):
def play_sound(filename):
call('core.tracklist.clear')
logging.debug('Cleared tracklist, adding ' + filename)
logging.debug('Cleared tracklist, now playing ' + filename)
add_uri(filename)
call('core.playback.play')
@@ -86,6 +86,7 @@ def list_files(dir):
def btn1_callback(channel):
logging.debug("PREV")
json = call('core.playback.get_time_position').json()
position = json['result']
if (position < 4):
@@ -94,6 +95,7 @@ def btn1_callback(channel):
call('core.playback.seek', '{ "time_position": 0 }')
def btn2_callback(channel):
logging.debug("PLAY/PAUSE")
json = call('core.playback.get_time_position').json()
state = json['result']
if (state == 'playing'):
@@ -102,6 +104,7 @@ def btn2_callback(channel):
call('core.playback.play')
def btn3_callback(channel):
logging.debug("NEXT")
call('core.playback.next')
@@ -111,6 +114,10 @@ GPIO.add_event_detect(PIN_NEXT, GPIO.FALLING, callback=btn3_callback, bouncetime
try:
# FIXME turn this into a proper service and wait for the network interface (and mopidy)
# https://www.dexterindustries.com/howto/run-a-program-on-your-raspberry-pi-at-startup/#systemd
time.sleep(20)
play_sound(SOUND_OK)
while True:
@@ -155,20 +162,24 @@ try:
if (not os.path.isfile(full_path)):
logging.debug("Not a file, recursively add all audio files in directory")
filenames = list_files(full_path)
playback_started = False
for filename in filenames:
if filename.lower().endswith(MUSIC_EXTENSIONS):
add_uri("file://" + urllib.parse.quote(filename))
if (not playback_started):
call('core.playback.play')
playback_started = True
else:
logging.debug("It's a file")
add_uri("file://" + urllib.parse.quote(full_path))
call('core.playback.play')
else:
logging.debug("URL, open directly")
add_uri(qr_code)
logging.debug("Start playback")
call('core.playback.play')
call('core.playback.play')
except subprocess.CalledProcessError as e:
logging.error("Error starting playback, mocp returned {}".format(e.returncode))