def isIntervalActive(self): try: strBeginTime = self.getStartTime() strEndTime = self.getEndTime() dateToday = getDateToday() curTime = DateTime() calculatedStartTime = ParseTimeStringToDate(dateToday, self._begin) calculatedEndTime = ParseTimeStringToDate(dateToday, self._end) #End is before start -> interval is over midnight if calculatedEndTime.isBefore(calculatedStartTime): if (curTime.isAfter(calculatedStartTime)): calculatedEndTime = calculatedEndTime.plusDays(1) elif (curTime.isBefore(calculatedEndTime)): calculatedStartTime = calculatedStartTime.minusDays(1) else: calculatedEndTime = calculatedEndTime.plusDays(1) getLogger().debug(( "Processing interval '{}' - '{}' -> '{}' - '{}' - Current Time '{}', isActive='{}'" ).format(self.getStartTime(), self.getEndTime(), calculatedStartTime, calculatedEndTime, curTime, (curTime.isAfter(calculatedStartTime) and curTime.isBefore(calculatedEndTime)))) return (curTime.isAfter(calculatedStartTime) and curTime.isBefore(calculatedEndTime)) except: LogException() return False
def isActive(self): try: curDateTime = DateTime() if (self._arrSunInfo is not None): sunInfoYesterday = self._arrSunInfo[0] sunInfoToday = self._arrSunInfo[1] sunInfoTomorrow = self._arrSunInfo[2] #After Sunset yesterday, Before sunrise Today -> Night if (curDateTime.isAfter(sunInfoYesterday.getSunsetStart()) and curDateTime.isBefore(sunInfoToday.getSunriseEnd())): getLogger().debug( ("NIGHT -> '{}' is between '{}' - '{}'").format( curDateTime, sunInfoYesterday.getSunsetStart(), sunInfoToday.getSunriseEnd())) return True #After Sunrise today, Before sunset Today -> Day if (curDateTime.isAfter(sunInfoToday.getSunriseEnd()) and curDateTime.isBefore(sunInfoToday.getSunsetStart())): getLogger().debug( ("DAY -> '{}' is between '{}' - '{}'").format( curDateTime, sunInfoToday.getSunriseEnd(), sunInfoToday.getSunsetStart())) return False #After Sunset today, Before Sunrise Tomorrow -> Night elif (curDateTime.isAfter(sunInfoToday.getSunsetStart()) and curDateTime.isBefore(sunInfoTomorrow.getSunriseEnd())): getLogger().debug( ("NIGHT -> '{}' is between '{}' - '{}'").format( curDateTime, sunInfoToday.getSunsetStart(), sunInfoTomorrow.getSunriseEnd())) return True getLogger().warn( ("'{}' didn't evalute night/day '{}', '{}', '{}', '{}'" ).format(curDateTime, sunInfoYesterday.getSunsetStart(), sunInfoToday.getSunriseEnd(), sunInfoToday.getSunsetStart(), sunInfoTomorrow.getSunriseEnd())) else: getLogger().warn("No SunInfo data found - Evaluating 'False'") return False except: LogException() return False
def create_timers(start_times): """Creates Timers to transition the time of day based on the passed in list of DateTime Item names. If an Item is dated with yesterday, the Item is updated to today. The ETOD_ITEM is commanded to the current time of day if it's not already the correct state. Arguments: - start_times: list of names for DateTime Items containing the start times for each time period """ now = DateTime().now() most_recent_time = now.minusDays(1) most_recent_state = items[ETOD_ITEM] for time in start_times: item_time = DateTime(str(items[time])) trigger_time = to_today(items[time]) # Update the Item with today's date if it was for yesterday. if item_time.isBefore(trigger_time): log.debug("Item {} is yesterday, updating to today".format(time)) events.postUpdate(time, str(trigger_time)) # Get the etod state from the metadata. state = get_value(time, NAMESPACE) # If it's in the past but after most_recent, update most_recent. if trigger_time.isBefore(now) and trigger_time.isAfter( most_recent_time): log.debug( "NOW: {} start time {} is in the past but after {}".format( state, trigger_time, most_recent_time)) most_recent_time = trigger_time most_recent_state = get_value(time, NAMESPACE) # If it's in the future, schedule a Timer. elif trigger_time.isAfter(now): log.debug("FUTURE: {} Scheduleing Timer for {}".format( state, trigger_time)) timers.check(state, trigger_time, function=lambda st=state: etod_transition(st)) # If it's in the past but not after most_recent_time we can ignore it. else: log.debug( "PAST: {} start time of {} is before now {} and before {}". format(state, trigger_time, now, most_recent_time)) log.info("Created {} timers.".format(len(timers.timers))) log.info("The current time of day is {}".format(most_recent_state)) send_command_if_different(ETOD_ITEM, most_recent_state)