first threading version

This commit is contained in:
2017-10-31 17:25:52 +01:00
parent f9f2bfcdd7
commit e2fcb44601
2 changed files with 79 additions and 37 deletions
+57 -37
View File
@@ -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)
+22
View File
@@ -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)