C# Azure IotHub模块';双属性变化检测
我有一个Azure IoHub,其中包含一个边缘设备。在这个edge设备中,我有几个模块在运行,我可以通过使用连接字符串连接到任何单个模块来更改其twin属性 现在,我想让模块在其twin属性发生更改时执行一些操作,但该模块无权访问其连接字符串,并且不应该这样做,因为它不需要连接到自身 模块如何在没有连接字符串的情况下检测其twin属性更改? 我已经学习了本教程,但它使用连接字符串来检测更改: 以下是此模块的代码:C# Azure IotHub模块';双属性变化检测,c#,azure,azure-iot-hub,C#,Azure,Azure Iot Hub,我有一个Azure IoHub,其中包含一个边缘设备。在这个edge设备中,我有几个模块在运行,我可以通过使用连接字符串连接到任何单个模块来更改其twin属性 现在,我想让模块在其twin属性发生更改时执行一些操作,但该模块无权访问其连接字符串,并且不应该这样做,因为它不需要连接到自身 模块如何在没有连接字符串的情况下检测其twin属性更改? 我已经学习了本教程,但它使用连接字符串来检测更改: 以下是此模块的代码: using System; using Microsoft.Azure.Devi
using System;
using Microsoft.Azure.Devices.Client;
using Microsoft.Azure.Devices.Shared;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace DataSyncService
{
class Program
{
private const string ModuleConnectionString = "CONNECTION STRING";
private static ModuleClient Client = null;
static void ConnectionStatusChangeHandler(ConnectionStatus status,
ConnectionStatusChangeReason reason)
{
Console.WriteLine("Connection Status Changed to {0}; the reason is {1}",
status, reason);
}
static void Main(string[] args)
{
Microsoft.Azure.Devices.Client.TransportType transport =
Microsoft.Azure.Devices.Client.TransportType.Amqp;
try
{
Client =
ModuleClient.CreateFromConnectionString(ModuleConnectionString, transport);
Client.SetConnectionStatusChangesHandler(ConnectionStatusChangeHandler);
// I want to set this callback but this requires a client and the client requires
// a connection string.
Client.SetDesiredPropertyUpdateCallbackAsync(OnDesiredPropertyChanged, null).Wait();
Console.WriteLine("Retrieving twin");
var twinTask = Client.GetTwinAsync();
twinTask.Wait();
var twin = twinTask.Result;
Console.WriteLine(JsonConvert.SerializeObject(twin.Properties));
Console.WriteLine("Sending app start time as reported property");
TwinCollection reportedProperties = new TwinCollection();
reportedProperties["DateTimeLastAppLaunch"] = DateTime.Now;
Client.UpdateReportedPropertiesAsync(reportedProperties);
}
catch (AggregateException ex)
{
Console.WriteLine("Error in sample: {0}", ex);
}
Console.WriteLine("Waiting for Events. Press enter to exit...");
Console.ReadLine();
Client.CloseAsync().Wait();
}
private static async Task OnDesiredPropertyChanged(TwinCollection desiredProperties,
object userContext)
{
Console.WriteLine("desired property change:");
Console.WriteLine(JsonConvert.SerializeObject(desiredProperties));
Console.WriteLine("Sending current time as reported property");
TwinCollection reportedProperties = new TwinCollection
{
["DateTimeLastDesiredPropertyChangeReceived"] = DateTime.Now
};
await Client.UpdateReportedPropertiesAsync(reportedProperties).ConfigureAwait(false);
}
}
}
当您在VisualStudio代码中创建一个新模块时,它会为您生成一个模板模块,该模板模块将向您展示它是如何完成的。我将包括以下重要内容:
static async Task Init()
{
MqttTransportSettings mqttSetting = new MqttTransportSettings(TransportType.Mqtt_Tcp_Only);
ITransportSettings[] settings = { mqttSetting };
// Open a connection to the Edge runtime
ModuleClient ioTHubModuleClient = await ModuleClient.CreateFromEnvironmentAsync(settings);
await ioTHubModuleClient.OpenAsync();
Console.WriteLine("IoT Hub module client initialized.");
// Register callback to be called when a message is received by the module
await ioTHubModuleClient.SetInputMessageHandlerAsync("input1", PipeMessage, ioTHubModuleClient);
}
这是因为Azure IoT Edge运行时将创建模块作为Docker容器,连接设置作为环境变量。当您调用时,模块客户端使用这些变量连接到IoT Hub
ModuleClient IoTubModuleClient=等待
ModuleClient.CreateFromEnvironmentAsync(设置)
有一个很好的示例,它还包括侦听双更新