|
|
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
|
+} |