KAMBLY
KAMBLY

Reputation: 51

How to access attributes from thingsboard serverside

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

Answers (1)

lupz
lupz

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.

  1. define attribute access in the widget's datasource or
  2. access attributes by using the HTTP-Api

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

Related Questions