def _enable_psm5003_serial(): logger.sensors_logger.debug("Enabling Serial for Pimoroni PSM5003") try: serial_disabled = True boot_config_lines = get_file_content("/boot/config.txt").split( "\n") new_config = "" for line in boot_config_lines: if line.strip() == "dtoverlay=pi3-miniuart-bt": serial_disabled = False new_config += line + "\n" if serial_disabled: logger.sensors_logger.info( "Serial not enabled for PSM5003 - Enabling") logger.primary_logger.warning( "You must reboot the System before the PSM5003 will function properly" ) os.system( "raspi-config nonint set_config_var enable_uart 1 /boot/config.txt" ) os.system("raspi-config nonint do_serial 1") time.sleep(0.5) new_config = new_config.strip() new_config += "\n\n# Kootnet Sensors Addition\ndtoverlay=pi3-miniuart-bt\n" write_file_to_disk("/boot/config.txt", new_config) except Exception as error: logger.sensors_logger.error( "Pimoroni PSM5003 Enable Serial - Failed: " + str(error))
def _change_program_version(new_version): ks_version_file = get_file_content( original_source_dir + "operations_modules/software_version.py") control_version_file_content = get_file_content( original_source_dir + "extras/deb_package_files/DEBIAN/control") new_ks_version_file = "" for line in ks_version_file.split("\n"): if line.split("=")[0].strip() == "version": new_ks_version_file += f'version = "{new_version.get_version_string()}"\n' else: new_ks_version_file += f"{line}\n" new_ks_version_file = new_ks_version_file.strip() + "\n" write_file_to_disk( original_source_dir + "operations_modules/software_version.py", new_ks_version_file) new_control_version = "" for line in control_version_file_content.split("\n"): if line.split(":")[0].strip() == "Version": nv = f"Version: {new_version.major_version}.{new_version.feature_version}-{new_version.minor_version}\n" new_control_version += nv else: new_control_version += f"{line}\n" new_control_version = new_control_version.strip() + "\n" write_file_to_disk( original_source_dir + "extras/deb_package_files/DEBIAN/control", new_control_version)
def save_list_to_file(self): ip_list = [ str(self.sensor_ip_dns1), str(self.sensor_ip_dns2), str(self.sensor_ip_dns3), str(self.sensor_ip_dns4), str(self.sensor_ip_dns5), str(self.sensor_ip_dns6), str(self.sensor_ip_dns7), str(self.sensor_ip_dns8), str(self.sensor_ip_dns9), str(self.sensor_ip_dns10), str(self.sensor_ip_dns11), str(self.sensor_ip_dns12), str(self.sensor_ip_dns13), str(self.sensor_ip_dns14), str(self.sensor_ip_dns15), str(self.sensor_ip_dns16), str(self.sensor_ip_dns17), str(self.sensor_ip_dns18), str(self.sensor_ip_dns19), str(self.sensor_ip_dns20) ] save_text = "" for index, ip_address in enumerate(ip_list): save_text += ip_address + " = IP Address #" + str(index) + "\n" write_file_to_disk(self.ip_list_location, save_text)
def set_wifi_config(): logger.network_logger.debug("* Wifi Set by " + str(request.remote_addr)) try: new_wifi_config = request.form.get("command_data") write_file_to_disk(file_locations.wifi_config_file, new_wifi_config) return "OK" except Exception as error: log_msg = "Failed to set Primary Configuration from " + str(request.remote_addr) logger.network_logger.error(log_msg + " - " + str(error)) return "Failed"
def update_with_html_request(self, html_request): """ Updates the MQTT Broker configuration based on provided HTML configuration data. """ logger.network_logger.debug( "Starting HTML MQTT Broker Configuration Update Check") self.enable_mqtt_broker = 0 if html_request.form.get("enable_broker_server") is not None: self.enable_mqtt_broker = 1 write_file_to_disk( file_locations.mosquitto_configuration, str(html_request.form.get("mosquitto_config")).strip()) self.update_configuration_settings_list()
def _start_mosquitto_install_worker(): os.system(terminal_install_mqtt_mosquitto) if not os.path.isfile(file_locations.mosquitto_configuration): default_mqtt_config = "listener 1883\nallow_anonymous true" write_file_to_disk(file_locations.mosquitto_configuration, default_mqtt_config) if os.path.isfile("/usr/sbin/mosquitto") or os.path.isfile( "/usr/bin/mosquitto"): logger.primary_logger.info("MQTT Mosquitto Broker has been installed") 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.error( "MQTT Mosquitto did not install - Unable to start MQTT Broker") app_cached_variables.mqtt_broker_dummy_thread.current_state = "Install Error"
def _check_sensor_id(): """ Checks for Sensor Checkin ID file, if missing, creates one. The Sensor ID is Randomly Generated and used to track Software Usage and Online History of Sensors. """ try: if not os.path.isfile(file_locations.sensor_id): random_id = ''.join([ random.choice(string.ascii_letters + string.digits) for _ in range(32) ]) write_file_to_disk(file_locations.sensor_id, random_id) else: random_id = get_file_content(file_locations.sensor_id).strip() except Exception as error: logger.primary_logger.error("Problem Creating Sensor ID: " + str(error)) random_id = "Error" app_cached_variables.tmp_sensor_id = "KS" + random_id
def save_config_to_file(self): """ Saves configuration to file. """ logger.primary_logger.debug("Saving Configuration to " + str(self.config_file_location)) write_file_to_disk(self.config_file_location, self.get_config_as_str())
def remote_management_receive_configuration(request): config_type = str(request.form.get("config_selection")) new_config_str = str(request.form.get("new_config_str")).strip() logger.network_logger.info("* Received Push Configuration '" + config_type + "' from " + str(request.remote_addr)) if config_type == "primary": config_instance = CreatePrimaryConfiguration(load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "Main") if new_config is not None: app_config_access.primary_config = new_config elif config_type == "urls": config_instance = CreateURLConfiguration(load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "URLs") if new_config is not None: app_config_access.urls_config = new_config app_cached_variables.restart_automatic_upgrades_thread = True elif config_type == "upgrades": config_instance = CreateUpgradesConfiguration(load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "Upgrades") if new_config is not None: app_config_access.upgrades_config = new_config app_cached_variables.restart_automatic_upgrades_thread = True elif config_type == "installed_sensors": config_instance = CreateInstalledSensorsConfiguration( load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "Installed Sensors") if new_config is not None: app_config_access.installed_sensors = new_config # This also restarts most Kootnet Sensors 'servers' (recording for example) sensor_access.sensors_direct.__init__() elif config_type == "sensor_offsets": config_instance = CreateSensorOffsetsConfiguration( load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "Sensor Offsets") if new_config is not None: app_config_access.sensor_offsets = new_config elif config_type == "checkins": config_instance = CreateCheckinConfiguration(load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "Checkins") if new_config is not None: app_config_access.checkin_config = new_config app_cached_variables.restart_sensor_checkin_thread = True elif config_type == "interval_recording": config_instance = CreateIntervalRecordingConfiguration( load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "Interval Recording") if new_config is not None: app_config_access.interval_recording_config = new_config app_cached_variables.restart_interval_recording_thread = True elif config_type == "variance_trigger_recording": config_instance = CreateTriggerVariancesConfiguration( load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "Trigger Variance Recording") if new_config is not None: app_config_access.trigger_variances = new_config atpro_notifications.manage_service_restart() elif config_type == "high_low_recording": config_instance = CreateTriggerHighLowConfiguration( load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "Trigger High/Low Recording") if new_config is not None: app_config_access.trigger_high_low = new_config atpro_notifications.manage_service_restart() elif config_type == "display": config_instance = CreateDisplayConfiguration(load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "Display") if new_config is not None: app_config_access.display_config = new_config app_cached_variables.restart_mini_display_thread = True elif config_type == "email": config_instance = CreateEmailConfiguration(load_from_file=False) new_config = _apply_config(config_instance, new_config_str, "Email") if new_config is not None: app_config_access.email_config = new_config app_cached_variables.restart_report_email_thread = True app_cached_variables.restart_graph_email_thread = True elif config_type == "open_sense_map": app_config_access.open_sense_map_config.open_sense_map_enabled = _get_3rd_party_active_state( request) app_config_access.open_sense_map_config.interval_seconds = _get_3rd_party_interval( request) app_config_access.open_sense_map_config.update_configuration_settings_list( ) app_config_access.open_sense_map_config.save_config_to_file() app_cached_variables.restart_open_sense_map_thread = True logger.network_logger.info( "Applied Open Sense Map configuration push successfully") elif config_type == "weather_underground": app_config_access.weather_underground_config.weather_underground_enabled = _get_3rd_party_active_state( request) app_config_access.weather_underground_config.interval_seconds = _get_3rd_party_interval( request) app_config_access.weather_underground_config.update_configuration_settings_list( ) app_config_access.weather_underground_config.save_config_to_file() app_cached_variables.restart_weather_underground_thread = True logger.network_logger.info( "Applied Weather Underground configuration push successfully") elif config_type == "luftdaten": app_config_access.luftdaten_config.luftdaten_enabled = _get_3rd_party_active_state( request) app_config_access.luftdaten_config.interval_seconds = _get_3rd_party_interval( request) app_config_access.luftdaten_config.update_configuration_settings_list() app_config_access.luftdaten_config.save_config_to_file() app_cached_variables.restart_luftdaten_thread = True logger.network_logger.info( "Applied Luftdaten configuration push successfully") elif config_type == "wifi": if app_cached_variables.running_with_root: write_file_to_disk(file_locations.wifi_config_file, new_config_str.strip()) atpro_notifications.manage_service_restart() else: logger.primary_logger.warning( "Wifi set skipped, not running with root") elif config_type == "network": if app_cached_variables.running_with_root: write_file_to_disk(file_locations.dhcpcd_config_file, new_config_str.strip()) atpro_notifications.manage_service_restart() else: logger.primary_logger.warning( "Network set skipped, not running with root") return "Received"
def update_cached_variables(): """ Updates app_cached_variables.py variables. """ global first_run if first_run: first_run = False thread_function(_update_ks_info_table_data) demo_mode = app_config_access.primary_config.demo_mode if not app_cached_variables.running_with_root: click_msg = "Without root access, the following functions will be unavailable - " click_msg += "Hardware Sensors, Network Configurations, Upgrade & Power commands" icon = "fas fa-exclamation-triangle" notification_short_msg = "Warning: Not running with root<br>Click Here for more information" atpro_notifications.add_custom_message(notification_short_msg, click_msg=click_msg, icon=icon) if demo_mode: click_msg = "In Demo mode, there are no login prompts and the following functions will be unavailable - " click_msg += "Network Configurations, SSL, Change Login, Upgrade & Power commands" notification_short_msg = "Info: Running in Demo mode<br>Click Here for more information" atpro_notifications.add_custom_message(notification_short_msg, click_msg=click_msg) if default_http_flask_user == app_cached_variables.http_flask_user and not demo_mode: if verify_password_to_hash(default_http_flask_password): atpro_notifications.manage_default_login_detected() if app_cached_variables.current_platform == "Linux": try: os_release_content_lines = get_file_content( "/etc/os-release").split("\n") os_release_name = "" for line in os_release_content_lines: name_and_value = line.split("=") if name_and_value[0].strip() == "PRETTY_NAME": os_release_name = name_and_value[1].strip()[1:-1] app_cached_variables.operating_system_name = str(os_release_name) except Exception as error: logger.sensors_logger.error("Error caching OS Name: " + str(error)) app_cached_variables.operating_system_name = "NA" if app_cached_variables.operating_system_name[:8] == "Raspbian": try: if app_cached_variables.running_with_root: wifi_config_lines = get_file_content( file_locations.wifi_config_file).split("\n") else: wifi_config_lines = "" app_cached_variables.wifi_country_code = network_wifi.get_wifi_country_code( wifi_config_lines) app_cached_variables.wifi_ssid = network_wifi.get_wifi_ssid( wifi_config_lines) app_cached_variables.wifi_security_type = network_wifi.get_wifi_security_type( wifi_config_lines) app_cached_variables.wifi_psk = network_wifi.get_wifi_psk( wifi_config_lines) except Exception as error: logger.primary_logger.warning( "Error checking WiFi configuration: " + str(error)) try: dhcpcd_config_lines = get_file_content( file_locations.dhcpcd_config_file).split("\n") if not network_ip.check_for_dhcp(dhcpcd_config_lines): app_cached_variables.ip = network_ip.get_dhcpcd_ip( dhcpcd_config_lines) app_cached_variables.ip_subnet = network_ip.get_subnet( dhcpcd_config_lines) app_cached_variables.gateway = network_ip.get_gateway( dhcpcd_config_lines) app_cached_variables.dns1 = network_ip.get_dns( dhcpcd_config_lines) app_cached_variables.dns2 = network_ip.get_dns( dhcpcd_config_lines, dns_server=1) except Exception as error: logger.primary_logger.warning("Error checking dhcpcd.conf: " + str(error)) try: app_cached_variables.total_ram_memory = round( psutil.virtual_memory().total / 1024 / 1024 / 1024, 3) except Exception as error: logger.primary_logger.warning("Error caching total RAM: " + str(error)) try: app_cached_variables.total_disk_space = round( psutil.disk_usage("/").total / 1024 / 1024 / 1024, 2) except Exception as error: logger.primary_logger.warning("Error caching total Disk Space: " + str(error)) last_updated = "" if not os.path.isfile(file_locations.program_last_updated): logger.sensors_logger.debug( "Previous version file not found - Creating version file") write_file_to_disk(file_locations.program_last_updated, "No Update Detected") last_updated_file = get_file_content(file_locations.program_last_updated) last_updated_lines = last_updated_file.split("\n") for line in last_updated_lines: last_updated += str(line) app_cached_variables.program_last_updated = last_updated.strip() app_cached_variables.zipped_db_backup_list = get_names_list_from_dir( file_locations.database_backup_folder) app_cached_variables.uploaded_databases_list = get_names_list_from_dir( file_locations.uploaded_databases_folder) _update_cached_sensor_reboot_count() _update_cached_note_variables() _update_cached_ip() _update_cached_hostname()