Skip to content

Commit

Permalink
feat: add new functions
Browse files Browse the repository at this point in the history
  • Loading branch information
birddevelper committed Apr 20, 2024
1 parent 9a50969 commit 8303519
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,17 @@ Currently these very basic functionalities of the **IoTHubRegistryManager** are

- **create_device_with_sas**
- **get_twin**
- **get_twins**
- **update_twin**
- **get_device**
- **get_devices**
- **delete_device**

The package are designed, so that, if it is used as existing azure-iot-hub alternative in the project, there is no need to make any changes to the codebase.
The package are designed, so that, if it is used as alternative to existing azure-iot-hub in the project, it need to make only little changes to the codebase.

## Usage

Simply import and create IoTHubRegistryManager using connection string, then you can call api functions.

## Sample code

Expand Down
59 changes: 58 additions & 1 deletion azure_iot_hub_api/azure_iot_hub.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import tempfile
from typing import List
from azure.cli.core import get_default_cli

from .models import Device, Twin
Expand Down Expand Up @@ -89,4 +90,60 @@ def delete_device(self, device_id: str, etag=None) -> None:
f"iot hub device-identity delete --device-id {device_id} "
f"--login {self.connection_string} --etag {etag}"
)
self._invoke(cmd)
self._invoke(cmd)



def get_devices(self, max_number_of_devices:int=-1) -> List[Device]:
"""Get the identities of multiple devices from the IoTHub identity
registry.
:param int max_number_of_devices: This parameter when specified, defines the maximum number
of device identities that are returned. Any value outside the range of
1-1000 is considered to be 1000. Default value is -1 which means unlimited.
:returns: List of device info.
"""
cmd = (
f"iot hub device-identity list --top {max_number_of_devices}"
f" --login {self.connection_string}"
)

result = self._invoke(cmd)
devices = [ Device.from_dictionary(device) for device in result ]

return devices


def get_twins(self, max_number_of_device_twins:int=-1) -> List[Twin]:
"""Get the twins of multiple devices from the IoTHub identity
registry.
:param int max_number_of_device_twins: This parameter when specified, defines the maximum number
of device twins that are returned. Any value outside the range of
1-1000 is considered to be 1000. Default value is -1 which means unlimited.
:returns: List of device info.
"""
cmd = (
f"iot hub device-twin list --top {max_number_of_device_twins}"
f" --login {self.connection_string}"
)

result = self._invoke(cmd)
twins = [ Twin.from_dictionary(device) for device in result ]

return twins



def get_device(self, device_id:str) -> Device:
"""Retrieves a device identity from IoTHub.
:param str device_id: The name (Id) of the device.
:returns: The Device object containing the requested device.
"""
cmd = f"iot hub device-identity show --device-id {device_id} --login {self.connection_string}"
result = self._invoke(cmd)
return Device.from_dictionary(result)

0 comments on commit 8303519

Please sign in to comment.