From: Constantin Berhard Date: Fri, 4 Oct 2013 10:16:01 +0000 (+0200) Subject: sound support X-Git-Url: https://git.ralfj.de/saartuer.git/commitdiff_plain/5a866fd8add83733d9e1cde55ecbd23f69661658?ds=sidebyside;hp=-c sound support --- 5a866fd8add83733d9e1cde55ecbd23f69661658 diff --git a/libtuer.py b/libtuer.py index 60f491b..e31ce27 100644 --- a/libtuer.py +++ b/libtuer.py @@ -1,4 +1,4 @@ -import logging, logging.handlers, os, time, queue, threading +import logging, logging.handlers, os, time, queue, threading, subprocess, multiprocessing # logging function class Logger: @@ -28,6 +28,17 @@ class Logger: logger = Logger() +# run a command asynchronously and log the return value if not 0 +# prefix must be a string identifying the code position where the call came from +def fire_and_forget (cmd, log, prefix): + def _fire_and_forget (cmd, log, prefix): + with open("/dev/null", "w") as fnull: + retcode = subprocess.call(cmd, stdout=fnull, stderr=fnull) + if retcode is not 0: + log("%sReturn code %d at command: %s" % (prefix,retcode,str(cmd))) + p = multiprocessing.Process(target=_fire_and_forget, args=(cmd,log,prefix)) + p.start() + # Threaded callback class class ThreadFunction(): _CALL = 0 diff --git a/statemachine.py b/statemachine.py index b3d1e2a..c93a1a3 100644 --- a/statemachine.py +++ b/statemachine.py @@ -1,18 +1,32 @@ -from libtuer import ThreadFunction, logger +from libtuer import ThreadFunction, logger, fire_and_forget from actor import Actor +import os, random # logger.{debug,info,warning,error,critical} +def play_sound (what): + try: + soundfiles = os.listdir(SOUNDS_DIRECTORY+what) + except FileNotFoundError: + logger.error("StateMachine: Unable to list sound files in %s" % (SOUNDS_DIRECTORY+what)) + return + soundfile = SOUNDS_DIRECTORY + what + '/' + random.choice(soundfiles) + fire_and_forget ([SOUNDS_PLAYER,soundfile], logger.error, "StateMachine: ") + + # StateOpening constants OPEN_REPEAT_TIMEOUT = 8 OPEN_REPEAT_NUMBER = 3 -def play_sound (what): - print ("I would now play the sound %s... IF I HAD SOUNDS!" % what) - # StateAboutToOpen constants -ABOUTOPEN_NERVLIST = [(5, lambda : play_sound("heydrückdenknopf.mp3")), (10, lambda:play_sound("alterichmeinsernst.mp3"))] -# TODO: erzeuge mehr nerv +ABOUTOPEN_NERVLIST = [(5, lambda : play_sound("flipswitch")), (10, lambda:play_sound("flipswitch")), (10, lambda:Logger.warning("Space open but switch not flipped for 10 seconds")),\ + (20, lambda:play_sound("flipswitch")), (30, lambda:play_sound("flipswitch")), (30, lambda:Logger.error("Space open but switch not flipped for 30 seconds")),\ + (40, lambda:play_sound("flipswitch")), (50, lambda:play_sound("flipswitch")), (56, lambda:play_sound("flipswitch")), (60, lambda:Logger.critical("Space open but switch not flipped for 60 seconds"))] + +# play_sound constants +SOUNDS_DIRECTORY = "/opt/tuer/sounds/" +SOUNDS_PLAYER = "/usr/bin/mplayer" + class StateMachine(): # commands you can send