def notifications(): # Grab Notification Settings notification_keys = [ 'ph_status_sms', 'ph_status_pb', 'ph_status_email', 'fx6filter_status_sms', 'fx6filter_status_pb', 'fx6filter_status_email', 'f406filter_status_sms', 'f406filter_status_pb', 'f406filter_status_email', 'uv_status_sms', 'uv_status_pb', 'uv_status_email', 'phcontroller_status_sms', 'phcontroller_status_pb', 'phcontroller_status_email', 'airpump_status_sms', 'airpump_status_pb', 'airpump_status_email', 'temp_status_sms', 'temp_status_pb', 'temp_status_email', 'waterlevel_status_sms', 'waterlevel_status_pb', 'waterlevel_status_email', 'nh3_status_sms', 'nh3_status_pb', 'nh3_status_email', 'tds_status_sms', 'tds_status_pb', 'tds_status_email', 'system_wide', 'edit_notifications' ] notification_data = {} for key in notification_keys: notification_data[key] = read_mysql_database("notifications", key) system_logging = read_mysql_database("logging", "system_logging") log_level = read_mysql_database("logging", "log_level") return render_template('notifications.html', **notification_data, system_logging=system_logging, log_level=log_level)
def check_temp_ph_parameters(measurement, tank_name, influx_table, measurement_name): """Check that pH and Temperature tank parameters are within limits. Alerts us if they are not.""" log.debug("check_temp_ph_parameters() called") current_measurement = read_influx_database(influx_table, tank_name) log.debug(f"The current {measurement_name} of {tank_name} is {current_measurement:1.2f}") system_measurement_low = getattr(system_info, measurement + '_low') system_measurement_high = getattr(system_info, measurement + '_high') if current_measurement < system_measurement_low: measurement_alert_time = getattr(system_info, measurement + '_alert_time') alert_sent = read_mysql_database(measurement, "alert_sent") if alert_sent: resend_alert = last_alert_time(measurement, measurement_alert_time) if resend_alert: notify(measurement, "Fish Tank Controller", f"Your {tank_name} tank is still reporting a LOW {measurement_name} of {current_measurement}!") update_mysql_database(measurement, "last_alert_sent_time", current_timestamp) log.debug(f"{measurement_name} Alert Notification RESENT at {current_military_time}.") log.debug(f"Tank {measurement_name} is LOW!") else: notify(measurement, "Fish Tank Controller", f"Your {tank_name} tank has reported a LOW {measurement_name} of {current_measurement}!") update_mysql_database(measurement, "last_alert_sent_time", current_timestamp) update_mysql_database(measurement, "alert_sent", True) log.debug(f"{measurement_name} Alert Notification sent at {current_military_time}.") log.warning(f"{tank_name} has reported a LOW {measurement_name}!") update_mysql_database("led_status", measurement + '_low', True) update_mysql_database(measurement, "ok", False) elif current_measurement > system_measurement_high: measurement_alert_time = getattr(system_info, measurement + '_alert_time') alert_sent = read_mysql_database(measurement, "alert_sent") if alert_sent: resend_alert = last_alert_time(measurement, measurement_alert_time) if resend_alert: notify(measurement, "Fish Tank Controller", f"Your {tank_name} tank is still reported a HIGH {measurement_name} of {current_measurement}!") update_mysql_database(measurement, "last_alert_sent_time", current_timestamp) log.debug(f"{measurement_name} Alert Notification RESENT at {current_military_time}.") log.debug(f"Tank {measurement_name} is HIGH!") else: notify(measurement, "Fish Tank Controller", f"Your {tank_name} tank has reported a HIGH {measurement_name} of {current_measurement}!") update_mysql_database(measurement, "last_alert_sent_time", current_timestamp) update_mysql_database(measurement, "alert_sent", True) log.debug(f"{measurement_name} Alert Notification sent at {current_military_time}.") log.warning(f"{tank_name} has reported a HIGH {measurement_name}!") update_mysql_database("led_status", measurement + '_high', True) update_mysql_database(measurement, "ok", False) else: ok = read_mysql_database(measurement, "ok") if not ok: update_mysql_database("led_status", measurement + '_high', False) update_mysql_database("led_status", measurement + '_low', False) update_mysql_database(measurement, "ok", True) update_mysql_database(measurement, "alert_sent", False) log.debug(f"{tank_name} has reported you are back to a NORMAL {measurement_name}!") notify(measurement, "Fish Tank Controller", f"Your {tank_name} tank {measurement_name} is back to NORMAL, now reporting {current_measurement}!") log.debug(f"{tank_name} has reported a NORMAL {measurement_name}!")
def gardenpi(): # Get Power Readings log.debug('gardenpi() called') if system_info.power_monitoring: current_power_keys = [ 'total_current_power_utilization', 'total_current_power_import', 'total_current_solar_production' ] power_data = {} for key in current_power_keys: power_data[key] = read_mysql_database("power_solar", key) else: power_data = { 'total_current_power_utilization': 0, 'total_current_power_import': 0, 'total_current_solar_production': 0 } any_zones_running = neptune.any_zones_running('water') any_power_zones_running = neptune.any_zones_running('power') current_water_source = (neptune.get_water_source()['source_to_use']) return render_template('gardenpi_index.html', any_zones_running=any_zones_running, current_water_source=current_water_source, any_power_zones_running=any_power_zones_running, **power_data)
def toggle_debug(): log_level = read_mysql_database("logging", "log_level") if log_level == 'DEBUG': update_mysql_database("logging", "log_level", 'INFO') else: update_mysql_database("logging", "log_level", 'DEBUG') return redirect(url_for('notifications'))
def toggle_logging(): system_logging = read_mysql_database("logging", "system_logging") if system_logging: update_mysql_database("logging", "system_logging", False) else: update_mysql_database("logging", "system_logging", True) return redirect(url_for('notifications'))
def modify_notifications(notifications): current_notification_setting = read_mysql_database("notifications", notifications) if current_notification_setting: update_mysql_database("notifications", notifications, False) else: update_mysql_database("notifications", notifications, True) return redirect(url_for('notifications'))
def toggle_edit_notifications_menu(): edit_notifications_menu = read_mysql_database("notifications", "edit_notifications") if edit_notifications_menu: update_mysql_database("notifications", "edit_notifications", False) else: update_mysql_database("notifications", "edit_notifications", True) return redirect(url_for('notifications'))
def last_alert_time(table, alert_time_delta): """ Calculates the time we last sent and an alert for a particular notification. Prevents continuous notifications from being sent out. """ last_alert_sent_time = int(read_mysql_database(table, "last_alert_sent_time")) last_alert_delta = (current_timestamp - int(last_alert_sent_time)) / 60 log.debug(f"Last alert for {table} was sent {last_alert_delta:.1f} minutes ago.") return last_alert_delta > alert_time_delta
def toggle_notifications_systemwide(): notifications_systemwide = read_mysql_database("notifications", "system_wide") if notifications_systemwide: update_mysql_database("notifications", "system_wide", False) update_mysql_database("notifications", "edit_notifications", False) else: update_mysql_database("notifications", "system_wide", True) return redirect(url_for('notifications'))
def toggle_uv_pump_power(): uv_pump_status = read_mysql_database("led_status", "uv_pump_status") if uv_pump_status: toggle_plug_power('UV Light', 'off') update_mysql_database("led_status", "uv_pump_status", False) return redirect(url_for('tank_control')) else: toggle_plug_power('UV Light', 'on') update_mysql_database("led_status", "uv_pump_status", True) return redirect(url_for('tank_control'))
def toggle_406_power(): f406_status = read_mysql_database("led_status", "406_status") if f406_status: toggle_plug_power('406 Filter', 'off') update_mysql_database("led_status", "406_status", False) return redirect(url_for('tank_control')) else: toggle_plug_power('406 Filter', 'on') update_mysql_database("led_status", "406_status", True) return redirect(url_for('tank_control'))
def toggle_airpump_power(): airpump_status = read_mysql_database("led_status", "airpump_status") if airpump_status: toggle_plug_power('Air Pump', 'off') update_mysql_database("led_status", "airpump_status", False) return redirect(url_for('tank_control')) else: toggle_plug_power('Air Pump', 'on') update_mysql_database("led_status", "airpump_status", True) return redirect(url_for('tank_control'))
def toggle_ph_controller_power(): ph_controller_status = read_mysql_database("led_status", "injecting_co2_led") if ph_controller_status: toggle_plug_power('PH Controller', 'off') update_mysql_database("led_status", "injecting_co2_led", False) return redirect(url_for('tank_control')) else: toggle_plug_power('PH Controller', 'on') update_mysql_database("led_status", "injecting_co2_led", True) return redirect(url_for('tank_control'))
def last_update_time(table, update_time_delta): """ Calculates the time we last updated our tank reading based on calling function. Certain readings are only updated ever x minutes while others may be xx minutes. This function handles figuring out if it is time to run an update. Once calculated it will return True or False to the calling function. This helps with throttling requests to APIs such as Thingspeak and Seneye. """ log.debug("last_update_time() Started") last_update_time = int(read_mysql_database(table, "last_update_time")) last_update_delta = (current_timestamp - int(last_update_time)) / 60 log.debug(f"Last update was {last_update_delta:1.0f} minutes ago.") return last_update_delta > update_time_delta
def tank_control(): # Get current tank Parameters current_parameters_keys = [ 'temp', 'ph', 'nh3', 'nh4', 'ec', 'tds', 'o2', 'par', 'lux', 'kelvin' ] parameter_data = {} for key in current_parameters_keys: parameter_data[key] = read_influx_database(key, "'75planted'") # Check Status led_status = [ 'tank_overall_status_led', 'temp_status_high', 'temp_status_low', 'ph_status_low', 'ph_status_high', 'nh3_status', 'tds_status', 'injecting_co2_led', 'fx6_status', 'f406_status', 'airpump_status', 'uv_pump_status' ] led_data = {} for key in led_status: led_data[key] = read_mysql_database("led_status", key) # Get Power Readings current_power_keys = [ 'total_current_power_utilization', 'total_current_power_import', 'total_current_solar_production' ] power_data = {} for key in current_power_keys: power_data[key] = read_mysql_database("power_solar", key) current_military_time = read_mysql_database("system_status", "current_military_time") return render_template('index.html', **parameter_data, **led_data, **power_data, current_military_time=current_military_time)