Beispiel #1
0
def iot_digitaltwin_properties_list(cmd,
                                    device_id,
                                    source_model,
                                    interface=None,
                                    schema=False,
                                    repo_endpoint=PNP_ENDPOINT,
                                    repo_id=None,
                                    repo_login=None,
                                    hub_name=None,
                                    resource_group_name=None,
                                    login=None):
    result = []
    target_interfaces = []
    source_model = source_model.lower()
    device_interfaces = _iot_digitaltwin_interface_list(
        cmd, device_id, hub_name, resource_group_name, login)
    interface_list = _get_device_default_interface_dict(device_interfaces)
    target_interface = next(
        (item for item in interface_list if item['name'] == interface), None)
    if interface and not target_interface:
        raise CLIError('Target interface is not implemented by the device!')

    if interface:
        target_interfaces.append(target_interface)
    else:
        target_interfaces = interface_list

    for entity in target_interfaces:
        interface_result = {
            'name': entity['name'],
            'urn_id': entity['urn_id'],
            'properties': {}
        }
        interface_properties = []
        found_properties = []
        if source_model == ModelSourceType.device.value.lower():
            found_properties = _device_interface_elements(
                cmd, device_id, entity['urn_id'], INTERFACE_PROPERTY, hub_name,
                resource_group_name, login)
        else:
            if source_model == ModelSourceType.private.value.lower():
                _validate_repository(repo_id, repo_login)
            found_properties = _pnp_interface_elements(cmd, entity['urn_id'],
                                                       INTERFACE_PROPERTY,
                                                       repo_endpoint, repo_id,
                                                       repo_login)
        for prop in found_properties:
            prop.pop('@type', None)
            if schema:
                interface_properties.append(prop)
            else:
                interface_properties.append(prop.get('name'))
        interface_result['properties'] = interface_properties
        result.append(interface_result)
    return {'interfaces': result}
Beispiel #2
0
def iot_digitaltwin_monitor_events(cmd,
                                   device_id,
                                   interface,
                                   source_model,
                                   repo_endpoint=PNP_ENDPOINT,
                                   repo_id=None,
                                   consumer_group='$Default',
                                   timeout=300,
                                   hub_name=None,
                                   resource_group_name=None,
                                   yes=False,
                                   properties=None,
                                   repair=False,
                                   login=None,
                                   repo_login=None):
    source_model = source_model.lower()
    pnp_context = {'enabled': True, 'interface': {}}
    device_interfaces = _iot_digitaltwin_interface_list(
        cmd, device_id, hub_name, resource_group_name, login)
    interface_list = _get_device_default_interface_dict(device_interfaces)

    target_interface = next(
        (k for k in interface_list if k['name'] == interface), None)
    if interface and not target_interface:
        raise CLIError('Target interface is not implemented by the device!')

    pnp_context['interface'][target_interface['urn_id']] = {}
    found_telemetry = []
    if source_model == ModelSourceType.device.value.lower():
        found_telemetry = _device_interface_elements(
            cmd, device_id, target_interface['urn_id'], INTERFACE_TELEMETRY,
            hub_name, resource_group_name, login)
    else:
        if source_model == ModelSourceType.private.value.lower():
            _validate_repository(repo_id, repo_login)
        found_telemetry = _pnp_interface_elements(cmd,
                                                  target_interface['urn_id'],
                                                  INTERFACE_TELEMETRY,
                                                  repo_endpoint, repo_id,
                                                  repo_login)

    for telemetry in found_telemetry:
        telemetry_data = {
            'display': telemetry.get('displayName'),
            'unit': telemetry.get('unit')
        }
        pnp_context['interface'][target_interface['urn_id']][
            telemetry['name']] = telemetry_data

    _iot_hub_monitor_events(cmd=cmd,
                            interface=target_interface['urn_id'],
                            pnp_context=pnp_context,
                            hub_name=hub_name,
                            device_id=device_id,
                            consumer_group=consumer_group,
                            timeout=timeout,
                            enqueued_time=None,
                            resource_group_name=resource_group_name,
                            yes=yes,
                            properties=properties,
                            repair=repair,
                            login=login)
def iot_digitaltwin_command_list(
    cmd,
    device_id,
    source_model,
    interface=None,
    schema=False,
    repo_endpoint=PNP_ENDPOINT,
    repo_id=None,
    repo_login=None,
    hub_name=None,
    resource_group_name=None,
    login=None,
):
    result = []
    target_interfaces = []
    source_model = source_model.lower()
    device_interfaces = _iot_digitaltwin_interface_list(
        cmd, device_id, hub_name, resource_group_name, login)
    interface_list = _get_device_default_interface_dict(device_interfaces)
    target_interface = next(
        (item for item in interface_list if item["name"] == interface), None)
    if interface and not target_interface:
        raise CLIError("Target interface is not implemented by the device!")

    if interface:
        target_interfaces.append(target_interface)
    else:
        target_interfaces = interface_list

    for entity in target_interfaces:
        interface_result = {
            "name": entity["name"],
            "urn_id": entity["urn_id"],
            "commands": {},
        }
        interface_commands = []
        found_commands = []
        if source_model == ModelSourceType.device.value.lower():
            found_commands = _device_interface_elements(
                cmd,
                device_id,
                entity["urn_id"],
                INTERFACE_COMMAND,
                hub_name,
                resource_group_name,
                login,
            )
        else:
            if source_model == ModelSourceType.private.value.lower():
                _validate_repository(repo_id, repo_login)
            found_commands = _pnp_interface_elements(
                cmd,
                entity["urn_id"],
                INTERFACE_COMMAND,
                repo_endpoint,
                repo_id,
                repo_login,
            )
        for command in found_commands:
            command.pop("@type", None)
            if schema:
                interface_commands.append(command)
            else:
                interface_commands.append(command.get("name"))
        interface_result["commands"] = interface_commands
        result.append(interface_result)
    return {"interfaces": result}