import subprocess
import types
import ConfigParser
+import datetime
+import traceback
logger = logging.getLogger()
logger.setLevel(logging.INFO)
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
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:
time.sleep(0.6)
for id in lst1:
switchPower(id,False)
+ eventPresent()
+
+########################
def exitHandler(signum, frame):
logging.info("Power Switch Daemon stopping")
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:
continue
except Exception, ex:
- logging.error("main: "+str(ex))
+ logging.error("main: "+str(ex))
+ traceback.print_exc(file=sys.stdout)
try:
sockhandle.close()
except: