presence and power on/off timer improvements
authorrealraum <realraum@realraum.at>
Fri, 14 May 2010 23:57:03 +0000 (23:57 +0000)
committerrealraum <realraum@realraum.at>
Fri, 14 May 2010 23:57:03 +0000 (23:57 +0000)
switch-power.py
track-presence.py

index a9ebc29..9e673b8 100755 (executable)
@@ -36,10 +36,12 @@ class UWSConfig:
     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_logo','logo')
-    self.config_parser.set('slug','ids_present_day','werkzeug')
+    self.config_parser.set('slug','ids_present_day_bright_room','werkzeug')
+    self.config_parser.set('slug','ids_present_day_dark_room','decke werkzeug')
     self.config_parser.set('slug','ids_present_night','werkzeug schreibtisch idee labor')
-    self.config_parser.set('slug','ids_panic','idee schreibtisch labor werkzeug')
-    self.config_parser.set('slug','ids_nonpresent_off','lichter stereo deckehinten')
+    self.config_parser.set('slug','ids_panic','idee schreibtisch labor werkzeug deckevorne deckehinten')
+    self.config_parser.set('slug','ids_nonpresent_off','lichter stereo')
+    self.config_parser.set('slug','light_threshold_brightness','400')
     #self.config_parser.set('slug','time_day','6:00-17:00')
     self.config_parser.add_section('debug')
     self.config_parser.set('debug','enabled',"False")
@@ -118,7 +120,7 @@ def switchPower(powerid,turn_on=False):
 
 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}
+  dusk_per_month = {1:16, 2:17, 3:18, 4:20, 5:20, 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])
@@ -130,6 +132,13 @@ def isWolfHour():
 ######### EVENTS ###############  
 unixts_last_movement=0
 status_presense=None
+room_is_bright=None
+
+def eventRoomGotBright():
+  room_is_bright=True
+
+def eventRoomGotDark():
+  room_is_bright=False
 
 def eventDaylightStart():
   for id in uwscfg.slug_ids_logo.split(" "):
@@ -169,7 +178,10 @@ def eventPresent():
   global status_presense
   status_presense=True
   if haveDaylight():
-    present_ids=uwscfg.slug_ids_present_day
+    if room_is_bright:
+      present_ids=uwscfg.slug_ids_present_day_bright_room
+    else:
+      present_ids=uwscfg.slug_ids_present_day_dark_room
   else:
     present_ids=uwscfg.slug_ids_present_night
   logging.info("event: someone present, switching on: "+present_ids)
@@ -233,6 +245,7 @@ else:
 RE_PRESENCE = re.compile(r'Presence: (yes|no)(?:, (opened|closed), (.+))?')
 RE_BUTTON = re.compile(r'PanicButton|button\d?')
 RE_MOVEMENT = re.compile(r'movement')
+RE_PHOTO = re.compile(r'photo\d: [^0-9]*?(\d+)')
 daylight=None
 wolfhour=None
 unixts_last_periodical=0
@@ -290,6 +303,13 @@ while True:
       if not m is None:
         eventMovement()
         continue
+      m = RE_PHOTO.match(line)
+      if not m is None:
+        if m.group(1) >= int(uwscfg.slug_light_threshold_brightness):
+          eventRoomGotBright()
+        else:
+          eventRoomGotDark()
+        continue
           
   except Exception, ex:
     logging.error("main: "+str(ex))
index 7778406..532f5e0 100755 (executable)
@@ -302,6 +302,7 @@ class StatusTracker: #(threading.Thread):
     self.presence_notify_lock=threading.Lock()
     #timer
     self.timer=None
+    self.timer_timeout=0
     
   def doorOpen(self,who,how):
     self.uwscfg.checkConfigUpdates()
@@ -364,12 +365,13 @@ class StatusTracker: #(threading.Thread):
 
 
   def checkAgainIn(self, sec):
-    #~ if not self.timer is None:
-      #~ logging.debug("checkAgainIn: canceled previous running Timer")
-      #~ self.timer.cancel()
-    logging.debug("checkAgainIn: starting Timer with timeout %fs" % sec)
-    self.timer=threading.Timer(sec, self.checkPresenceStateChangeAndNotify)
-    self.timer.start()
+    if self.timer_timeout < time.time():
+      logging.debug("checkAgainIn: starting Timer with timeout %fs" % sec)
+      self.timer=threading.Timer(sec, self.checkPresenceStateChangeAndNotify)
+      self.timer.start()
+      self.timer_timeout = time.time() + sec
+    else:
+      logging.debug("checkAgainIn: not starting timer, already one scheduled in %fs" % (time.time() - self.timer_timeout))
   
   def somebodyPresent(self):
     with self.lock:
@@ -381,10 +383,9 @@ class StatusTracker: #(threading.Thread):
         else:
           return False
       elif self.door_manual_switch_used and time.time() - self.last_door_operation_unixts <= float(self.uwscfg.tracker_sec_wait_after_manual_close):
-        #start timer, checkPresenceStateChangeAndNotify after tracker_sec_wait_movement
         self.checkAgainIn(float(self.uwscfg.tracker_sec_wait_after_manual_close))
+        return self.last_somebody_present_result
       elif not self.door_manual_switch_used and time.time() - self.last_door_operation_unixts <= float(self.uwscfg.tracker_sec_necessary_to_move_through_door):
-        #start timer, checkPresenceStateChangeAndNotify after tracker_sec_wait_movement
         self.checkAgainIn(float(self.uwscfg.tracker_sec_necessary_to_move_through_door))
         return self.last_somebody_present_result
       elif self.last_movement_unixts > self.last_door_operation_unixts - float(self.uwscfg.tracker_sec_movement_before_manual_switch) and self.door_manual_switch_used: