-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
85 lines (74 loc) · 3.27 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//fs = require('fs');
const mqtt = require('mqtt');
const axios = require('axios');
const { v4: uuidv4 } = require('uuid');
// Load configuration
var config = require('/config/config.json');
// Setup MQTT connection
var client = mqtt.connect('mqtt://'+ config.mqtt.host);
client.on('connect', function () {
client.subscribe(config.mqtt.topic +'/#', function (err) {
console.log('Subscribed to MQTT topic: '+ config.mqtt.topic);
});
});
// On receive MQTT message
client.on('message', function (topic, message) {
try {
var topicPath = topic.split('/');
// Skip initial status message
if(topicPath[1] != 'available') {
var camera = topicPath[1];
var mappedCamera = config.cameraMap.find(i => i.frigateName == camera);
// Only handle events from mapped cameras
if(mappedCamera !== undefined) {
// Only handle 'events'
if(topicPath[2] == 'events') {
var eventData = JSON.parse(message);
// Only hanle 'end' event
if(topicPath[3] == 'end') {
// Skip false positives
if(!eventData.false_positive) {
// Gather event data
var event = {
guid: uuidv4(),
cameraId: mappedCamera.nxwId,
name: eventData.label,
startTimeMs: Math.round(eventData.start_time * 1000),
durationMs: Math.round((eventData.end_time - eventData.start_time) * 1000),
tag: 'frigate'
};
// Send data to NX Witness
axios.request({
method: 'get',
url: 'http://'+ config.nxwitness.host +':'+ config.nxwitness.port +'/ec2/bookmarks/add',
auth: {
username: config.nxwitness.username,
password: config.nxwitness.password
},
responseType: 'json',
params: Object.assign({ format: 'json' }, event)
})
.then(function (response) {
console.log('Notified NX Witness of "'+ event.name +'" event on "'+ camera +'"');
})
.catch(function (error) {
console.log('Something went wrong trying to notify NX Witness');
console.log(error);
});
}
else {
console.log('Skipping false positive event from: '+ camera);
}
}
}
}
else {
console.log('Received event from unconfigured camera: '+ camera);
}
}
}
catch (e) {
console.log('Something went wrong');
console.log(e);
}
});