我的上下文涉及 IoT 中心和 IoT 边缘设备以及 Azure IoT 中的自定义 IoT 边缘模块。
我知道每个边缘模块都可以将属性存储在云端的 IoT HUB 中,称为模块孪生(JSON 文档),而且我也知道模块孪生的目的是通过使用加密的连接来处理和传播更改到边缘侧物联网中心。
关于 Module Twin 的安全性,我想知道两件事:
谢谢
c# code
我可以在模块身份孪生中看到Azure IoT的模块孪生。
AFAIK,模块孪生以 JSON 文档的形式存储在边缘设备上。
代码:-
var connectionString = "HostName=sampath23sa454.azure-devices.net;SharedAc";
var registryManager = RegistryManager.CreateFromConnectionString(connectionString);
var edgeDeviceId = "sampathp"; // Replace with the ID of your edge device
var moduleId = "OPCPublisher"; // Replace with the ID of your module
var query = registryManager.CreateQuery($"SELECT * FROM devices.modules WHERE deviceId = '{edgeDeviceId}' AND moduleId = '{moduleId}'", 1);
while (query.HasMoreResults)
{
var page = await query.GetNextAsTwinAsync();
foreach (var twin in page)
{
// Update twin properties and tags
twin.Properties.Desired["sampleProperty"] = "newValue";
twin.Tags = new TwinCollection();
twin.Tags["location"] = "Building A";
twin.Tags["temperature"] = "72F";
// Update twin in the registry
try
{
var updatedTwin = await registryManager.UpdateTwinAsync(twin.DeviceId, twin.ModuleId, twin, twin.ETag);
Console.WriteLine("Twin updated successfully!");
Console.WriteLine($"Device ID: {updatedTwin.DeviceId}");
Console.WriteLine($"Module ID: {updatedTwin.ModuleId}");
Console.WriteLine($"Desired properties: {updatedTwin.Properties.Desired.ToJson()}");
Console.WriteLine($"Reported properties: {updatedTwin.Properties.Reported.ToJson()}");
Console.WriteLine($"Tags: {updatedTwin.Tags.ToJson()}");
}
catch (Exception ex)
{
Console.WriteLine($"Error updating twin: {ex.Message}");
}
}
}
输出:-
其他参考资料:-