def create_insteon_command(self): options = { 'On': '0F 11 FF', 'Off': '0F 13 FF', 'none': '0F', 'Ramp': '0F 11' } #need to figure out how to get this the address if (self.action == 'Ramp'): command = '02 62 %s %s %s' % (DEVICES[self.target], options[self.action], self.level) elif (self.action == 'On'): command = '02 62 %s %s' % (DEVICES[self.target], options[self.action]) elif (self.action == 'Off'): command = '02 62 %s %s' % (DEVICES[self.target], options[self.action]) else: log_str("didn't get a correction action") log_str("Created Command: %s" % command) command = self.ascii2bin(command) return command
def make_event_list(self): log_str('number of event is: %i' % len(self.events)) self.event_list = [ ] #have to zero this out becuase it gets remade during a triggered event for i in range(0, len(self.events)): self.event_list.append((i, self.events[i].get_command_time(), self.events[i].get_command()))
def create_command(self): if self.protocol == 'X10': return self.create_X10_command() elif self.protocol == 'Insteon': return self.create_insteon_command() else: log_str("Command protocol does not match")
def create_X10_command(self): #need to figure out how to get this the address options = {'On': '280', 'Off': '380', 'none': ' '} house = '6' #"A" house code command = '02 63 %s %s' % (house, DEVICES[self.target]) command1 = '02 63 %s %s' % (house, options[self.target]) log_str("Created X10 Command: %s" % command + command1) command = self.ascii2bin(command + command1) return command
def create_X10_command(self): #need to figure out how to get this the address options = {'On':'280', 'Off':'380', 'none':' '} house = '6' #"A" house code command = '02 63 %s %s' % (house, DEVICES[self.target]) command1 = '02 63 %s %s' % (house, options[self.target]) log_str("Created X10 Command: %s" % command + command1) command = self.ascii2bin(command + command1) return command
def handle_read(self): received = self.recv(1024) log_str("Received: %s" % binascii.hexlify(received).upper()) #now I make a handler to handle the incoming messages self.buffer = self.trigger_handler.parse_mesg( binascii.hexlify(received).upper()) if (len(self.buffer) > 0): self.handle_write() self.sched.make_event_list() self.sched.sort_event_list() self.sched.next_event_index = self.sched.determine_inital_event_index( )
def get_next_event_command(self): cur_index = self.next_event_index self.next_event_index = self.next_event_index + 1 if (self.next_event_index == len(self.events)): log_str("Ran last event of the week reseting to 0") self.next_event_index = 0 self.ran_last_event = True else: self.ran_last_event = False ##log_str("just got the next command the index is %i" %cur_index) #When I run this need #break apart the parts list [next_num, next_time, next_command] = self.event_list[cur_index] return (next_command)
def __init__(self, trigger, trigger_action, target, action, time_lag, time_min, time_max, protocol, percent): self.trigger = DEVICES[trigger] self.trigger_action = self.get_trigger_action(trigger_action) self.target = target self.action = action self.time_lag = time_lag self.time_lag_minutes = int(time_lag[3:5]) self.time_lag_hours = int(time_lag[0:2]) self.time_min = time_min self.time_max = time_max self.time_min_minutes = int(time_min[0:2])+int(time_min[3:5])*60 self.time_max_minutes = int(time_max[0:2])+int(time_max[3:5])*60 self.level = self.percent_to_level(percent) self.percent = percent self.protocol = protocol log_str('trigger.self.action = %s' % self.action)
def __init__(self, trigger, trigger_action, target, action, time_lag, time_min, time_max, protocol, percent): self.trigger = DEVICES[trigger] self.trigger_action = self.get_trigger_action(trigger_action) self.target = target self.action = action self.time_lag = time_lag self.time_lag_minutes = int(time_lag[3:5]) self.time_lag_hours = int(time_lag[0:2]) self.time_min = time_min self.time_max = time_max self.time_min_minutes = int(time_min[0:2]) + int(time_min[3:5]) * 60 self.time_max_minutes = int(time_max[0:2]) + int(time_max[3:5]) * 60 self.level = self.percent_to_level(percent) self.percent = percent self.protocol = protocol log_str('trigger.self.action = %s' % self.action)
def parse_mesg(self, mesg): #FUTURE EXPANSION this should work well here. I add temp events through this #they will run once on the time I say and then be gone when the week is refreshed #need to figure out how to refresh #self.scheduler.events.append(Event('X10other','Off','12:00','Mon','X10','00')) #self.scheduler.make_event_list() #self.scheduler.sort_event_list() #action => motion = 11. no motion = 13 log_str("parsing %s" % mesg) event_prefix = mesg[:4] event_device = mesg[4:10] event_destination = mesg[10:16] event_action = mesg[18:20] #log_str("len of triggers: %i" % len(self.triggers)) immediate_command = "" #still need to check for time #still need to generate command now it is hard coded. now = datetime.datetime.now() now_in_minutes = int(now.strftime("%H")) + int(now.strftime("%M")) * 60 for i in range(0, len(self.triggers)): if ((self.triggers[i].time_min_minutes < now_in_minutes) and (self.triggers[i].time_max_minutes > now_in_minutes)): if ((event_device == self.triggers[i].trigger) and (event_action == self.triggers[i].trigger_action) and (event_destination == "1EB35B")): if ((self.triggers[i].time_lag_minutes == 0) and (self.triggers[i].time_lag_hours == 0)): immediate_command = self.triggers[i].get_command() else: ##log_str("tigger matched") action_time = now + datetime.timedelta( minutes=self.triggers[i].time_lag_minutes) time_str = "%s:%s" % (action_time.strftime("%H"), action_time.strftime("%M")) ##log_str("level: %s"%self.triggers[i].level) self.scheduler.events.append( Event(self.triggers[i].target, self.triggers[i].action, time_str, action_time.strftime("%a"), self.triggers[i].protocol, self.triggers[i].percent)) return (immediate_command)
def create_insteon_command(self): options = {'On':'0F 11 FF', 'Off':'0F 13 FF', 'none':'0F', 'Ramp':'0F 11'} #need to figure out how to get this the address if (self.action == 'Ramp'): command = '02 62 %s %s %s' % (DEVICES[self.target], options[self.action], self.level) elif (self.action == 'On'): command = '02 62 %s %s' % (DEVICES[self.target], options[self.action]) elif (self.action == 'Off'): command = '02 62 %s %s' % (DEVICES[self.target], options[self.action]) else: log_str("didn't get a correction action") log_str("Created Command: %s" % command) command = self.ascii2bin(command) return command
def event_to_run(self): #check to see if I need to reset to a new week and start the sequence again. #self.reset_to_new_week() #Here the next event is checked if it needs to be run. #I first check to see if I have run the last event if (self.ran_last_event == False): #break apart the parts list #this used to be outside the if -- MAKE SURE IT WORKS [next_num, next_time, next_command] = self.event_list[self.next_event_index] ##log_str("n: %i" %next_time) log_str("t: %i" % self.cur_week_secs()) #if I haven't run the last event I check to see if there are more events to run if (next_time < self.cur_week_secs()): log_str("There is a command to run") #if there is any event to run I return true and then get_next_event_command is run return True return False
def determine_inital_event_index(self): log_str("in determin_inital_event") self.ran_last_event = False i = 0 ##log_str(len(self.events)) #just need the times here so make a list, must be a better way to do it times = [x[1] for x in self.event_list] while (times[i] < self.cur_week_secs()): ##log_str("time is: %s and current secods is: %i"% (times[i], self.cur_week_secs())) i = i + 1 if (i >= len(self.events)): log_str('i is: %i' % i) log_str('No events to schedule') self.ran_last_event = True break ##log_str("Next event to run is event %i" %i) return i