如何从 Thingsboard 服务器端访问属性

KAM*_*BLY 3 attributes thingsboard

我知道如何从客户端访问属性,但我想从 thingsboard 服务器端访问设备的属性。因此,在开发新的小部件时,我可以显示所有属性的列表,并在必要时更改它们。我怎样才能做到这一点?

lup*_*upz 5

我们必须非常清楚服务器端客户端。事实上,Thingsboard 小部件在浏览器的客户端上运行。

据我了解,我们有两个小部件中的属性访问选项。

  1. 在小部件的数据源中定义属性访问或
  2. 使用 HTTP-Api 访问属性

Thingsboard 提供了一个方便的“服务”来使用 HTTP-Api,我们可以这样使用:

var entityId, attributeKey, myAttribute, attributeService;

entityId         = ... // entity id from the widgets datasource.
attributeKey     = 'MyAttribute';
myAttribute      = {
    key: attributeKey,
    value: 'MyAttributeValue';
};

attributeService = self.ctx.$scope.$injector.get('attributeService');

// Access attributes.
attributeService.getEntityAttributesValues('DEVICE', entityId, 'SERVER_SCOPE', attributeKey)
    .then(function success(attributes) {
        // Use the attribute value.
    });

// Write attributes.
attributeService.saveEntityAttributes(
        'DEVICE', entityId, 'SERVER_SCOPE', [myAttribute]);
Run Code Online (Sandbox Code Playgroud)

Http-Api 有更多选项,甚至该服务还提供了一些更方便的功能。在 thingsboard/ui/src/app/api/attribute.service.js 中 查看该服务的来源

注意:这是指 Thingsboard 版本低于 3 的 ui 模块。