-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
195 lines (171 loc) · 6.39 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
var express = require('express');
var app = express();
var server = require('http').createServer(app);
var bodyParser = require('body-parser');
var path = require('path');
var config = {}; try {config=require('./config.json')} catch(err){};
var port = process.env.PORT || config.port || 3000;
const Blink = require('./blink.js');
var blink = new Blink(config.email,config.password,config.unique_id,config.account_id,config.client_id,config.authtoken,config.region_tier,config.accepted_age_of_data_in_seconds);
server.listen(port, function () { console.log('\x1b[44m SERVER LISTENING ON PORT '+port+' \x1b[0m'); blink_init(); });
process.on('SIGINT', function(){ if (config.SIGINT==undefined) {config.SIGINT=true; console.log('SIGINT'); process.exit(0);} });
process.on('SIGTERM', function(){ if (config.SIGTERM==undefined) {config.SIGTERM=true; console.log('SIGTERM'); process.exit(0);} });
app.use(bodyParser.json({ strict: true }));
app.use(function (error, req, res, next){next()}); // don't show error-message, if it's not JSON ... just ignore it
app.use(bodyParser.urlencoded({ extended: true }));
app.use('(/blink)?/', express.static(__dirname + '/public'));
app.use('(/blink)?/api', function(req,res) {
//res.header("Access-Control-Allow-Origin", "*");
//res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
//console.log('>>> '+req.params.file+' > '+JSON.stringify(req.body));
switch (req.body.command) {
case 'login':
if (req.body.email) {blink.email=req.body.email};
if (req.body.password) {blink.password=req.body.password};
blink.LOGIN((r)=>{
res.set('Content-Type','application/json'); res.end(r);
});
break;
case 'verify':
blink.VERIFY(req.body.pin,(r)=>{
res.set('Content-Type','application/json'); res.end(r);
});
break;
case 'logout':
blink.LOGOUT((r)=>{
res.set('Content-Type','application/json'); res.end(r);
});
break;
case 'get_index':
blink.GET_INDEX((r)=>{
res.set('Content-Type','application/json'); res.end(r);
},req.body.force);
break;
case 'update_cam':
blink.UPDATE_CAM(req.body.cam_id,undefined,(r)=>{
res.set('Content-Type','application/json'); res.end(r);
});
break;
case 'capture_video':
blink.CAPTURE_VIDEO(req.body.cam_id,undefined,(r)=>{
res.set('Content-Type','application/json'); res.end(r);
});
break;
case 'get_command_status':
blink.GET_COMMAND_STATUS(req.body.cam_id,req.body.command_id,(r)=>{
res.set('Content-Type','application/json'); res.end(r);
});
break;
case 'get_log':
res.set('Content-Type','text/plain');
res.end(blink.log.read());
break;
default:
res.end('{"error":"unknown api-call"}');
break;
}
});
app.use('(/blink)?/cam/:id?', function(req,res) {
blink.GET_IMAGE(req.params.id,(r)=>{
if (r) {
res.set('Content-Type','image/jpeg');
res.end(r,'binary');
} else {res.end('HELLO. THIS IS BLINK.\nI DON\'T SEE THIS CAM HERE.')}
});
});
app.use('(/blink)?/media/thumbnail/:media_id?', function(req,res) {
blink.GET_MEDIA_THUMBNAIL(req.params.media_id||undefined,(r)=>{
if (r) {
res.set('Content-Type','image/jpeg');
res.end(r,'binary');
} else {res.end('HELLO. THIS IS BLINK.\nI DON\'T SEE A THUMBNAIL HERE.')}
});
});
app.use('(/blink)?/media/:media_id?', function(req,res) {
blink.GET_MEDIA(req.params.media_id||undefined,(r)=>{
if (r) {
res.set('Content-Type','video/mp4');
res.end(r,'binary');
} else {res.end('HELLO. THIS IS BLINK.\nI DON\'T SEE MEDIA.MP4 HERE.')}
});
});
app.use('(/blink)?/:file?', function(req,res) {
switch (req.params.file) {
case 'cam.jpg':
blink.GET_IMAGE(undefined,(r)=>{
if (r) {
res.set('Content-Type','image/jpeg');
res.end(r,'binary');
} else {res.end('HELLO. THIS IS BLINK.\nI DON\'T SEE CAM.JPG HERE.')}
});
break;
case 'media.mp4':
blink.GET_MEDIA(undefined,(r)=>{
if (r) {
res.set('Content-Type','video/mp4');
res.end(r,'binary');
} else {res.end('HELLO. THIS IS BLINK.\nI DON\'T SEE MEDIA.MP4 HERE.')}
});
break;
//case undefined:
// res.sendFile('index.html',{root:path.join(__dirname,'public')});
// break;
default:
res.sendStatus(404);
break;
}
});
function blink_init() {
console.log('Hello. This is Blink.\nLet\'s start with your credentials.');
prompt('> email : ',blink.email,false,(email)=>{
blink.email=email;
//prompt('> unique_id : ',blink.unique_id,false,(unique_id)=>{
// blink.unique_id=unique_id;
prompt('> password : ',blink.password,true,(password)=>{
blink.password=password;
if ( (password=='') && (blink.account_id&&blink.account_id!='') && (blink.client_id&&blink.client_id!='') && (blink.authtoken&&blink.authtoken!='') && (blink.region_tier&&blink.region_tier!='') ) {
// for testing-purpose: if credentials are present > ready to go without logging in (requesting new authtoken) again
console.log('[TESTING-MODE] resuming with authtoken from config.json '+blink.authtoken);
blink.write_log('>OK RESUMING '+blink.email+' | ACCOUNT_ID '+blink.account_id+' | CLIENT_ID '+blink.client_id+' | AUTHTOKEN '+blink.authtoken+' | REGION_TIER '+blink.region_tier);
blink.GET_INDEX(()=>{});
} else {blink.LOGIN((r)=>{
console.log('Ok. http://localhost:'+port+' is now linked to your Blink-account.');
blink.GET_INDEX(()=>{});
// if verification is required, prompt for PIN (will automatically be sent from Blink by email)
if (blink.client_verification_required) {
console.log('Sorry. Blink requests verification. Please check you email for the required PIN.')
prompt('> PIN : ',undefined,false,(pin)=>{
blink.VERIFY(pin,(r)=>{
console.log('Ok. Thanks for verifying.')
blink.GET_INDEX(()=>{});
});
});
};
})}
})
//});
});
}
function prompt (question,preferred_value,isPassword,callback) {
if ((preferred_value&&preferred_value!='')||config.headless_mode) {
console.log(question+(isPassword?'':preferred_value));
callback(preferred_value);
} else {
let readline = require('readline');
let rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
rl.question(question, function(answer) {
rl.close();
if (isPassword) {process.stdout.write('\n')};
callback(answer);
});
rl._writeToOutput = function _writeToOutput(stringToWrite) {
if (isPassword)
rl.output.write("*");
else
rl.output.write(stringToWrite);
};
};
};