def timer(self): if self.get_timer_enabled(): logger.debug( 'Checking timer time table for switch %s with %s entries.', self.get_name(), len(self.__timer_time_table)) switch_state = terrariumUtils.is_time(self.__timer_time_table) logmessage = 'State not changed.' if switch_state is not None and 'dimmer' not in self.get_hardware_type( ) and self.get_state() != switch_state: logmessage = 'Switched to state %s.' % ('on' if switch_state else 'off') if switch_state is None: self.__calculate_time_table() switch_state = False if switch_state is True: self.on() else: self.off() logger.info('Timer action is done for switch %s. %s', self.get_name(), logmessage)
def is_time(self): logger.debug('Checking timer time table for switch %s with %s entries.', self.get_name(),len(self.__timer_time_table)) is_time = terrariumUtils.is_time(self.__timer_time_table) if is_time is None: self.__calculate_time_table() is_time = False logger.debug('Timer action is done for switch %s. Is it time to play?: %s.', self.get_name(),('Yes' if is_time else 'Nope')) return is_time
def __engine_loop(self): logger.info('Starting engine') while True: logger.debug('Environment starts new checks') starttime = time.time() self.__update_environment_state() # Light checks and actions if self.light['enabled']: logger.debug('Environment lighting is enabled in mode %s' % self.light['mode']) toggle_on = terrariumUtils.is_time(self.light['time_table']) if toggle_on is None: self.__update_timing('light') if toggle_on: if not self.is_light_on(): logger.info( 'Environment is turning on the lights based on %s' % self.light['mode']) self.light_on() else: if self.is_light_on(): logger.info( 'Environment is turning off the lights based on %s' % self.light['mode']) self.light_off() #else: # logger.debug('Make sure that the lights are off when not enabled at all.') # if self.is_light_on(): # logger.info('Environment is turning off the lights due to disabling it') # self.light_off() # Sprayer checks and actions if self.sprayer['enabled']: toggle_on = False extra_logging_message = '' logger.debug('Environment spraying is enabled.') logger.debug('Environment spraying is based on: %s' % self.sprayer['mode']) if 'sensor' == self.sprayer['mode']: # Only spray when the lights are on. Or when explicit enabled during the nights. if self.sprayer['night_enabled'] or self.is_light_on(): # Spray based on the average humidity values of the used sensors toggle_on = self.sprayer['humidity'][ 'current'] < self.sprayer['humidity']['alarm_min'] if toggle_on: extra_logging_message = 'Sprayer humdity value %f%% is lower then alarm %f%%.' % ( self.sprayer['humidity']['current'], self.sprayer['humidity']['alarm_min']) else: # Spray based on time table toggle_on = terrariumUtils.is_time( self.sprayer['time_table']) if toggle_on is None: self.__update_timing('sprayer') if toggle_on and len(self.sprayer['sensors']) > 0: # Use the extra added sensors for finetuning the trigger action toggle_on = self.sprayer['humidity'][ 'current'] < self.sprayer['humidity']['alarm_min'] if toggle_on: extra_logging_message = 'Sprayer humdity value %f%% is lower then alarm %f%%.' % ( self.sprayer['humidity']['current'], self.sprayer['humidity']['alarm_min']) if toggle_on: if self.is_door_open(): logger.warning( 'Environment could not spray for %f seconds based on %s mode because of an open door.%s' % (self.sprayer['spray_duration'], self.sprayer['mode'], extra_logging_message)) else: if not self.is_sprayer_on(): logger.info( 'Environment is turning on the sprayer for %f seconds based on %s mode.%s' % (self.sprayer['spray_duration'], self.sprayer['mode'], extra_logging_message)) self.sprayer_on() else: if self.is_sprayer_on(): logger.info( 'Environment is turning off the sprayer based on %s mode.' % (self.sprayer['mode'], )) self.sprayer_off() #else: # logger.debug('Make sure that the spayer is off when not enabled at all.') # if self.is_sprayer_on(): # logger.info('Environment is turning off the sprayer due to disabling it') # self.sprayer_off() # Watertank checks and actions if self.watertank['enabled']: toggle_on = False extra_logging_message = '' logger.debug('Environment watertank is enabled.') logger.debug('Environment watertank is based on: %s' % self.watertank['mode']) if 'sensor' == self.watertank['mode']: # Spray based on the average humidity values of the used sensors toggle_on = self.watertank['height'] - self.watertank[ 'distance']['current'] < self.watertank['distance'][ 'alarm_min'] if toggle_on: extra_logging_message = 'Water tank level value %f%% is lower then alarm %f%%.' % ( self.watertank['height'] - self.watertank['distance']['current'], self.watertank['distance']['alarm_min']) else: # Spray based on time table toggle_on = terrariumUtils.is_time( self.watertank['time_table']) if toggle_on is None: self.__update_timing('watertank') if toggle_on and len(self.watertank['sensors']) > 0: # Use the extra added sensors for finetuning the trigger action toggle_on = self.watertank['height'] - self.watertank[ 'distance']['current'] < self.watertank[ 'distance']['alarm_min'] if toggle_on: extra_logging_message = 'Water tank level value %f%% is lower then alarm %f%%.' % ( self.watertank['height'] - self.watertank['distance']['current'], self.watertank['distance']['alarm_min']) if toggle_on: if not self.is_watertank_on(): logger.info( 'Environment is turning on the water punt for %f seconds based on %s mode.%s' % (self.watertank['pump_duration'], self.watertank['mode'], extra_logging_message)) self.watertank_on() else: if self.is_watertank_on(): logger.info( 'Environment is turning off the water pump based on %s mode.' % (self.watertank['mode'], )) self.watertank_off() #else: # logger.debug('Make sure that the water pump is off when not enabled at all.') # if self.is_watertank_on(): # logger.info('Environment is turning off the water pump due to disabling it') # self.watertank_off() # Heater checks and actions if self.heater['enabled']: toggle_on = None extra_logging_message = '' logger.debug('Environment heater is enabled.') logger.debug('Environment heater is based on: %s' % self.heater['mode']) if 'sensor' == self.heater['mode']: # Only heat when the lights are off. Or when explicit enabled during the day. if self.heater['day_enabled'] or self.is_light_off(): # Heat based on the average temperature values of the used sensors if self.heater['temperature']['current'] < self.heater[ 'temperature']['alarm_min']: toggle_on = True extra_logging_message = 'Heater temperature value %f%% is lower then alarm %f%%.' % ( self.heater['temperature']['current'], self.heater['temperature']['alarm_min']) elif self.heater['temperature'][ 'current'] > self.heater['temperature'][ 'alarm_max']: toggle_on = False extra_logging_message = 'Heater temperature value %f%% is higher then alarm %f%%.' % ( self.heater['temperature']['current'], self.heater['temperature']['alarm_max']) else: # Force off when lights are on! if self.is_heater_on(): logger.info( 'Environment is turning off the heater due to lights on based on %s mode.' % (self.heater['mode'], )) toggle_on = False else: # Heat based on time table toggle_on = terrariumUtils.is_time( self.heater['time_table']) if toggle_on is None: self.__update_timing('heater') if toggle_on and len(self.heater['sensors']) > 0: # Reset toggle based on the extra available sensors toggle_on = None # Use the extra added sensors for finetuning the trigger action if self.heater['temperature']['current'] < self.heater[ 'temperature']['alarm_min']: toggle_on = True extra_logging_message = 'Heater temperature value %f%% is lower then alarm %f%%.' % ( self.heater['temperature']['current'], self.heater['temperature']['alarm_min']) elif self.heater['temperature'][ 'current'] > self.heater['temperature'][ 'alarm_max']: toggle_on = False extra_logging_message = 'Heater temperature value %f%% is higher then alarm %f%%.' % ( self.heater['temperature']['current'], self.heater['temperature']['alarm_max']) if toggle_on is True: if not self.is_heater_on(): logger.info( 'Environment is turning on the heater based on %s mode.%s' % (self.heater['mode'], extra_logging_message)) self.heater_on() elif toggle_on is False: if self.is_heater_on(): logger.info( 'Environment is turning off the heater based on %s mode.%s' % (self.heater['mode'], extra_logging_message)) self.heater_off() #else: # logger.debug('Make sure that the heating is off when not enabled at all.') # if self.is_heater_on(): # logger.info('Environment is turning off the heater due to disabling it') # self.heater_off() # Cooler checks and actions if self.cooler['enabled']: toggle_on = None extra_logging_message = '' logger.debug('Environment cooler is enabled.') logger.debug('Environment cooler is based on: %s' % self.cooler['mode']) if 'sensor' == self.cooler['mode']: # Only cool when the lights are on. Or when explicit enabled during the night. if self.cooler['night_enabled'] or self.is_light_on(): # Cooler based on the average temperature values of the used sensors if self.cooler['temperature']['current'] < self.cooler[ 'temperature']['alarm_min']: toggle_on = False extra_logging_message = 'Cooler temperature value %f%% is lower then alarm %f%%.' % ( self.cooler['temperature']['current'], self.cooler['temperature']['alarm_min']) elif self.cooler['temperature'][ 'current'] > self.cooler['temperature'][ 'alarm_max']: toggle_on = True extra_logging_message = 'Cooler temperature value %f%% is higher then alarm %f%%.' % ( self.cooler['temperature']['current'], self.cooler['temperature']['alarm_max']) else: # Force off when lights are on! if self.is_cooler_on(): logger.info( 'Environment is turning off the cooler due to lights on based on %s mode.' % (self.cooler['mode'], )) toggle_on = False else: # Cooler based on time table toggle_on = terrariumUtils.is_time( self.cooler['time_table']) if toggle_on is None: self.__update_timing('cooler') if toggle_on and len(self.cooler['sensors']) > 0: # Reset toggle based on the extra available sensors toggle_on = None # Use the extra added sensors for finetuning the trigger action if self.cooler['temperature']['current'] < self.cooler[ 'temperature']['alarm_min']: toggle_on = False extra_logging_message = 'Cooler temperature value %f%% is lower then alarm %f%%.' % ( self.cooler['temperature']['current'], self.cooler['temperature']['alarm_min']) elif self.cooler['temperature'][ 'current'] > self.cooler['temperature'][ 'alarm_max']: toggle_on = True extra_logging_message = 'Cooler temperature value %f%% is higher then alarm %f%%.' % ( self.cooler['temperature']['current'], self.cooler['temperature']['alarm_max']) if toggle_on is True: if not self.is_cooler_on(): logger.info( 'Environment is turning on the cooler based on %s mode.%s' % (self.cooler['mode'], extra_logging_message)) self.cooler_on() elif toggle_on is False: if self.is_cooler_on(): logger.info( 'Environment is turning off the cooler based on %s mode.%s' % (self.cooler['mode'], extra_logging_message)) self.cooler_off() #else: # logger.debug('Make sure that the cooler is off when not enabled at all.') # if self.is_cooler_on(): # logger.info('Environment is turning off the cooler due to disabling it') # self.cooler_off() duration = time.time() - starttime if duration < terrariumEnvironment.LOOP_TIMEOUT: logger.info( 'Update done in %.5f seconds. Waiting for %.5f seconds for next round' % (duration, terrariumEnvironment.LOOP_TIMEOUT - duration)) sleep(terrariumEnvironment.LOOP_TIMEOUT - duration) # TODO: Config setting else: logger.warning( 'Update took to much time. Needed %.5f seconds which is %.5f more then the limit %s' % (duration, duration - terrariumEnvironment.LOOP_TIMEOUT, terrariumEnvironment.LOOP_TIMEOUT))
def is_time_max(self): return terrariumUtils.is_true(terrariumUtils.is_time(self.timer_max_data['time_table']))
def is_time_min(self): return terrariumUtils.is_time(self.timer_min_data['time_table'])