示例#1
0
def get_illuminance():
    try:
        lux = ltr559.get_lux()
        local_data_gateway.post_metrics_update('light', 'ok')
        return lux
    except Exception as exception:
        logger.error(
            f'Unable to read from ltr559 (light) sensor due to {exception}')
        local_data_gateway.post_metrics_update('light', 'errors')
        setup()
示例#2
0
def get_measurement():
    try:
        result = bh1745.get_rgb_scaled()
        local_data_gateway.post_metrics_update('rgb', 'ok')
        return result
    except Exception as exception:
        logger.error(
            f' Unable to take measurement from uv sensor due to {exception}')
        local_data_gateway.post_metrics_update('rgb', 'errors')
        # TODO add reboot sensor
        return 0, 0, 0
示例#3
0
def get_measurements():
    try:
        uva, uvb = uv_sensor.get_measurements()
        uv_comp1, uv_comp2 = uv_sensor.get_comparitor_readings()
        local_data_gateway.post_metrics_update('uv', 'ok')
        return uv_sensor.convert_to_index(uva, uvb, uv_comp1, uv_comp2)
    except Exception as exception:
        logger.error(
            f' Unable to take measurement from uv sensor due to {exception}')
        local_data_gateway.post_metrics_update('uv', 'errors')
        # TODO add reboot sensor
        return 0, 0, 0
示例#4
0
def get_measurement():
    try:
        data = gas.read_all()
        oxidising = data.oxidising / 1000
        reducing = data.reducing / 1000
        nh3 = data.nh3 / 1000
        local_data_gateway.post_metrics_update('gas', 'ok')
        return oxidising, reducing, nh3
    except Exception as exception:
        logger.error(f'Unable to read from gas sensor due to {exception}')
        local_data_gateway.post_metrics_update('gas', 'errors')
        return 0, 0, 0
示例#5
0
def get_measurement():
    if weather_sensor.get_sensor_data():
        local_data_gateway.post_metrics_update('weather', 'ok')
        return {
            'temp': weather_sensor.data.temperature,
            'pressure': weather_sensor.data.pressure,
            'humidity': weather_sensor.data.humidity,
            'gas_resistance': weather_sensor.data.gas_resistance
        }
    else:
        logger.warning("Weather sensor didn't return data")
        local_data_gateway.post_metrics_update('weather', 'errors')
        return {'temp': 0, 'pressure': 0, 'humidity': 0, 'gas_resistance': 0}
示例#6
0
def get_motion() -> int:
    try:
        sample()
        value = 0
        for i in range(1, len(points)):
            value += abs(points[i] - points[i - 1])
        local_data_gateway.post_metrics_update('motion', 'ok')
        return value
    except Exception as exception:
        logger.info(f'Exception occurred while getting data {exception}',
                    exception)
        local_data_gateway.post_metrics_update('motion', 'errors')
        reset()
        return 0
示例#7
0
def digest():
    # load and save them to file for stats
    counter = 0
    errors = 0  # add when error was happen last time
    warnings = 0
    while True:
        counter += 1
        start_time = timer()

        result = local_data_gateway.get_data_for(
            config_service.get_url_for_dump1090(), 5)

        if 'error' in result:
            logger.error(result['error'])
            local_data_gateway.post_metrics_update('flight', 'errors')
            errors += 1
            logger.error('Errors: {}'.format(errors))
        else:
            aircraft_storage.save_raw_reading(result)
            aircraft_storage.save_processed_data(result)
            if counter % 2 == 0:
                local_data_gateway.post_healthcheck_beat('other', 'radar')
            local_data_gateway.post_metrics_update('flight', 'ok')

        end_time = timer()

        measurement = int((end_time - start_time) * 1000)
        measurement_time = str(measurement)  # in ms

        if measurement > config_service.max_latency():
            warnings += 1
            logger.warning("Measurement {} was slow.It took {} ms".format(
                counter, measurement))

        if counter % 2 == 0:
            local_data_gateway.post_healthcheck_beat('other', 'digest')
        display_stats()
        measurement_message = 'Measurement no. {} It took {} milliseconds to process. Errors: {}. Warnings: {}'.format(
            counter, measurement_time, errors, warnings)
        logger.info(measurement_message)
        remaining_time = refresh_rate_in_seconds - (float(measurement_time) /
                                                    1000)

        if remaining_time > 0:
            time.sleep(remaining_time)
def get_measurement():
    global pms5003
    p_1 = 0
    p_2 = 0
    p_10 = 0

    try:
        pms_data = pms5003.read()
        local_data_gateway.post_metrics_update('pollution', 'ok')
    except pmsReadTimeoutError as exception:
        logger.warning("Failed to read PMS5003 due to: {}".format(exception),
                       exc_info=True)
        local_data_gateway.post_metrics_update('pollution', 'errors')
        logger.info('Restarting sensor.. (it will takes ... 5 seconds')
        pms5003 = PMS5003()
        time.sleep(5)
    else:
        p_1 = float(pms_data.pm_ug_per_m3(1.0))
        p_2 = float(pms_data.pm_ug_per_m3(2.5))
        p_10 = float(pms_data.pm_ug_per_m3(10))
    return p_1, p_2, p_10
示例#9
0
def get_data_from_measurement() -> dict:
    environment = environment_service.get_measurement()
    aqi = "n/a"
    eco2 = ""
    tvoc = ""
    try:
        eco2 = air_quality_service.get_eco2_measurement_as_string()
        tvoc = air_quality_service.get_tvoc_measurement_as_string()
        local_data_gateway.post_metrics_update('air_quality', 'ok')
    except Exception as air_quality_exception:
        logger.error(
            f'Unable to read from air quality sensor due to {air_quality_exception}'
        )
        local_data_gateway.post_metrics_update('air_quality', 'errors')

    red, green, blue = two_led_service.get_measurement()
    colour = dom_utils.to_hex(red, green, blue)
    motion = motion_service.get_motion()
    two_led_service.warn_if_dom_shakes_his_legs(motion)

    uva_index, uvb_index, avg_uv_index = uv_service.get_measurements()

    return {
        "temp": environment['temp'],
        "pressure": environment['pressure'],
        "humidity": environment['humidity'],
        "gas_resistance": "{:.2f}".format(environment['gas_resistance']),
        "aqi": aqi,
        "colour": colour,
        "motion": motion,
        "uva_index": uva_index,
        "uvb_index": uvb_index,
        "r": red,
        "g": green,
        "b": blue,
        "eco2": eco2,
        "tvoc": tvoc,
    }