我知道如何从客户端访问属性,但我想从 thingsboard 服务器端访问设备的属性。因此,在开发新的小部件时,我可以显示所有属性的列表并在必要时更改它们。我怎样才能做到这一点?
1711 次
1 回答
4
我们必须非常清楚server-side和client-side。事实上,Thingsboard 小部件在浏览器的客户端运行。
据我了解,我们有两个选项可用于小部件中的属性访问。
- 在小部件的数据源中定义属性访问或
- 使用 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]);
Http-Api 有更多选项,甚至服务提供了一些更方便的功能。在 thingsboard/ui/src/app/api/attribute.service.js 查看服务的来源
注意:这是指低于 3 的 Thingsboard 版本的 ui 模块。
于 2018-11-08T10:03:19.487 回答