예제 #1
0
  def update_timer_data(self,weather):
    logger.debug('Updating timer data for %s running in mode: %s' % (self.get_type(),self.get_mode()))
    if self.in_weather_mode() or self.in_weather_inverse_mode():

      # Upate times based on weather
      if self.in_weather_inverse_mode():
        self.config['alarm_min']['timer_start']  = datetime.datetime.fromtimestamp(weather.get_sun_set())
        self.config['alarm_min']['timer_stop']   = datetime.datetime.fromtimestamp(weather.get_sun_rise())

      else:
        self.config['alarm_min']['timer_start']  = datetime.datetime.fromtimestamp(weather.get_sun_rise())
        self.config['alarm_min']['timer_stop']   = datetime.datetime.fromtimestamp(weather.get_sun_set())

      self.config['alarm_min']['timer_on'] = None
      self.config['alarm_min']['timer_off'] = None
      self.config['alarm_max']['timer_on'] = None
      self.config['alarm_max']['timer_off'] = None

      if self.get_type() == 'light':
        # Duration check
        duration = self.config['alarm_min']['timer_stop'] - self.config['alarm_min']['timer_start']
        # Reduce the amount of hours if to much
        if self.config['max_hours'] > 0 and duration > datetime.timedelta(hours=self.config['max_hours']):
          duration -= datetime.timedelta(hours=self.config['max_hours'])
          self.config['alarm_min']['timer_start'] += datetime.timedelta(seconds=duration.total_seconds()/2)
          self.config['alarm_min']['timer_stop'] -= datetime.timedelta(seconds=duration.total_seconds()/2)
        # Increase the amount of hours if to little
        elif self.config['min_hours'] > 0 and duration < datetime.timedelta(hours=self.config['min_hours']):
          duration = datetime.timedelta(hours=self.config['min_hours']) - duration
          self.config['alarm_min']['timer_start'] -= datetime.timedelta(seconds=duration.total_seconds()/2)
          self.config['alarm_min']['timer_stop'] += datetime.timedelta(seconds=duration.total_seconds()/2)

        # Shift hours
        self.config['alarm_min']['timer_start'] += datetime.timedelta(hours=self.config['hours_shift'])
        self.config['alarm_min']['timer_stop'] += datetime.timedelta(hours=self.config['hours_shift'])


      self.config['alarm_min']['timer_start'] = self.config['alarm_min']['timer_start'].strftime('%H:%M')
      self.config['alarm_min']['timer_stop'] = self.config['alarm_min']['timer_stop'].strftime('%H:%M')

      # Swap weather times..... for night period
      self.config['alarm_max']['timer_start'] = self.config['alarm_min']['timer_stop'] if self.get_type() == 'light' else self.config['alarm_min']['timer_start']
      self.config['alarm_max']['timer_stop'] = self.config['alarm_min']['timer_start'] if self.get_type() == 'light' else self.config['alarm_min']['timer_stop']

    if not self.in_sensor_mode() and len(list(self.config['alarm_min'].keys())) > 0:
      self.timer_min_data['time_table'] = terrariumUtils.calculate_time_table(self.config['alarm_min']['timer_start'],
                                                                              self.config['alarm_min']['timer_stop'],
                                                                              self.config['alarm_min']['timer_on'],
                                                                              self.config['alarm_min']['timer_off'])

      self.timer_min_data['duration'] = terrariumUtils.duration(self.timer_min_data['time_table'])

    if not self.in_sensor_mode() and len(list(self.config['alarm_max'].keys())) > 0:
      self.timer_max_data['time_table'] = terrariumUtils.calculate_time_table(self.config['alarm_max']['timer_start'],
                                                                              self.config['alarm_max']['timer_stop'],
                                                                              self.config['alarm_max']['timer_on'],
                                                                              self.config['alarm_max']['timer_off'])

      self.timer_max_data['duration'] = terrariumUtils.duration(self.timer_max_data['time_table'])
예제 #2
0
    def __update_timing(self, part=None):
        if part is None or part == 'light':
            on_duration = self.light['on_duration']
            off_duration = self.light['off_duration']

            if self.light['mode'] == 'weather':
                on_duration = off_duration = None

                # Upate times based on weather
                self.light['on'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['rise'])
                self.light['off'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['set'])

                # Duration check
                duration = self.light['off'] - self.light['on']
                # Reduce the amount of hours if to much
                if self.light[
                        'max_hours'] > 0 and duration > datetime.timedelta(
                            hours=self.light['max_hours']):
                    duration -= datetime.timedelta(
                        hours=self.light['max_hours'])
                    self.light['on'] += datetime.timedelta(
                        seconds=duration.total_seconds() / 2)
                    self.light['off'] -= datetime.timedelta(
                        seconds=duration.total_seconds() / 2)
                # Increase the amount of hours if to little
                elif self.light[
                        'min_hours'] > 0 and duration < datetime.timedelta(
                            hours=self.light['min_hours']):
                    duration = datetime.timedelta(
                        hours=self.light['min_hours']) - duration
                    self.light['on'] -= datetime.timedelta(
                        seconds=duration.total_seconds() / 2)
                    self.light['off'] += datetime.timedelta(
                        seconds=duration.total_seconds() / 2)

                # Shift hours
                self.light['on'] += datetime.timedelta(
                    hours=self.light['hours_shift'])
                self.light['off'] += datetime.timedelta(
                    hours=self.light['hours_shift'])

                self.light['on'] = self.light['on'].strftime('%H:%M')
                self.light['off'] = self.light['off'].strftime('%H:%M')

            self.light['time_table'] = terrariumUtils.calculate_time_table(
                self.light['on'], self.light['off'], on_duration, off_duration)

            self.light['duration'] = terrariumUtils.duration(
                self.light['time_table'])

        if part is None or part == 'sprayer':
            self.sprayer['time_table'] = terrariumUtils.calculate_time_table(
                self.sprayer['on'], self.sprayer['off'],
                self.sprayer['on_duration'], self.sprayer['off_duration'])

            self.sprayer['duration'] = terrariumUtils.duration(
                self.sprayer['time_table'])

        if part is None or part == 'watertank':
            self.watertank['time_table'] = terrariumUtils.calculate_time_table(
                self.watertank['on'], self.watertank['off'],
                self.watertank['on_duration'], self.watertank['off_duration'])

            self.watertank['duration'] = terrariumUtils.duration(
                self.watertank['time_table'])

        if part is None or part == 'heater':
            if self.heater['mode'] == 'weather':
                self.heater['on'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['set']).strftime('%H:%M')
                self.heater['off'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['rise']).strftime('%H:%M')

            self.heater['time_table'] = terrariumUtils.calculate_time_table(
                self.heater['on'], self.heater['off'],
                self.heater['on_duration'], self.heater['off_duration'])

            self.heater['duration'] = terrariumUtils.duration(
                self.heater['time_table'])

        if part is None or part == 'cooler':
            if self.cooler['mode'] == 'weather':
                self.cooler['on'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['rise']).strftime('%H:%M')
                self.cooler['off'] = datetime.datetime.fromtimestamp(
                    self.weather.get_data()['sun']['set']).strftime('%H:%M')

            self.cooler['time_table'] = terrariumUtils.calculate_time_table(
                self.cooler['on'], self.cooler['off'],
                self.cooler['on_duration'], self.cooler['off_duration'])

            self.cooler['duration'] = terrariumUtils.duration(
                self.cooler['time_table'])
예제 #3
0
 def get_duration(self):
   return terrariumUtils.duration(self.__timer_time_table)