Azure 函数事件中心触发 Flutter Web 应用程序

问题描述 投票:0回答:1

我创建了事件中心并从我的 iothub 进行消息路由。现在使用azure函数事件中心触发器正在发生并且能够监视监视器部分中的数据。我希望这些数据发送到 flutter,因为我需要一个 API 端点从 webapp 调用,以便可以将数据传输到,但我只得到了 http 触发器的 URL。不适用于事件中心触发器,我需要调用 URL 还是需要使用 http 触发器?最好的选择是什么?如果有任何替代选择,请建议。

flutter azure azure-functions azure-eventhub
1个回答
0
投票
import azure.functions as func
import logging
import json

app = func.FunctionApp(http_auth_level=func.AuthLevel.FUNCTION)

#@app.route(route="http_trigger")
#def http_trigger(req: func.HttpRequest) -> func.HttpResponse:
#    logging.info('Python HTTP trigger function processed a request.')

#    name = req.params.get('name')
#    if not name:
#        try:
#            req_body = req.get_json()
#        except ValueError:
#            pass
#        else:
#            name = req_body.get('name')
#    if name:
#        return func.HttpResponse(f"Hello, {name}. This HTTP triggered function executed successfully.")
#    else:
#        return func.HttpResponse(
#             "This HTTP triggered function executed successfully. Pass a name in the query string or in the request body for a personalized response.",
#             status_code=200
#        )


@app.event_hub_message_trigger(arg_name="azeventhub", event_hub_name="eventhubiot",
                               connection="AZHUB_events_IOTHUB") 
def eventhub_trigger(azeventhub: func.EventHubEvent)-> func.HttpResponse:
    logging.info('Python EventHub trigger processed an event: %s',
                azeventhub.get_body().decode('utf-8'))
    device_id = None
    temperature = None

    for azeventhub in azeventhub.get_body().decode('utf-8').split('\n'):
        if azeventhub.strip():
            key, value = azeventhub.split(':')
            if key.strip() == 'device_id':
                device_id = value.strip()
            elif key.strip() == 'temperature':
                temperature = value.strip()

    response_body = json.dumps(response_body)

    return func.HttpResponse(response_body, status_code=200, mimetype='application/json')

这是我正在使用的代码,上面的注释是之前创建的http触发器。我在这里使用内置端点来读取数据它工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.