def get_seneye_data(): """Queries the Seneye API for various monitored parameters and records them for use by other modules.""" log.debug("get_seneye_data() called") update_time_delta = system_info.seneye_update_time do_seneye_update_now = last_update_time("seneye_data", update_time_delta) if do_seneye_update_now: log.debug("Time to update Seneye Readings") url = "https://api.seneye.com/v1/devices/76433?IncludeState=1&user={}&pwd={}".format(system_info.seneye_email, system_info.seneye_password) headers = {'Accept': 'application/json', 'Content-Type': 'application/json'} api_request = requests.get(url, headers=headers) response = api_request.json() to_json = json.dumps(response) dict_json = json.loads(to_json) keys = ['nh3', 'nh4', 'o2', 'lux', 'par', 'kelvin'] for key in dict_json['exps']: if key in keys: value = dict_json['exps'][key]['curr'] update_influx_database(key, float(value), system_info.tank_name) # Convert temperature from C to F and write both values to influx if requested temp_in_c = float(dict_json['exps']['temperature']['curr']) temp_in_f = (9.0 / 5.0 * temp_in_c) + 32 if system_info.Temp_in_F: update_influx_database("temperature", temp_in_f, system_info.tank_name) else: update_influx_database("temperature", temp_in_c, system_info.tank_name) update_mysql_database("seneye_data", "last_update_time", current_timestamp) else: log.debug("Not time to update Seneye yet!")
def get_atlas_data(): """ Queries the Thingspeak API (used by the Atlas Sensors) for various monitored parameters amd records them for use by other modules. """ log.debug("get_atlas_data() called") update_time_delta = system_info.atlas_update_time do_atlas_update_now = last_update_time("atlas_data", update_time_delta) if do_atlas_update_now: log.debug("Time to update Atlas Readings") url = "https://api.thingspeak.com/channels/{}/feed/last.json?api_key={}&results=2".format( system_info.thingspeak_channel, system_info.thingspeak_api_key) headers = {'Accept': 'application/json', 'Content-Type': 'application/json'} api_request = requests.get(url, headers=headers) response = api_request.json() ph = float(response['field1']) update_influx_database("ph", ph, system_info.tank_name) ec = (round(float(response['field2']),1)) # TDS is a math function of EC, here we calculate TDS for storage or printing. tds = (round(float(ec * .55),1)) update_influx_database("ec", ec, system_info.tank_name) update_influx_database("tds", tds, system_info.tank_name) temp_in_C_thingspeak = (float(response['field3'])) update_influx_database("Temp_in_C_thingspeak", temp_in_C_thingspeak, system_info.tank_name) if system_info.Temp_in_F: temp = (round((9.0 / 5.0 * temp_in_C_thingspeak) + 32,1)) update_influx_database("temp", temp, system_info.tank_name) update_mysql_database("atlas_data", "last_update_time", current_timestamp) else: log.debug("Not time to update Atlas yet!")
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 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 check_ph_controller_status(): """Check if our pH Controller is physically running.""" log.debug("check_ph_controller_status() called") ph_controller_current_watts = power_strip.get_current_power("PH Controller") if ph_controller_current_watts > system_info.ph_controller_watts: update_mysql_database("led_status", "injecting_co2_led", True) else: update_mysql_database("led_status", "injecting_co2_led", False)
def check_406_filter_status(): """Check if our 406 filter is physically running.""" log.debug("check_406_filter_status() called") f406_filter_current_watts = power_strip.get_current_power("406 Filter") if f406_filter_current_watts > system_info.f406_filter_watts: update_mysql_database("led_status", "f406_status", True) else: update_mysql_database("led_status", "f406_status", False)
def check_uv_pump_status(): """Check if our UV light and pump is physically running.""" log.debug("check_uv_pump_status() called") uv_pump_current_watts = power_strip.get_current_power("UV Light") if uv_pump_current_watts > system_info.uv_pump_watts: update_mysql_database("led_status", "uv_pump_status", True) else: update_mysql_database("led_status", "uv_pump_status", False)
def check_airpump_status(): """Check if our air pump is physically running.""" log.debug("check_airpump_status() called") airpump_current_watts = power_strip.get_current_power("Air Pump") if airpump_current_watts > system_info.air_pump_watts: update_mysql_database("led_status", "airpump_status", True) else: update_mysql_database("led_status", "airpump_status", False)
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 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 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 check_parameters(measurement, tank_name, influx_table, measurement_name): """Check that nh3, nh4 and tds/ec tank parameters are within limits. Alerts us if they are not.""" log.debug("check_tank_parameters() called") current_measurement = read_influx_database(influx_table, tank_name) if measurement_name == 'nh3': log.debug(f"The current {measurement_name} of {tank_name} is {current_measurement:1.3f}") else: log.debug(f"The current {measurement_name} of {tank_name} is {current_measurement:1.0f}") measurement_limit = getattr(system_info, measurement) if current_measurement > measurement_limit: measurement_limit_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_limit_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.warning(f"Tank {measurement_name} is LOW!") 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, True) update_mysql_database(measurement, "ok", False) else: ok = read_mysql_database(measurement, "ok") if not ok: update_mysql_database("led_status", measurement, 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}!")