Reputation: 51
I know how to access attributes from the client side but I want to access attributes of a device from the thingsboard server side. So when developing a new widget I can display a list of all attributes and change them if necessary. How can I do that?
Upvotes: 3
Views: 3312
Reputation: 3638
We have to be very clear about server-side and client-side. In fact a Thingsboard widget runs on the client-side in the browser.
As far as I understand we have two options for attribute access in a widget.
Thingsboard provides a handy "service" for using the HTTP-Api that we can use like this:
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]);
There are more options with the Http-Api and even the service provides some more handy functions. Check out the sources of the service at thingsboard/ui/src/app/api/attribute.service.js
NOTE: This refers to the ui modules for Thingsboard Version lower then 3.
Upvotes: 5