X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=switch-power.py;h=cb0ada87688cbafcc852220650c1a37724b245dc;hb=02319119bfafbf48ef331090f7b4c2800901d6c5;hp=1ee8421324158e1ad1883d3c9c97e679e7b69359;hpb=a77ea26fe191866073a2407e20b71487dd9fbac0;p=svn42.git diff --git a/switch-power.py b/switch-power.py index 1ee8421..cb0ada8 100755 --- a/switch-power.py +++ b/switch-power.py @@ -15,6 +15,7 @@ import subprocess import types import ConfigParser import datetime +import traceback logger = logging.getLogger() logger.setLevel(logging.INFO) @@ -36,7 +37,7 @@ class UWSConfig: self.config_parser.set('slug','ids_nonpresent_off','idee schreibtisch labor werkzeug stereo logo') #self.config_parser.set('slug','time_day','6:00-17:00') self.config_parser.add_section('debug') - self.config_parser.set('debug','enabled',"True") + self.config_parser.set('debug','enabled',"False") self.config_parser.add_section('tracker') self.config_parser.set('tracker','socket',"/var/run/tuer/presence.socket") self.config_mtime=0 @@ -109,22 +110,34 @@ def switchPower(powerid,turn_on=False): else: onoff="off" touchURL(uwscfg.slug_cgiuri.replace("%ID%",powerid).replace("%ONOFF%",onoff)) - -def eventPresent(): + +def haveDaylight(): + dawn_per_month = {1:8, 2:7, 3:6, 4:6, 5:5, 6:5, 7:5, 8:6, 9:7, 10:8, 11:8, 12:8} + dusk_per_month = {1:16, 2:17, 3:18, 4:20, 5:21, 6:21, 7:21, 8:20, 9:19, 10:18, 11:16, 12:16} hour = datetime.datetime.now().hour - if hour > 6 and hour < 18: + month = datetime.datetime.now().month + return (hour >= dawn_per_month[month] and hour < dusk_per_month[month]) + +######### EVENTS ############### + +def eventPresent(): + if haveDaylight(): present_ids=uwscfg.slug_ids_present_day else: present_ids=uwscfg.slug_ids_present_night + logging.info("event: someone present, switching on: "+present_ids) for id in present_ids.split(" "): switchPower(id,True) def eventNobodyHere(): - for id in uwscfg.slug_ids_nonpresent_off.split(" "): + present_ids=uwscfg.slug_ids_nonpresent_off + logging.info("event: noone here, switching off: "+present_ids) + for id in present_ids.split(" "): switchPower(id,False) switchPower(id,False) def eventPanic(): + logging.info("event: Panic:, switching around: "+uwscfg.slug_ids_panic) lst1 = uwscfg.slug_ids_panic.split(" ") lst2 = lst1 lst2.append(lst2.pop(0)) @@ -141,6 +154,8 @@ def eventPanic(): switchPower(id,False) eventPresent() +######################## + def exitHandler(signum, frame): logging.info("Power Switch Daemon stopping") try: @@ -166,7 +181,7 @@ else: uwscfg = UWSConfig() #socket.setdefaulttimeout(10.0) #affects all new Socket Connections (urllib as well) -RE_PRESENCE = re.compile(r'Presence: (yes|no)') +RE_PRESENCE = re.compile(r'Presence: (yes|no)(?:, (opened|closed), (.+))?') RE_BUTTON = re.compile(r'PanicButton|button\d?') while True: try: @@ -201,7 +216,8 @@ while True: continue except Exception, ex: - logging.error("main: "+str(ex)) + logging.error("main: "+str(ex)) + traceback.print_exc(file=sys.stdout) try: sockhandle.close() except: