first threading version
This commit is contained in:
+56
-36
@@ -8,6 +8,8 @@ import select # for polling zbarcam, see http://stackoverflow.com/a/10759061/37
|
|||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
import urllib
|
import urllib
|
||||||
|
import threading
|
||||||
|
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')
|
||||||
logging.info('Initializing')
|
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')
|
MUSIC_EXTENSIONS = ('.aac', '.ac3', '.aiff', '.amr', '.au', '.flac', '.m4a', '.mid', '.mka', '.mp3', '.ogg', '.ra', '.voc', '.wav', '.wma')
|
||||||
|
|
||||||
REQUEST_TIMEOUT = 5
|
REQUEST_TIMEOUT = 5
|
||||||
|
qr_scanning = False
|
||||||
creating_playlist = False
|
creating_playlist = False
|
||||||
request_running = False
|
|
||||||
|
|
||||||
# photo sensor on PIN 17
|
# photo sensor on PIN 17
|
||||||
PIN_SENSOR = 17
|
PIN_SENSOR = 17
|
||||||
@@ -90,6 +92,49 @@ def list_files(dir):
|
|||||||
r.append(os.path.join(path, name))
|
r.append(os.path.join(path, name))
|
||||||
return r
|
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):
|
def prev_callback(channel):
|
||||||
@@ -112,16 +157,11 @@ def next_callback(channel):
|
|||||||
logging.info("NEXT")
|
logging.info("NEXT")
|
||||||
call('core.playback.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_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_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_NEXT, GPIO.FALLING, callback=next_callback, bouncetime=400)
|
||||||
|
|
||||||
#GPIO.add_event_detect(PIN_SENSOR, GPIO.RISING, callback=sensor_callback, bouncetime=400)
|
|
||||||
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# wait for the web server to be available
|
# wait for the web server to be available
|
||||||
@@ -142,6 +182,12 @@ try:
|
|||||||
GPIO.wait_for_edge(PIN_SENSOR, GPIO.RISING)
|
GPIO.wait_for_edge(PIN_SENSOR, GPIO.RISING)
|
||||||
|
|
||||||
logging.info('Photo sensor active, activating light and camera')
|
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)
|
play_sound(SOUND_SCANNING)
|
||||||
|
|
||||||
# turn LED on
|
# turn LED on
|
||||||
@@ -159,6 +205,8 @@ try:
|
|||||||
while ((time.time() - start_time) < QR_SCANNER_TIMEOUT and (not poll_result)):
|
while ((time.time() - start_time) < QR_SCANNER_TIMEOUT and (not poll_result)):
|
||||||
poll_result = poll_obj.poll(100)
|
poll_result = poll_obj.poll(100)
|
||||||
|
|
||||||
|
qr_scanning = False
|
||||||
|
|
||||||
if (poll_result):
|
if (poll_result):
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -172,38 +220,10 @@ try:
|
|||||||
qr_code = qr_code.decode("utf-8") # python3
|
qr_code = qr_code.decode("utf-8") # python3
|
||||||
logging.info("QR Code: " + qr_code)
|
logging.info("QR Code: " + qr_code)
|
||||||
|
|
||||||
if (not (qr_code.startswith("http://") or qr_code.startswith("spotify:"))):
|
Thread(target = create_playlist, args=(qr_code,)).start()
|
||||||
# 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:
|
except FileNotFoundError as e:
|
||||||
|
creating_playlist = False
|
||||||
logging.error("Could not open directory {}".format(directory))
|
logging.error("Could not open directory {}".format(directory))
|
||||||
play_sound(SOUND_PLAYBACK_ERROR)
|
play_sound(SOUND_PLAYBACK_ERROR)
|
||||||
|
|
||||||
|
|||||||
@@ -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)
|
||||||
|
|
||||||
Reference in New Issue
Block a user