Esempio n. 1
0
def run_managed_configuration(group_id):

    request = esperclient.V0CommandRequest(
        enterprise=enterprise_id,
        command_type="GROUP",
        device_type="all",
        groups = [group_id],
        command="UPDATE_DEVICE_CONFIG",
        command_args=command_args
    )

    try:
        # Create a command request
        api_response = api_instance.create_command(enterprise_id, request)
        #print(api_response)

        request_id = api_response.id
        response = api_instance.get_command_request_status(enterprise_id, request_id)
        status = response.results[0]
        #print(status)

        while status.state not in ["Command Success", "Command Failure", "Command TimeOut", "Command Cancelled", "Command Queued"]:
            response = api_instance.get_command_request_status(enterprise_id, request_id)
            status = response.results[0]
            time.sleep(1)
            #print(status)

    except ApiException as e:
        print("Exception when calling CommandsV2Api->create_command: %s\n" % e)
def setAppState(device_id,
                pkg_name,
                appVer=None,
                state="HIDE",
                maxAttempt=Globals.MAX_RETRY):
    pkgName = pkg_name
    if not appVer:
        _, app = getdeviceapps(device_id,
                               createAppList=False,
                               useEnterprise=Globals.USE_ENTERPRISE_APP)
        if app["results"] and "application" in app["results"][0]:
            app = list(
                filter(
                    lambda x: x["application"]["package_name"] == pkg_name,
                    app["results"],
                ))
        else:
            app = list(
                filter(
                    lambda x: x["package_name"] == pkg_name,
                    app["results"],
                ))
        if app:
            app = app[0]
        if "application" in app:
            appVer = app["application"]["version"]["version_code"]
        elif "version_code" in app:
            appVer = app["version_code"]
    if pkgName and appVer:
        args = V0CommandArgs(
            app_state=state,
            app_version=appVer,
            package_name=pkgName,
        )
        args.version_code = appVer
        request = esperclient.V0CommandRequest(
            enterprise=Globals.enterprise_id,
            command_type="DEVICE",
            device_type=Globals.CMD_DEVICE_TYPE,
            devices=[device_id],
            command="SET_APP_STATE",
            command_args=args,
        )
        api_instance = getCommandsApiInstance()
        for attempt in range(maxAttempt):
            try:
                api_response = api_instance.create_command(
                    Globals.enterprise_id, request)
                break
            except Exception as e:
                if attempt == maxAttempt - 1:
                    ApiToolLog().LogError(e)
                    raise e
                time.sleep(1)
        ignoreQueued = False if Globals.REACH_QUEUED_ONLY else True
        return waitForCommandToFinish(Globals.frame,
                                      api_response.id,
                                      ignoreQueue=ignoreQueued)
def toggleKioskMode(
    frame,
    deviceid,
    appToUse,
    isKiosk,
    timeout=Globals.COMMAND_TIMEOUT,
    maxAttempt=Globals.MAX_RETRY,
):
    """Toggles Kiosk Mode On/Off"""
    api_instance = getCommandsApiInstance()
    if isKiosk:
        command_args = esperclient.V0CommandArgs(package_name=appToUse)
    else:
        command_args = {}
    command = esperclient.V0CommandRequest(
        enterprise=Globals.enterprise_id,
        command_type="DEVICE",
        device_type=Globals.CMD_DEVICE_TYPE,
        devices=[deviceid],
        command="SET_KIOSK_APP",
        command_args=command_args,
    )
    api_response = None
    for attempt in range(maxAttempt):
        try:
            api_response = api_instance.create_command(Globals.enterprise_id,
                                                       command)
            break
        except Exception as e:
            if hasattr(e, "body") and "invalid device id" in e.body:
                logBadResponse("create command api", api_response, None)
                return None
            if attempt == maxAttempt - 1:
                ApiToolLog().LogError(e)
                raise e
            time.sleep(Globals.RETRY_SLEEP)
    response = None
    for attempt in range(maxAttempt):
        try:
            response = api_instance.get_command_request_status(
                Globals.enterprise_id, api_response.id)
            break
        except Exception as e:
            if attempt == maxAttempt - 1:
                ApiToolLog().LogError(e)
                raise e
            time.sleep(Globals.RETRY_SLEEP)
    status = response.results[0].state
    ignoreQueued = False if Globals.REACH_QUEUED_ONLY else True
    status = waitForCommandToFinish(frame,
                                    api_response.id,
                                    ignoreQueue=ignoreQueued,
                                    timeout=timeout)
    return status
