Exemple #1
0
def _pms5003():
    try:
        pm_readings = sensor_access.get_particulate_matter()
        pm10_reading = 0.0
        pm25_reading = 0.0
        if pm_readings is not None:
            if database_variables.particulate_matter_10 in pm_readings:
                pm10_reading = pm_readings[database_variables.particulate_matter_10]
            if database_variables.particulate_matter_2_5 in pm_readings:
                pm25_reading = pm_readings[database_variables.particulate_matter_2_5]
            headers = {"X-PIN": "1",
                       "X-Sensor": "raspi-" + luftdaten_config.station_id,
                       "Content-Type": "application/json",
                       "cache-control": "no-cache"}

            post_reply = requests.post(url=luftdaten_config.luftdaten_url,
                                       json={"software_version": luftdaten_config.return_software_version,
                                             "sensordatavalues": [{"value_type": "P1", "value": str(pm10_reading)},
                                                                  {"value_type": "P2", "value": str(pm25_reading)}]},
                                       headers=headers)
            if post_reply.ok:
                logger.network_logger.debug("Luftdaten - PMS5003 OK - Status Code: " + str(post_reply.status_code))
            else:
                logger.network_logger.warning("Luftdaten - PMS5003 Failed - Status Code: " + str(post_reply.status_code))
    except Exception as error:
        logger.network_logger.warning("Luftdaten - PMS5003 Failed: " + str(error))
def get_particulate_matter_10():
    logger.network_logger.debug("* Particulate Matter Sensors sent to " + str(request.remote_addr))
    return _get_filtered_reading(sensor_access.get_particulate_matter(), db_v.particulate_matter_10)
def get_weather_underground_readings():
    """
    Returns supported sensor readings for Weather Underground in URL format.
    Example:  &tempf=59.56&humidity=15.65
    """
    round_decimal_to = 5
    return_readings_str = ""

    temp_c = sensor_access.get_environment_temperature()
    if temp_c is not None:
        temp_c = temp_c[database_variables.env_temperature]
        try:
            temperature_f = (float(temp_c) * (9.0 / 5.0)) + 32.0
            if weather_underground_config.outdoor_sensor:
                return_readings_str += "&tempf=" + str(
                    round(temperature_f, round_decimal_to))
            else:
                return_readings_str += "&indoortempf=" + str(
                    round(temperature_f, round_decimal_to))
        except Exception as error:
            log_msg = "Weather Underground - Unable to calculate Temperature into fahrenheit: " + str(
                error)
            logger.sensors_logger.error(log_msg)

    humidity = sensor_access.get_humidity()
    if humidity is not None:
        humidity = humidity[database_variables.humidity]
        if weather_underground_config.outdoor_sensor:
            return_readings_str += "&humidity=" + str(
                round(humidity, round_decimal_to))
        else:
            return_readings_str += "&indoorhumidity=" + str(
                round(humidity, round_decimal_to))

    out_door_dew_point = sensor_access.get_dew_point()
    if out_door_dew_point is not None and weather_underground_config.outdoor_sensor:
        out_door_dew_point = out_door_dew_point[database_variables.dew_point]
        try:
            dew_point_f = (float(out_door_dew_point) * (9.0 / 5.0)) + 32.0
            return_readings_str += "&dewptf=" + str(
                round(dew_point_f, round_decimal_to))
        except Exception as error:
            log_msg = "Weather Underground - Unable to calculate Dew Point into fahrenheit: " + str(
                error)
            logger.sensors_logger.error(log_msg)

    pressure_hpa = sensor_access.get_pressure()
    if pressure_hpa is not None:
        pressure_hpa = pressure_hpa[database_variables.pressure]
        try:
            baromin = float(pressure_hpa) * 0.029529983071445
            return_readings_str += "&baromin=" + str(
                round(baromin, round_decimal_to))
        except Exception as error:
            logger.sensors_logger.error(
                "Weather Underground - Unable to calculate Pressure into Hg: "
                + str(error))

    ultra_violet = sensor_access.get_ultra_violet()
    if ultra_violet is not None:
        if database_variables.ultra_violet_index in ultra_violet:
            uv_index = ultra_violet[database_variables.ultra_violet_index]
            return_readings_str += "&UV=" + str(
                round(uv_index, round_decimal_to))

    pm_readings = sensor_access.get_particulate_matter()
    if pm_readings is not None:
        if database_variables.particulate_matter_2_5 in pm_readings:
            pm_2_5 = pm_readings[database_variables.particulate_matter_2_5]
            return_readings_str += "&AqPM2.5=" + str(
                round(pm_2_5, round_decimal_to))
        if database_variables.particulate_matter_10 in pm_readings:
            pm_10 = pm_readings[database_variables.particulate_matter_10]
            return_readings_str += "&AqPM10=" + str(
                round(pm_10, round_decimal_to))
    return return_readings_str
Exemple #4
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