繁体   English   中英

Azure 事件中心 - 如何通过 https 从传感器接收数据?

[英]Azure Event Hub - How to receive data from sensor via https?

我有一个传感器可以跟踪进出建筑物的人。 在每次有人进出时,传感器都会通过 HTTPS 将数据发送到我指定的任何 URL 或 IP 地址。 如果我将笔记本电脑设置为监听传入流量的网络服务器,我可以接收数据。

我想要做的是 go 无服务器并使用 Azure 事件中心。 我设置了一个事件中心并通过使用 python 脚本从我的笔记本电脑推送数据来运行测试,我可以成功接收数据。 这是我用来测试的示例代码; 我正在使用连接字符串进行身份验证。

import time
import os
import uuid
import datetime
import random
import json

from azure.eventhub import EventHubProducerClient, EventData

# This script simulates the production of events for 10 devices.
devices = []
for x in range(0, 10):
    devices.append(str(uuid.uuid4()))

# Create a producer client to produce and publish events to the event hub.
producer = EventHubProducerClient.from_connection_string(conn_str="My End Point String", eventhub_name="my eventhub name")
for y in range(0,20):    # For each device, produce 20 events. 
    event_data_batch = producer.create_batch() # Create a batch. You will add events to the batch later. 
    for dev in devices:
        # Create a dummy reading.
        reading = {'id': dev, 'timestamp': str(datetime.datetime.utcnow()), 'uv': random.random(), 'temperature': random.randint(70, 100), 'humidity': random.randint(70, 100)}
        s = json.dumps(reading) # Convert the reading into a JSON string.
        event_data_batch.add(EventData(s)) # Add event data to the batch.
    producer.send_batch(event_data_batch) # Send the batch of events to the event hub.

# Close the producer.    
producer.close()

在我的传感器上,我没有输入这两件事的选项。 我只能选择输入 URL/IP。

知道如何设置吗?

我建议以这种方式移动数据: Sensor > Azure Functions HTTP trigger > Azure Event Hubs

总之,您的传感器将调用托管在 Azure 函数上的 HTTP 端点,并且 function 将在将传感器数据转发到 eventhub 之前对其进行检查和转换。

这是有关 HTTP 触发器的文档 - https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-http-webhook-trigger?tabs=csharp

这是有关事件中心 output 绑定的文档 - https://docs.microsoft.com/en-us/azure/azure-functions/functions-bindings-event-hubs-output?tabs=csharp

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM