Ejemplo n.º 1
0
    def reset_modem_hardly(self):
        logger.info("Modem is resetting via hardware...")

        sbc = supported_sbcs.get(conf.sbc)
        sbc.modem_power_disable()
        time.sleep(2)
        sbc.modem_power_enable()
Ejemplo n.º 2
0
    def read_geoloc_data(self):
        """
        Reads required data from modem in order to use at geolocation API
        """
        logger.info("Getting raw geolocation data...")
        radio_type_id = 2

        output = send_at_com('AT+QENG="servingcell"', "OK")
        if output[2] == 0:
            data = output[0].split(",")
            radio_type = data[radio_type_id].replace('"', '').casefold()

            try:
                for key in self.serving_cell_response_map:
                    if key.find(radio_type) != -1:
                        temp = self.serving_cell_response_map.get(key, {})

                for key in temp:
                    self.geolocation[key] = data[temp[key]].replace(
                        '"', '').casefold()
            except:
                raise ValueError("Geolocation data is broken")
        else:
            raise RuntimeError(output[0])

        # str/hex/int conversation
        try:
            for key in self.geolocation:
                if key in ["tac", "lac", "psc", "cid"]:
                    self.geolocation[key] = int(self.geolocation[key], 16)
                elif key in ["mcc", "mnc"]:
                    self.geolocation[key] = int(self.geolocation[key])
        except:
            raise ValueError(
                "read_geoloc_data --> error occured converting hex to int")
Ejemplo n.º 3
0
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!")
Ejemplo n.º 4
0
    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])
Ejemplo n.º 5
0
 def reset_modem_hardly(self):
     logger.info("Modem is resetting via hardware...")
     self.deregister_network()
     time.sleep(
         5)  # wait a while before rebooting to complete nvm processes
     sbc = supported_sbcs.get(conf.sbc)
     sbc.modem_power_disable()
     time.sleep(2)
     sbc.modem_power_enable()
Ejemplo n.º 6
0
    def deregister_network(self):
        """
        Deregister from network and disable auto-registering
        """
        output = send_at_com("AT+COPS=2", "OK")

        if output[2] == 0:
            logger.info("Modem is deregistered from network")
        else:
            raise RuntimeError("Network deregistering is failed!")
    def wrapper_func(event, context):

        logger.debug("Received %s request with event: %s" %
                     (event['RequestType'], json.dumps(event)))

        response = {
            "StackId": event["StackId"],
            "RequestId": event["RequestId"],
            "LogicalResourceId": event["LogicalResourceId"],
            "PhysicalResourceId": "custom_resource_physical_id",
            "Status": SUCCESS,
        }
        if event.get("PhysicalResourceId", False):
            response["PhysicalResourceId"] = event["PhysicalResourceId"]

        if base_response is not None:
            response.update(base_response)

        try:
            response.update(func(event, context))
        except NoResponse:
            # Do nothing, maybe we're being rescheduled?
            return
        except Exception as e:
            logger.exception("Failed to execute resource function")
            reason = "Exception was raised while handling custom resource."
            reason += " Message {}".format(e.args or e.message)
            response.update({
                "Status": FAILED,
                "Reason": reason,
                "Data": {
                    'FailureReason': reason
                }
            })

        serialized = json.dumps(response)
        logger.info("Responding to '%s' request with: %s" %
                    (event['RequestType'], serialized))

        req = requests.put(event['ResponseURL'],
                           data=serialized,
                           headers={
                               'Content-Length': str(len(serialized)),
                               'Content-Type': ''
                           })

        try:
            req
            logger.debug("Request to CFN API succeeded, nothing to do here")
        except requests.HTTPError as e:
            logger.error("Callback to CFN API failed with status %d" % e.code)
            logger.error("Response: %s" % e.reason)
        except requests.ConnectionError as e:
            logger.error("Failed to reach the server - %s" % e.reason)
Ejemplo n.º 8
0
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 {}
Ejemplo n.º 9
0
 def reset_modem_softly(self):
     logger.info("Modem is resetting softly...")
     output = send_at_com(self.reboot_command, "OK")
     if output[2] == 0:
         try:
             self.wait_until_modem_turned_off()
             self.wait_until_modem_started()
         except Exception as error:
             raise error
     else:
         raise RuntimeError("Reboot command couldn't be reach to the modem!")
Ejemplo n.º 10
0
 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:
             raise NetworkRegFailed("Network not registered: ", output)
     else:
         raise NetworkRegFailed("Error occured sending AT+CREG?: ", output)
