Commit 438c3e67ec4bc9826758b3848b37057a35c4232b

Authored by Igor Kulikov
1 parent ecb5c50d

UI: Add alarm service.

@@ -72,7 +72,7 @@ public class AlarmController extends BaseController { @@ -72,7 +72,7 @@ public class AlarmController extends BaseController {
72 } 72 }
73 } 73 }
74 74
75 - @PreAuthorize("hasAuthority('TENANT_ADMIN')") 75 + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
76 @RequestMapping(value = "/alarm/{alarmId}/ack", method = RequestMethod.POST) 76 @RequestMapping(value = "/alarm/{alarmId}/ack", method = RequestMethod.POST)
77 @ResponseStatus(value = HttpStatus.OK) 77 @ResponseStatus(value = HttpStatus.OK)
78 public void ackAlarm(@PathVariable("alarmId") String strAlarmId) throws ThingsboardException { 78 public void ackAlarm(@PathVariable("alarmId") String strAlarmId) throws ThingsboardException {
@@ -86,7 +86,7 @@ public class AlarmController extends BaseController { @@ -86,7 +86,7 @@ public class AlarmController extends BaseController {
86 } 86 }
87 } 87 }
88 88
89 - @PreAuthorize("hasAuthority('TENANT_ADMIN')") 89 + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN', 'CUSTOMER_USER')")
90 @RequestMapping(value = "/alarm/{alarmId}/clear", method = RequestMethod.POST) 90 @RequestMapping(value = "/alarm/{alarmId}/clear", method = RequestMethod.POST)
91 @ResponseStatus(value = HttpStatus.OK) 91 @ResponseStatus(value = HttpStatus.OK)
92 public void clearAlarm(@PathVariable("alarmId") String strAlarmId) throws ThingsboardException { 92 public void clearAlarm(@PathVariable("alarmId") String strAlarmId) throws ThingsboardException {
  1 +/*
  2 + * Copyright © 2016-2017 The Thingsboard Authors
  3 + *
  4 + * Licensed under the Apache License, Version 2.0 (the "License");
  5 + * you may not use this file except in compliance with the License.
  6 + * You may obtain a copy of the License at
  7 + *
  8 + * http://www.apache.org/licenses/LICENSE-2.0
  9 + *
  10 + * Unless required by applicable law or agreed to in writing, software
  11 + * distributed under the License is distributed on an "AS IS" BASIS,
  12 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13 + * See the License for the specific language governing permissions and
  14 + * limitations under the License.
  15 + */
  16 +export default angular.module('thingsboard.api.alarm', [])
  17 + .factory('alarmService', AlarmService)
  18 + .name;
  19 +
  20 +/*@ngInject*/
  21 +function AlarmService($http, $q, $interval, $filter) {
  22 + var service = {
  23 + getAlarm: getAlarm,
  24 + saveAlarm: saveAlarm,
  25 + ackAlarm: ackAlarm,
  26 + clearAlarm: clearAlarm,
  27 + getAlarms: getAlarms,
  28 + pollAlarms: pollAlarms,
  29 + cancelPollAlarms: cancelPollAlarms
  30 + }
  31 +
  32 + return service;
  33 +
  34 + function getAlarm(alarmId, ignoreErrors, config) {
  35 + var deferred = $q.defer();
  36 + var url = '/api/alarm/' + alarmId;
  37 + if (!config) {
  38 + config = {};
  39 + }
  40 + config = Object.assign(config, { ignoreErrors: ignoreErrors });
  41 + $http.get(url, config).then(function success(response) {
  42 + deferred.resolve(response.data);
  43 + }, function fail() {
  44 + deferred.reject();
  45 + });
  46 + return deferred.promise;
  47 + }
  48 +
  49 + function saveAlarm(alarm, ignoreErrors, config) {
  50 + var deferred = $q.defer();
  51 + var url = '/api/alarm';
  52 + if (!config) {
  53 + config = {};
  54 + }
  55 + config = Object.assign(config, { ignoreErrors: ignoreErrors });
  56 + $http.post(url, alarm, config).then(function success(response) {
  57 + deferred.resolve(response.data);
  58 + }, function fail() {
  59 + deferred.reject();
  60 + });
  61 + return deferred.promise;
  62 + }
  63 +
  64 + function ackAlarm(alarmId, ignoreErrors, config) {
  65 + var deferred = $q.defer();
  66 + var url = '/api/alarm/' + alarmId + '/ack';
  67 + if (!config) {
  68 + config = {};
  69 + }
  70 + config = Object.assign(config, { ignoreErrors: ignoreErrors });
  71 + $http.post(url, null, config).then(function success(response) {
  72 + deferred.resolve(response.data);
  73 + }, function fail() {
  74 + deferred.reject();
  75 + });
  76 + return deferred.promise;
  77 + }
  78 +
  79 + function clearAlarm(alarmId, ignoreErrors, config) {
  80 + var deferred = $q.defer();
  81 + var url = '/api/alarm/' + alarmId + '/clear';
  82 + if (!config) {
  83 + config = {};
  84 + }
  85 + config = Object.assign(config, { ignoreErrors: ignoreErrors });
  86 + $http.post(url, null, config).then(function success(response) {
  87 + deferred.resolve(response.data);
  88 + }, function fail() {
  89 + deferred.reject();
  90 + });
  91 + return deferred.promise;
  92 + }
  93 +
  94 + function getAlarms(entityType, entityId, pageLink, alarmStatus, ascOrder, config) {
  95 + var deferred = $q.defer();
  96 + var url = '/api/alarm/' + entityType + '/' + entityId + '?limit=' + pageLink.limit;
  97 +
  98 + if (angular.isDefined(pageLink.startTime)) {
  99 + url += '&startTime=' + pageLink.startTime;
  100 + }
  101 + if (angular.isDefined(pageLink.endTime)) {
  102 + url += '&endTime=' + pageLink.endTime;
  103 + }
  104 + if (angular.isDefined(pageLink.idOffset)) {
  105 + url += '&offset=' + pageLink.idOffset;
  106 + }
  107 + if (alarmStatus) {
  108 + url += '&status=' + alarmStatus;
  109 + }
  110 + if (angular.isDefined(ascOrder) && ascOrder != null) {
  111 + url += '&ascOrder=' + (ascOrder ? 'true' : 'false');
  112 + }
  113 +
  114 + $http.get(url, config).then(function success(response) {
  115 + deferred.resolve(response.data);
  116 + }, function fail() {
  117 + deferred.reject();
  118 + });
  119 + return deferred.promise;
  120 + }
  121 +
  122 + function fetchAlarms(alarmsQuery, pageLink, deferred, alarmsList) {
  123 + getAlarms(alarmsQuery.entityType, alarmsQuery.entityId,
  124 + pageLink, alarmsQuery.alarmStatus, false, {ignoreLoading: true}).then(
  125 + function success(alarms) {
  126 + if (!alarmsList) {
  127 + alarmsList = [];
  128 + }
  129 + alarmsList = alarmsList.concat(alarms.data);
  130 + if (alarms.hasNext && !alarmsQuery.limit) {
  131 + fetchAlarms(alarmsQuery, alarms.nextPageLink, deferred, alarmsList);
  132 + } else {
  133 + alarmsList = $filter('orderBy')(alarmsList, ['-createdTime']);
  134 + deferred.resolve(alarmsList);
  135 + }
  136 + },
  137 + function fail() {
  138 + deferred.reject();
  139 + }
  140 + );
  141 + }
  142 +
  143 + function getAlarmsByQuery(alarmsQuery) {
  144 + var deferred = $q.defer();
  145 + var time = Date.now();
  146 + var pageLink;
  147 + if (alarmsQuery.limit) {
  148 + pageLink = {
  149 + limit: alarmsQuery.limit
  150 + };
  151 + } else {
  152 + pageLink = {
  153 + limit: 100,
  154 + startTime: time - alarmsQuery.interval
  155 + };
  156 + }
  157 + fetchAlarms(alarmsQuery, pageLink, deferred);
  158 + return deferred.promise;
  159 + }
  160 +
  161 + function onPollAlarms(alarmsQuery) {
  162 + getAlarmsByQuery(alarmsQuery).then(
  163 + function success(alarms) {
  164 + alarmsQuery.onAlarms(alarms);
  165 + },
  166 + function fail() {}
  167 + );
  168 + }
  169 +
  170 + function pollAlarms(entityType, entityId, alarmStatus, interval, limit, pollingInterval, onAlarms) {
  171 + var alarmsQuery = {
  172 + entityType: entityType,
  173 + entityId: entityId,
  174 + alarmStatus: alarmStatus,
  175 + interval: interval,
  176 + limit: limit,
  177 + onAlarms: onAlarms
  178 + };
  179 + onPollAlarms(alarmsQuery);
  180 + return $interval(onPollAlarms, pollingInterval, 0, false, alarmsQuery);
  181 + }
  182 +
  183 + function cancelPollAlarms(pollPromise) {
  184 + if (angular.isDefined(pollPromise)) {
  185 + $interval.cancel(pollPromise);
  186 + }
  187 + }
  188 +
  189 +}
@@ -67,6 +67,7 @@ import thingsboardApiEntityRelation from './api/entity-relation.service'; @@ -67,6 +67,7 @@ import thingsboardApiEntityRelation from './api/entity-relation.service';
67 import thingsboardApiAsset from './api/asset.service'; 67 import thingsboardApiAsset from './api/asset.service';
68 import thingsboardApiAttribute from './api/attribute.service'; 68 import thingsboardApiAttribute from './api/attribute.service';
69 import thingsboardApiEntity from './api/entity.service'; 69 import thingsboardApiEntity from './api/entity.service';
  70 +import thingsboardApiAlarm from './api/alarm.service';
70 71
71 import 'typeface-roboto'; 72 import 'typeface-roboto';
72 import 'font-awesome/css/font-awesome.min.css'; 73 import 'font-awesome/css/font-awesome.min.css';
@@ -124,6 +125,7 @@ angular.module('thingsboard', [ @@ -124,6 +125,7 @@ angular.module('thingsboard', [
124 thingsboardApiAsset, 125 thingsboardApiAsset,
125 thingsboardApiAttribute, 126 thingsboardApiAttribute,
126 thingsboardApiEntity, 127 thingsboardApiEntity,
  128 + thingsboardApiAlarm,
127 uiRouter]) 129 uiRouter])
128 .config(AppConfig) 130 .config(AppConfig)
129 .factory('globalInterceptor', GlobalInterceptor) 131 .factory('globalInterceptor', GlobalInterceptor)
@@ -59,6 +59,12 @@ export default angular.module('thingsboard.types', []) @@ -59,6 +59,12 @@ export default angular.module('thingsboard.types', [])
59 name: "aggregation.none" 59 name: "aggregation.none"
60 } 60 }
61 }, 61 },
  62 + alarmStatus: {
  63 + activeUnack: "ACTIVE_UNACK",
  64 + activeAck: "ACTIVE_ACK",
  65 + clearedUnack: "CLEARED_UNACK",
  66 + clearedAck: "CLEARED_ACK"
  67 + },
62 position: { 68 position: {
63 top: { 69 top: {
64 value: "top", 70 value: "top",