Skip to content

Commit

Permalink
add iot hub features
Browse files Browse the repository at this point in the history
  • Loading branch information
ks6088ts committed Oct 17, 2024
1 parent c7765e0 commit fdea4e2
Show file tree
Hide file tree
Showing 9 changed files with 173 additions and 1 deletion.
11 changes: 11 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,14 @@ docs-serve: ## serve documentation

.PHONY: ci-test-docs
ci-test-docs: docs ## run CI test for documentation

# ---
# Project
# ---

.PHONY: server
server: ## run server
poetry run uvicorn workshop_azure_iot.core:app \
--host 0.0.0.0 \
--port 8000 \
--reload
1 change: 1 addition & 0 deletions iot_hub.env.template
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
IOT_HUB_DEVICE_CONNECTION_STRING="CHANGE_ME"
91 changes: 90 additions & 1 deletion poetry.lock

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

3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ typer = "^0.12.5"
fastapi = {extras = ["standard"], version = "^0.115.2"}
azure-functions = "^1.21.3"
pydantic-settings = "^2.5.2"
azure-iot-device = "^2.14.0"

[tool.poetry.group.dev.dependencies]
pre-commit = "^4.0.1"
Expand All @@ -33,6 +34,8 @@ build-backend = "poetry.core.masonry.api"
[tool.ruff]
line-length = 120
target-version = "py310"

[tool.ruff.lint]
select = ["E", "F", "I", "UP"]
ignore = ["D203"]

Expand Down
14 changes: 14 additions & 0 deletions tests/test_iot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from logging import getLogger

from tests.utilities import client

logger = getLogger(__name__)


def test_iot():
path_format = "/iot_hub/{0}"
response = client.get(
url=path_format.format("device_twin"),
)
assert response.status_code == 200
logger.info(f"response: {response.json()}")
2 changes: 2 additions & 0 deletions workshop_azure_iot/core.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
from fastapi import FastAPI

from workshop_azure_iot.routers.core import router as core_router
from workshop_azure_iot.routers.iot_hub import router as iot_hub_router

app = FastAPI(
docs_url="/",
)

for router in [
core_router,
iot_hub_router,
# Add routers here
]:
app.include_router(router)
20 changes: 20 additions & 0 deletions workshop_azure_iot/internals/iot_hub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
from logging import getLogger

from azure.iot.device.aio import IoTHubDeviceClient

from workshop_azure_iot.settings.iot_hub import Settings

logger = getLogger(__name__)


class Client:
def __init__(self, settings: Settings) -> None:
self.settings = settings

async def get_device_twin(self) -> dict:
client = IoTHubDeviceClient.create_from_connection_string(self.settings.iot_hub_device_connection_string)
# FIXME: to make it faster, connection should be established once and reused
await client.connect()
twin = await client.get_twin()
await client.shutdown()
return twin
26 changes: 26 additions & 0 deletions workshop_azure_iot/routers/iot_hub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from logging import getLogger

from fastapi import APIRouter, status
from fastapi.responses import JSONResponse

from workshop_azure_iot.internals.iot_hub import Client
from workshop_azure_iot.settings.iot_hub import Settings

logger = getLogger(__name__)

client = Client(
settings=Settings(),
)

router = APIRouter(
prefix="/iot_hub",
tags=["iot_hub"],
)


@router.get("/device_twin")
async def get_device_twin():
return JSONResponse(
status_code=status.HTTP_200_OK,
content=await client.get_device_twin(),
)
6 changes: 6 additions & 0 deletions workshop_azure_iot/settings/iot_hub.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from pydantic_settings import BaseSettings, SettingsConfigDict


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

0 comments on commit fdea4e2

Please sign in to comment.