From e2fcb446014815f7eaa2994076bf3a9dfe9a1d44 Mon Sep 17 00:00:00 2001 From: Tilman Date: Tue, 31 Oct 2017 17:25:52 +0100 Subject: [PATCH] first threading version --- code/piplayer3.py | 94 +++++++++++++++++++++++++++----------------- code/test/threads.py | 22 +++++++++++ 2 files changed, 79 insertions(+), 37 deletions(-) create mode 100644 code/test/threads.py diff --git a/code/piplayer3.py b/code/piplayer3.py index ab2ab8c..e9f42ce 100644 --- a/code/piplayer3.py +++ b/code/piplayer3.py @@ -8,6 +8,8 @@ import select # for polling zbarcam, see http://stackoverflow.com/a/10759061/37 import os import requests import urllib +import threading +from threading import Thread logging.basicConfig(level=logging.INFO, format='%(asctime)s %(levelname)s - %(message)s') logging.info('Initializing') @@ -24,8 +26,8 @@ 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') REQUEST_TIMEOUT = 5 +qr_scanning = False creating_playlist = False -request_running = False # photo sensor on PIN 17 PIN_SENSOR = 17 @@ -90,6 +92,49 @@ def list_files(dir): r.append(os.path.join(path, name)) return r +def create_playlist(qr_code): + logging.info('Creating playlist') + + creating_playlist = True + + if (qr_scanning): + creating_playlist = False + logging.info('Stopping creating playlist') + return + + if (not (qr_code.startswith("http://") or qr_code.startswith("spotify:"))): + # create full path + if (qr_code.startswith("/")): + qr_code = qr_code[1:] + full_path = MUSIC_BASE_DIRECTORY + qr_code + + 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 a new code is being scanned, stop adding to the playlist + if (qr_scanning): + creating_playlist = False + logging.info('Stopping creating playlist') + return + + if filename.lower().endswith(MUSIC_EXTENSIONS): + add_uri("file://" + urllib.parse.quote(filename)) + if (not playback_started): + playback_started = True + call('core.playback.play') + + 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) + call('core.playback.play') + def prev_callback(channel): @@ -112,16 +157,11 @@ def next_callback(channel): logging.info("NEXT") call('core.playback.next') -def sensor_callback(channel): - logging.info("SENSOR!!!!! ================================================================= !!!11!1elf") - GPIO.add_event_detect(PIN_PREV, GPIO.FALLING, callback=prev_callback, bouncetime=400) GPIO.add_event_detect(PIN_PLAY, GPIO.FALLING, callback=play_callback, bouncetime=400) GPIO.add_event_detect(PIN_NEXT, GPIO.FALLING, callback=next_callback, bouncetime=400) -#GPIO.add_event_detect(PIN_SENSOR, GPIO.RISING, callback=sensor_callback, bouncetime=400) - try: # wait for the web server to be available @@ -142,6 +182,12 @@ try: GPIO.wait_for_edge(PIN_SENSOR, GPIO.RISING) logging.info('Photo sensor active, activating light and camera') + qr_scanning = True + + while (creating_playlist): + logging.info('Waiting for the playlist creation to stop') + time.sleep(1) + play_sound(SOUND_SCANNING) # turn LED on @@ -158,6 +204,8 @@ try: poll_result = False while ((time.time() - start_time) < QR_SCANNER_TIMEOUT and (not poll_result)): poll_result = poll_obj.poll(100) + + qr_scanning = False if (poll_result): @@ -171,39 +219,11 @@ try: qr_code = zbarcam.stdout.readline().rstrip() qr_code = qr_code.decode("utf-8") # python3 logging.info("QR Code: " + qr_code) + + Thread(target = create_playlist, args=(qr_code,)).start() - if (not (qr_code.startswith("http://") or qr_code.startswith("spotify:"))): - # create full path - if (qr_code.startswith("/")): - qr_code = qr_code[1:] - full_path = MUSIC_BASE_DIRECTORY + qr_code - - 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): - playback_started = True - call('core.playback.play') - - 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) - call('core.playback.play') - - except subprocess.CalledProcessError as e: - logging.error("Error starting playback, mocp returned {}".format(e.returncode)) - logging.error(e.output) - play_sound(SOUND_PLAYBACK_ERROR) except FileNotFoundError as e: + creating_playlist = False logging.error("Could not open directory {}".format(directory)) play_sound(SOUND_PLAYBACK_ERROR) diff --git a/code/test/threads.py b/code/test/threads.py new file mode 100644 index 0000000..71b3c19 --- /dev/null +++ b/code/test/threads.py @@ -0,0 +1,22 @@ +import threading +from threading import Thread +import time + +def func1(): + for x in range(0, 3): + print('Working 1') + time.sleep(1) + + +def func2(): + for x in range(0, 3): + print('Working 2') + time.sleep(1) + +if __name__ == '__main__': + Thread(target = func1).start() + Thread(target = func2).start() + for x in range(0, 3): + print('Working MAIN') + time.sleep(1) +