From f5d026767cba3f646cd1cbeef4dcc9dde04fc2bd Mon Sep 17 00:00:00 2001 From: mandlm Date: Thu, 7 Jan 2016 16:10:42 +0100 Subject: [PATCH] Added a script to switch mopidy config file --- Moped/SwitchMopidy.py | 67 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) create mode 100755 Moped/SwitchMopidy.py diff --git a/Moped/SwitchMopidy.py b/Moped/SwitchMopidy.py new file mode 100755 index 0000000..f94a5be --- /dev/null +++ b/Moped/SwitchMopidy.py @@ -0,0 +1,67 @@ +#!/usr/bin/python + +import glob +import os +import subprocess + +import RPi.GPIO as GPIO + +mopidyConf = '/etc/mopidy/mopidy.conf' +userDir = '/etc/mopidy/conf.user/' + +def stopMopidy(): + command = ['service', 'mopidy', 'stop'] + subprocess.call(command, shell=False) + +def startMopidy(): + command = ['service', 'mopidy', 'start'] + subprocess.call(command, shell=False) + +def getConfigs(): + return sorted(glob.glob(os.path.join(userDir, '*.conf'))) + +def getCurrentConfig(): + if os.path.islink(mopidyConf): + return os.path.realpath(mopidyConf) + else: + return None + +def getNextConfig(): + currentConfig = getCurrentConfig() + availableConfigs = getConfigs() + currentIndex = availableConfigs.index(currentConfig) + nextIndex = (currentIndex + 1) % len(availableConfigs) + return availableConfigs[nextIndex] + +def setConfig(newConfig): + if os.path.islink(mopidyConf) and os.path.isfile(newConfig): + os.unlink(mopidyConf) + os.symlink(newConfig, mopidyConf) + +def switchConfig(): + setConfig(getNextConfig()) + +def buttonHandler(channel): + print 'Switching to ' + getNextConfig() + try: + stopMopidy() + switchConfig() + startMopidy() + except OSError as e: + print 'Error: ' + e.strerror + +if __name__ == '__main__': + buttonPin = 4 + dummyPin = 15 + + GPIO.setmode(GPIO.BCM) + GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + GPIO.setup(dummyPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) + + GPIO.add_event_detect(buttonPin, GPIO.FALLING, callback=buttonHandler, bouncetime=1000) + + try: + while True: + GPIO.wait_for_edge(dummyPin, GPIO.RISING) + finally: + GPIO.cleanup()