Skip to content

Commit

Permalink
call direct method
Browse files Browse the repository at this point in the history
  • Loading branch information
ks6088ts committed Oct 18, 2024
1 parent 2702aef commit 5604cde
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 1 deletion.
2 changes: 2 additions & 0 deletions iot_hub.env.template
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
IOT_HUB_DEVICE_CONNECTION_STRING="HostName=CHANGE_ME.azure-devices.net;DeviceId=CHANGE_ME;SharedAccessKey=CHANGE_ME"
IOT_HUB_DEVICE_ID="CHANGE_ME"
IOT_HUB_CONNECTION_STRING="HostName=CHANGE_ME.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey=CHANGE_ME"
106 changes: 105 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ azure-iot-device = "^2.14.0"
openai = "^1.51.2"
azure-storage-blob = "^12.23.1"
opencv-python-headless = "^4.10.0.84"
azure-iot-hub = "^2.6.1"

[tool.poetry.group.dev.dependencies]
pre-commit = "^4.0.1"
Expand Down
15 changes: 15 additions & 0 deletions workshop_azure_iot/internals/iot_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from logging import getLogger

from azure.iot.device.aio import IoTHubDeviceClient
from azure.iot.hub import IoTHubRegistryManager
from azure.iot.hub.models import CloudToDeviceMethod

from workshop_azure_iot.settings.iot_hub import Settings

Expand Down Expand Up @@ -42,3 +44,16 @@ async def patch_device_twin(self, reported_properties: dict):
# https://learn.microsoft.com/azure/iot-hub/how-to-device-twins?pivots=programming-language-python#patch-reported-device-twin-properties
await self.connect()
await self.device_client.patch_twin_reported_properties(reported_properties_patch=reported_properties)

async def call_direct_method(self, method_name: str, payload: dict):
# https://learn.microsoft.com/ja-jp/azure/iot-hub/device-management-python#create-a-device-app-with-a-direct-method
try:
registry_manager = IoTHubRegistryManager(connection_string=self.settings.iot_hub_connection_string)
deviceMethod = CloudToDeviceMethod(method_name=method_name, payload=payload)
return registry_manager.invoke_device_method(
device_id=self.settings.iot_hub_device_id,
direct_method_request=deviceMethod,
)
except Exception as e:
logger.error(f"Failed to connect to IoT Hub: {e}")
return {"error": f"Failed to connect to IoT Hub: {e}"}
14 changes: 14 additions & 0 deletions workshop_azure_iot/routers/iot_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,17 @@ async def patch_device_twin(reported_properties: dict):
status_code=status.HTTP_200_OK,
content=await client.patch_device_twin(reported_properties),
)


@router.post("/call_direct_method")
async def call_direct_method(
method_name: str,
payload: dict,
):
return JSONResponse(
status_code=status.HTTP_200_OK,
content=await client.call_direct_method(
method_name=method_name,
payload=payload,
),
)
2 changes: 2 additions & 0 deletions workshop_azure_iot/settings/iot_hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@

class Settings(BaseSettings):
iot_hub_device_connection_string: str
iot_hub_connection_string: str
iot_hub_device_id: str
model_config = SettingsConfigDict(env_file="iot_hub.env")

0 comments on commit 5604cde

Please sign in to comment.