1 | "use strict";
|
2 | var __assign = (this && this.__assign) || Object.assign || function(t) {
|
3 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
4 | s = arguments[i];
|
5 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
6 | t[p] = s[p];
|
7 | }
|
8 | return t;
|
9 | };
|
10 | Object.defineProperty(exports, "__esModule", { value: true });
|
11 | var _ = require("lodash");
|
12 | var moment = require("moment-timezone");
|
13 | var request = require("request-promise");
|
14 | var AlarmHelpers = (function () {
|
15 | function AlarmHelpers(config) {
|
16 | |
17 |
|
18 |
|
19 |
|
20 | this.context = config.context;
|
21 | this.config = this.context.config;
|
22 | this.notificationUri = config.notificationUri;
|
23 | }
|
24 | |
25 |
|
26 |
|
27 | AlarmHelpers.prototype.webhookId = function () {
|
28 | return (this.context)
|
29 | ? this.context.webhookId
|
30 | : undefined;
|
31 | };
|
32 | |
33 |
|
34 |
|
35 | AlarmHelpers.prototype.companyId = function () {
|
36 | return (this.context && this.context.event)
|
37 | ? this.context.event.companyId
|
38 | : undefined;
|
39 | };
|
40 | |
41 |
|
42 |
|
43 | AlarmHelpers.prototype.location = function () {
|
44 | return (this.context)
|
45 | ? this.context.location
|
46 | : undefined;
|
47 | };
|
48 | |
49 |
|
50 |
|
51 | AlarmHelpers.prototype.locationName = function () {
|
52 | return (this.context && this.context.location)
|
53 | ? this.context.location.name
|
54 | : undefined;
|
55 | };
|
56 | |
57 |
|
58 |
|
59 | AlarmHelpers.prototype.locationId = function () {
|
60 | return (this.context && this.context.location)
|
61 | ? this.context.location.id
|
62 | : undefined;
|
63 | };
|
64 | |
65 |
|
66 |
|
67 | AlarmHelpers.prototype.timezone = function () {
|
68 | return (this.context && this.context.location)
|
69 | ? this.context.location.timezone
|
70 | : '';
|
71 | };
|
72 | |
73 |
|
74 |
|
75 | AlarmHelpers.prototype.camera = function () {
|
76 | return (this.context)
|
77 | ? this.context.camera
|
78 | : undefined;
|
79 | };
|
80 | |
81 |
|
82 |
|
83 | AlarmHelpers.prototype.cameraName = function () {
|
84 | return (this.context && this.context.camera)
|
85 | ? this.context.camera.name
|
86 | : undefined;
|
87 | };
|
88 | |
89 |
|
90 |
|
91 | AlarmHelpers.prototype.cameraId = function () {
|
92 | return (this.context && this.context.camera)
|
93 | ? this.context.camera.id
|
94 | : undefined;
|
95 | };
|
96 | |
97 |
|
98 |
|
99 | AlarmHelpers.prototype.geofence = function () {
|
100 | return (this.context)
|
101 | ? this.context.geofence
|
102 | : undefined;
|
103 | };
|
104 | |
105 |
|
106 |
|
107 | AlarmHelpers.prototype.geofenceStatus = function () {
|
108 | return (this.context && this.context.geofence)
|
109 | ? this.context.geofence.status
|
110 | : undefined;
|
111 | };
|
112 | |
113 |
|
114 |
|
115 | AlarmHelpers.prototype.geofenceUsers = function () {
|
116 | return (this.context && this.context.geofence)
|
117 | ? this.context.geofence.users
|
118 | : undefined;
|
119 | };
|
120 | |
121 |
|
122 |
|
123 | AlarmHelpers.prototype.geofenceUsersPresence = function () {
|
124 | return (this.context && this.context.geofence)
|
125 | ? _.map(this.context.geofence.users, function (geofenceUser) {
|
126 | return geofenceUser.presence;
|
127 | })
|
128 | : undefined;
|
129 | };
|
130 | |
131 |
|
132 |
|
133 | AlarmHelpers.prototype.geofenceUsersDevice = function () {
|
134 | return (this.context && this.context.geofence)
|
135 | ? _.map(this.context.geofence.users, function (geofenceUser) {
|
136 | return geofenceUser.deviceName;
|
137 | })
|
138 | : undefined;
|
139 | };
|
140 | |
141 |
|
142 |
|
143 | AlarmHelpers.prototype.users = function () {
|
144 | return (this.context)
|
145 | ? this.context.users
|
146 | : undefined;
|
147 | };
|
148 | |
149 |
|
150 |
|
151 | AlarmHelpers.prototype.userIds = function () {
|
152 | return (this.context)
|
153 | ? _.map(this.context.users, function (user) {
|
154 | return user.id;
|
155 | })
|
156 | : undefined;
|
157 | };
|
158 | |
159 |
|
160 |
|
161 | AlarmHelpers.prototype.userNames = function () {
|
162 | return (this.context)
|
163 | ? _.map(this.context.users, function (user) {
|
164 | return user.name;
|
165 | })
|
166 | : undefined;
|
167 | };
|
168 | |
169 |
|
170 |
|
171 | AlarmHelpers.prototype.userEmails = function () {
|
172 | return (this.context)
|
173 | ? _.map(this.context.users, function (user) {
|
174 | return user.email;
|
175 | })
|
176 | : undefined;
|
177 | };
|
178 | |
179 |
|
180 |
|
181 | AlarmHelpers.prototype.eventId = function () {
|
182 | return (this.context && this.context.event)
|
183 | ? this.context.event.id
|
184 | : undefined;
|
185 | };
|
186 | |
187 |
|
188 |
|
189 | AlarmHelpers.prototype.event = function () {
|
190 | return (this.context)
|
191 | ? this.context.event
|
192 | : undefined;
|
193 | };
|
194 | |
195 |
|
196 |
|
197 | AlarmHelpers.prototype.identityId = function () {
|
198 | return (this.context && this.context.identity)
|
199 | ? this.context.identity.id
|
200 | : undefined;
|
201 | };
|
202 | |
203 |
|
204 |
|
205 | AlarmHelpers.prototype.identityName = function () {
|
206 | return (this.context && this.context.identity)
|
207 | ? this.context.identity.name
|
208 | : undefined;
|
209 | };
|
210 | |
211 |
|
212 |
|
213 | AlarmHelpers.prototype.topic = function () {
|
214 | return (this.context && this.context.event)
|
215 | ? this.context.event.topic
|
216 | : undefined;
|
217 | };
|
218 | |
219 |
|
220 |
|
221 | AlarmHelpers.prototype.timestamp = function () {
|
222 | return (this.context && this.context.event)
|
223 | ? this.context.event.timestamp
|
224 | : undefined;
|
225 | };
|
226 | |
227 |
|
228 |
|
229 | AlarmHelpers.prototype.feature = function () {
|
230 | return (this.context && this.context.event && this.context.event.data)
|
231 | ? this.context.event.data.feature
|
232 | : undefined;
|
233 | };
|
234 | |
235 |
|
236 |
|
237 | AlarmHelpers.prototype.boundingBox = function () {
|
238 | return (this.context && this.context.event && this.context.event.data)
|
239 | ? this.context.event.data.boundingBox
|
240 | : undefined;
|
241 | };
|
242 | |
243 |
|
244 |
|
245 | AlarmHelpers.prototype.image = function () {
|
246 | return (this.context)
|
247 | ? this.context.imageUrl
|
248 | : undefined;
|
249 | };
|
250 | |
251 |
|
252 |
|
253 | AlarmHelpers.prototype.faceDetected = function () {
|
254 | return (this.context && this.context.event && this.context.event.data)
|
255 | ? this.context.event.data.feature === 'FACE'
|
256 | : false;
|
257 | };
|
258 | |
259 |
|
260 |
|
261 | AlarmHelpers.prototype.personDetected = function () {
|
262 | return (this.context && this.context.event && this.context.event.data)
|
263 | ? this.context.event.data.feature === 'PERSON'
|
264 | : false;
|
265 | };
|
266 | |
267 |
|
268 |
|
269 | AlarmHelpers.prototype.carDetected = function () {
|
270 | return (this.context && this.context.event && this.context.event.data)
|
271 | ? this.context.event.data.feature === 'CAR'
|
272 | : false;
|
273 | };
|
274 | |
275 |
|
276 |
|
277 |
|
278 | AlarmHelpers.prototype.personDetectedWithConfidence = function (confidence) {
|
279 | return (this.context && this.context.event && this.context.event.data)
|
280 | ? this.context.event.data.feature === 'PERSON' && this.context.event.data.confidence >= confidence
|
281 | : false;
|
282 | };
|
283 | |
284 |
|
285 |
|
286 |
|
287 | AlarmHelpers.prototype.matchesLocation = function () {
|
288 | return (this.config && this.config.location && this.config.location.id)
|
289 | ? this.config.location.id === this.locationId()
|
290 | : true;
|
291 | };
|
292 | |
293 |
|
294 |
|
295 |
|
296 | AlarmHelpers.prototype.matchesGeofenceStatus = function () {
|
297 | return (this.config && this.config.location && this.config.location.geofence)
|
298 | ? this.config.location.geofence === this.geofenceStatus()
|
299 | : true;
|
300 | };
|
301 | |
302 |
|
303 |
|
304 |
|
305 | AlarmHelpers.prototype.matchesCameras = function () {
|
306 | return (this.config && this.config.location && this.config.location.cameras)
|
307 | ? _.includes(this.config.location.cameras, this.cameraId())
|
308 | : true;
|
309 | };
|
310 | |
311 |
|
312 |
|
313 | AlarmHelpers.prototype.matchesFeature = function () {
|
314 | return (this.config && this.config.features)
|
315 | ? _.includes(this.config.features, this.feature())
|
316 | : true;
|
317 | };
|
318 | |
319 |
|
320 |
|
321 |
|
322 | AlarmHelpers.prototype.matchesIdentities = function () {
|
323 | return (this.config && this.config.identities)
|
324 | ? _.includes(this.config.identities, this.identityId())
|
325 | : true;
|
326 | };
|
327 | |
328 |
|
329 |
|
330 |
|
331 |
|
332 | AlarmHelpers.prototype.inSchedule = function () {
|
333 | var timeAtTimezone = moment().tz(this.timezone());
|
334 | var currentTime = parseInt(timeAtTimezone.format('HHmm'), 10);
|
335 | var currentDay = timeAtTimezone.format('dddd').toUpperCase();
|
336 | if (this.config && this.config.schedule) {
|
337 | if (this.config.schedule.isInverted) {
|
338 | return !_.includes(_.range(this.config.schedule.start, this.config.schedule.end), currentTime) &&
|
339 | !_.includes(this.config.schedule.days, currentDay);
|
340 | }
|
341 | return _.includes(_.range(this.config.schedule.start, this.config.schedule.end), currentTime) &&
|
342 | _.includes(this.config.schedule.days, currentDay);
|
343 | }
|
344 | return true;
|
345 | };
|
346 | |
347 |
|
348 |
|
349 | AlarmHelpers.prototype.formattedTimestamp = function () {
|
350 | return moment(this.timestamp()).tz(this.timezone()).toISOString(true);
|
351 | };
|
352 | |
353 |
|
354 |
|
355 |
|
356 |
|
357 | AlarmHelpers.prototype.sendNotification = function (notification) {
|
358 | return request({
|
359 | method: 'POST',
|
360 | uri: this.notificationUri ? this.notificationUri : "https://api.nirovision.com/v1/notifications",
|
361 | headers: {
|
362 | 'Authorization': "Bearer " + this.context.accessToken,
|
363 | 'nirovision-event-id': this.context.event.id,
|
364 | },
|
365 | body: __assign({ companyId: this.companyId() }, notification),
|
366 | json: true,
|
367 | timeout: 15000,
|
368 | });
|
369 | };
|
370 | return AlarmHelpers;
|
371 | }());
|
372 | exports.default = AlarmHelpers;
|