Ejemplo n.º 11
0
    def set_modem_eps_data_centric(self):
        output = send_at_com(self.eps_mode_status_command, self.eps_data_centric_response)

        if output[2] == 0:
            logger.info("Modem mode for EPS is OK")
        else:
            output = send_at_com(self.eps_mode_setter_command, "OK")

            if output[2] == 0:
                logger.info("Modem mode for EPS updated succesfully")
            else:
                raise ModemNotReachable("Modem mode for EPS couldn't be set successfully!")
Ejemplo n.º 12
0
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("")
Ejemplo n.º 13
0
    def configure_apn(self):
        apn_with_quotes = '"%s"' % conf.apn
        output = send_at_com("AT+CGDCONT?", apn_with_quotes)

        if output[2] == 0:
            logger.info("APN is up-to-date.")
        else:
            output = send_at_com(f'AT+CGDCONT=1,"IPV4V6","{conf.apn}"', "OK")

            if output[2] == 0:
                logger.info("APN is updated succesfully : %s", conf.apn)
            else:
                raise ModemNotReachable("APN couldn't be set successfully!")
 def exclude(self, resource_generator):
     items = []
     for resource in resource_generator:
         logger.debug('include resource {}'.format(resource.arn))
         if resource.arn:
             items.append({'policy': self.policy.arn, 'arn': resource.arn})
         if resource.assumed_role != self.role.name:
             resource.detach_policy(self.policy.arn)
         else:
             resource.stop_assume_role(self.role.name)
     if len(items) > 0:
         logger.info('Remove resources {}'.format(items))
         self.table.remove(items)
Ejemplo n.º 15
0
    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])
Ejemplo n.º 16
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
Ejemplo n.º 17
0
    def reset_usb_interface(self):
        logger.info("USB interface is reset...")

        vendor_id = self.vid
        product_id = self.pid

        vendor_id_int = int(vendor_id, 16)
        product_id_int = int(product_id, 16)

        try:
            dev = usb.core.find(idVendor=vendor_id_int, idProduct=product_id_int)
            dev.reset()
        except Exception as error:
            raise error
Ejemplo n.º 18
0
    def initiate_ecm(self, connection_delay=10):
        logger.info("Checking the ECM initialization...")
        output = send_at_com(self.pdp_status_command, "OK")
        if output[2] == 0:
            if output[0].find("0,1") != -1 or output[0].find("1,1") != -1:
                logger.info("ECM is already initiated.")
                time.sleep(10)
                return 0

        logger.info("ECM Connection is initiating...")
        output = send_at_com(self.pdp_activate_command, "OK")

        if output[2] == 0:
            for _ in range(2):
                output = send_at_com(self.pdp_status_command, "OK")

                if output[2] == 0:
                    if output[0].find("0,1") != -1 or output[0].find(
                            "1,1") != -1:
                        logger.info("ECM is initiated.")
                        time.sleep(connection_delay)
                        return 0
                    else:
                        time.sleep(5)
                else:
                    time.sleep(2)

            raise PDPContextFailed("ECM initiation timeout!")
        else:
            raise PDPContextFailed("ECM initiation failed!")
 def create(self, path, name, base_policy):
     try:
         response = self.iam_client.create_policy(
             Path='/{}/'.format(path),
             PolicyName=name,
             PolicyDocument=base_policy,
             Description='Set the access to resources provisioned by products \
             associated with Service Catalog Portfolio {}'.format(name)
         )
         logger.info('Policy created: {}'.format(name))
         return response['Policy']['Arn']
     except ClientError as e:
         if e.response['Error']['Code'] == 'EntityAlreadyExists':
             return self.find(path, name)
Ejemplo n.º 20
0
 def reset_modem_softly(self):
     logger.info("Modem is resetting softly...")
     self.deregister_network()
     time.sleep(
         5)  # wait a while before rebooting to complete nvm processes
     output = send_at_com(self.reboot_command, "OK")
     if output[2] == 0:
         try:
             self.wait_until_modem_turned_off()
             self.wait_until_modem_started()
         except Exception as error:
             raise error
     else:
         raise RuntimeError(
             "Reboot command couldn't be reach to the modem!")
Ejemplo n.º 21
0
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
Ejemplo n.º 22
0
    def wait_until_modem_interface_up(self):
        counter = 0
        logger.debug("Interface Name: %s", self.interface_name)
        # Check modem connection interface
        for _ in range(20):
            output = shell_command("route -n")
            if output[0].find(self.interface_name) != -1:
                logger.info("Modem interface is detected.")
                counter = 0
                break
            else:
                time.sleep(1)
                counter += 1

        if counter != 0:
            raise ModemNotFound("Modem interface couln't be detected.")
