def POST(self): qdict = web.input() station_seconds = {} for station in stations.enabled_stations(): mm_str = "mm" + str(station.index) ss_str = "ss" + str(station.index) if mm_str in qdict and ss_str in qdict: seconds = int(qdict[mm_str] or 0) * 60 + int(qdict[ss_str] or 0) station_seconds[station.index] = seconds run_once.set(station_seconds) raise web.seeother('/')
def run(self): weather.add_callback(self.update) self._sleep(10) # Wait for weather callback before starting while not self._stop_event.is_set(): try: log.clear(NAME) if plugin_options['enabled']: log.debug(NAME, _(u'Checking weather status') + '...') info = [] days = 0 total_info = {'rain_mm': 0.0} for day in range(-plugin_options['days_history'], plugin_options['days_forecast'] + 1): check_date = datetime.date.today( ) + datetime.timedelta(days=day) hourly_data = weather.get_hourly_data(check_date) if hourly_data: days += 1 info += hourly_data total_info['rain_mm'] += weather.get_rain(check_date) log.info( NAME, _(u'Using') + ' %d ' % days + _(u'days of information.')) total_info.update({ 'temp_c': sum([val['temperature'] for val in info]) / len(info), 'wind_ms': sum([val['windSpeed'] for val in info]) / len(info), 'humidity': sum([val['humidity'] for val in info]) / len(info) }) # We assume that the default 100% provides 4mm water per day (normal need) # We calculate what we will need to provide using the mean data of X days around today water_needed = 4 * days # 4mm per day water_needed *= 1 + (total_info['temp_c'] - 20) / 15 # 5 => 0%, 35 => 200% water_needed *= 1 + (total_info['wind_ms'] / 100 ) # 0 => 100%, 20 => 120% water_needed *= 1 - (total_info['humidity'] - 50) / 200 # 0 => 125%, 100 => 75% water_needed = round(water_needed, 1) water_left = water_needed - total_info['rain_mm'] water_left = round(max(0, min(100, water_left)), 1) water_adjustment = round((water_left / (4 * days)) * 100, 1) water_adjustment = float( max(plugin_options['wl_min'], min(plugin_options['wl_max'], water_adjustment))) log.info( NAME, _(u'Water needed') + ' %d ' % days + _('days') + ': %.1fmm' % water_needed) log.info( NAME, _(u'Total rainfall') + ': %.1fmm' % total_info['rain_mm']) log.info(NAME, u'_______________________________') log.info(NAME, _(u'Irrigation needed') + ': %.1fmm' % water_left) log.info( NAME, _(u'Weather Adjustment') + ': %.1f%%' % water_adjustment) level_adjustments[NAME] = water_adjustment / 100 if plugin_options['protect_enabled']: current_data = weather.get_current_data() temp_local_unit = current_data[ 'temperature'] if options.temp_unit == "C" else 32.0 + 9.0 / 5.0 * current_data[ 'temperature'] log.debug( NAME, _(u'Temperature') + ': %.1f %s' % (temp_local_unit, options.temp_unit)) month = time.localtime().tm_mon # Current month. if temp_local_unit < plugin_options[ 'protect_temp'] and month in plugin_options[ 'protect_months']: station_seconds = {} for station in stations.enabled_stations(): if station.index in plugin_options[ 'protect_stations']: station_seconds[ station.index] = plugin_options[ 'protect_minutes'] * 60 else: station_seconds[station.index] = 0 for station in stations.enabled_stations(): if run_once.is_active(datetime.datetime.now(), station.index): break else: log.debug(NAME, _(u'Protection activated.')) run_once.set(station_seconds) self._sleep(3600) else: log.clear(NAME) log.info(NAME, _(u'Plug-in is disabled.')) if NAME in level_adjustments: del level_adjustments[NAME] self._sleep(24 * 3600) except Exception: log.error( NAME, _(u'Weather-based water level plug-in') + ':\n' + traceback.format_exc()) self._sleep(3600) weather.remove_callback(self.update)
def run(self): weather.add_callback(self.update) self._sleep(10) # Wait for weather callback before starting while not self._stop.is_set(): try: log.clear(NAME) if plugin_options['enabled']: log.debug(NAME, _('Checking weather status') + '...') history = weather.get_wunderground_history( plugin_options['days_history']) forecast = weather.get_wunderground_forecast( plugin_options['days_forecast']) today = weather.get_wunderground_conditions() info = {} for day in range(-20, 20): if day in history: day_info = history[day] elif day in forecast: day_info = forecast[day] else: continue info[day] = day_info if 0 in info and 'rain_mm' in today: day_time = datetime.datetime.now().time() day_left = 1.0 - (day_time.hour * 60 + day_time.minute) / 24.0 / 60 info[0]['rain_mm'] = info[0][ 'rain_mm'] * day_left + today['rain_mm'] if not info: log.info(NAME, str(history)) log.info(NAME, str(today)) log.info(NAME, str(forecast)) raise Exception(_('No information available!')) log.info( NAME, _('Using') + ' %d ' % len(info) + _('days of information.')) total_info = { 'temp_c': sum([val['temp_c'] for val in info.values()]) / len(info), 'rain_mm': sum([val['rain_mm'] for val in info.values()]), 'wind_ms': sum([val['wind_ms'] for val in info.values()]) / len(info), 'humidity': sum([val['humidity'] for val in info.values()]) / len(info) } # We assume that the default 100% provides 4mm water per day (normal need) # We calculate what we will need to provide using the mean data of X days around today water_needed = 4 * len(info) # 4mm per day water_needed *= 1 + (total_info['temp_c'] - 20) / 15 # 5 => 0%, 35 => 200% water_needed *= 1 + (total_info['wind_ms'] / 100 ) # 0 => 100%, 20 => 120% water_needed *= 1 - (total_info['humidity'] - 50) / 200 # 0 => 125%, 100 => 75% water_needed = round(water_needed, 1) water_left = water_needed - total_info['rain_mm'] water_left = round(max(0, min(100, water_left)), 1) water_adjustment = round( (water_left / (4 * len(info))) * 100, 1) water_adjustment = float( max(plugin_options['wl_min'], min(plugin_options['wl_max'], water_adjustment))) log.info( NAME, _('Water needed') + ' %d ' % len(info) + _('days') + ': %.1fmm' % water_needed) log.info( NAME, _('Total rainfall') + ': %.1fmm' % total_info['rain_mm']) log.info(NAME, '_______________________________') log.info(NAME, _('Irrigation needed') + ': %.1fmm' % water_left) log.info( NAME, _('Weather Adjustment') + ': %.1f%%' % water_adjustment) level_adjustments[NAME] = water_adjustment / 100 if plugin_options['protect_enabled']: log.debug( NAME, _('Temperature') + ': %s' % today['temperature_string']) month = time.localtime().tm_mon # Current month. cold = (today['temp_c'] < plugin_options['protect_temp']) if options.temp_unit == "C" else \ (today['temp_f'] < plugin_options['protect_temp']) if cold and month in plugin_options['protect_months']: station_seconds = {} for station in stations.enabled_stations(): if station.index in plugin_options[ 'protect_stations']: station_seconds[ station.index] = plugin_options[ 'protect_minutes'] * 60 else: station_seconds[station.index] = 0 for station in stations.enabled_stations(): if run_once.is_active(datetime.datetime.now(), station.index): break else: log.debug(NAME, _('Protection activated.')) run_once.set(station_seconds) self._sleep(3600) else: log.clear(NAME) log.info(NAME, _('Plug-in is disabled.')) if NAME in level_adjustments: del level_adjustments[NAME] self._sleep(24 * 3600) except Exception: log.error( NAME, _('Weather-based water level plug-in') + ':\n' + traceback.format_exc()) self._sleep(3600) weather.remove_callback(self.update)