def main(): measurement_counter = 0 setup() while True: measurement_counter += 1 logger.debug('Measurement No.{}'.format(measurement_counter)) start_time = timer() measurement = get_measurement() # FIXME temporary disabled denviro_display.display_on_screen(measurement) measurement['cpu_temp'] = commands.get_cpu_temp() end_time = timer() measurement_time = str(int((end_time - start_time) * 1000)) # in ms measurement['measurement_time'] = measurement_time logger.info('it took ' + str(measurement_time) + ' milliseconds to measure it.') cl_display.print_measurement(measurement) # FIXME do not work due to “sounddevice.PortAudioError: Error querying device -1" error # logger.warning(noise_service.get_noise_measurement()) data_files.store_enviro_measurement(measurement) sensor_warnings_service.get_current_warnings_for_enviro() if measurement_counter % 2 == 0: local_data_gateway.post_healthcheck_beat('denviro', 'app') remaining_time_in_millis = 5 - (float(measurement_time) / 1000) if int(measurement_time) > config_service.max_latency(fast=False): logger.warning("Measurement {} was slow.It took {} ms".format(measurement_counter, measurement_time)) if remaining_time_in_millis > 0: time.sleep(remaining_time_in_millis)
def get_colour_for_cpu(): cpu_temp = float(re.sub('[^0-9.]', '', commands.get_cpu_temp())) config = config_service.load_cfg() if cpu_temp > config['sensor']['cpu_temp_fatal']: return ['Fatal', 255, 16, 1] elif cpu_temp > config['sensor']['cpu_temp_error']: return ['Fatal', 246, 108, 8] elif cpu_temp > config['sensor']['cpu_temp_warn']: return ['Warn', 255, 215, 0] else: return ['Good', 106, 168, 79]
def main(): measurement_counter = 0 two_led_service.led_startup_show() while True: measurement_counter += 1 logger.debug('Getting measurement no.{}'.format(measurement_counter)) start_time = timer() data = get_data_from_measurement() data['cpu_temp'] = commands.get_cpu_temp() end_time = timer() measurement_time = int((end_time - start_time) * 1000) # in ms logger.info( 'Measurement no. {} took {} milliseconds to measure it.'.format( measurement_counter, measurement_time)) data['measurement_counter'] = measurement_counter data['measurement_time'] = str(measurement_time) data_files.store_measurement( data, motion_service.get_current_motion_difference()) cl_display.print_measurement(data) if measurement_counter % 2 == 0: local_data_gateway.post_healthcheck_beat('denva', 'app') remaining_of_five_s = 5 - (float(measurement_time) / 1000) if measurement_time > config_service.max_latency(fast=False): logger.warning("Measurement {} was slow.It took {} ms".format( measurement_counter, measurement_time)) if measurement_counter % 10 == 0: led_matrix_service.display_fasting_mode() if remaining_of_five_s > 0: time.sleep(remaining_of_five_s ) # it should be 5 seconds between measurements
def display_on_screen(measurement: dict): cycle = 0 draw.rectangle((0, 0, 160, 80), fill="black") color1 = [ '', random.randrange(0, 255, 1), random.randrange(0, 255, 1), random.randrange(0, 255, 1) ] color2 = [ '', random.randrange(0, 255, 1), random.randrange(0, 255, 1), random.randrange(0, 255, 1) ] color3 = [ '', random.randrange(0, 255, 1), random.randrange(0, 255, 1), random.randrange(0, 255, 1) ] line3 = '' if cycle % 6 == 0: line1 = 'CPU Temp: {}'.format(commands.get_cpu_temp()) line2 = 'IP: {}'.format(commands.get_ip()) line3 = 'Uptime: {}'.format(commands.get_uptime()) elif cycle % 6 == 1: line1 = 'RAM avail.: {}'.format( system_data_service.get_memory_available_in_mb()) line2 = 'Space: {}'.format(commands.get_space_available()) line3 = 'Data Space: {}'.format(commands.get_data_space_available()) color1 = get_colour_for_cpu() elif cycle % 6 == 2: line1 = 'light: {}'.format(measurement["light"]) line2 = 'proximity: {}'.format(measurement["proximity"]) elif cycle % 6 == 3: line1 = 'nh 3: {:.2f}'.format(measurement["nh3"]) line2 = 'oxidised: {:.2f}'.format(measurement["oxidised"]) line3 = 'reduced: {:.2f}'.format(measurement["reduced"]) else: line1 = 'pm 1: {}'.format(measurement["pm1"]) line2 = 'pm 2.5: {}'.format(measurement["pm25"]) line3 = 'pm 10: {}'.format(measurement["pm10"]) color1 = get_colour(measurement["pm1"]) color2 = get_colour(measurement["pm25"]) color3 = get_colour(measurement["pm10"]) draw.text((0, 0), line1, font=font, fill=(color1[1], color1[2], color1[3])) draw.text((0, 16), line2, font=font, fill=(color2[1], color2[2], color2[3])) draw.text((0, 32), line3, font=font, fill=(color3[1], color3[2], color2[3])) # shade_of_grey = random.randrange(128, 255, 1) # draw.text((0, 48), line4, font=font, fill=(shade_of_grey, shade_of_grey, shade_of_grey)) st7735.display(img) cycle += 1
def draw_image_on_screen(data, app_uptime): global cycle warnings_list = sensor_warnings_service.get_warnings_as_list(data) img = Image.open( "/home/pi/denva-master/src/images/background.png").convert(oled.mode) draw = ImageDraw.Draw(img) draw.rectangle([(0, 0), (128, 128)], fill="black") if len(warnings_list) > 0 and cycle % 3 == 2: draw.text((0, 0), "WARNINGS", fill="white", font=rr_14) y = 2 for warning in warnings_list: y += 14 draw.text((0, y), warning, fill="white", font=rr_12) elif cycle % 6 == 1: statuses = web_data_gateway.get_status() draw.text((0, 0), "Train & Tubes", fill="white", font=rr_14) y = 2 for status in statuses: y += 14 draw.text((0, y), status, fill="white", font=rr_12) else: draw.text((0, 0), "Temp: {}".format(data["temp"]), fill="white", font=rr_12) draw.text((0, 14), "Pressure: {}".format(data["pressure"]), fill="white", font=rr_12) if cycle % 2 == 0: draw.text((0, 28), "Humidity: {}".format(data["humidity"]), fill="white", font=rr_12) draw.text((0, 42), "Motion: {:05.02f}".format(data["motion"]), fill="white", font=rr_12) draw.text( (0, 56), "Colour: {}".format(dom_utils.get_color_name(data["colour"])), fill="white", font=rr_12) draw.text( (0, 70), "UVA: {}".format(get_description_for.uv(data["uva_index"])), fill="white", font=rr_12) else: draw.text((0, 28), "eco2: {}".format(data["eco2"]), fill="white", font=rr_12) draw.text( (0, 42), "Tvoc: {}".format( get_description_for.iqa_from_tvoc(data["tvoc"])['score']), fill="white", font=rr_12) draw.text( (0, 56), "Brightness: {}".format( get_description_for.brightness(data["r"], data["g"], data["b"])), fill="white", font=rr_12) draw.text( (0, 70), "UVB: {}".format(get_description_for.uv(data["uvb_index"])), fill="white", font=rr_12) if cycle % 7 == 0: draw.text((0, 84), 'CPU: {}'.format(commands.get_cpu_temp()), fill="white", font=rr_12) elif cycle % 7 == 1: draw.text((0, 84), commands.get_uptime(), fill="white", font=rr_12) elif cycle % 7 == 2: draw.text((0, 84), 'CPU: {}'.format(commands.get_cpu_speed()), fill="white", font=rr_12) elif cycle % 7 == 3: draw.text((0, 84), 'IP: {}'.format(commands.get_ip()), fill="white", font=rr_12) elif cycle % 7 == 4: draw.text((0, 84), 'Space: {} MB'.format(commands.get_space_available()), fill="white", font=rr_12) elif cycle % 7 == 5: draw.text((0, 84), 'Data: {} MB'.format(commands.get_data_space_available()), fill="white", font=rr_12) elif cycle % 7 == 6: draw.text((0, 84), 'RAM avail.: {}'.format( system_data_service.get_memory_available_in_mb()), fill="white", font=rr_12) else: draw.text((0, 84), app_uptime, fill="white", font=rr_12) oled.display(img) cycle += 1 if cycle > 12: cycle = 0