def _identify_setup(): global modem queue.set_step(sub=0, base=1, success=2, fail=15, interval=2, is_ok=False, retry=20) try: new_id = identify_setup() except Exception as error: logger.error("identify_setup -> %s", error) queue.is_ok = False else: if new_id != {}: modem.imei = new_id.get("imei", "") modem.iccid = new_id.get("iccid", "") modem.sw_version = new_id.get("sw_version", "") queue.is_ok = True if conf.debug_mode and conf.verbose_mode: print("") print( "********************************************************************" ) print("[?] MODEM REPORT") print("-------------------------") attrs = vars(modem) print("\n".join("[+] %s : %s" % item for item in attrs.items())) print( "********************************************************************" ) print("")
def check_sim_ready(self): logger.info("Checking the SIM is ready...") output = send_at_com("AT+CPIN?", "CPIN: READY") if output[2] == 0: logger.info("SIM is ready.") else: logger.error(output[0]) raise SIMNotReady(output[0])
def on(self, msg): try: self.light.on(transition=2) except: logger.error('Can not turn on device %s', self.name) return client.publish(self.get_status_topic(), json.dumps({'isOn': True})) self.status(msg=None)
def off(self, msg): try: self.light.off() except: logger.error('Can not turn off device %s', self.name) return client.publish(self.get_status_topic(), json.dumps({'isOn': False})) self.status(msg=None)
def get_interface_priority(self): try: usables = self.find_usable_interfaces() except Exception as error: logger.error("find_usable_interfaces() --> %s", error) for if_name in usables: for interface in self.interfaces: if if_name == interface.name: priority = usables.index(if_name) + 1 interface.priority = priority
def manage_network(): network.check_interfaces() network.get_interface_metrics() network.check_and_create_monitoring() try: network.adjust_priorities() except Exception as error: logger.error("adjust_priorities() --> %s", error) network.debug_routes()
def shell_command(command): try: com = command.split(" ") cp = subprocess.run(com, universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) except Exception as error: logger.error("Message: %s", error) return ("", "", 1) else: return (cp.stdout, cp.stderr, cp.returncode)
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 check_network(self): logger.info("Checking the network is ready...") output = send_at_com("AT+CREG?", "OK") if output[2] == 0: if output[0].find("+CREG: 0,1") != -1 or output[0].find("+CREG: 0,5") != -1: logger.info("Network is registered.") else: logger.error(output[0]) raise NetworkRegFailed(output[0]) else: logger.error(output[0]) raise NetworkRegFailed(output[0])
def _check_internet(): global first_connection_flag if queue.sub == "check_internet_base": queue.set_step( sub="organizer", base="check_internet_base", success="check_internet_base", fail="diagnose_base", interval=conf.check_internet_interval, is_ok=False, retry=1, ) elif queue.sub == "check_internet_after_rci": queue.set_step( sub="organizer", base="check_internet_after_rci", success="check_internet_base", fail="reset_usb_interface", interval=10, is_ok=False, retry=0, ) elif queue.sub == "check_internet_after_rui": queue.set_step( sub="organizer", base="check_internet_after_rui", success="check_internet_base", fail="reset_modem_softly", interval=10, is_ok=False, retry=0, ) try: modem.check_internet() except Exception as error: logger.error("check_internet() -> %s", error) queue.is_ok = False else: if not first_connection_flag: logger.info("Internet connection is established") first_connection_flag = True if modem.incident_flag: modem.monitor["fixed_incident"] += 1 modem.incident_flag = False logger.info("Internet connection is restored") queue.is_ok = True
def check_interfaces(self): try: usables = self.find_usable_interfaces() except Exception as error: logger.error("find_usable_interfaces() --> %s", error) actual = [ interface.name for interface in self.interfaces ] for usable_if in usables: if usable_if not in actual: self.create_interface(usable_if) for actual_if in actual: if actual_if not in usables: self.remove_interface(actual_if)
def _check_internet(): global first_connection_flag if queue.sub == 5: queue.set_step( sub=0, base=5, success=5, fail=6, interval=conf.check_internet_interval, is_ok=False, retry=1, ) elif queue.sub == 8: queue.set_step(sub=0, base=8, success=5, fail=9, interval=10, is_ok=False, retry=0) elif queue.sub == 10: queue.set_step(sub=0, base=10, success=5, fail=11, interval=10, is_ok=False, retry=0) try: modem.check_internet() except Exception as error: logger.error("check_internet() -> %s", error) queue.is_ok = False else: if not first_connection_flag: logger.info("Internet connection is established") first_connection_flag = True if modem.incident_flag: modem.monitor["fixed_incident"] += 1 modem.incident_flag = False logger.info("Internet connection is restored") queue.is_ok = True
def _check_sim_ready(): queue.set_step(sub=0, base=14, success=3, fail=13, interval=1, is_ok=False, retry=5) try: modem.check_sim_ready() except Exception as error: logger.error("check_sim_ready() -> %s", error) queue.is_ok = False else: queue.is_ok = True
def _reset_modem_hardly(): queue.set_step(sub=0, base=12, success=16, fail=16, interval=1, is_ok=False, retry=1) try: modem.reset_modem_hardly() except Exception as error: logger.error("reset_modem_hardly() -> %s", error) queue.is_ok = False else: queue.is_ok = True
def _check_network(): queue.set_step(sub=0, base=3, success=4, fail=13, interval=5, is_ok=False, retry=120) try: modem.check_network() except Exception as error: logger.error("check_network() -> %s", error) queue.is_ok = False else: queue.is_ok = True
def _reset_usb_interface(): queue.set_step(sub=0, base=9, success=10, fail=11, interval=1, is_ok=False, retry=2) try: modem.reset_usb_interface() except Exception as error: logger.error("reset_usb_interface() -> %s", error) queue.is_ok = False else: queue.is_ok = True
def send_at_com(command, desired): try: cp = subprocess.run( ["atcom", command, "--find", desired], universal_newlines=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, ) except Exception as error: logger.error("Message: %s", error) return ("", "", 1) else: if cp.returncode == 0: return (cp.stdout, cp.stderr, cp.returncode) else: return ("", "", 1)
def _initiate_ecm(): queue.set_step(sub=0, base=4, success=5, fail=13, interval=0.1, is_ok=False, retry=5) try: modem.initiate_ecm() except Exception as error: logger.error("initiate_ecm() -> %s", error) queue.is_ok = False else: queue.is_ok = True
def _diagnose(): modem.monitor["cellular_connection"] = False modem.incident_flag = True diag_type = 0 diag = Diagnostic(modem) if queue.sub == "diagnose_base": queue.set_step( sub="organizer", base="diagnose_base", success="reset_connection_interface", fail="reset_connection_interface", interval=0.1, is_ok=False, retry=5, ) diag_type = 0 elif queue.sub == "diagnose_repeated": queue.set_step( sub="organizer", base="diagnose_repeated", success="reset_modem_softly", fail="reset_modem_softly", interval=0.1, is_ok=False, retry=5, ) diag_type = 1 elif queue.sub == "diagnose_last_exit": queue.set_step( sub="organizer", base="diagnose_last_exit", success="reset_modem_hardly", fail="reset_modem_hardly", interval=0.1, is_ok=False, retry=5, ) diag_type = 1 try: diag.diagnose(diag_type) except Exception as error: logger.error("diagnose() -> %s", error) queue.is_ok = False else: queue.is_ok = True
def brightness(self, msg): level = msg.get('level') if type(level) is not int: logger.error('Invalid brightness level provided for light: %s', self.name) return self.light.on() self.light.set_brightness(level, 5) client.publish(self.get_status_topic(), json.dumps({ 'isOn': True, 'brightness': level })) self.status(msg=None)
def _reset_usb_interface(): queue.set_step( sub="organizer", base="reset_usb_interface", success="check_internet_after_rui", fail="reset_modem_softly", interval=1, is_ok=False, retry=2, ) try: modem.reset_usb_interface() except Exception as error: logger.error("reset_usb_interface() -> %s", error) queue.is_ok = False else: queue.is_ok = True
def _check_sim_ready(): queue.set_step( sub="organizer", base="check_sim_ready", success="check_network", fail="diagnose_repeated", interval=1, is_ok=False, retry=5, ) try: modem.check_sim_ready() except Exception as error: logger.error("check_sim_ready() -> %s", error) queue.is_ok = False else: queue.is_ok = True
def _reset_modem_hardly(): queue.set_step( sub="organizer", base="reset_modem_hardly", success="identify_modem", fail="identify_modem", interval=1, is_ok=False, retry=1, ) try: modem.reset_modem_hardly() except Exception as error: logger.error("reset_modem_hardly() -> %s", error) queue.is_ok = False else: queue.is_ok = True
def status(self, msg): try: status = self.light.status() except: logger.error('Can get status of device %s', self.name) return client.publish( self.get_status_topic(), json.dumps({ 'isOn': status.is_on, 'brightness': status.brightness, 'colorTemp': status.color_temp, 'rgb': status.rgb, 'hsv': status.hsv, 'dev': status.developer_mode, 'saveState': status.save_state_on_change, }))
def _identify_modem(): global modem queue.set_step(sub=0, base=16, success=1, fail=15, interval=2, is_ok=False, retry=20) try: module = identify_modem() except Exception as error: logger.error("identify_modem -> %s", error) queue.is_ok = False else: modem.update(module) queue.is_ok = True
def _initiate_ecm(): queue.set_step( sub="organizer", base="initiate_ecm", success="check_internet_base", fail="diagnose_repeated", interval=0.1, is_ok=False, retry=5, ) try: modem.initiate_ecm() except Exception as error: logger.error("initiate_ecm() -> %s", error) queue.is_ok = False else: queue.is_ok = True
def apply_configs(): if len(processing_requests) > 0: try: for request in processing_requests: filename = os.path.basename(request) done = filename + "_done" old = os.path.join(CONFIG_REQUEST_PATH, filename) new = os.path.join(CONFIG_REQUEST_PATH, done) os.rename(old, new) logger.info("Request --> %s is done.", filename) except Exception as error: logger.error("apply_configs() --> %s", error) else: processing_requests.clear() conf.reload_required = True logger.info("New configs are applied.")
def configure(): get_requests() for _ in range(len(waiting_requests)): save_configuration() apply_configs() if conf.reload_required: try: conf.update_config(get_configs()) except Exception as error: logger.error("conf.update_config() --> %s", error) else: conf.reload_required = False if conf.is_config_changed(): logger.info("Configuration is changed.") if conf.modem_config_required: logger.info("Modem configuration will be start soon.") # go to modem configuration step queue.set_step(sub=0, base=2, success=14, fail=13, interval=1, is_ok=False, retry=5) conf.modem_config_required = False if conf.log_config_required: if conf.debug_mode is True: update_log_debug(logger, True) else: update_log_debug(logger, False) conf.log_config_required = False conf.config_changed = False config_report()
def _identify_modem(): global modem queue.set_step( sub="organizer", base="identify_modem", success="identify_setup", fail="diagnose_last_exit", interval=2, is_ok=False, retry=20, ) try: module = identify_modem() except Exception as error: logger.error("identify_modem -> %s", error) queue.is_ok = False else: modem = module queue.is_ok = True
def _configure_modem(): queue.set_step(sub=0, base=2, success=14, fail=13, interval=1, is_ok=False, retry=5) try: modem.configure_modem() except ModemNotSupported: queue.is_ok = False except ModemNotFound: queue.is_ok = False except Exception as error: logger.error("configure_modem() -> %s", error) queue.is_ok = False else: queue.is_ok = True