X-Git-Url: https://git.realraum.at/?a=blobdiff_plain;f=switch-power.py;h=cb0ada87688cbafcc852220650c1a37724b245dc;hb=02319119bfafbf48ef331090f7b4c2800901d6c5;hp=5364de18e250409cb4923c37d1d2571e49da6f6d;hpb=449707893d7c9475f8c749a6e37babe7cf2518cd;p=svn42.git diff --git a/switch-power.py b/switch-power.py index 5364de1..cb0ada8 100755 --- a/switch-power.py +++ b/switch-power.py @@ -14,6 +14,8 @@ import socket import subprocess import types import ConfigParser +import datetime +import traceback logger = logging.getLogger() logger.setLevel(logging.INFO) @@ -29,11 +31,13 @@ class UWSConfig: self.config_parser=ConfigParser.ConfigParser() self.config_parser.add_section('slug') self.config_parser.set('slug','cgiuri','http://slug.realraum.at/cgi-bin/switch.cgi?id=%ID%&power=%ONOFF%') - self.config_parser.set('slug','ids_present','logo werkzeug') + self.config_parser.set('slug','ids_present_day','logo werkzeug') + self.config_parser.set('slug','ids_present_night','logo werkzeug schreibtisch idee labor') self.config_parser.set('slug','ids_panic','idee schreibtisch labor werkzeug') 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 @@ -106,21 +110,39 @@ def switchPower(powerid,turn_on=False): else: onoff="off" touchURL(uwscfg.slug_cgiuri.replace("%ID%",powerid).replace("%ONOFF%",onoff)) - + +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 + month = datetime.datetime.now().month + return (hour >= dawn_per_month[month] and hour < dusk_per_month[month]) + +######### EVENTS ############### + def eventPresent(): - for id in uwscfg.slug_ids_present.split(" "): + 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)) #guarantee list has even number of elements by multiplying it with a factor of 2 - lst=zip(lst1,lst2) * 4 + lst=zip(lst1,lst2) * 8 lst2=None switchPower(lst[0][0],True) for (id1,id2) in lst: @@ -130,6 +152,9 @@ def eventPanic(): time.sleep(0.6) for id in lst1: switchPower(id,False) + eventPresent() + +######################## def exitHandler(signum, frame): logging.info("Power Switch Daemon stopping") @@ -156,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: @@ -191,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: