-
Notifications
You must be signed in to change notification settings - Fork 0
/
onvif.js
156 lines (126 loc) · 3.35 KB
/
onvif.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
152
153
154
155
156
const request = require('request');
var fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
var cron = require('node-cron');
const { exec } = require("child_process");
mkdirp.sync('/mnt/QNAP');
var nvrs = [{host:'192.168.3.220',title:'220',max:64},{host:'192.168.3.221',title:'221',max:40}];
var cams = [];
var datetitle = timeStamp();
//cron.schedule('*/5 * * * *', function(){
// return cek();
//});
//NAS'a BAGLAN
//------------------
exec("mount -t cifs -o username=admin,password=5ozyurt //172.16.3.1/Snaps /mnt/QNAP", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
//------------------
return cek();
function cek(){
datetitle = timeStamp();
console.log('BASLIYOR __ '+ datetitle);
cams = [];
/// TEST
/*
cams.push({
nvr : '192.168.3.220',
datetitle : datetitle,
channel : 50,
id :'220'+'_'+50
});
return al();
*/
/// TEST
for(var i=0;i<nvrs.length;i++){
for(var j=1;j<=nvrs[i].max;j++){
cams.push({
nvr : nvrs[i].host,
datetitle : datetitle,
channel : j,
id : nvrs[i].title+'_'+j
});
}
}
return al()
}
function kucukDosyalariTemizle(saveDir){
exec("find "+saveDir+"/ -name \"*.jpg\" -type 'f' -size -20k -delete", (error, stdout, stderr) => {
if (error) {
console.log(`error: ${error.message}`);
return;
}
if (stderr) {
console.log(`stderr: ${stderr}`);
return;
}
console.log(`stdout: ${stdout}`);
});
}
function al(){
var saveDir = '/mnt/QNAP'+'/'+timeStampJustDate();
if(cams.length == 0){
kucukDosyalariTemizle(saveDir);
console.log('Komple Bitti 300 Saniye Sonra Başlayacak : '+timeStamp());
return setTimeout(cek,1000*1200);
return false;
}else
console.log(cams.length +' : '+cams[cams.length-1].id);
var kanal = cams.pop();
if (!fs.existsSync(saveDir))
fs.mkdirSync(saveDir);
var writeStream = fs.createWriteStream(path.join(saveDir,kanal.id+'_#_'+kanal.datetitle+'.jpg'));
let snapshotUri = "http://"+kanal.nvr+"/cgi-bin/snapshot.cgi?channel="+kanal.channel+"&loginuse=admin&loginpas=oz5527431";
request({
method: 'GET',
uri: snapshotUri,
gzip: false,
timeout: 20000,
//encoding: 'binary',
auth: {
user: "admin",
pass: "oz5527431",
sendImmediately: false
}
})
.on('response', function (proxyResponse) {
console.log(proxyResponse.headers['content-length']);
})
.on('error',function (error) {
if(error.code === 'ETIMEDOUT' || error.code == 'ESOCKETTIMEDOUT'){
console.log('HATA : ' + cams[cams.length-1].id);
setTimeout(al,15000);
}else
console.log(error);
})
.on('close', function (asd) {
console.log('Done');
return al();
})
.pipe(writeStream)
.on('end', function() {
//process.stdout.write('REPL stream ended.');
});
}
function timeStamp() {
// Create a date object with the current time
var now = new Date();
return now.getFullYear()+"-"+addZeroBefore(now.getMonth()+1)+"-"+addZeroBefore(now.getDate())+" "+addZeroBefore(now.getHours())+'_'+addZeroBefore(now.getMinutes());
}
function timeStampJustDate() {
// Create a date object with the current time
var now = new Date();
return now.getFullYear()+"-"+addZeroBefore(now.getMonth()+1)+"-"+addZeroBefore(now.getDate());
}
function addZeroBefore(n) {
return (n < 10 ? '0' : '') + n;
}