/* * Copyright © 2016-2019 The Thingsboard Authors * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ export default angular.module('thingsboard.api.attribute', []) .factory('attributeService', AttributeService) .name; /*@ngInject*/ function AttributeService($http, $q, $filter, types, telemetryWebsocketService) { var entityAttributesSubscriptionMap = {}; var service = { getEntityKeys: getEntityKeys, getEntityTimeseriesValues: getEntityTimeseriesValues, getEntityAttributesValues: getEntityAttributesValues, getEntityAttributes: getEntityAttributes, subscribeForEntityAttributes: subscribeForEntityAttributes, unsubscribeForEntityAttributes: unsubscribeForEntityAttributes, saveEntityAttributes: saveEntityAttributes, deleteEntityAttributes: deleteEntityAttributes, saveEntityTimeseries: saveEntityTimeseries, deleteEntityTimeseries: deleteEntityTimeseries } return service; function getEntityKeys(entityType, entityId, query, type, config) { var deferred = $q.defer(); var url = '/api/plugins/telemetry/' + entityType + '/' + entityId + '/keys/'; if (type === types.dataKeyType.timeseries) { url += 'timeseries'; } else if (type === types.dataKeyType.attribute) { url += 'attributes'; } $http.get(url, config).then(function success(response) { var result = []; if (response.data) { if (query) { var dataKeys = response.data; var lowercaseQuery = angular.lowercase(query); for (var i=0; i -1) { attribute = attributes[index]; } else { attribute = { key: key }; index = attributes.push(attribute)-1; keys[key] = index; } var attrData = data[key][0]; attribute.lastUpdateTs = attrData[0]; attribute.value = attrData[1]; } if (entityAttributesSubscription.subscriptionCallback) { entityAttributesSubscription.subscriptionCallback(attributes); } } } function subscribeForEntityAttributes(entityType, entityId, attributeScope) { var subscriptionId = entityType + entityId + attributeScope; var entityAttributesSubscription = entityAttributesSubscriptionMap[subscriptionId]; if (!entityAttributesSubscription) { var subscriptionCommand = { entityType: entityType, entityId: entityId, scope: attributeScope }; var type = attributeScope === types.latestTelemetry.value ? types.dataKeyType.timeseries : types.dataKeyType.attribute; var subscriber = { subscriptionCommands: [subscriptionCommand], type: type, onData: function (data) { if (data.data) { onSubscriptionData(data.data, subscriptionId); } } }; entityAttributesSubscription = { subscriber: subscriber, attributes: null }; entityAttributesSubscriptionMap[subscriptionId] = entityAttributesSubscription; telemetryWebsocketService.subscribe(subscriber); } return subscriptionId; } function unsubscribeForEntityAttributes(subscriptionId) { var entityAttributesSubscription = entityAttributesSubscriptionMap[subscriptionId]; if (entityAttributesSubscription) { telemetryWebsocketService.unsubscribe(entityAttributesSubscription.subscriber); delete entityAttributesSubscriptionMap[subscriptionId]; } } function saveEntityAttributes(entityType, entityId, attributeScope, attributes, config) { config = config || {}; var deferred = $q.defer(); var attributesData = {}; var deleteAttributes = []; for (var a=0; a 0) { keys += ','; } keys += attributes[i].key; } var url = '/api/plugins/telemetry/' + entityType + '/' + entityId + '/' + attributeScope + '?keys=' + keys; $http.delete(url, config).then(function success() { deferred.resolve(); }, function fail() { deferred.reject(); }); return deferred.promise; } function deleteEntityTimeseries(entityType, entityId, timeseries, config) { config = config || {}; var deferred = $q.defer(); var keys = ''; for (var i = 0; i < timeseries.length; i++) { if (i > 0) { keys += ','; } keys += timeseries[i].key; } var url = '/api/plugins/telemetry/' + entityType + '/' + entityId + '/timeseries/delete' + '?keys=' + keys; $http.delete(url, config).then(function success() { deferred.resolve(); }, function fail() { deferred.reject(); }); return deferred.promise; } }