Ejemplo n.º 1
0
def get_device(
    cmd,
    app_id: str,
    device_id: str,
    token: str,
    central_dns_suffix=CENTRAL_ENDPOINT,
) -> Device:
    """
    Get device info given a device id

    Args:
        cmd: command passed into az
        device_id: unique case-sensitive device id,
        app_id: name of app (used for forming request URL)
        token: (OPTIONAL) authorization token to fetch device details from IoTC.
            MUST INCLUDE type (e.g. 'SharedAccessToken ...', 'Bearer ...')
        central_dns_suffix: {centralDnsSuffixInPath} as found in docs

    Returns:
        device: dict
    """

    url = "https://{}.{}/{}/{}".format(app_id, central_dns_suffix, BASE_PATH,
                                       device_id)
    headers = _utility.get_headers(token, cmd)

    response = requests.get(url, headers=headers)
    result = _utility.try_extract_result(response)
    return Device(result)
Ejemplo n.º 2
0
 def _create_parser(self, device_template: Template, message: Message,
                    args: CommonParserArguments):
     device_provider = CentralDeviceProvider(cmd=None, app_id=None)
     template_provider = CentralDeviceTemplateProvider(cmd=None,
                                                       app_id=None)
     device_provider.get_device = mock.MagicMock(return_value=Device({}))
     template_provider.get_device_template = mock.MagicMock(
         return_value=device_template)
     return central_parser.CentralParser(
         message=message,
         central_device_provider=device_provider,
         central_template_provider=template_provider,
         common_parser_args=args,
     )
Ejemplo n.º 3
0
def create_device(
    cmd,
    app_id: str,
    device_id: str,
    device_name: str,
    instance_of: str,
    simulated: bool,
    token: str,
    central_dns_suffix=CENTRAL_ENDPOINT,
) -> Device:
    """
    Create a device in IoTC

    Args:
        cmd: command passed into az
        app_id: name of app (used for forming request URL)
        device_id: unique case-sensitive device id
        device_name: (non-unique) human readable name for the device
        instance_of: (optional) string that maps to the device_template_id
            of the device template that this device is to be an instance of
        simulated: if IoTC is to simulate data for this device
        token: (OPTIONAL) authorization token to fetch device details from IoTC.
            MUST INCLUDE type (e.g. 'SharedAccessToken ...', 'Bearer ...')
        central_dns_suffix: {centralDnsSuffixInPath} as found in docs

    Returns:
        device: dict
    """

    if not device_name:
        device_name = device_id

    url = "https://{}.{}/{}/{}".format(app_id, central_dns_suffix, BASE_PATH,
                                       device_id)
    headers = _utility.get_headers(token, cmd, has_json_payload=True)
    payload = {
        "displayName": device_name,
        "simulated": simulated,
        "approved": True,
    }
    if instance_of:
        payload["instanceOf"] = instance_of

    response = requests.put(url, headers=headers, json=payload)
    result = _utility.try_extract_result(response)
    return Device(result)
Ejemplo n.º 4
0
def get_device_registration_summary(
    cmd,
    app_id: str,
    token: str,
    central_dns_suffix=CENTRAL_ENDPOINT,
):
    """
    Get device registration summary for a given app

    Args:
        cmd: command passed into az
        app_id: name of app (used for forming request URL)
        token: (OPTIONAL) authorization token to fetch device details from IoTC.
            MUST INCLUDE type (e.g. 'SharedAccessToken ...', 'Bearer ...')
        central_dns_suffix: {centralDnsSuffixInPath} as found in docs

    Returns:
        registration summary
    """

    registration_summary = {status.value: 0 for status in DeviceStatus}

    url = "https://{}.{}/{}".format(app_id, central_dns_suffix, BASE_PATH)
    headers = _utility.get_headers(token, cmd)
    logger.warning(
        "This command may take a long time to complete if your app contains a lot of devices"
    )
    while url:
        response = requests.get(url, headers=headers)
        result = _utility.try_extract_result(response)

        if "value" not in result:
            raise CLIError("Value is not present in body: {}".format(result))

        for device in result["value"]:
            registration_summary[Device(device).device_status.value] += 1

        print("Processed {} devices...".format(
            sum(registration_summary.values())))
        url = result.get("nextLink")
    return registration_summary
Ejemplo n.º 5
0
def list_devices(
    cmd,
    app_id: str,
    token: str,
    max_pages=1,
    central_dns_suffix=CENTRAL_ENDPOINT,
) -> List[Device]:
    """
    Get a list of all devices in IoTC app

    Args:
        cmd: command passed into az
        app_id: name of app (used for forming request URL)
        token: (OPTIONAL) authorization token to fetch device details from IoTC.
            MUST INCLUDE type (e.g. 'SharedAccessToken ...', 'Bearer ...')
        central_dns_suffix: {centralDnsSuffixInPath} as found in docs

    Returns:
        list of devices
    """

    devices = []

    url = "https://{}.{}/{}".format(app_id, central_dns_suffix, BASE_PATH)
    headers = _utility.get_headers(token, cmd)

    pages_processed = 0
    while (pages_processed <= max_pages) and url:
        response = requests.get(url, headers=headers)
        result = _utility.try_extract_result(response)

        if "value" not in result:
            raise CLIError("Value is not present in body: {}".format(result))

        devices = devices + [Device(device) for device in result["value"]]

        url = result.get("nextLink")
        pages_processed = pages_processed + 1

    return devices