def identify_modem(): global identified_module # Get old system setup if it is exist old_system_id = {} if os.path.isfile(SYSTEM_PATH): try: old_system_id = read_yaml_all(SYSTEM_PATH) except Exception as error: logger.warning("Old system_id in system.yaml file couln't be read!") system_id["modem_vendor"] = "" system_id["modem_name"] = "" system_id["modem_vendor_id"] = "" system_id["modem_product_id"] = "" logger.info("Tyring to detect modem...") output = shell_command("lsusb") if output[2] == 0: for module in modules: if output[0].find(module.pid) != -1: system_id["modem_vendor"] = module.vendor_name system_id["modem_name"] = module.module_name system_id["modem_vendor_id"] = module.vid system_id["modem_product_id"] = module.pid identified_module = module return identified_module logger.warning("Modem don't exist in list of supported modems!") for module in modules: if output[0].find(module.vid) != -1: system_id["modem_vendor"] = module.vendor_name system_id["modem_name"] = default_modules.get(str(module.vid)).module_name system_id["modem_vendor_id"] = module.vid system_id["modem_product_id"] = default_modules.get(str(module.vid)).pid identified_module = default_modules.get(str(module.vid)) return identified_module logger.warning("Modem vendor couldn't be found!") # clear modem identification data system_id["modem_vendor"] = None system_id["modem_name"] = None system_id["modem_vendor_id"] = None system_id["modem_product_id"] = None system_id["iccid"] = None system_id["imei"] = None system_id["sw_version"] = None if old_system_id.get("modem_vendor") is not None: try: write_yaml_all(SYSTEM_PATH, system_id) except Exception as error: raise RuntimeError("Save ID's to file") from error raise RuntimeError("Modem vendor couldn't be found!") else: raise RuntimeError("lsusb command error!")
def get_actual_configs(): if os.path.isfile(CONFIG_PATH): try: actual_cfg = read_yaml_all(CONFIG_PATH) except Exception as error: raise RuntimeError("get_actual_configs() -->") from error else: return actual_cfg else: logger.info("Config file doesn't exist!") return {}
def update_geolocation(modem, immediately=False): global last_check periodically = False geolocation_data = {} old_geolocation = {} if os.path.isfile(GEOLOCATION_PATH): try: old_geolocation = read_yaml_all(GEOLOCATION_PATH) except: logger.warning("Old geolocation data in geolocation.yaml file couln't be read!") else: now = int(time.time()) if now - last_check > 24*60*60: # a day periodically = True last_check = now old_geolocation.pop("last_update", None) else: immediately = True if immediately or periodically: logger.info("Checking geolocation data...") try: modem.read_geoloc_data() except: logger.error("Error occured getting geolocation data") else: for key in modem.geolocation: geolocation_data[key] = modem.geolocation[key] if geolocation_data != old_geolocation and geolocation_data != {}: geolocation_data["last_update"] = int(time.time()) # Save ID's to file try: write_yaml_all(GEOLOCATION_PATH, geolocation_data) except Exception as error: logger.error("write_yaml_all(GEOLOCATION_PATH, geolocation_data) -> %s", error) else: logger.info("Geolocation data updated with changes.") # GEOLOCATION REPORT if conf.debug_mode and conf.verbose_mode: print("") print("********************************************************************") print("[?] GEOLOCATION REPORT") print("-------------------------") for item in geolocation_data.items(): print(f"[+] {item[0]} --> {item[1]}") print("********************************************************************") print("")
def get_configs(): if os.path.isfile(CONFIG_PATH): try: config.update(read_yaml_all(CONFIG_PATH)) except Exception as error: print(str(error)) else: print("Config file doesn't exist! Restoring default configs...") conf.restore_defaults() config.update({}) old_config.update(config) try: print("Creating config.yaml with default configs...") default_conf = {} for item in vars(conf): if item in configs_showed_at_frontend: default_conf[item] = conf.__getattribute__(item) write_yaml_all(CONFIG_PATH, default_conf) except Exception as error: print(str(error)) return conf if config == old_config: return conf conf.set_verbose_mode_config(config.get("verbose_mode")) conf.set_debug_mode_config(config.get("debug_mode")) conf.set_acceptable_apns_config(config.get("acceptable_apns")) conf.set_apn_config(config.get("apn")) conf.set_ping_timeout_config(config.get("ping_timeout")) conf.set_other_ping_timeout_config(config.get("other_ping_timeout")) conf.set_check_internet_interval_config( config.get("check_internet_interval")) conf.set_send_monitoring_data_interval_config( config.get("send_monitoring_data_interval")) conf.set_network_priority_config(config.get("network_priority")) conf.set_cellular_interfaces_config(config.get("cellular_interfaces")) conf.set_logger_level_config(config.get("logger_level")) conf.config_changed = True old_config.update(config) return conf
def compare_request(request_file): try: request = read_yaml_all(request_file).get("configs", "") except Exception as error: raise error else: request_keys = set(request.keys()) actual_keys = set(actual_configs.keys()) shared_keys = request_keys.intersection(actual_keys) added = request_keys - actual_keys modified = { x: (request[x], actual_configs[x]) for x in shared_keys if request[x] != actual_configs[x] } diff = {} for key in added: diff[key] = request[key] for key in modified: diff[key] = request[key] return diff
def identify_setup(): # Get old system setup if it is exist old_system_id = {} if os.path.isfile(SYSTEM_PATH): try: old_system_id = read_yaml_all(SYSTEM_PATH) except Exception as error: logger.warning("Old system_id in system.yaml file couln't be read!") logger.info("[?] System identifying...") # Turn off AT command echo (Required) logger.debug("[+] Turning off AT command echo") try: _turn_off_echo() except Exception as error: raise error # Product Name (Optional) logger.debug("[+] Product Name") try: _identify_product_name() except Exception as error: logger.warning("Modem name identification failed!") system_id["modem_name"] = "Unknown" # IMEI (Optional) logger.debug("[+] IMEI") try: _identify_imei() except Exception as error: logger.warning("IMEI identification failed!") system_id["imei"] = "Unknown" # SW version (Optional) logger.debug("[+] Modem firmware revision") try: _identify_fw_version() except Exception as error: logger.warning("Modem firmware ver. identification failed!") system_id["sw_version"] = "Unknown" # ICCID (Optional) logger.debug("[+] SIM ICCID") try: _identify_iccid() except Exception as error: logger.warning("SIM ICCID identification failed!") system_id["iccid"] = "Unknown" # OS (Optional) logger.debug("[>] OS Identification") try: _identify_os() except Exception as error: logger.warning("OS identification failed!") # Board (Optional) logger.debug("[+] Board Identification") try: _identify_board() except Exception as error: logger.warning("Board identification failed!") try: system_id["last_update"] = int(time.time()) except Exception as error: logger.error("identify() timestamp -> %s", error) # IDENTIFICATION REPORT if conf.debug_mode and conf.verbose_mode: print("") print("********************************************************************") print("[?] IDENTIFICATION REPORT") print("-------------------------") for item in system_id.items(): print(f"[+] {item[0]} --> {item[1]}") print("********************************************************************") print("") # Save ID's to file try: write_yaml_all(SYSTEM_PATH, system_id) except Exception as error: raise error if system_id != old_system_id: logger.warning("System setup has changed!") return system_id or {}
def monitor(): # Get old system setup if it is exist old_monitor = {} if os.path.isfile(MONITOR_PATH): try: old_monitor = read_yaml_all(MONITOR_PATH) except: logger.warning( "Old monitor data in monitor.yaml file couln't be read!") monitor_data["last_update"] = old_monitor.get("last_update", int(time.time())) # Modem Manager monitoring data try: monitor_data["cellular_connection"] = modem.monitor.get( "cellular_connection") monitor_data["selected_apn"] = modem.get_apn() incident_count = monitor_data.get("fixed_incident", 0) old_incident_count = old_monitor.get("fixed_incident", 0) if incident_count >= old_incident_count: monitor_data["fixed_incident"] = modem.get_fixed_incident_count() else: monitor_data["fixed_incident"] = old_incident_count except Exception as error: logger.error("monitor() @modem -> %s", error) try: monitor_data["signal_quality"] = modem.get_signal_quality() except Exception as error: logger.error("monitor() @modem -> %s", error) try: monitor_data["roaming_operator"] = modem.get_roaming_operator() except Exception as error: logger.error("monitor() @modem -> %s", error) try: monitor_data["active_lte_tech"] = modem.get_active_lte_tech() except Exception as error: logger.error("monitor() @modem -> %s", error) # Network Manager monitoring data try: monitor_data["usable_interfaces"] = network.find_usable_interfaces() monitor_data["active_interface"] = network.find_active_interface() monitor_data["ifs_status"] = network.monitor except Exception as error: logger.error("monitor() @network -> %s", error) if monitor_data != old_monitor: monitor_data["last_update"] = int(time.time()) # Save ID's to file try: write_yaml_all(MONITOR_PATH, monitor_data) except Exception as error: logger.error("write_yaml_all(MONITOR_PATH, modem.monitor) -> %s", error) else: logger.info("Monitoring data updated with changes.") # MONITOR REPORT if conf.debug_mode and conf.verbose_mode: print("") print( "********************************************************************" ) print("[?] MONITOR REPORT") print("-------------------------") for item in monitor_data.items(): print(f"[+] {item[0]} --> {item[1]}") print( "********************************************************************" ) print("") # END OF MONITOR REPORT else: # logger.debug("No change on monitoring data.") pass
import time import os.path import usb.core from helpers.config_parser import conf from helpers.logger import logger from helpers.commander import shell_command, send_at_com from helpers.yamlio import read_yaml_all, write_yaml_all, DIAG_FOLDER_PATH, MONITOR_PATH from helpers.exceptions import * from helpers.sbc_support import supported_sbcs from helpers.modem_support.modem_support import DefaultModule old_monitor = {} if os.path.isfile(MONITOR_PATH): try: old_monitor = read_yaml_all(MONITOR_PATH) except: logger.warning("Old monitor data in monitor.yaml file couln't be read!") def parse_output(output, header, end): header += " " header_size = len(header) index_of_data = output[0].find(header) + header_size end_of_data = index_of_data + output[0][index_of_data:].find(end) sig_data = output[0][index_of_data:end_of_data] return sig_data class Modem(DefaultModule): imei = ""
#!/usr/bin/python3 import os.path from helpers.yamlio import read_yaml_all, ENV_PATH env = {} core_env = {} # Check the .env.yaml file exist. if os.path.isfile(ENV_PATH): try: env = read_yaml_all(ENV_PATH) except Exception as error: print(str(error)) else: core_env = env.get("core", {}) else: print(".env.yaml file doesn't exist!") configs_showed_at_frontend = ["apn", "network_priority"] default_config = { "apn": core_env.get("apn", "super"), "sbc": core_env.get("sbc", "rpi4"), "debug_mode": False, "verbose_mode": False, "check_internet_interval": 60, "send_monitoring_data_interval": 25, "ping_timeout": 9, "other_ping_timeout": 3, "network_priority": {