UNPKG

13 kBJavaScriptView Raw
1"use strict";
2var __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};
10Object.defineProperty(exports, "__esModule", { value: true });
11var _ = require("lodash");
12var moment = require("moment-timezone");
13var request = require("request-promise");
14var AlarmHelpers = /** @class */ (function () {
15 function AlarmHelpers(config) {
16 /**
17 * Every time an 'event' happens, a 'context' object is sent from Nirovision
18 * containing information pertinent to the event.
19 */
20 this.context = config.context;
21 this.config = this.context.config;
22 this.notificationUri = config.notificationUri;
23 }
24 /**
25 * Returns the webhookId of the event.
26 */
27 AlarmHelpers.prototype.webhookId = function () {
28 return (this.context)
29 ? this.context.webhookId
30 : undefined;
31 };
32 /**
33 * Returns the id of the company.
34 */
35 AlarmHelpers.prototype.companyId = function () {
36 return (this.context && this.context.event)
37 ? this.context.event.companyId
38 : undefined;
39 };
40 /**
41 * Returns all information about the location.
42 */
43 AlarmHelpers.prototype.location = function () {
44 return (this.context)
45 ? this.context.location
46 : undefined;
47 };
48 /**
49 * Returns the name of the location.
50 */
51 AlarmHelpers.prototype.locationName = function () {
52 return (this.context && this.context.location)
53 ? this.context.location.name
54 : undefined;
55 };
56 /**
57 * Returns the id of the location.
58 */
59 AlarmHelpers.prototype.locationId = function () {
60 return (this.context && this.context.location)
61 ? this.context.location.id
62 : undefined;
63 };
64 /**
65 * Returns the timezone of the location.
66 */
67 AlarmHelpers.prototype.timezone = function () {
68 return (this.context && this.context.location)
69 ? this.context.location.timezone
70 : '';
71 };
72 /**
73 * Returns all information about the camera.
74 */
75 AlarmHelpers.prototype.camera = function () {
76 return (this.context)
77 ? this.context.camera
78 : undefined;
79 };
80 /**
81 * Returns the name of the camera.
82 */
83 AlarmHelpers.prototype.cameraName = function () {
84 return (this.context && this.context.camera)
85 ? this.context.camera.name
86 : undefined;
87 };
88 /**
89 * Returns the id of the camera.
90 */
91 AlarmHelpers.prototype.cameraId = function () {
92 return (this.context && this.context.camera)
93 ? this.context.camera.id
94 : undefined;
95 };
96 /**
97 * Returns all information about the geofence.
98 */
99 AlarmHelpers.prototype.geofence = function () {
100 return (this.context)
101 ? this.context.geofence
102 : undefined;
103 };
104 /**
105 * Returns the status of the geofence.
106 */
107 AlarmHelpers.prototype.geofenceStatus = function () {
108 return (this.context && this.context.geofence)
109 ? this.context.geofence.status
110 : undefined;
111 };
112 /**
113 * Returns all information about each user on the geofence.
114 */
115 AlarmHelpers.prototype.geofenceUsers = function () {
116 return (this.context && this.context.geofence)
117 ? this.context.geofence.users
118 : undefined;
119 };
120 /**
121 * Returns the presence of each user on the geofence.
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 * Returns the device of each user on the geofence.
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 * Returns all information about each user on the company.
142 */
143 AlarmHelpers.prototype.users = function () {
144 return (this.context)
145 ? this.context.users
146 : undefined;
147 };
148 /**
149 * Returns the id of each user on the company.
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 * Returns the name of each user on the company.
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 * Returns the email of each user on the company.
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 * Returns the eventId of the event.
180 */
181 AlarmHelpers.prototype.eventId = function () {
182 return (this.context && this.context.event)
183 ? this.context.event.id
184 : undefined;
185 };
186 /**
187 * Returns all information about the event.
188 */
189 AlarmHelpers.prototype.event = function () {
190 return (this.context)
191 ? this.context.event
192 : undefined;
193 };
194 /**
195 * Returns the id of the identity associated with the event if it exists.
196 */
197 AlarmHelpers.prototype.identityId = function () {
198 return (this.context && this.context.identity)
199 ? this.context.identity.id
200 : undefined;
201 };
202 /**
203 * Returns the identity name associated with the event if it exists.
204 */
205 AlarmHelpers.prototype.identityName = function () {
206 return (this.context && this.context.identity)
207 ? this.context.identity.name
208 : undefined;
209 };
210 /**
211 * Returns the topic of the event.
212 */
213 AlarmHelpers.prototype.topic = function () {
214 return (this.context && this.context.event)
215 ? this.context.event.topic
216 : undefined;
217 };
218 /**
219 * Returns the timestamp of the event.
220 */
221 AlarmHelpers.prototype.timestamp = function () {
222 return (this.context && this.context.event)
223 ? this.context.event.timestamp
224 : undefined;
225 };
226 /**
227 * Returns the tagged feature of the event if it exists.
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 * Returns the bounding box of the event if it exists.
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 * Returns the image url of the event.
244 */
245 AlarmHelpers.prototype.image = function () {
246 return (this.context)
247 ? this.context.imageUrl
248 : undefined;
249 };
250 /**
251 * Checks if the the event was tagged with a 'face' feature.
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 * Checks if the the event was tagged with a 'person' feature.
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 * Checks if the the event was tagged with a 'car' feature.
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 * Checks if the the event was tagged with a 'person' feature and that the confidence
276 * was greater than the input confidence.
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 * Checks whether the location id in the alarm configuration matches the location id in
285 * the event.
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 * Checks whether the geofence status in the alarm configuration matches the geofence status
294 * in the event.
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 * Checks whether the camera(s) id(s) in the alarm configuration matches the camera id
303 * in the event.
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 * Checks whether the feature in the alarm configuration matches the feature in the event.
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 * Checks whether the identity(s) id(s) in the alarm configuration matches the identity id
320 * in the event.
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 * Gets the current time in military time and the current day and checks it against the
329 * schedule that may be specified in the alarm configuration. This can be useful when
330 * choosing whether to trigger a script based on the schedule.
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 * Formats the timestamp sent in the event to be ISO8601 format.
348 */
349 AlarmHelpers.prototype.formattedTimestamp = function () {
350 return moment(this.timestamp()).tz(this.timezone()).toISOString(true);
351 };
352 /**
353 * Sends a notification to the Nirovision notification service or another specified service.
354 * Generally this function will always want to be used as the final step of the script, after
355 * the conditions of the script have been met and the desired notification has been constructed.
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}());
372exports.default = AlarmHelpers;