-import logging, logging.handlers, os, time, queue, threading
+import logging, logging.handlers, os, time, queue, threading, subprocess, multiprocessing
# logging function
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
-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