-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode_helper.js
151 lines (141 loc) · 6.98 KB
/
node_helper.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
var NodeHelper = require('node_helper');
const request = require('request');
const fs = require('fs');
var xml2js = require('xml2js');
var parser = new xml2js.Parser();
var isActive = 0;
const now = new Date();
var eventDetails_lasttimestamp = Date.now();
var InactiveEpochTime = now.getTime();
//var ImageEpochTime = Math.round(now.getTime() / 1000);
var ImageEpochTime = Date.now();
var resultSet = '';
const imagepath = './modules/MMM-HikVisionMotion/';
let imagedeleteregex = /[.]jfif$/;
module.exports = NodeHelper.create({
start: function() {
console.log("Starting node_helper for module: " + this.name);
this.config = null;
},
checkMotion: function(){
var self = this;
var host = this.config.host;
var port = this.config.port;
var user = this.config.user;
var password = this.config.password;
var imageFreshness = this.config.imageFreshness;
var imageDelete = ((imageFreshness + 60) * 1000);
try {
request
.get('http://' +host+ ':' +port+ '/ISAPI/Event/notification/alertStream')
.auth(user,password,false)
.on('data', (data) => {
if(typeof(data) === 'object') {
//if(typeof(data) != 'undefined') {
console.log(typeof(data));
//if((typeof(data) != 'undefined') && (typeof(data) === 'string')) {
parser.parseString(data, function(err, result) {
try {
if(result && typeof(result) != 'undefined'){
if((typeof(result['EventNotificationAlert']) != 'undefined') && (typeof(result['EventNotificationAlert']['eventType']) != 'undefined') && (typeof(result['EventNotificationAlert']['eventState']) != 'undefined') && (typeof(result['EventNotificationAlert']['activePostCount']) != 'undefined')) {
var code = result['EventNotificationAlert']['eventType'][0]
var action = result['EventNotificationAlert']['eventState'][0]
var count = parseInt(result['EventNotificationAlert']['activePostCount'][0])
if (code === 'VMD' || code === 'linedetection'){
eventDetails_lasttimestamp = Date.now();
if (action === 'active' && isActive === 0){
console.log('Action Start');
isActive = 1;
ImageEpochTime = Date.now();
ImageID = 'camera-' +ImageEpochTime+ '.jfif';
fs.readdirSync(imagepath) //delete old images
.filter(f => (imagedeleteregex.test(f)) && ((new Date().getTime() - new Date(fs.statSync(imagepath + f).mtime).getTime()) > imageDelete))
.map(f => fs.unlinkSync(imagepath + f));
request.get('http://' +host+ ':' +port+ '/ISAPI/Streaming/channels/101/picture').auth(user,password,false).pipe(fs.createWriteStream('./modules/MMM-HikVisionMotion/' +ImageID));
resultSet = 'Motion Detected: ' +ImageEpochTime+ ':' + ImageID;
setTimeout(function(){
var stats = fs.statSync(imagepath + ImageID);
var mtime = stats.mtime;
var emtime = new Date(mtime).getTime()
while (emtime != eutime){
setTimeout(function(){
//pause function to allow time for data change to happen to the file
}, 500);
var stats = fs.statSync(imagepath + ImageID);
var utime = stats.mtime;
var eutime = new Date(utime).getTime()
console.log("mtime: "+emtime+" utime: "+eutime);
}
self.sendSocketNotification("MMM_HIKVISIONMOTION_RESULT_VALUES", resultSet,);
}, 1000);
//setTimeout(function(){
// self.sendSocketNotification("MMM_HIKVISIONMOTION_RESULT_VALUES", resultSet,);
//}, 5000);
};
};
//console.log(((Date.now() - eventDetails_lasttimestamp)/1000) > 2);
if (((Date.now() - eventDetails_lasttimestamp)/1000) > 2) {
isActive = 0;
InactiveEpochTime = Date.now();
resultSet = 'No Motion Detected: ' +InactiveEpochTime;
//console.log(resultSet);
self.sendSocketNotification("MMM_HIKVISIONMOTION_RESULT_VALUES", resultSet,);
};
}
}
}
catch(err) {
console.error("XML Parser Error");
console.error(err);
var resultSet = 'XML Parser Error';
self.sendSocketNotification("MMM_HIKVISIONMOTION_RESULT_VALUES", resultSet,);
self.sendSocketNotification("MMM_HIKVISIONMOTION_ERROR", resultSet,);
}
});
}
})
.on('close', () => {
resultSet = 'ERROR';
self.sendSocketNotification("MMM_HIKVISIONMOTION_ERROR", resultSet,);
})
.on('disconnect', () => {// Try to reconnect after 30s
resultSet = 'ERROR';
self.sendSocketNotification("MMM_HIKVISIONMOTION_ERROR", resultSet,);
})
.on('error', (err) => {
console.log(err);
resultSet = 'ERROR';
self.sendSocketNotification("MMM_HIKVISIONMOTION_ERROR", resultSet,);
});
//self.sendSocketNotification("MMM_HIKVISIONMOTION_RESULT_VALUES", resultSet,);
}
catch (e) {
console.error("Error obtaining data from API");
console.error(e);
var resultSet = 'Error obtaining data from API';
self.sendSocketNotification("MMM_HIKVISIONMOTION_RESULT_VALUES", resultSet,);
self.sendSocketNotification("MMM_HIKVISIONMOTION_ERROR", resultSet,);
}
},
sendImage: function(resultSet) {
var self = this;
self.sendSocketNotification("MMM_HIKVISIONMOTION_RESULT_VALUES", resultSet,);
},
socketNotificationReceived: function(notification, payload) {
//const config = payload.config;
if (notification === "MMM_HIKVISIONMOTION_GET_MOTION") {
try {
this.config = payload;
//console.log(this.config);
this.checkMotion();
}
catch (e) {
console.log("Error obtaining data from API");
console.log(e);
var resultSet = 'Error collecting usage data';
self.sendSocketNotification("MMM_HIKVISIONMOTION_RESULT_VALUES", resultSet,);
self.sendSocketNotification("MMM_HIKVISIONMOTION_ERROR", resultSet,);
}
}
},
});