-
Notifications
You must be signed in to change notification settings - Fork 1
/
hikvision.js
144 lines (132 loc) · 5.34 KB
/
hikvision.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
#!/usr/bin/nodejs
var ipcamera = require('node-hikvision-api');
var download = require('image-downloader');
var fs = require('fs');
var gcs = require('@google-cloud/storage')(gcsconfig);
const path = './images/';
// image-downloader options
var imgOptions = {
url : 'http://admin:Iri$h001@192.168.0.101/Streaming/Channels/101/picture',
//url : 'http://www.kanyini.io/wp-content/uploads/2015/08/keys.png',
dest: path + getDateTime() + '_startupPhoto.jpg',
};
// @google-cloud/storage options
var gcsconfig = {
projectId: 'Vision-test-01-9bbd739b0829' ,
keyFilename: './keys/Vision-test-01-9bbd739b0829.json'
};
var bucket = gcs.bucket('cctv001');
// mode-hikvision-api options
var options = {
host : '192.168.0.101',
port : '80',
user : 'admin',
pass : 'Iri$h001',
log : false,
};
const cameraName = 'Glasson_Front';
var hikvision = new ipcamera.hikvision(options);
// Log each application restart
console.log(getDateTime() + ' application restarting..');
getImage();
// Callback on connect
hikvision.on('connect', function(){
console.log(getDateTime() + ' Connected to ' + options.host);
});
// Callback on error
hikvision.on('error', function(error){
console.log(getDateTime() + ' Error ' + error);
});
// Monitor Camera Alarms
hikvision.on('alarm', function(code,action,index) {
if (code === 'VideoMotion' && action === 'Start') {
console.log(getDateTime() + ' Channel ' + index + ': Video Motion Detected');
// Captures 3 sequential images from camera
getImages();
}
if (code === 'VideoMotion' && action === 'Stop') console.log(getDateTime() + ' Channel ' + index + ': Video Motion Ended')
if (code === 'LineDetection' && action === 'Start') console.log(getDateTime() + ' Channel ' + index + ': Line Cross Detected')
if (code === 'LineDetection' && action === 'Stop') console.log(getDateTime() + ' Channel ' + index + ': Line Cross Ended')
if (code === 'AlarmLocal' && action === 'Start') console.log(getDateTime() + ' Channel ' + index + ': Local Alarm Triggered: ' + index)
if (code === 'AlarmLocal' && action === 'Stop') console.log(getDateTime() + ' Channel ' + index + ': Local Alarm Ended: ' + index)
if (code === 'VideoLoss' && action === 'Start') console.log(getDateTime() + ' Channel ' + index + ': Video Lost!')
if (code === 'VideoLoss' && action === 'Stop') console.log(getDateTime() + ' Channel ' + index + ': Video Found!')
if (code === 'VideoBlind' && action === 'Start') console.log(getDateTime() + ' Channel ' + index + ': Video Blind!')
if (code === 'VideoBlind' && action === 'Stop') console.log(getDateTime() + ' Channel ' + index + ': Video Unblind!')
});
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var ms = date.getMilliseconds();
ms = (ms < 10 ? "0" : "") + ms;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + "-" + month + "-" + day + " " + hour + ":" + min + ":" + sec + ":" + ms;
}
function getImages(){
// downloads 3 still images from camera to disk
// the .then () returns a promise
// so subsequent download.image() functions are executed synchronously
// see http://exploringjs.com/es6/ch_promises.html
imgOptions.dest = path + getDateTime() + '_img2.jpg';
download.image(imgOptions)
.then(({ filename }) => {
console.log('File saved to', filename);
storeImage(filename);
imgOptions.dest = path + getDateTime() + '_img2.jpg';
download.image(imgOptions)
.then(({ filename }) => {
console.log('File saved to', filename);
storeImage(filename);
imgOptions.dest = path + getDateTime() + '_img3.jpg';
download.image(imgOptions)
.then(({ filename }) => {
console.log('File saved to', filename);
storeImage(filename);
})
.catch((err) => {
console.log('Error getting file ' + imgOptions.dest);
throw err
})
})
.catch((err) => {
console.log('Error getting file ' + imgOptions.dest);
throw err
})
})
.catch((err) => {
console.log('Error getting file ' + imgOptions.dest);
throw err
})
}
function getImage(){
// downloads 1 still image from camera to disk
imgOptions.dest = path + getDateTime() + ' stillImage.jpg';
download.image(imgOptions)
.then(({ filename }) => {
console.log('File saved to ', filename);
storeImage(filename);
})
.catch((err) => {
console.log('Error getting file ' + imgOptions.dest);
throw err
})
}
function storeImage(filename) {
bucket.upload(filename, function (err, file) {
if (!err) {
console.log(`File: ` + file.name + ` uploaded successfully`);
}
if (err) {
console.log('File upload failed: ' + err);
}
});
}