Before putting all into callbacks
This commit is contained in:
+40
-22
@@ -23,6 +23,10 @@ QR_SCANNER_TIMEOUT = 3
|
||||
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
|
||||
creating_playlist = False
|
||||
request_running = False
|
||||
|
||||
# photo sensor on PIN 17
|
||||
PIN_SENSOR = 17
|
||||
|
||||
@@ -53,15 +57,18 @@ GPIO.setup(PIN_PLAY, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
GPIO.setup(PIN_NEXT, GPIO.IN, pull_up_down=GPIO.PUD_UP)
|
||||
|
||||
|
||||
|
||||
def call(method, params=None):
|
||||
if (params is None):
|
||||
json = '{"jsonrpc": "2.0", "id": 1, "method": "' + method + '"}'
|
||||
else:
|
||||
json = '{"jsonrpc": "2.0", "id": 1, "method": "' + method + '" , "params": ' + params + '}'
|
||||
|
||||
response = requests.post(MOPIDY_RPC_URL, data=json)
|
||||
logging.debug(response.text)
|
||||
logging.info('Request: ' + json)
|
||||
try:
|
||||
response = requests.post(MOPIDY_RPC_URL, data=json, timeout=REQUEST_TIMEOUT)
|
||||
except:
|
||||
logging.error('Exeption during request')
|
||||
logging.info('Response: ' + response.text)
|
||||
return response
|
||||
|
||||
def add_uri(filename):
|
||||
@@ -85,44 +92,56 @@ def list_files(dir):
|
||||
|
||||
|
||||
|
||||
def btn1_callback(channel):
|
||||
logging.debug("PREV")
|
||||
def prev_callback(channel):
|
||||
logging.info("PREV")
|
||||
json = call('core.playback.get_time_position').json()
|
||||
if (json['result'] < 4000):
|
||||
call('core.playback.previous')
|
||||
else:
|
||||
call('core.playback.seek', '{ "time_position": 0 }')
|
||||
|
||||
def btn2_callback(channel):
|
||||
logging.debug("PLAY/PAUSE")
|
||||
def play_callback(channel):
|
||||
logging.info("PLAY/PAUSE")
|
||||
json = call('core.playback.get_state').json()
|
||||
if (json['result'] == 'playing'):
|
||||
call('core.playback.pause')
|
||||
else:
|
||||
call('core.playback.play')
|
||||
|
||||
def btn3_callback(channel):
|
||||
logging.debug("NEXT")
|
||||
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=btn1_callback, bouncetime=400)
|
||||
GPIO.add_event_detect(PIN_PLAY, GPIO.FALLING, callback=btn2_callback, bouncetime=400)
|
||||
GPIO.add_event_detect(PIN_NEXT, GPIO.FALLING, callback=btn3_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_NEXT, GPIO.FALLING, callback=next_callback, bouncetime=400)
|
||||
|
||||
#GPIO.add_event_detect(PIN_SENSOR, GPIO.RISING, callback=sensor_callback, bouncetime=400)
|
||||
|
||||
|
||||
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)
|
||||
# wait for the web server to be available
|
||||
response = ''
|
||||
while response == '':
|
||||
try:
|
||||
logging.info("Trying to connect to the server...")
|
||||
response = requests.post(MOPIDY_RPC_URL, data='{"jsonrpc": "2.0", "id": 1, "method": "core.tracklist.clear"}', timeout=REQUEST_TIMEOUT)
|
||||
except:
|
||||
logging.debug("Connection refused by the server, wait 5 seconds")
|
||||
time.sleep(5)
|
||||
continue
|
||||
|
||||
play_sound(SOUND_OK)
|
||||
|
||||
while True:
|
||||
logging.debug('Wait for photo sensor')
|
||||
logging.info('Wait for photo sensor')
|
||||
GPIO.wait_for_edge(PIN_SENSOR, GPIO.RISING)
|
||||
|
||||
logging.debug('Photo sensor active, activating light and camera')
|
||||
logging.info('Photo sensor active, activating light and camera')
|
||||
play_sound(SOUND_SCANNING)
|
||||
|
||||
# turn LED on
|
||||
@@ -147,7 +166,7 @@ try:
|
||||
play_sound(SOUND_OK)
|
||||
|
||||
call('core.tracklist.clear')
|
||||
logging.debug("Stopped")
|
||||
logging.info("Stopped")
|
||||
|
||||
qr_code = zbarcam.stdout.readline().rstrip()
|
||||
qr_code = qr_code.decode("utf-8") # python3
|
||||
@@ -155,6 +174,8 @@ try:
|
||||
|
||||
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)):
|
||||
@@ -165,9 +186,8 @@ try:
|
||||
if filename.lower().endswith(MUSIC_EXTENSIONS):
|
||||
add_uri("file://" + urllib.parse.quote(filename))
|
||||
if (not playback_started):
|
||||
call('core.playback.play')
|
||||
playback_started = True
|
||||
|
||||
call('core.playback.play')
|
||||
|
||||
else:
|
||||
logging.debug("It's a file")
|
||||
@@ -192,8 +212,6 @@ try:
|
||||
play_sound(SOUND_SCAN_FAIL)
|
||||
|
||||
zbarcam.terminate()
|
||||
|
||||
# LED off
|
||||
GPIO.output(PIN_LED, GPIO.LOW)
|
||||
|
||||
# wait until sensor is not blocked anymore
|
||||
|
||||
Reference in New Issue
Block a user