def update_program_development(): app_generic_functions.thread_function( os.system, args="bash " + file_locations.http_upgrade_script + " dev") return render_template("message_return.html", URL="/", TextMessage="Development Upgrade in Progress", TextMessage2="Please wait ...")
def start_mqtt_broker_server(): """ Starts MQTT Broker server Mosquitto's service. """ if app_config_access.mqtt_broker_config.enable_mqtt_broker: app_cached_variables.mqtt_broker_dummy_thread.current_state = "Starting" if check_mqtt_broker_server_running(): logger.primary_logger.info(" -- Mosquitto Server already running") app_cached_variables.mqtt_broker_dummy_thread.current_state = "Running" else: if app_cached_variables.running_with_root: if os.path.isfile("/usr/sbin/mosquitto") or os.path.isfile( "/usr/bin/mosquitto"): os.system(terminal_enable_start_mosquitto) logger.primary_logger.info(" -- Starting Mosquitto Server") app_cached_variables.mqtt_broker_dummy_thread.current_state = "Running" else: logger.primary_logger.info( "MQTT Mosquitto Broker not installed, attempting install" ) app_cached_variables.mqtt_broker_dummy_thread.current_state = "Installing" thread_function(_start_mosquitto_install_worker) else: logger.primary_logger.warning( "Unable to Start MQTT Broker Mosquitto, root required") app_cached_variables.mqtt_broker_dummy_thread.current_state = "NA" else: logger.primary_logger.debug("MQTT Broker Disabled in Configuration")
def reboot_system(): app_generic_functions.thread_function(os.system, args="sleep 5 && reboot") return render_template( "message_return.html", URL="/", TextMessage="Rebooting in 4 Seconds", TextMessage2="It may take a few minutes for the system to reboot")
def shutdown_system(): app_generic_functions.thread_function(os.system, args="shutdown now") return render_template( "message_return.html", URL="/", TextMessage="Shutting Down Unit", TextMessage2="Please wait 15 seconds before removing power")
def start_mqtt_subscriber_server(): """ Starts MQTT Subscriber server. """ if app_config_access.mqtt_subscriber_config.enable_mqtt_subscriber: thread_function(CreateMQTTSubscriberTopicsRetrieval) logger.primary_logger.info(" -- MQTT Subscriber Started") else: logger.primary_logger.debug("MQTT Subscriber Disabled in Configuration")
def restart_program(): app_generic_functions.thread_function( os.system, args="systemctl restart KootnetEthServer") return render_template( "message_return.html", URL="/", TextMessage="Re-Starting Program", TextMessage2= "You will automatically be redirected to home in 10 seconds")
def upgrade_linux_os(thread_the_function=True): if app_cached_variables.sensor_ready_for_upgrade and app_cached_variables.pip_ready_for_upgrades: app_cached_variables.sensor_ready_for_upgrade = False if thread_the_function: thread_function(_upgrade_linux_os_thread) else: _upgrade_linux_os_thread() else: logger.network_logger.warning("Unable to start Linux OS Upgrade - already running")
def delete_sensors_older_then(): delete_sensors_older_days = 365000 # 1000 years if request.form.get("delete_sensors_older_days") is not None: delete_sensors_older_days = float( request.form.get("delete_sensors_older_days")) thread_function(_thread_delete_sensors_older_then, args=delete_sensors_older_days) return get_message_page("Old Sensor Clean-up Started", "Check Logs for more info", page_url="sensor-checkin-view")
def __init__(self): app = Flask(__name__) app.secret_key = sha256(urandom(32)).hexdigest() Compress(app) app.register_blueprint(html_functional_routes) app.register_blueprint(html_basic_routes) app.register_blueprint(html_atpro_main_routes) app.register_blueprint(html_atpro_notes_routes) app.register_blueprint(html_atpro_graphing_routes) app.register_blueprint(html_live_graph_sensor_wrappers_routes) app.register_blueprint(html_atpro_remote_management_routes) app.register_blueprint(html_atpro_mqtt_subscriber_routes) app.register_blueprint(html_atpro_sensor_check_ins_routes) app.register_blueprint(html_atpro_logs_routes) app.register_blueprint(html_atpro_settings_routes) app.register_blueprint(html_atpro_settings_sql_recording_routes) app.register_blueprint(html_atpro_settings_mqtt_routes) app.register_blueprint(html_atpro_settings_email_routes) app.register_blueprint(html_atpro_settings_3rd_party_routes) app.register_blueprint(html_atpro_system_routes) app.register_blueprint(html_atpro_system_raw_configs_routes) app.register_blueprint(html_atpro_system_sql_db_routes) app.register_blueprint(html_atpro_system_networking_routes) app.register_blueprint(html_atpro_system_commands_routes) app.register_blueprint(html_sensor_readings_routes) app.register_blueprint(html_local_download_routes) app.register_blueprint(html_get_set_config_routes) app.register_blueprint(html_system_commands_routes) app.register_blueprint(html_sensor_check_ins_routes) set_http_auth_from_file() thread_function(update_cached_variables) try: # Removes excessive "SSL Error" messages to console when using a self signed certificate hub.Hub.NOT_ERROR = (Exception,) except Exception as error: logger.primary_logger.warning("Error lowering HTTP Server Logging: " + str(error)) try: flask_http_ip = app_config_access.primary_config.flask_http_ip flask_port_number = app_config_access.primary_config.web_portal_port http_server = pywsgi.WSGIServer((flask_http_ip, flask_port_number), app, keyfile=file_locations.http_ssl_key, certfile=file_locations.http_ssl_crt) logger.primary_logger.info(" -- HTTPS Server Started on port " + str(flask_port_number)) http_server.serve_forever() except Exception as error: logger.primary_logger.critical("--- Failed to Start HTTPS Server: " + str(error))
def system_reboot(): if not app_config_access.primary_config.demo_mode: logger.network_logger.info("** System Reboot Initiated by " + str(request.remote_addr)) app_generic_functions.thread_function( os.system, args=app_cached_variables.bash_commands["RebootSystem"]) return_msg = "This may take a few Minutes ..." return get_message_page("Rebooting System", message=return_msg, page_url="sensor-dashboard") return demo_mode_enabled_msg
def system_shutdown(): if not app_config_access.primary_config.demo_mode: logger.network_logger.info("** System Shutdown Initiated by " + str(request.remote_addr)) app_generic_functions.thread_function( os.system, args=app_cached_variables.bash_commands["ShutdownSystem"]) msg = "You must physically turn the sensor back on for future access" return get_message_page("Shutting Down System", message=msg, page_url="sensor-dashboard") return demo_mode_enabled_msg
def run_program_start_checks(): """ This function is used before most of the program has started. Sets file permissions, checks the database and generates the HTTPS certificates if not present. """ logger.primary_logger.info(" -- Pre-Start Initializations Started") _check_sensor_id() _check_ssl_files() _set_file_permissions() _add_tct_terminal_alias() run_configuration_upgrade_checks() logger.primary_logger.info(" -- Pre-Start Initializations Complete") if software_version.old_version != software_version.version: run_database_integrity_check(file_locations.sensor_database, quick=False) run_database_integrity_check(file_locations.sensor_checkin_database, quick=False) run_database_integrity_check(file_locations.mqtt_subscriber_database, quick=False) thread_function(check_main_database_structure) thread_function(check_checkin_database_structure) thread_function(check_mqtt_subscriber_database_structure) else: run_database_integrity_check(file_locations.sensor_database) run_database_integrity_check(file_locations.sensor_checkin_database) run_database_integrity_check(file_locations.mqtt_subscriber_database) thread_function(_create_secondary_python_venv)
def generate_plotly_graph(graph_request, graph_config=None): if graph_config is None: if graph_request.form.get("button_function") == "create": logger.network_logger.debug("* Plotly Graph Create Initiated by " + str(request.remote_addr)) app_config_access.db_graphs_config.update_with_html_request(graph_request) app_config_access.db_graphs_config.save_config_to_file() graph_config = app_config_access.db_graphs_config elif graph_request.form.get("button_function") == "email": logger.network_logger.debug("* Plotly Graph Email Config Update Initiated by " + str(request.remote_addr)) app_config_access.email_db_graph_config.update_with_html_request(graph_request) app_config_access.email_db_graph_config.save_config_to_file() if graph_config is not None and not server_plotly_graph_variables.graph_creation_in_progress: new_graph_data = server_plotly_graph_variables.CreateGraphData(graph_config) thread_function(server_plotly_graph.create_plotly_graph, args=new_graph_data)
def html_atpro_sensor_settings_upgrades(): bad_cred_msg = "The username must be alphanumeric and the password must not contain single quotes or =" if not validate_smb_username(request.form.get("smb_username")) \ or not validate_smb_password(request.form.get("smb_password")) and request.form.get("smb_password") != "": logger.primary_logger.warning( "The provided SMB username or password has invalid characters") return get_message_page("Invalid SMB username or password", message=bad_cred_msg, page_url="sensor-settings") app_config_access.upgrades_config.update_with_html_request(request) app_config_access.upgrades_config.save_config_to_file() thread_function(update_checks_interface.update_versions_info_variables) app_cached_variables.restart_automatic_upgrades_thread = True return get_message_page("Upgrade Settings Updated", page_url="sensor-settings")
def upgrade_python_pip_modules(python_location=None): requirements_file_location = file_locations.program_root_dir + "/requirements.txt" hardware_requirements_location = file_locations.program_root_dir + "/requirements_hw_sensors.txt" all_python_modules_list = [] if app_cached_variables.pip_ready_for_upgrades and app_cached_variables.sensor_ready_for_upgrade: if os.path.isfile(file_locations.program_root_dir + "/requirements.txt"): requirements_text = get_file_content(requirements_file_location).strip() all_python_modules_list = requirements_text.split("\n") if app_cached_variables.running_with_root and running_on_pi(): if os.path.isfile(file_locations.program_root_dir + "/requirements_hw_sensors.txt"): requirements_text = get_file_content(hardware_requirements_location).strip() all_python_modules_list = all_python_modules_list + requirements_text.split("\n") app_cached_variables.pip_ready_for_upgrades = False if python_location is not None: _pip_upgrades_thread([all_python_modules_list, python_location]) else: thread_function(_pip_upgrades_thread, args=[all_python_modules_list, python_location]) else: logger.network_logger.warning("Unable to start Python Module Upgrades - already running")
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 html_atpro_send_reports_email(): logger.network_logger.debug("** HTML Reports Email Sent - Source: " + str(request.remote_addr)) button_pressed = request.form.get("test_email_button") email_address = request.form.get("test_email_address") msg = "Check the Network logs for more information" if email_is_valid(email_address): if button_pressed == "reports": thread_function(send_report_emails, args=[email_address]) return get_message_page("Reports email is being sent", msg, page_url="sensor-settings") elif button_pressed == "graphs": thread_function(send_db_graph_emails, args=[email_address]) return get_message_page("Graph email is being sent", msg, page_url="sensor-settings") elif button_pressed == "settings": thread_function(send_test_email, args=email_address) return get_message_page("Test email is being sent", msg, page_url="sensor-settings") return get_message_page("Please Specify a valid email address to send to", page_url="sensor-settings")
def start_tests(): if request.form.get("run_mtr") is not None and request.form.get("run_iperf") is not None: app_variables.html_mtr_checked = "checked" app_variables.html_iperf_checked = "checked" app_generic_functions.thread_function(run_commands.start_all_tests) elif request.form.get("run_mtr") is not None: app_variables.html_mtr_checked = "checked" app_variables.html_iperf_checked = "" app_generic_functions.thread_function(run_commands.start_mtr) elif request.form.get("run_iperf") is not None: app_variables.html_iperf_checked = "checked" app_variables.html_mtr_checked = "" app_generic_functions.thread_function(run_commands.start_iperf) return html_root()
def restart_services(): """ Reloads systemd service files & restarts KootnetSensors service. """ thread_function(_restart_services_thread)
def html_atpro_sensor_checkins_generate_sensors_html_list(): if app_cached_variables.checkins_sensors_html_table_list != updating_checkin_info_html_msg: thread_function(_generate_sensors_checkins_html_list) return html_atpro_checkin_sensors_list()
def clear_check_ins_counts(): thread_function(_thread_clear_check_ins_counts) return_msg = "Clearing all but the last Sensor Checkin from all sensors, check Logs for more info" return get_message_page("Redundant Sensor Checkins Clean-up Started", return_msg, page_url="sensor-checkin-view")
def html_atpro_mqtt_subscriber_generate_sensors_html_list(): if app_cached_variables.mqtt_subscriber_sensors_html_list != generating_mqtt_table_html: thread_function(_generate_mqtt_subscriber_sensors_html_list) return html_atpro_mqtt_subscriber_sensors_list()
def generate_config_report(ip_list, threaded=True): if threaded: thread_function(_thread_config_report, args=ip_list) else: _thread_config_report(ip_list)
def html_atpro_sensor_settings_urls_reset(): app_config_access.urls_config.reset_urls_to_default() thread_function(update_checks_interface.update_versions_info_variables) return get_message_page("URLs Configuration Reset", page_url="sensor-settings")
def html_atpro_sensor_settings_urls(): app_config_access.urls_config.update_with_html_request(request) app_config_access.urls_config.save_config_to_file() thread_function(update_checks_interface.update_versions_info_variables) app_cached_variables.restart_automatic_upgrades_thread = True return get_message_page("URL Settings Updated", page_url="sensor-settings")
def _button_functions(button_pressed, request_ip="N/A"): try: if button_pressed == "download-main-db-raw": logger.network_logger.debug( "* Download RAW Main SQL Database Accessed by " + request_ip) sensor_database = file_locations.sensor_database if os.path.isfile(sensor_database): sql_filename = _add_host_and_ip_to_filename( "Sensor_Database", "sqlite") return send_file(sensor_database, as_attachment=True, attachment_filename=sql_filename) return "Kootnet Sensors main database not found" elif button_pressed == "download-mqtt-sub-db-raw": logger.network_logger.debug( "* Download RAW MQTT SQL Database Accessed by " + str(request.remote_addr)) mqtt_subscriber_database = file_locations.mqtt_subscriber_database if os.path.isfile(mqtt_subscriber_database): sql_filename = _add_host_and_ip_to_filename( "MQTT_Database", "sqlite") return send_file(mqtt_subscriber_database, as_attachment=True, attachment_filename=sql_filename) return "Kootnet Sensors MQTT Subscriber database not found" elif button_pressed == "download-checkin-db-raw": logger.network_logger.debug( "* Download RAW Checkin SQL Database Accessed by " + str(request_ip)) sensor_checkin_database = file_locations.sensor_checkin_database if os.path.isfile(sensor_checkin_database): sql_filename = _add_host_and_ip_to_filename( "Sensors_Checkin_Database", "sqlite") return send_file(sensor_checkin_database, as_attachment=True, attachment_filename=sql_filename) return "Kootnet Sensors Checkin database not found" elif button_pressed == "download-main-db-zip": if not app_cached_variables.creating_zip_main_db: database_zipped = file_locations.database_zipped if os.path.isfile(database_zipped): zip_filename = _add_host_and_ip_to_filename( "Main_Database", "zip") logger.network_logger.debug( "* Download Zipped Main SQL Database Accessed by " + request_ip) return send_file(database_zipped, as_attachment=True, attachment_filename=zip_filename) return "Zipped Main SQL database not found, 'Generate New Zip' before trying again." else: return "Creating database zip, please try again later ..." elif button_pressed == "download-mqtt-sub-db-zip": if not app_cached_variables.creating_zip_mqtt_sub_db: mqtt_database_zipped = file_locations.mqtt_database_zipped if os.path.isfile(mqtt_database_zipped): zip_filename = _add_host_and_ip_to_filename( "MQTT_Subscriber_Database", "zip") log_msg = "* Download Zipped MQTT Subscriber SQL Database Accessed by " logger.network_logger.debug(log_msg + request_ip) return send_file(mqtt_database_zipped, as_attachment=True, attachment_filename=zip_filename) return "Zipped MQTT Subscriber SQL database not found, 'Generate New Zip' before trying again." else: return "Creating database zip, please try again later ..." elif button_pressed == "download-checkin-db-zip": if not app_cached_variables.creating_zip_checkin_db: checkin_database_zipped = file_locations.checkin_database_zipped if os.path.isfile(checkin_database_zipped): zip_filename = _add_host_and_ip_to_filename( "Checkin_Database", "zip") logger.network_logger.debug( "* Download Zipped Checkin SQL Database Accessed by " + request_ip) return send_file(checkin_database_zipped, as_attachment=True, attachment_filename=zip_filename) return "Zipped Sensor Checkin SQL database not found, 'Generate New Zip' before trying again." else: return "Creating database zip, please try again later ..." elif button_pressed == "create-main-db-zip": if not app_cached_variables.creating_zip_main_db: thread_function(_zip_main_db_worker) elif button_pressed == "create-mqtt-sub-db-zip": if not app_cached_variables.creating_zip_mqtt_sub_db: thread_function(_zip_mqtt_sub_db_worker) elif button_pressed == "create-checkin-db-zip": if not app_cached_variables.creating_zip_checkin_db: thread_function(_zip_checkin_db_worker) except Exception as error: log_msg = "HTML Database Request Error from " + request_ip + " using command " + button_pressed + ": " logger.primary_logger.error(log_msg + str(error)) return get_html_atpro_index( run_script="SelectNav('sensor-system', skip_menu_select=true);")
def start_hardware_interactive_server(): """ Starts a Monitored Thread of the Hardware Interactive Server """ thread_function(_hardware_interactive_server)
def atpro_upgrade_urls(url_path): title = "Function Disabled" message = "Kootnet Sensors must be running as a service with root access" if primary_config.demo_mode: message = "Function Disabled in Demo mode" msg_page = get_message_page(title, message, full_reload=False) if running_as_service and running_with_root and not primary_config.demo_mode: title = "Error!" message = "An Error occurred" system_command = "exit" if str(url_path) == "system-restart-program": logger.network_logger.info("** Program Restart Initiated by " + str(request.remote_addr)) title = "Restarting Program" message = "The web interface will be temporarily unavailable" system_command = app_cached_variables.bash_commands[ "RestartService"] elif str(url_path) == "system-restart": logger.network_logger.info("** System Restart Initiated by " + str(request.remote_addr)) title = "Restarting System" message = "The web interface will be temporarily unavailable" system_command = app_cached_variables.bash_commands["RebootSystem"] elif str(url_path) == "system-shutdown": logger.network_logger.info("** System Shutdown Initiated by " + str(request.remote_addr)) title = "Shutting Down" message = "You will be unable to access the web interface until some one turns the sensor back on" system_command = app_cached_variables.bash_commands[ "ShutdownSystem"] elif str(url_path) == "upgrade-http-std": logger.network_logger.info("* Upgrade - HTTP Initiated by " + str(request.remote_addr)) title = "Upgrade Not Available" message = "The latest Standard version or higher is already running" update_checks_interface = CreateUpdateChecksInterface( start_auto_checks=False) if update_checks_interface.standard_update_available: title = "Upgrade Started" message = "Standard Upgrade by HTTP Started. This may take awhile ..." upgrade_interface = CreateUpgradeScriptInterface() upgrade_interface.start_kootnet_sensors_upgrade() elif str(url_path) == "upgrade-http-dev": logger.network_logger.info( "** Developer Upgrade - HTTP Initiated by " + str(request.remote_addr)) title = "Upgrade Not Available" message = "The latest Developmental version or higher is already running" update_checks_interface = CreateUpdateChecksInterface( start_auto_checks=False) if update_checks_interface.developmental_update_available: title = "Upgrade Started" message = "Development Upgrade by HTTP Started. This may take awhile ..." upgrade_interface = CreateUpgradeScriptInterface() upgrade_interface.dev_upgrade = True upgrade_interface.start_kootnet_sensors_upgrade() elif str(url_path) == "upgrade-http-std-clean": logger.network_logger.info( "** Clean Upgrade - HTTP Initiated by " + str(request.remote_addr)) title = "Upgrade Started" message = "Re-installing the latest Standard version of Kootnet Sensors. This may take awhile ..." upgrade_interface = CreateUpgradeScriptInterface() upgrade_interface.clean_upgrade = True upgrade_interface.start_kootnet_sensors_upgrade() elif str(url_path) == "upgrade-http-dev-clean": logger.network_logger.info( "** DEV Clean Upgrade - HTTP Initiated by " + str(request.remote_addr)) title = "Upgrade Started" message = "Re-installing the latest Developmental version of Kootnet Sensors. This may take awhile ..." upgrade_interface = CreateUpgradeScriptInterface() upgrade_interface.dev_upgrade = True upgrade_interface.clean_upgrade = True upgrade_interface.start_kootnet_sensors_upgrade() elif str(url_path) == "upgrade-smb-std": logger.network_logger.info("* Upgrade - SMB Initiated by " + str(request.remote_addr)) title = "Upgrade Started" message = "Standard Upgrade by SMB Started. This may take awhile ..." upgrade_interface = CreateUpgradeScriptInterface() upgrade_interface.download_type = download_type_smb upgrade_interface.start_kootnet_sensors_upgrade() elif str(url_path) == "upgrade-smb-dev": logger.network_logger.info( "** Developer Upgrade - SMB Initiated by " + str(request.remote_addr)) title = "Upgrade Started" message = "Development Upgrade by SMB Started. This may take awhile ..." upgrade_interface = CreateUpgradeScriptInterface() upgrade_interface.download_type = download_type_smb upgrade_interface.dev_upgrade = True upgrade_interface.start_kootnet_sensors_upgrade() elif str(url_path) == "upgrade-os": logger.network_logger.info( "** System OS Upgrade - SMB Initiated by " + str(request.remote_addr)) title = "Upgrade Started" message = "Sensor's operating system upgrade started. This may take awhile ..." click_msg = "Kootnet Sensors is currently doing a Operating System Upgrade. " + \ "Once complete, the system will restart and this message will disappear" notification_short_msg = "Operating System upgrade in progress ...<br>Click Here for more information" atpro_notifications.add_custom_message(notification_short_msg, click_msg) upgrade_linux_os() elif str(url_path) == "upgrade-py3-modules": logger.network_logger.info( "** Python3 Module Upgrades Initiated by " + str(request.remote_addr)) title = "Upgrades Started" message = "Python3 Module Upgrades Started. This may take awhile ..." click_msg = "Kootnet Sensors is currently doing a Python Module Upgrade. " + \ "Once complete, the software will restart and this message will disappear" notification_short_msg = "Python Module upgrades in progress ...<br>Click Here for more information" atpro_notifications.add_custom_message(notification_short_msg, click_msg) upgrade_python_pip_modules() msg_page = get_message_page(title, message, full_reload=False) thread_function(os.system, args=system_command) return msg_page
def remote_management_main_post(request): run_command = request.form.get("rsm-run-command") selected_ip_list = str(request.form.get("selected_ip_list")) if run_command == "ChangeIPList": app_config_access.sensor_control_config.selected_ip_list = selected_ip_list app_config_access.sensor_control_config.change_ip_list() elif run_command == "SaveSettings": app_config_access.sensor_control_config.update_with_html_request( request) app_config_access.sensor_control_config.save_config_to_file() elif run_command == "SaveSettings-report-emails": app_config_access.email_reports_config.update_with_html_request( request) app_config_access.email_reports_config.save_config_to_file() elif run_command == "RunAction": sc_action = request.form.get("selected_action") sc_download_type = request.form.get("selected_send_type") app_config_access.sensor_control_config.update_with_html_request( request) ip_list_raw = app_config_access.sensor_control_config.get_raw_ip_addresses_as_list( ) if len(ip_list_raw) > 0: if sc_action == app_config_access.sensor_control_config.radio_check_status: return check_sensor_status_sensor_control(ip_list_raw) elif sc_action == app_config_access.sensor_control_config.radio_report_combo: thread_function(_thread_combo_report, args=ip_list_raw) elif sc_action == app_config_access.sensor_control_config.radio_report_system: generate_system_report(ip_list_raw) elif sc_action == app_config_access.sensor_control_config.radio_report_config: generate_config_report(ip_list_raw) elif sc_action == app_config_access.sensor_control_config.radio_report_test_sensors: generate_readings_report(ip_list_raw) elif sc_action == app_config_access.sensor_control_config.radio_report_sensors_latency: generate_latency_report(ip_list_raw) elif sc_action == app_config_access.sensor_control_config.radio_download_reports: rm_cached_variables.creating_combo_reports_zip = True logger.network_logger.info( "Sensor Control - Reports Zip Generation Started") clear_zip_names() thread_function(put_all_reports_zipped_to_cache, args=ip_list_raw) else: ip_list = app_config_access.sensor_control_config.get_clean_ip_addresses_as_list( ) if len(ip_list) < 1: msg_1 = "All sensors appear to be Offline" return get_message_page(msg_1, page_url="sensor-rm") elif sc_action == app_config_access.sensor_control_config.radio_download_databases: download_sql_databases = app_config_access.sensor_control_config.radio_download_databases if sc_download_type == app_config_access.sensor_control_config.radio_send_type_direct: return downloads_direct_rsm( ip_list, download_type=download_sql_databases) else: rm_cached_variables.creating_databases_zip = True logger.network_logger.info( "Sensor Control - Databases Zip Generation Started" ) thread_function(create_all_databases_zipped, args=ip_list) elif sc_action == app_config_access.sensor_control_config.radio_download_logs: clear_zip_names() if sc_download_type == app_config_access.sensor_control_config.radio_send_type_direct: download_logs = app_config_access.sensor_control_config.radio_download_logs return downloads_direct_rsm( ip_list, download_type=download_logs) elif sc_download_type == app_config_access.sensor_control_config.radio_send_type_relayed: rm_cached_variables.creating_logs_zip = True logger.network_logger.info( "Sensor Control - Multi Sensors Logs Zip Generation Started" ) thread_function(create_multiple_sensor_logs_zipped, args=ip_list) elif sc_action == app_config_access.sensor_control_config.radio_create_the_big_zip: logger.network_logger.info( "Sensor Control - The Big Zip Generation Started") rm_cached_variables.creating_the_big_zip = True thread_function(create_the_big_zip, args=ip_list) else: return _sensor_addresses_required_msg() elif run_command == "DownloadRSMReportsZip": try: if not rm_cached_variables.creating_combo_reports_zip: if rm_cached_variables.sc_reports_zip_name != "": zip_file = rm_cached_variables.sc_in_memory_zip zip_filename = rm_cached_variables.sc_reports_zip_name rm_cached_variables.sc_reports_zip_name = "" return send_file(zip_file, attachment_filename=zip_filename, as_attachment=True) except Exception as error: logger.network_logger.error("Send Reports Zip Error: " + str(error)) msg_1 = "Problem loading Zip" msg_2 = str(error) return get_message_page(msg_1, msg_2, page_url="sensor-rm") rm_cached_variables.sc_reports_zip_name = "" elif run_command == "DownloadRSMDatabasesZip": if not rm_cached_variables.creating_databases_zip: if rm_cached_variables.sc_databases_zip_name != "": try: if rm_cached_variables.sc_databases_zip_in_memory: zip_file = rm_cached_variables.sc_in_memory_zip else: zip_file = file_locations.html_sensor_control_databases_zip zip_filename = rm_cached_variables.sc_databases_zip_name rm_cached_variables.sc_databases_zip_name = "" rm_cached_variables.sc_databases_zip_in_memory = False return send_file(zip_file, attachment_filename=zip_filename, as_attachment=True) except Exception as error: logger.network_logger.error("Send Databases Zip Error: " + str(error)) rm_cached_variables.sc_databases_zip_name = "" rm_cached_variables.sc_databases_zip_in_memory = False msg_1 = "Problem loading Zip" msg_2 = str(error) return get_message_page(msg_1, msg_2, page_url="sensor-rm") elif run_command == "DownloadRSMLogsZip": try: if not rm_cached_variables.creating_logs_zip: if rm_cached_variables.sc_logs_zip_name != "": zip_file = rm_cached_variables.sc_in_memory_zip zip_filename = rm_cached_variables.sc_logs_zip_name rm_cached_variables.sc_logs_zip_name = "" return send_file(zip_file, attachment_filename=zip_filename, as_attachment=True) except Exception as error: logger.network_logger.error("Send SC Logs Zip Error: " + str(error)) msg_1 = "Problem loading Zip" msg_2 = str(error) return get_message_page(msg_1, msg_2, page_url="sensor-rm") rm_cached_variables.sc_logs_zip_name = "" elif run_command == "DownloadRSMBigZip": try: if not rm_cached_variables.creating_the_big_zip: if rm_cached_variables.sc_big_zip_name != "": if rm_cached_variables.sc_big_zip_in_memory: zip_file = rm_cached_variables.sc_in_memory_zip rm_cached_variables.sc_big_zip_name = "" rm_cached_variables.sc_big_zip_in_memory = False else: zip_file = file_locations.html_sensor_control_big_zip zip_filename = rm_cached_variables.sc_big_zip_name return send_file(zip_file, attachment_filename=zip_filename, as_attachment=True) except Exception as error: logger.network_logger.error("Send Big Zip Error: " + str(error)) msg_1 = "Problem loading Zip" msg_2 = str(error) return get_message_page(msg_1, msg_2, page_url="sensor-rm") rm_cached_variables.sc_big_zip_in_memory = False return get_html_atpro_index(run_script="SelectNav('sensor-rm');")
def display_message(text_msg, check_test=False): """ If a Supported Display is installed, shows provided text message on it. """ logger.sensors_logger.debug("* Displaying Text on LED Screen: " + str(text_msg)[:50]) text_msg = str(text_msg) if app_config_access.display_config.enable_display: if len(text_msg) > 0 or check_test: text_msg = "-- " + text_msg display_missing = True if app_config_access.installed_sensors.kootnet_dummy_sensor: if not check_test: thread_function(sensors_direct.dummy_sensors.display_text, args=text_msg) display_missing = False if app_config_access.installed_sensors.raspberry_pi_sense_hat: if not check_test: thread_function(sensors_direct.rp_sense_hat_a.display_text, args=text_msg) display_missing = False if app_config_access.installed_sensors.pimoroni_matrix_11x7: if not check_test: thread_function( sensors_direct.pimoroni_matrix_11x7_a.display_text, args=text_msg) display_missing = False if app_config_access.installed_sensors.pimoroni_st7735: if not check_test: thread_function( sensors_direct.pimoroni_st7735_a.display_text, args=text_msg) display_missing = False if app_config_access.installed_sensors.pimoroni_mono_oled_luma: if not check_test: thread_function( sensors_direct.pimoroni_mono_oled_luma_a.display_text, args=text_msg) display_missing = False if app_config_access.installed_sensors.pimoroni_enviroplus: if not check_test: thread_function( sensors_direct.pimoroni_enviroplus_a.display_text, args=text_msg) display_missing = False if app_config_access.installed_sensors.pimoroni_enviro2: if not check_test: thread_function( sensors_direct.pimoroni_enviro2_a.display_text, args=text_msg) display_missing = False if display_missing: return False return True else: logger.sensors_logger.debug( "* Unable to Display Text: Display disabled in Primary Configuration" ) return False