Added a script to switch mopidy config file

This commit is contained in:
mandlm 2016-01-07 16:10:42 +01:00
parent 0e8bc288b8
commit f5d026767c

67
Moped/SwitchMopidy.py Executable file
View File

@ -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()