How do I setup the Azure Function to update digital twins

Viewed 469

I am using Visual Studio Code and python. I downloaded the Azure Functions extension for Visual Studio Code and created a local project. I edited the init.py file to process a sample telemetry message and extract person count and I added codes to publish telemetry messages for a digital twin (from SDK for Azure Digital Twins API in python). However, after I deploy the function and try to connect it to the IoT Hub, Azure cannot find the function inside the function app (see snapshot).

Here is my code for __init__.py:

import logging
import azure.functions as func
from azure.identity import DefaultAzureCredential
from azure.digitaltwins.core import DigitalTwinsClient


def processHubToDTEvents(telemetry_payload):

    url = "https://ADT-instance-xut.api.wcus.digitaltwins.azure.net"
    credential = DefaultAzureCredential()
    service_client = DigitalTwinsClient(url, credential)

    digita_twin_id = "spaceTwin-1"
    service_client.publish_telemetry(
        digita_twin_id,
        telemetry_payload
    )


def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    req_body = req.get_json()

    detected = req_body.get("body")
    count = 0
    for obj in detected:
        if obj.get('label') == "person":
            count += 1

    telemetry_payload = '{"Occupancy": ' + str(count) + '}'

    processHubToDTEvents(telemetry_payload)

    return func.HttpResponse(telemetry_payload)

I would really appreciate it if someone could explain how I could setup the Azure Function app. Thanks in advance

1 Answers
Related