def executeCommandOnDevice(
    frame,
    command_args,
    schedule=None,
    schedule_type="IMMEDIATE",
    command_type="UPDATE_DEVICE_CONFIG",
    maxAttempt=Globals.MAX_RETRY,
):
    """ Execute a Command on a Device """
    statusList = []
    for deviceToUse in frame.sidePanel.selectedDevicesList:
        request = esperclient.V0CommandRequest(
            enterprise=Globals.enterprise_id,
            command_type="DEVICE",
            device_type=Globals.CMD_DEVICE_TYPE,
            devices=[deviceToUse],
            command=command_type,
            command_args=command_args,
            schedule="IMMEDIATE"
            if command_type != "UPDATE_LATEST_DPC" else "WINDOW",
            schedule_args=schedule,
        )
        api_instance = getCommandsApiInstance()
        api_response = None
        for attempt in range(maxAttempt):
            try:
                api_response = api_instance.create_command(
                    Globals.enterprise_id, request)
                break
            except Exception as e:
                if attempt == maxAttempt - 1:
                    ApiToolLog().LogError(e)
                    raise e
                time.sleep(Globals.RETRY_SLEEP)
        ignoreQueued = False if Globals.REACH_QUEUED_ONLY else True
        last_status = waitForCommandToFinish(frame,
                                             api_response.id,
                                             ignoreQueue=ignoreQueued)
        if hasattr(last_status, "state"):
            entry = {"Device": deviceToUse, "status": last_status.state}
            if hasattr(last_status, "reason"):
                entry["Reason"] = last_status.reason
            statusList.append(entry)
        else:
            statusList.append({"Device": deviceToUse, "Status": last_status})
    return statusList
Esempio n. 5
0
    def push_app_to_device(self, device_guid, app_version):
        command_status, error = False, None
        command_pending = True
        command_args = esperclient.V0CommandArgs(app_version=app_version)
        command_request = esperclient.V0CommandRequest(
            command_type='DEVICE',
            devices=[device_guid],
            command='INSTALL',
            command_args=command_args,
            device_type='all')
        try:
            api_response = self.cmd_api_instance.create_command(
                self.eid, command_request)
            print("Push successful:")
            command_status = True
            command_id = api_response.id
            while command_pending:
                try:
                    # get status list for command request
                    status_api_response = self.cmd_api_instance.get_command_request_status(
                        self.eid, command_id)
                except ApiException as e:
                    print(
                        "Exception when calling CommandsV2Api->get_command_request_status: %s\n"
                        % e)
                    return False, "Failed to get status"

                command_state = status_api_response.results[0].state
                if command_state == 'Command Success':
                    return True, None
                elif command_state == 'Command Failure':
                    return False, "Failed to install app on device"
                elif command_state == 'Command TimeOut':
                    return False, 'No response from device'
                print("Awaiting response from device on command status")

                time.sleep(2)
        except ApiException as e:
            print("Exception when calling run_command: %s\n" % e)
            error_json = json.loads(e.body)
            error = error_json['message']
            error = error.strip()

        return command_status, error
def setdevicename(
    frame,
    deviceid,
    devicename,
    ignoreQueue,
    timeout=Globals.COMMAND_TIMEOUT,
    maxAttempt=Globals.MAX_RETRY,
):
    """Pushes New Name To Name"""
    api_instance = getCommandsApiInstance()
    args = esperclient.V0CommandArgs(device_alias_name=devicename)
    command = esperclient.V0CommandRequest(
        command_type="DEVICE",
        devices=[deviceid],
        command="UPDATE_DEVICE_CONFIG",
        command_args=args,
        device_type=Globals.CMD_DEVICE_TYPE,
    )
    api_response = None
    for attempt in range(maxAttempt):
        try:
            api_response = api_instance.create_command(Globals.enterprise_id,
                                                       command)
            break
        except Exception as e:
            if attempt == maxAttempt - 1:
                ApiToolLog().LogError(e)
                raise e
            time.sleep(Globals.RETRY_SLEEP)
    response = None
    for attempt in range(maxAttempt):
        try:
            response = api_instance.get_command_request_status(
                Globals.enterprise_id, api_response.id)
            break
        except Exception as e:
            if attempt == maxAttempt - 1:
                ApiToolLog().LogError(e)
                raise e
            time.sleep(Globals.RETRY_SLEEP)
    status = response.results[0].state
    status = waitForCommandToFinish(frame, api_response.id, ignoreQueue,
                                    timeout)
    return status