def _worker_thread_and_monitor(self): if self.args is not None: monitored_thread = Thread(target=self.function, args=self.args) else: monitored_thread = Thread(target=self.function) monitored_thread.daemon = True monitored_thread.start() primary_logger.debug(self.thread_name + " started") while True: time.sleep(30) if not monitored_thread.is_alive(): self.is_running = False self.current_restart_count += 1 if self.current_restart_count < self.max_restart_count: if self.args is not None: monitored_thread = Thread(target=self.function, args=self.args) else: monitored_thread = Thread(target=self.function) monitored_thread.daemon = True monitored_thread.start() self.is_running = True else: log_msg = self.thread_name + " has attempted to restart " + str( self.current_restart_count) primary_logger.critical( log_msg + " Times. No further restart attempts will be made.") while True: time.sleep(600)
def load_installed_hardware_from_file(self): """ Loads Installed Hardware configuration from local disk. """ log_msg = "Loading Installed Hardware Configuration from: " + file_locations.installed_hardware_file_location primary_logger.debug(log_msg) if os.path.isfile(file_locations.installed_hardware_file_location): try: config_file_lines = get_file_content( file_locations.installed_hardware_file_location).split( "\n") config_list = [] for line in config_file_lines: config_list.append(line.split("=")[0].strip()) self.installed_interactive_hw["WaveShare27"] = int( config_list[1]) except Exception as error: primary_logger.error( "Unable to load Installed Hardware configuration File: " + str(error)) self.write_installed_hardware_to_file() else: primary_logger.warning( "No Installed Hardware Configuration file found, saving default" ) self.write_installed_hardware_to_file()
def __init__(self): self.full_system_text = get_raspberry_pi_model() self.band_width_message = "Available Bandwidth:\n Unknown Mbps" if self.full_system_text == "Raspberry Pi 3 Model B Plus": self.band_width_message = "Available Bandwidth:\n up to 299 Mbps" elif self.full_system_text == "Raspberry Pi 4 Model B": self.band_width_message = "Available Bandwidth:\n up to 999 Mbps" try: os.system("raspi-config nonint do_spi 0") primary_logger.debug("Enabled SPI for WaveShare 2.7 E-Paper") except Exception as error: primary_logger.error("Error Enabling SPI for WaveShare 2.7 E-Paper: " + str(error)) self.display_in_use = False # GPIO key to Pin #s self.key1 = 5 self.key2 = 6 self.key3 = 13 self.key4 = 19 self.rpi_gpio_import = __import__('RPi.GPIO', fromlist=["setmode", "BCM", "setup", "IN", "PUD_UP", "input"]) self.epd2in7_import = __import__("supported_hardware.drivers.waveshare.epd2in7", fromlist=["EPD", "EPD_WIDTH", "EPD_HEIGHT"]) self.esp = self.epd2in7_import.EPD() self.esp.init() self.rpi_gpio_import.setmode(self.rpi_gpio_import.BCM) self.rpi_gpio_import.setup(self.key1, self.rpi_gpio_import.IN, pull_up_down=self.rpi_gpio_import.PUD_UP) self.rpi_gpio_import.setup(self.key2, self.rpi_gpio_import.IN, pull_up_down=self.rpi_gpio_import.PUD_UP) self.rpi_gpio_import.setup(self.key3, self.rpi_gpio_import.IN, pull_up_down=self.rpi_gpio_import.PUD_UP) self.rpi_gpio_import.setup(self.key4, self.rpi_gpio_import.IN, pull_up_down=self.rpi_gpio_import.PUD_UP)
def get_remote_data(url): """ Returns requested HTTP data (based on the provided URL). """ try: tmp_data = requests.get(url=url, timeout=0.5) return_data = tmp_data.content except Exception as error: primary_logger.debug("Unable to retrieve remote data: " + str(error)) return_data = "NA" return return_data
def send_command(url): """ Sends command URL using requests. """ try: requests.get(url=url, timeout=5, headers={'Connection': 'close'}) primary_logger.debug(str(url) + " sent OK") return True except Exception as error: primary_logger.warning("Error sending '" + str(url) + "': " + str(error)) return False
def display_message(self, text_message): # 255 to clear the frame self.display_in_use = True display_image = Image.new("1", (self.epd2in7_import.EPD_WIDTH, self.epd2in7_import.EPD_HEIGHT), 255) draw = ImageDraw.Draw(display_image) font18 = ImageFont.truetype(file_locations.location_truetype_font, 16) draw.text((2, 0), text_message, font=font18, fill=0) primary_logger.debug(text_message) self.esp.display(self.esp.getbuffer(display_image)) self.display_in_use = False
def __init__(self): self.app_version = "1.4.0" self.full_system_text = get_raspberry_pi_model() primary_logger.debug("Running on " + str(self.full_system_text)) primary_logger.info("Test Results Saving to: " + file_locations.location_save_report_folder) self.running_on_rpi = False if self.full_system_text[:12] == "Raspberry Pi": self.running_on_rpi = True self.mtr_running = False self.iperf_running = False self.is_iperf_server = 1 self.iperf_port = "9000" self.iperf_run_options = "-O 1" self.mtr_run_count = "10" self.remote_tester_ip = "8.8.8.8" self.schedule_run_on_boot = 1 self.schedule_run_every_minutes_enabled = 0 self.schedule_run_every_minutes = 0 self.schedule_run_once_enabled = 0 self.schedule_run_once_at_time = "" self.local_ethernet_dhcp = 1 self.local_ethernet_adapter_name = "eth0" self.local_ethernet_ip = "" self.local_ethernet_subnet = "" self.local_ethernet_gateway = "" self.local_ethernet_dns1 = "" self.local_ethernet_dns2 = "" self.local_wireless_dhcp = 1 self.local_wireless_adapter_name = "wlan0" self.local_wireless_ip = "" self.local_wireless_subnet = "" self.local_wireless_gateway = "" self.local_wireless_dns1 = "" self.local_wireless_dns2 = "" self.wifi_country_code = "" self.wifi_ssid = "" self.wifi_security_type = "" self.wifi_pass_key = "" self.installed_interactive_hw = {"WaveShare27": 0} self.using_dummy_access = False self.button_function_level = 0 self.load_config_from_file() self.load_installed_hardware_from_file() self.load_dhcpcd_conf_from_file() self.load_wpa_supplicant_wifi()
def check_tester_online_status(ip, port): """ requests HTTP connection to Tester IP. Return sensor status. """ url = "http://" + ip + ":" + str(port) + "/CheckOnlineStatus" try: html_request_response = requests.get(url=url, timeout=0.5) if html_request_response.status_code == 200: return "Online" except Exception as error: primary_logger.debug("Error checking remote tester @ " + str(ip) + ": " + str(error)) return "Offline" return "Offline"
def get_dhcpcd_ip(wireless=False): try: if wireless: address = os.popen("ip addr show wlan0").read().split( "inet ")[1].split("/")[0] else: address = os.popen("ip addr show eth0").read().split( "inet ")[1].split("/")[0] return address except Exception as error: primary_logger.debug("Interface is Inactive: " + str(error)) return "Inactive"
def run_command(command_num): if hardware_access.display_in_use: primary_logger.debug( "Display In Use, skipping Command. Try again in a few seconds.") else: display_msg = "" if command_num == 0: if current_config.button_function_level == 0: thread_function(hardware_access.display_message, args="Running MTR Test\n\nPlease Wait ...") start_mtr() display_msg = hardware_access.get_mtr_message() elif current_config.button_function_level == 1: display_msg = hardware_access.get_sys_info_message() primary_logger.debug(display_msg) elif current_config.button_function_level == 2: display_msg = "Shutting Down\nRemote Server\n\nPlease Wait 15 Seconds\nBefore Powering Down ..." port = app_variables.flask_http_port send_command("http://" + current_config.remote_tester_ip + ":" + str(port) + "/Shutdown") elif command_num == 1: if current_config.button_function_level == 0: thread_function(hardware_access.display_message, args="Running iPerf3 Test\n\nPlease Wait ...") start_iperf() display_msg = hardware_access.get_iperf_message() elif current_config.button_function_level == 1: os.system("systemctl start KootnetTesterUpgradeOnline.service") display_msg = hardware_access.get_upgrade_message() elif current_config.button_function_level == 2: display_msg = "Shutting Down\n\nPlease Wait 15 Seconds\nBefore Powering Down ..." thread_function(os.system, args="sleep 4 && shutdown now") elif command_num == 2: if current_config.button_function_level == 0: display_msg = "Nothing Yet" elif current_config.button_function_level == 1: os.system( "systemctl start KootnetTesterUpgradeOnlineDev.service") display_msg = hardware_access.get_upgrade_message( development_upgrade=True) elif current_config.button_function_level == 2: display_msg = "Nothing Yet" elif command_num == 3: if current_config.button_function_level == 0: current_config.button_function_level += 1 elif current_config.button_function_level == 1: current_config.button_function_level += 1 elif current_config.button_function_level == 2: current_config.button_function_level = 0 display_msg = hardware_access.get_button_functions_message( current_config.button_function_level) thread_function(hardware_access.display_message, args=display_msg)
def load_config_from_file(self): """ Loads Primary configuration from local disk and set's accordingly. """ primary_logger.debug("Loading Primary Configuration from: " + file_locations.config_file_location) if os.path.isfile(file_locations.config_file_location): try: config_file_lines = get_file_content( file_locations.config_file_location).split("\n") config_list = [] for line in config_file_lines: config_list.append(line.split("=")[0].strip()) self.is_iperf_server = int(config_list[1]) self.remote_tester_ip = config_list[2] self.iperf_port = config_list[3] self.mtr_run_count = config_list[4] if len(config_list) > 4: if config_list[5].strip() == "0": self.schedule_run_every_minutes = 0 else: self.schedule_run_every_minutes = int(config_list[5]) if len(config_list) > 5: if config_list[6].strip() == "0": self.schedule_run_on_boot = 0 else: self.schedule_run_on_boot = int(config_list[6]) if self.schedule_run_every_minutes < 5: self.schedule_run_every_minutes = 5 if len(config_list) > 6: if config_list[7].strip() == "0": self.schedule_run_every_minutes_enabled = 0 else: self.schedule_run_every_minutes_enabled = int( config_list[7]) if len(config_list) > 7: if config_list[8].strip() == "0": self.schedule_run_once_enabled = 0 else: self.schedule_run_once_enabled = int( config_list[8]) self.schedule_run_once_at_time = config_list[9] except Exception as error: primary_logger.error( "Unable to load Configuration File, saving default: " + str(error)) self.write_config_to_file() else: primary_logger.warning( "No Configuration file found, saving default") self.write_config_to_file()
def set_html_config_ipv4(html_request, wireless_type=False): log_msg = "Starting HTML IPv4 Configuration Update for Ethernet or Wireless." primary_logger.debug(log_msg + " Wireless = " + str(wireless_type)) if wireless_type: current_config.local_wireless_ip = html_request.form.get("wifi_ip_address") current_config.local_wireless_subnet = html_request.form.get("wifi_ip_subnet") current_config.local_wireless_gateway = html_request.form.get("wifi_ip_gateway") current_config.local_wireless_dns1 = html_request.form.get("wifi_ip_dns1") current_config.local_wireless_dns2 = html_request.form.get("wifi_ip_dns2") else: current_config.local_ethernet_ip = html_request.form.get("ethernet_ip_address") current_config.local_ethernet_subnet = html_request.form.get("ethernet_ip_subnet") current_config.local_ethernet_gateway = html_request.form.get("ethernet_ip_gateway") current_config.local_ethernet_dns1 = html_request.form.get("ethernet_ip_dns1") current_config.local_ethernet_dns2 = html_request.form.get("ethernet_ip_dns2")
def set_html_config_wifi_connection(html_request): primary_logger.debug("Starting HTML Wireless Configuration Update") current_config.wifi_country_code = html_request.form.get("country_code") current_config.wifi_ssid = html_request.form.get("ssid1") current_config.wifi_security_type = html_request.form.get("wifi_security1") current_config.wifi_pass_key = html_request.form.get("wifi_key1")