def save_configuration(): num_of_req = len(waiting_requests) try: actual_configs.update(get_actual_configs()) except Exception as error: logger.error(error) return if num_of_req: req = waiting_requests[-1] try: diff = compare_request(req) except Exception as error: logger.error("compare_request() --> %s", error) else: for item in diff: actual_configs[item] = diff[item] # check modem reconfiguration is required if item in keys_required_modem_config: conf.modem_config_required = True # check log reconfiguration is required if item == "debug_mode": conf.log_config_required = True try: write_yaml_all(CONFIG_PATH, actual_configs) except Exception as error: logger.error("save_configuration --> %s", error) else: waiting_requests.pop() processing_requests.append(req)
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 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 diagnose(self, diag_type=0): logger.info("Diagnostic is working...") self.diag_modem_reachable() self.diag_apn_set() self.diag_modem_mode() self.diag_sim_ready() self.diag_network_register() self.diag_ecm_pdp_context() self.diag_usb_driver() self.diag_connection_interface() self.diag_usb_interface() for key, value in self.diagnostic.items(): # little endian if value is True: self.diagnostic_zip |= (1 << self.diagnostic_map[key]) elif value is False: self.diagnostic_zip &= ~(1 << self.diagnostic_map[key]) timestamp = int(time.time()) self.diagnostic["timestamp"] = timestamp diag = {"last_update": timestamp, "value": self.diagnostic_zip} if diag_type == 0: diag_file_name = "diagnostic.yaml" diag_file_path = DIAG_FOLDER_PATH + diag_file_name logger.info("Creating diagnostic report on --> %s", diag_file_path) write_yaml_all(diag_file_path, diag) else: diag_file_name = "diagnostic-repeated.yaml" diag_file_path = DIAG_FOLDER_PATH + diag_file_name logger.info("Creating diagnostic report on --> %s", diag_file_path) write_yaml_all(diag_file_path, diag) if conf.debug_mode and conf.verbose_mode: print("") print( "********************************************************************" ) print("[?] DIAGNOSTIC REPORT") print("---------------------") for item in self.diagnostic.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 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 {}
from helpers.commander import send_at_com, shell_command from helpers.yamlio import read_yaml_all, write_yaml_all, SYSTEM_PATH from helpers.exceptions import ModemNotReachable, ModemNotSupported from helpers.modem_support.modem_support import modules, default_modules from __version__ import version identified_module = None system_id = { "manager_version": version, } # Save ID's to file try: write_yaml_all(SYSTEM_PATH, system_id) except Exception as error: raise RuntimeError("Save ID's to file") from error 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!")
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
def diagnose(self, diag_type=0): self.diagnostic = { "con_interface": "", "modem_reachable": "", "usb_driver": "", "pdp_context": "", "network_reqister": "", "sim_ready": "", "modem_mode": "", "modem_apn": "", "timestamp": "", } logger.info("Diagnostic is working...") # 1 - Is connection interface exist? logger.debug("[1] : Is connection interface exist?") output = shell_command("route -n") if output[2] == 0: if output[0].find(self.interface_name) != -1: self.diagnostic["con_interface"] = True else: self.diagnostic["con_interface"] = False else: raise RuntimeError("Error occured processing shell command!") # 2 - Is USB interface exist? logger.debug("[2] : Is USB interface exist?") output = shell_command("lsusb") if output[2] == 0: if output[0].find(self.vid) != -1: self.diagnostic["usb_interface"] = True else: self.diagnostic["usb_interface"] = False else: raise RuntimeError("Error occured processing shell command!") # 3 - Is USB driver exist? logger.debug("[3] : Is USB driver exist?") output = shell_command("usb-devices") if output[2] == 0: if output[0].find("cdc_ether") != -1: if output[0].count("cdc_ether") >= 2: self.diagnostic["usb_driver"] = True else: self.diagnostic["usb_driver"] = False else: self.diagnostic["usb_driver"] = False else: raise RuntimeError("Error occured processing shell command!") # 4 - Is modem reachable? logger.debug("[4] : Is modem reachable?") output = send_at_com("AT", "OK") if output[2] == 0: self.diagnostic["modem_reachable"] = True else: self.diagnostic["modem_reachable"] = False # 5 - Is ECM PDP Context active? logger.debug("[5] : Is ECM PDP Context is active?") output = send_at_com(self.pdp_status_command, "1,1") if output[2] == 0: self.diagnostic["pdp_context"] = True else: self.diagnostic["pdp_context"] = False # 6 - Is the network registered? logger.debug("[6] : Is the network is registered?") try: self.check_network() except: self.diagnostic["network_reqister"] = False else: self.diagnostic["network_reqister"] = True # 7 - Is the APN OK? logger.debug("[7] : Is the APN is OK?") apn_with_quotes = '"%s"' % conf.apn output = send_at_com("AT+CGDCONT?", apn_with_quotes) if output[2] == 0: self.diagnostic["modem_apn"] = True else: self.diagnostic["modem_apn"] = False # 8 - Is the modem mode OK? logger.debug("[8] : Is the modem mode OK?") output = send_at_com(self.mode_status_command, self.ecm_mode_response) if output[2] == 0: self.diagnostic["modem_mode"] = True else: self.diagnostic["modem_mode"] = False # 9 - Is the SIM ready? logger.debug("[9] : Is the SIM ready?") output = send_at_com("AT+CPIN?", "READY") if output[2] == 0: self.diagnostic["sim_ready"] = True else: self.diagnostic["sim_ready"] = False timestamp = time.strftime("%Y-%m-%d_%H:%M:%S") self.diagnostic["timestamp"] = timestamp if diag_type == 0: diag_file_name = "cm-diag_" + str(timestamp) + ".yaml" diag_file_path = DIAG_FOLDER_PATH + diag_file_name logger.info("Creating diagnostic report on --> %s", diag_file_path) write_yaml_all(diag_file_path, self.diagnostic) else: diag_file_name = "cm-diag-repeated.yaml" diag_file_path = DIAG_FOLDER_PATH + diag_file_name logger.info("Creating diagnostic report on --> %s", diag_file_path) write_yaml_all(diag_file_path, self.diagnostic) if conf.debug_mode and conf.verbose_mode: print("") print("********************************************************************") print("[?] DIAGNOSTIC REPORT") print("---------------------") for item in self.diagnostic.items(): print(f"[+] {item[0]} --> {item[1]}") print("********************************************************************") print("")