-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
111 lines (97 loc) · 2.15 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
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
const Blink = require('node-blink-security');
const BlinkCamera = require('node-blink-security');
const fs = require('fs');
var blink;
var tok;
var https = require('https');
var base_url;
var base_host;
var emailaddress;
var password;
var syncmod;
var cameraID;
var intervalInMillisecs;
var filecount;
const config_data = require('./grabber_config.json');
emailaddress = config_data.emailaddress;
password = config_data.password;
syncmod = config_data.syncmod;
cameraID = unescape(config_data.cameraID);
intervalInMillisecs = config_data.intervalInMillisecs;
filecount = 0;
console.log("Please wait...login in progress...");
blink = new Blink(emailaddress, password);
blink.setupSystem(syncmod)
.then(() =>
{
console.log("Logged in.");
takePicture(cameraID);
fetchStill(cameraID);
setInterval(doIt(cameraID), intervalInMillisecs);
},
(error) =>
{
console.log("Login failed.");
console.log(error);
});
function doIt(cameraID)
{
return function()
{
blink = new Blink(emailaddress, password);
blink.setupSystem(syncmod)
.then(() =>
{
takePicture(cameraID);
fetchStill(cameraID);
},
(error) =>
{
console.log("Login failed.");
})
}
}
function fetchStill(cameraID)
{
bk = new BlinkCamera();
bk = blink._cameras[cameraID];
console.log("Retrieving still image from [" + syncmod + "] " + bk._name);
bk.fetchImageData()
.then(function(resp)
{
filecount=filecount+1;
fs.writeFile("["+filecount+ "-" + syncmod + "-" + bk._name + "]_" + bk._thumb.substring(bk._thumb.lastIndexOf("/")+1), resp, function(err)
{
if(err)
{
console.log(err);
}
console.log("Still saved");
});
}).then(function(json)
{
//console.log('Success:');
})
.catch(function(error)
{
console.log('Error: ', error);
});
}
function takePicture(cameraID)
{
bk = new BlinkCamera();
bk = blink._cameras[cameraID];
console.log("Taking new picture on [" + syncmod + "] " + bk._name);
bk.snapPicture()
.then(function(resp)
{
console.log("Still taken");
}).then(function(json)
{
//console.log('Success:');
})
.catch(function(error)
{
console.log('Error: ', error);
});
}