def get_gas_nh3():
    logger.network_logger.debug("* GAS Sensors sent to " + str(request.remote_addr))
    return _get_filtered_reading(sensor_access.get_gas(), db_v.gas_nh3)
Beispiel #2
0
def _open_sense_map_server():
    """ Sends compatible sensor readings to Open Sense Map every X seconds based on set Interval. """
    app_cached_variables.restart_open_sense_map_thread = False
    if osm_config.sense_box_id != "":
        url = osm_config.open_sense_map_main_url_start + "/" + osm_config.sense_box_id + "/data"
        url_header = {"content-type": "application/json"}

        while not app_cached_variables.restart_open_sense_map_thread:
            body_json = {}
            try:
                env_temperature = sensor_access.get_environment_temperature()
                if env_temperature is not None and osm_config.temperature_id != "":
                    body_json[osm_config.temperature_id] = env_temperature[
                        db_v.env_temperature]

                pressure_reading = sensor_access.get_pressure()
                if pressure_reading is not None and osm_config.pressure_id != "":
                    body_json[osm_config.pressure_id] = pressure_reading[
                        db_v.pressure]

                altitude_reading = sensor_access.get_altitude()
                if altitude_reading is not None and osm_config.altitude_id != "":
                    body_json[osm_config.altitude_id] = altitude_reading[
                        db_v.altitude]

                humidity_reading = sensor_access.get_humidity()
                if humidity_reading is not None and osm_config.humidity_id != "":
                    body_json[osm_config.humidity_id] = humidity_reading[
                        db_v.humidity]

                gas_readings = sensor_access.get_gas()
                if gas_readings is not None:
                    if db_v.gas_resistance_index in gas_readings and osm_config.gas_voc_id != "":
                        gas_voc = gas_readings[db_v.gas_resistance_index]
                        body_json[osm_config.gas_voc_id] = gas_voc
                    if db_v.gas_oxidising in gas_readings and osm_config.gas_oxidised_id != "":
                        gas_oxidised = gas_readings[db_v.gas_oxidising]
                        body_json[osm_config.gas_oxidised_id] = gas_oxidised
                    if db_v.gas_reducing in gas_readings and osm_config.gas_reduced_id != "":
                        gas_reduced = gas_readings[db_v.gas_reducing]
                        body_json[osm_config.gas_reduced_id] = gas_reduced
                    if db_v.gas_nh3 in gas_readings and osm_config.gas_nh3_id != "":
                        gas_nh3 = gas_readings[db_v.gas_nh3]
                        body_json[osm_config.gas_nh3_id] = gas_nh3

                lumen_reading = sensor_access.get_lumen()
                if lumen_reading is not None and osm_config.lumen_id != "":
                    body_json[osm_config.lumen_id] = lumen_reading[db_v.lumen]

                pm_readings = sensor_access.get_particulate_matter()
                if pm_readings is not None:
                    if db_v.particulate_matter_1 in pm_readings and osm_config.pm1_id != "":
                        pm1 = pm_readings[db_v.particulate_matter_1]
                        body_json[osm_config.pm1_id] = pm1
                    if db_v.particulate_matter_2_5 in pm_readings and osm_config.pm2_5_id != "":
                        pm2_5 = pm_readings[db_v.particulate_matter_2_5]
                        body_json[osm_config.pm2_5_id] = pm2_5
                    if db_v.particulate_matter_4 in pm_readings and osm_config.pm4_id != "":
                        pm4 = pm_readings[db_v.particulate_matter_4]
                        body_json[osm_config.pm4_id] = pm4
                    if db_v.particulate_matter_10 in pm_readings and osm_config.pm10_id != "":
                        pm10 = pm_readings[db_v.particulate_matter_10]
                        body_json[osm_config.pm10_id] = pm10

                colors = sensor_access.get_ems_colors()
                if colors is not None:
                    if db_v.red in colors and osm_config.red_id != "":
                        red = colors[db_v.red]
                        body_json[osm_config.red_id] = red
                    if db_v.orange in colors and osm_config.orange_id != "":
                        orange = colors[db_v.orange]
                        body_json[osm_config.orange_id] = orange
                    if db_v.yellow in colors and osm_config.yellow_id != "":
                        yellow = colors[db_v.yellow]
                        body_json[osm_config.yellow_id] = yellow
                    if db_v.green in colors and osm_config.green_id != "":
                        green = colors[db_v.green]
                        body_json[osm_config.green_id] = green
                    if db_v.blue in colors and osm_config.blue_id != "":
                        blue = colors[db_v.blue]
                        body_json[osm_config.blue_id] = blue
                    if db_v.violet in colors and osm_config.violet_id != "":
                        violet = colors[db_v.violet]
                        body_json[osm_config.violet_id] = violet

                uv_readings = sensor_access.get_ultra_violet()
                if uv_readings is not None:
                    if db_v.ultra_violet_index in uv_readings and osm_config.ultra_violet_index_id != "":
                        uv_index = uv_readings[db_v.ultra_violet_index]
                        body_json[osm_config.ultra_violet_index_id] = uv_index
                    if db_v.ultra_violet_a in uv_readings and osm_config.ultra_violet_a_id != "":
                        uv_a = uv_readings[db_v.ultra_violet_a]
                        body_json[osm_config.ultra_violet_a_id] = uv_a
                    if db_v.ultra_violet_b in uv_readings and osm_config.ultra_violet_b_id != "":
                        uv_b = uv_readings[db_v.ultra_violet_b]
                        body_json[osm_config.ultra_violet_b_id] = uv_b

                if len(body_json) > 0:
                    html_get_response = requests.post(url=url,
                                                      headers=url_header,
                                                      json=body_json)
                    status_code = str(html_get_response.status_code)
                    response_text = str(html_get_response.text)
                    if html_get_response.status_code == 201:
                        logger.network_logger.debug(
                            "Open Sense Map - Sent OK - Status Code: " +
                            status_code)
                    elif html_get_response.status_code == 415:
                        logger.network_logger.error(
                            "Open Sense Map: Invalid or Missing content type")
                    else:
                        log_msg = "Open Sense Map - Unknown Error " + status_code + ": " + response_text
                        logger.network_logger.error(log_msg)
                else:
                    log_msg = "Open Sense Map - No further updates will be attempted: " + \
                              "No Compatible Sensors or Missing Sensor IDs"
                    logger.network_logger.warning(log_msg)
                    while not app_cached_variables.restart_open_sense_map_thread:
                        sleep(5)
            except Exception as error:
                logger.network_logger.error(
                    "Open Sense Map - Error sending data")
                logger.network_logger.debug(
                    "Open Sense Map - Detailed Error: " + str(error))

            sleep_fraction_interval = 5
            sleep_total = 0
            while sleep_total < osm_config.interval_seconds and not app_cached_variables.restart_open_sense_map_thread:
                sleep(sleep_fraction_interval)
                sleep_total += sleep_fraction_interval