获取IoT Edge模块设置

时间:2018-09-17 16:01:08

标签: azure-iot-edge

在Azure门户中,您可以看到“ IoT Edge模块设置”:

enter image description here

如何从C#以编程方式访问此内容?

当我在边缘设备上获取模块列表时,似乎没有选择获取设置的选项吗?

1 个答案:

答案 0 :(得分:1)

您使用Microsoft Azure设备客户端SDK /软件包并通过以下方式对其进行攻击:

var registryManager = RegistryManager.CreateFromConnectionString(connString); //the connection string for your IOT Hub
var moduleTwins= new List<Twin>();
//var query = registryManager.CreateQuery($"SELECT * FROM devices", 100);
var query = registryManager.CreateQuery("SELECT * FROM devices.modules WHERE deviceId='MyEdgeDevice'",100);
while (query.HasMoreResults) {
    var page = await query.GetNextAsTwinAsync();
    moduleTwins.AddRange(page);
}

// this gets rid of all that version/metadata noise in a module/device twin
foreach (var module in moduleTwins) {
    module.Properties.Reported.ClearMetadata();
    module.Properties.Desired.ClearMetadata();
}

现在moduleTwins现在是Twin对象的可枚举列表(它是模块/设备twin的直接表示)。除了有趣之外,还有$ edgeAgent和$ edgeHub孪生子-包含一切-并且在edgeAgent的modules列表中,您会看到更多这些信息在$ edgeAgent的properties.desired.modules区域中找到的每个模块(图像,创建选项,重启策略等)

   "modules": {
      "MyNeatModule": {
        "type": "docker",
        "settings": {
          "image": "myAzureContainerRegistry.io/myneatmodule:amd64.debug",
          "createOptions": "{}"
        },
        "status": "running",
        "restartPolicy": "always",
        "version": "1.0"
      },

,您要查找的所有内容都在$ edgeAgent的properties.reported区域中:

"modules": {
          "SuperNeatAwesomeModule": {
            "exitCode": 0,
            "statusDescription": "running",
            "lastStartTimeUtc": "2018-11-01T18:39:09.8814806Z",
            "lastExitTimeUtc": "2018-11-01T18:05:40.8350456Z",
            "restartCount": 0,
            "lastRestartTimeUtc": "2018-11-01T18:05:40.8350456Z",
            "runtimeStatus": "running",
            "version": "1.0",
            "status": "running",
            "restartPolicy": "always",
            "type": "docker",
            "settings": {
              "image": "blahblahblah.azurecr.io/superneatmodule:0.0.2-amd64.debug",
              "imageHash": "sha256:ladkjsfaldkjfasdflkjasdfljasfljkasflja4",
              "createOptions": "{}",
              "env":{}