1

我已按照此链接编写自定义模块。在本教程中,名为tempSensor的模块将数据发送到另一个模块CSharpModule。就教程而言,我成功实现了它。

我想做的是:接收从IoTDeviceIoTEdge Device的遥测数据。

架构: Azure IoTHub 与 IoT 设备和 IoTEdge 设备连接

我尝试过什么:我尝试从使用 connectionString 连接到 ioTEdge 的模拟设备发送遥测数据。

发送数据的代码在这里

        //DeviceClient connected to IoTEdge     
        s_deviceClient = DeviceClient.CreateFromConnectionString(s_connectionString);

        private static async void SendDeviceToCloudMessagesAsync()
        {
            // Initial telemetry values
            double minTemperature = 20;
            double minHumidity = 60;
            Random rand = new Random();

            while (true)
            {
                double currentTemperature = minTemperature + rand.NextDouble() * 15;
                double currentHumidity = minHumidity + rand.NextDouble() * 20;

                // Create JSON message
                var telemetryDataPoint = new
                {
                    temperature = currentTemperature,
                    humidity = currentHumidity
                };
                var messageString = JsonConvert.SerializeObject(telemetryDataPoint);
                var message = new Message(Encoding.ASCII.GetBytes(messageString));

                // Add a custom application property to the message.                    
                message.Properties.Add("temperatureAlert", (currentTemperature > 30) ? "true" : "false");

                // Send the tlemetry message to endpoint output1
                await s_deviceClient.SendEventAsync("ouput1",message);

                //await s_deviceClient.SendEventAsync(message);
                Console.WriteLine("{0} > Sending message: {1}", DateTime.Now, messageString);

                await Task.Delay(s_telemetryInterval * 10000);
            }
        }

IoTEdge 自定义模块代码的接收端在这里...

        static async Task Init()
        {
            AmqpTransportSettings amqpSetting = new AmqpTransportSettings(TransportType.Amqp_Tcp_Only);
            ITransportSettings[] settings = { amqpSetting };

            // Open a connection to the Edge runtime
            ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
            await ioTHubModuleClient.OpenAsync();
            Console.WriteLine("IoT Hub module client initialized.");

        // Register a callback for messages that are received by the module.
            // await ioTHubModuleClient.SetImputMessageHandlerAsync("input1", PipeMessage, iotHubModuleClient);

            // Read the TemperatureThreshold value from the module twin's desired properties
            var moduleTwin = await ioTHubModuleClient.GetTwinAsync();
            var moduleTwinCollection = moduleTwin.Properties.Desired;
            try {
                temperatureThreshold = moduleTwinCollection["iothub-connection-device-id"];
            } catch(ArgumentOutOfRangeException e) {
                Console.WriteLine($"Property TemperatureThreshold not exist: {e.Message}"); 
            }

            // Attach a callback for updates to the module twin's desired properties.
            await ioTHubModuleClient.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertiesUpdate, null);

            // Register a callback for messages that are received by the module
            await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, ioTHubModuleClient);
        }

自定义模块的deployment.template.json 文件中的路由信息​​如下。

 "routes": {
      "aggregationModuleToIoTHub": "FROM /messages/modules/aggregationModule/outputs/* INTO $upstream"           
    }

但问题是从未调用过的回调PipeMessage 。我的理解是 IoTEdge 端点messages/input1没有消息。

4

1 回答 1

0

为确保我理解您的问题,您尝试将代码从IoT Edge外部的 IoT 设备发送到 IoT Edge,然后通过 IoT Edge 中的模块将该数据路由到 IoT 中心?那是对的吗?如果是这种情况,我们将 IoT 设备称为下游或“叶”设备。IoT Edge 需要像文档一样设置为“透明网关”。完成此操作后,您需要将 ;GatewayHostName= 添加到连接字符串的末尾,其中是您在 config.yaml 文件中用作“主机名”参数的名称。

于 2018-09-14T00:27:21.743 回答