daylight check
[svn42.git] / switch-power.py
index 131361a..cb0ada8 100755 (executable)
@@ -14,6 +14,8 @@ import socket
 import subprocess
 import types
 import ConfigParser
+import datetime
+import traceback
 
 logger = logging.getLogger()
 logger.setLevel(logging.INFO)
@@ -29,10 +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
@@ -105,17 +110,39 @@ def switchPower(powerid,turn_on=False):
   else:
     onoff="off"
   touchURL(uwscfg.slug_cgiuri.replace("%ID%",powerid).replace("%ONOFF%",onoff))
-  
-def eventPresent(somebody_present=False):
-  for id in uwscfg.slug_ids_present.split(" "):
-    switchPower(id,somebody_present)
+
+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():
+  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():
+  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: 
@@ -125,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")
@@ -151,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:
@@ -175,7 +205,10 @@ while True:
       m = RE_PRESENCE.match(line)
       if not m is None:
         status = m.group(1)
-        eventPresent(status == "yes")
+        if status == "yes":
+          eventPresent()
+        else:
+          eventNobodyHere()
         continue
       m = RE_BUTTON.match(line)
       if not m is None:
@@ -183,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: