如何在ThingsBoard服务端访问设备属性?widget开发中的实现方法
Got it, let's walk through how to access and modify device properties on the ThingsBoard server side specifically for your custom widget development—since you already know the client-side approach, we'll focus on the server-facing interactions using ThingsBoard's built-in widget APIs (no raw HTTP calls needed, which is safer and easier).
1. First: Get the Target Device Entity
Before accessing properties, you need to reference the device you're working with. Typically, you'll let users select a device via the widget's configuration panel, then pull that device info from the widget context:
// Inside your widget's controller var targetDevice = $scope.entity; // Auto-injected if you set up an entity selector in widget config var deviceId = targetDevice.id.id; // Extract the device's UUID
2. Fetch Device Properties (Server-Side)
ThingsBoard provides wrapped services to pull different types of properties—static attributes (server/client/shared) and telemetry data.
Fetch Static Attributes (Server/Client/Shared Scopes)
Use EntityService to grab all static attributes in one go:
$scope.thingsboard.entityService.getEntityAttributes(targetDevice.id, ['SERVER_SCOPE', 'CLIENT_SCOPE', 'SHARED_SCOPE']) .then(function(attributes) { // Bind attributes to your widget's view for display $scope.deviceAttrs = attributes; console.log("Server-side attrs:", attributes.SERVER_SCOPE); console.log("Client-side attrs:", attributes.CLIENT_SCOPE); }) .catch(function(error) { console.error("Failed to fetch attributes:", error); // Add user-facing error handling here, like a toast notification });
Fetch Telemetry (Time-Series) Properties
If you need the latest or historical telemetry data, use TimeseriesService:
// Get the latest telemetry values for specific keys $scope.thingsboard.timeseriesService.getLatestTimeseries(targetDevice.id, ['temperature', 'humidity']) .then(function(latestTelemetry) { $scope.latestTelem = latestTelemetry; });
3. Modify Device Properties (Server/Client/Shared)
You can update attributes directly from the widget using the same services—just specify the scope and the new values.
Update Server-Side Attributes
Server-scoped attributes can only be modified via the server, so use EntityService for this:
// Build the object of attributes to update var updatedServerAttrs = { maxTempThreshold: 36, deviceZone: "Office Floor 2" }; $scope.thingsboard.entityService.saveEntityAttributes(targetDevice.id, 'SERVER_SCOPE', updatedServerAttrs) .then(function() { alert("Server attributes updated successfully!"); $scope.refreshAttributes(); // Refresh to show new values }) .catch(function(error) { console.error("Failed to update server attributes:", error); });
Update Client-Side Attributes
Client-scoped attributes can be modified from both device and server—use the same service method but switch the scope:
var updatedClientAttrs = { deviceAlias: "Floor 2 Temp Sensor" }; $scope.thingsboard.entityService.saveEntityAttributes(targetDevice.id, 'CLIENT_SCOPE', updatedClientAttrs) .then(function() { $scope.refreshAttributes(); });
4. Display & Edit Properties in the Widget View
Bind the fetched attributes to your widget's HTML to let users view and edit them. Example markup:
<!-- Server-side Attributes Section --> <h4>Server-Side Properties</h4> <div ng-repeat="(key, value) in deviceAttrs.SERVER_SCOPE"> <label>{{key}}:</label> <input type="text" ng-model="updatedServerAttrs[key]" /> </div> <button ng-click="updateServerAttrs()">Save Changes</button> <!-- Client-side Attributes Section --> <h4>Client-Side Properties</h4> <div ng-repeat="(key, value) in deviceAttrs.CLIENT_SCOPE"> <label>{{key}}:</label> <input type="text" ng-model="updatedClientAttrs[key]" /> </div> <button ng-click="updateClientAttrs()">Save Changes</button>
Then add the corresponding functions to your controller:
$scope.updateServerAttrs = function() { $scope.thingsboard.entityService.saveEntityAttributes(targetDevice.id, 'SERVER_SCOPE', $scope.updatedServerAttrs) .then(function() { $scope.refreshAttributes(); }); }; $scope.refreshAttributes = function() { // Re-fetch attributes to refresh the view $scope.thingsboard.entityService.getEntityAttributes(targetDevice.id, ['SERVER_SCOPE', 'CLIENT_SCOPE']) .then(function(attributes) { $scope.deviceAttrs = attributes; // Initialize edit models with current values $scope.updatedServerAttrs = angular.copy(attributes.SERVER_SCOPE); $scope.updatedClientAttrs = angular.copy(attributes.CLIENT_SCOPE); }); }; // Call on widget load to initialize data $scope.refreshAttributes();
5. Quick Permission Notes
- Make sure the current user has read/write permissions for device attributes (configure this in ThingsBoard's role management)
- Custom widgets need to be created/deployed under tenant admin or system admin roles to access these server-side APIs
内容的提问来源于stack exchange,提问作者KAMBLY

