Azure设备twin添加新报告的属性

时间:2018-03-27 19:28:56

标签: node.js azure azure-iot-hub

我正在关注azure设备双胞胎教程https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-csharp-node-twin-how-to-configure

我可以让这项工作更新现有的报告属性。我不确定的是如何添加新报告的属性。

具体来说,代码看起来像代码片段:

 var currentTelemetryConfig = twin.properties.reported.telemetryConfig;
 currentTelemetryConfig.pendingConfig = 
 twin.properties.desired.telemetryConfig;
 currentTelemetryConfig.status = "Pending";

 telemetryConfig: currentTelemetryConfig

 var patch = {
    telemetryConfig: currentTelemetryConfig
 };

 twin.properties.reported.update(patch, function(err) {  
  if (err) {
         console.log(err);
  } else {
    console.log('success');
 }
}

我可以很容易地理解这对现有的属性更新(在这种情况下是telemetryConfig)是如何工作的,但是如果我试图做的改变是一个全新的属性会是什么样子?

如果我在某个时候决定我想要一个名为" favourite_colour" :"蓝色"?

在azure后端我可以添加这个但是如何动态构建var patch变量?

我尝试了这个,但它返回了一个错误:

twin.properties.reported.update(twin.properties.desired, function(err) {
     if (err) {
         console.log('Could not report properties');
     } else {
         console.log('Success');
     }
 });

这就是我的双胞胎的样子:

"properties": {
"desired": {
  "active": true,
  "pws": "xyz",
  "$metadata": {
    "$lastUpdated": "2018-03-27T18:21:57.010036Z",
    "$lastUpdatedVersion": 5,
    "active": {
      "$lastUpdated": "2018-03-27T18:21:57.010036Z",
      "$lastUpdatedVersion": 5
    },
    "pws": {
      "$lastUpdated": "2018-03-27T18:21:57.010036Z",
      "$lastUpdatedVersion": 5
    }
  },
  "$version": 5
},
"reported": {
  "telemetryConfig": 6,
  "$metadata": {
    "$lastUpdated": "2018-03-27T18:56:05.2445399Z",
    "telemetryConfig": {
      "$lastUpdated": "2018-03-27T18:56:05.2445399Z"
    }
  },
  "$version": 5
}

} }

1 个答案:

答案 0 :(得分:0)

我猜你想:

  

在设备端添加新报告的属性(favourite_colour)

我首先建议您在GitHub中阅读此sample。 主要此示例显示了在不同级别上监听双胞胎更新的不同方式。最顶层,设备双胞胎中的任何更新都会触发事件;或特定属性(favourite_color)。

我已编辑您提供的Microsoft文档中的示例以使用 favorite_color

'use strict';
var Client = require('azure-iot-device').Client;
var Protocol = require('azure-iot-device-mqtt').Mqtt;

var connectionString = '{iot hub connection string}';
var client = Client.fromConnectionString(connectionString, Protocol);

var initConfigChange = function(twin, patch) {
    twin.properties.reported.update(patch, function(err) {
        if (err) {
            console.log('Could not report properties');
        } else {
            console.log('Reported pending config change: ' + JSON.stringify(patch));
            setTimeout(function() {completeConfigChange(twin, patch);}, 30000);
        }
    });
}

var completeConfigChange =  function(twin, patch) {
    if (patch.telemetryConfig) {
        // Same as Sample
    } else if (patch.favourite_colour) {
        var currentfavourite_colour = twin.properties.reported.favourite_colour;
        currentfavourite_colour.color = currentfavourite_colour.pendingConfig.color;
        currentfavourite_colour.status = "Success";
        delete currentfavourite_colour.pendingConfig;

        var patch = {
            favourite_colour: currentfavourite_colour
        };    
        patch.favourite_colour.pendingConfig = null;
    }

    twin.properties.reported.update(patch, function(err) {
        if (err) {
            console.error('Error reporting properties: ' + err);
        } else {
            console.log('Reported completed config change: ' + JSON.stringify(patch));
        }
    });
};

client.open(function(err) {
    if (err) {
        console.error('could not open IotHub client');
    } else {
        client.getTwin(function(err, twin) {
            if (err) {
                console.error('could not get twin');
            } else {
                console.log('retrieved device twin');
                twin.properties.reported.favourite_colour = {
                    color: "green"
                }

                twin.on('properties.desired', function(desiredChange) {
                    console.log("received change: "+JSON.stringify(desiredChange));
                    if (desiredChange.telemetryConfig) {
                        // Same as sample
                    } else if (desiredChange.favourite_colour) {

                        var currentfavourite_colour = twin.properties.reported.favourite_colour;
                        currentfavourite_colour.pendingConfig = twin.properties.desired.favourite_colour;
                        currentfavourite_colour.status = "Pending Color";

                        var patch = {
                            favourite_colour: currentfavourite_colour
                        };
                        initConfigChange(twin, patch);
                    }

                });
            }
        });
    }
});

我所做的是使用 if else 语句检查报告的属性;正如我所提到的还有其他方法,请检查我之前提供的GitHub代码。一旦我有匹配的报告属性,我可以以与现有样本相同的方式更新。