Ejemplo n.º 23
0
    def read_geoloc_data(self):
        """
        Reads required data from modem in order to use at geolocation API
        """
        logger.info("Getting raw geolocation data...")
        radio_type_id = 0

        output = send_at_com("AT^SMONI", "OK")
        if output[2] == 0:
            data = parse_output(output, "^SMONI:", "\n").split(",")
            radio_type = data[radio_type_id].replace('"', '').casefold()

            if radio_type == "4g":
                radio_type = "lte"
            elif radio_type == "3g":
                radio_type = "wcdma"
            elif radio_type == "2g":
                radio_type = "gsm"

            try:
                for key in self.smoni_response_map:
                    if key.find(radio_type) != -1:
                        temp = self.smoni_response_map.get(key, {})

                for key in temp:
                    self.geolocation[key] = data[temp[key]].replace(
                        '"', '').casefold()
            except:
                raise ValueError("Geolocation data is broken")
        else:
            raise RuntimeError(output[0])

        # str/hex/int conversation
        try:
            for key in self.geolocation:
                if key in ["tac", "lac", "psc", "cid"]:
                    self.geolocation[key] = int(self.geolocation[key], 16)
                elif key in ["mcc", "mnc"]:
                    self.geolocation[key] = int(self.geolocation[key])
                elif key == "radio_type":
                    self.geolocation[key] = radio_type
        except:
            raise ValueError(
                "read_geoloc_data --> error occured converting hex to int")
Ejemplo n.º 24
0
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()
Ejemplo n.º 25
0
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.")
Ejemplo n.º 26
0
def handle_message(msg):
    logger.info('New message topic: %s, message: %s', msg.topic,
                str(msg.payload))

    splitted = msg.topic.split('/')
    deviceName = splitted[2]
    toExec = splitted[3]

    if toExec in ['status']:
        return

    light = devices.get(deviceName)
    try:
        jsonMsg = json.loads(str(msg.payload.decode("utf-8", "ignore")))
    except:
        jsonMsg = {}

    if light is None:
        logger.error('Device %s is not supported, update config file',
                     deviceName)
        return

    command = Command(light=light, deviceName=deviceName)

    handlers = dict(
        turnOn=command.on,
        turnOff=command.off,
        getStatus=command.status,
        setBrightness=command.brightness,
    )

    commandToExec = handlers.get(toExec)

    if (commandToExec is None):
        logger.error('Not supported command %s for device %s', toExec,
                     deviceName)
        return

    commandToExec(jsonMsg)

    del command
Ejemplo n.º 27
0
    def adjust_priorities(self):
        default_metric_factor = 10

        for ifs in self.interfaces:
            ifs.metric_factor = conf.network_priority.get(ifs.name, default_metric_factor)

        for iface in self.interfaces:
            # action when connection status changes
            if not iface.connection_status:
                iface.desired_metric = LOWEST_PRIORTY_FACTOR * 100
            else:
                iface.desired_metric = iface.metric_factor * 100

            # do changes
            if iface.actual_metric != iface.desired_metric:
                try:
                    self.adjust_metric(iface.name, iface.desired_metric)
                except:
                    logger.error("Error occured changing metric : %s", iface.name)
                else:
                    logger.info("%s metric changed : %s", iface.name, iface.desired_metric)
Ejemplo n.º 28
0
 def __init__(self):
     self.bot = TeleBot(settings.TOKEN)
     log.info('delete webhook')
     self.bot.delete_webhook()
     log.info('set webhook')
     self.bot.set_webhook(url=f"{settings.DOMAIN}/" f"{settings.TOKEN}")
     log.info('setup webhook complete')
 def include(self, resource_generator):
     items = []
     for resource in resource_generator:
         logger.debug('include resource {}'.format(resource.arn))
         if resource.access_to:
             items.append({
                 'arn': resource.arn,
                 'policy': self.policy.arn,
                 'sid': resource.statement_id,
                 'actions': resource.actions
             })
             if resource.assumed_role:
                 resource.attach_policy(self.policy.arn)
         if resource.access_from and not resource.assumed_role:
             try:
                 resource.assume_role(self.role.arn)
             except MissingTrustRelationshipError:
                 self.role.update_trust_document(
                     resource.trust_relationship_service)
                 if resource.service_name != 'ec2':
                     resource.assume_role(self.role.arn)
     if len(items) > 0:
         logger.info('Add resources {}'.format(items))
         self.table.add(items)
Ejemplo n.º 30
0
    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("")