Update piplayer4.py

This commit is contained in:
2018-11-30 22:25:20 +00:00
parent 537c3ffca1
commit 22ded54d64
+58 -38
View File
@@ -1,14 +1,17 @@
# piplayer4 - AudioLauncher # piplayer4 - AudioLauncher
# http://www.tilman.de/piplayer4 # http://www.tilman.de/piplayer4
# TODO
# logging evtl. ausdünnen
# implement seek forward/back
import RPi.GPIO as GPIO import RPi.GPIO as GPIO
import logging import logging
import time import time
import subprocess import subprocess
import select # for polling zbarcam, see http://stackoverflow.com/a/10759061/3761783 import select # for polling zbarcam, see http://stackoverflow.com/a/10759061/3761783
import os
import urllib
from socketIO_client import SocketIO, LoggingNamespace # https://gist.github.com/ivesdebruycker/4b08bdd5415609ce95e597c1d28e9b9e from socketIO_client import SocketIO, LoggingNamespace # https://gist.github.com/ivesdebruycker/4b08bdd5415609ce95e597c1d28e9b9e
from threading import Thread
logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s') logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s')
@@ -16,10 +19,10 @@ logging.info('Initializing TEST')
# Configuration # Configuration
MUSIC_BASE_DIRECTORY = "mnt/NAS/" MUSIC_BASE_DIRECTORY = "mnt/NAS/"
SOUND_SCANNING = "~/sounds/scanning.mp3" SOUND_SCANNING = "mnt/INTERNAL/sounds/scanning.mp3"
SOUND_OK = "~/sounds/ok.mp3" SOUND_OK = "mnt/INTERNAL/sounds/ok-05.mp3"
SOUND_SCAN_FAIL = "~/sounds/fail.mp3" SOUND_SCAN_FAIL = "mnt/INTERNAL/sounds/fail-05.mp3"
SOUND_PLAYBACK_ERROR = "~/sounds/error.mp3" SOUND_PLAYBACK_ERROR = "mnt/INTERNAL/sounds/error.mp3"
QR_SCANNER_TIMEOUT = 3 QR_SCANNER_TIMEOUT = 3
@@ -49,46 +52,62 @@ GPIO.setup(PIN_PLAY, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(PIN_NEXT, GPIO.IN, pull_up_down=GPIO.PUD_UP) GPIO.setup(PIN_NEXT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
pending_play = False # to determine whether we should send a play command when we receive a pushQueue event pending_play = False # to determine whether we should send a play command after we received a pushQueue event
is_paused = True # for toggling play/pause is_playing = False # for toggling play/pause (stores the current status from the pushState events)
socketIO = SocketIO('127.0.0.1', 3000) socketIO = SocketIO('127.0.0.1', 3000)
def play_sound(filename): def play_sound(filename):
socketIO.emit('clearQueue') socketIO.emit('clearQueue')
logging.debug('Cleared queue, now playing ' + filename) logging.info('Cleared queue, now playing ' + filename)
global pending_play
pending_play = True pending_play = True
socketIO.emit('addToQueue', {'uri':filename}) socketIO.emit('addToQueue', {'service':'mpd','uri':filename})
def prev_callback(channel): def prev_callback(channel):
logging.info("PREVIOUS") logging.info("PREV")
socketIO.emit('previous') socketIO.emit('prev')
## TODO implement seek ## TODO implement seek
def play_callback(channel): def play_callback(channel):
if (is_paused): global is_playing
logging.info("PLAY") if is_playing:
socketIO.emit('play')
is_paused = False
else:
logging.info("PAUSE") logging.info("PAUSE")
socketIO.emit('pause') socketIO.emit('pause')
is_paused = True is_playing = False
else:
logging.info("PLAY")
socketIO.emit('play')
is_playing = True
def next_callback(channel): def next_callback(channel):
logging.info("NEXT") logging.info("NEXT")
socketIO.emit('next') socketIO.emit('next')
## TODO implement seek ## TODO implement seek
def on_pushState(*args):
logging.debug(args[0]['status'])
global is_playing
if args[0]['status'] == 'play':
is_playing = True
else:
is_playing = False
def on_pushQueue(*args): def on_pushQueue(*args):
logging.debug("pushQueue: " + args[0]) logging.info("pushQueue")
logging.info(args[0])
global pending_play
logging.info(pending_play)
if pending_play: if pending_play:
if not args[0]: # if not args[0]:
play_sound(SOUND_PLAYBACK_ERROR) # play_sound(SOUND_PLAYBACK_ERROR)
else: # else:
socketIO.emit('play') socketIO.emit('play')
pending_play = False pending_play = False
def queue_events_thread():
socketIO.wait()
GPIO.add_event_detect(PIN_PREV, GPIO.FALLING, callback=prev_callback, bouncetime=400) GPIO.add_event_detect(PIN_PREV, GPIO.FALLING, callback=prev_callback, bouncetime=400)
@@ -97,16 +116,20 @@ GPIO.add_event_detect(PIN_NEXT, GPIO.FALLING, callback=next_callback, bouncetime
try: try:
# play_sound(SOUND_OK) play_sound(SOUND_OK)
socketIO.on('pushQueue', on_pushQueue)
socketIO.on('pushState', on_pushState)
listener_thread = Thread(target=queue_events_thread)
listener_thread.daemon = True
listener_thread.start()
#socketIO.on('pushQueue', on_pushQueue)
while True: while True:
logging.info('Wait for photo sensor') logging.info('Wait for photo sensor')
GPIO.wait_for_edge(PIN_SENSOR, GPIO.FALLING) # TEST GPIO.wait_for_edge(PIN_SENSOR, GPIO.FALLING) # TEST
logging.info('Photo sensor active, activating light and camera') logging.info('Photo sensor active, activating light and camera')
#play_sound(SOUND_SCANNING) play_sound(SOUND_SCANNING)
# turn LED on # turn LED on
GPIO.output(PIN_LED, GPIO.HIGH) GPIO.output(PIN_LED, GPIO.HIGH)
@@ -126,8 +149,8 @@ try:
if (poll_result): if (poll_result):
# play confirmation sound # play confirmation sound
#play_sound(SOUND_OK) play_sound(SOUND_OK)
#socketIO.emit('removeFromQueue', 0) socketIO.emit('removeFromQueue', 0)
qr_code = zbarcam.stdout.readline().rstrip() qr_code = zbarcam.stdout.readline().rstrip()
qr_code = qr_code.decode("utf-8") # python3 qr_code = qr_code.decode("utf-8") # python3
@@ -135,20 +158,17 @@ try:
pending_play = True pending_play = True
if (not qr_code.startswith("http://")): if qr_code.startswith("http://"):
socketIO.emit('addToQueue', {'service':'webradio','uri':qr_code})
elif qr_code.startswith("spotify:"):
socketIO.emit('addToQueue', {'service':'spop','uri':qr_code})
else:
# create full path # create full path
if (qr_code.startswith("/")): if (qr_code.startswith("/")):
qr_code = qr_code[1:] qr_code = qr_code[1:]
full_path = MUSIC_BASE_DIRECTORY + qr_code full_path = MUSIC_BASE_DIRECTORY + qr_code
logging.info("full_path: " + full_path) logging.info("full_path: " + full_path)
socketIO.emit('addToQueue', {'uri':full_path}) socketIO.emit('addToQueue', {'uri':full_path})
else:
socketIO.emit('addToQueue', {'service':'webradio','uri':qr_code})
###########socketIO.wait()
#https://github.com/invisibleroads/socketIO-client/issues/145
#https://github.com/miguelgrinberg/Flask-SocketIO/issues/194
socketIO.emit('play')
else: else:
logging.warning('Timeout on zbarcam') logging.warning('Timeout on zbarcam')