-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp.js
353 lines (323 loc) · 10.9 KB
/
app.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
if (process.env.DEBUG === '1')
{
require('inspector').open(9291, '0.0.0.0', true);
}
'use strict';
const Homey = require('homey');
class blinkApp extends Homey.App {
/**
* onInit is called when the app is initialized.
*/
async onInit() {
this.log('Blink app has been initialized');
// Register Flow-Action-Listener
this._flowActionCreateSnapshot = this.homey.flow.getActionCard('create_snapshot');
this._flowActionCreateSnapshot.registerRunListener(async (args, state) => {
try{
return await args.device.createSnapshot(args);
}
catch(error){
throw error;
}
});
this._flowActionCreateSnapshotStd = this.homey.flow.getActionCard('create_snapshot_std');
this._flowActionCreateSnapshotStd.registerRunListener(async (args, state) => {
try{
await args.device.createSnapshot(args);
return true;
}
catch(error){
throw error;
}
});
this._flowActionCreateVideo = this.homey.flow.getActionCard('create_video');
this._flowActionCreateVideo.registerRunListener(async (args, state) => {
try{
await args.device.createVideo(args);
return true;
}
catch(error){
throw error;
}
});
this._flowActionExportSnapshotSmb = this.homey.flow.getActionCard('export_snapshot_smb');
this._flowActionExportSnapshotSmb.registerRunListener(async (args, state) => {
try{
await this.exportSnapshotSmb(args);
return true;
}
catch(error){
throw error;
}
});
this._flowActionExportSnapshotFtp = this.homey.flow.getActionCard('export_snapshot_ftp');
this._flowActionExportSnapshotFtp.registerRunListener(async (args, state) => {
try{
return await this.exportSnapshotFtp(args);
return true;
}
catch(error){
throw error;
}
});
this._flowActionExportVideoSmb = this.homey.flow.getActionCard('export_video_smb');
this._flowActionExportVideoSmb.registerRunListener(async (args, state) => {
try{
await args.device.exportVideoSmb(args);
return true;
}
catch(error){
throw error;
}
});
this._flowActionExportVideoFtp = this.homey.flow.getActionCard('export_video_ftp');
this._flowActionExportVideoFtp.registerRunListener(async (args, state) => {
try{
await args.device.exportVideoFtp(args);
return true;
}
catch(error){
throw error;
}
});
this._flowActionSetCameraLght = this.homey.flow.getActionCard('set_camera_light');
this._flowActionSetCameraLght.registerRunListener(async (args, state) => {
try{
await args.device.setCameraLight(args.state == 'on' ? true : false);
return true;
}
catch(error){
throw error;
}
});
// Register Flow-Condition-Listener
this._flowConditionLocalUsage = this.homey.flow.getConditionCard("measure_local_usage")
.registerRunListener(async (args, state) => {
return (args.device.getCapabilityValue('measure_local_usage') > args.value);
})
this._flowConditionCloudUsage = this.homey.flow.getConditionCard("measure_cloud_usage")
.registerRunListener(async (args, state) => {
return (args.device.getCapabilityValue('measure_cloud_usage') > args.value);
})
this._flowConditionApiError = this.homey.flow.getConditionCard("alarm_api_error")
.registerRunListener(async (args, state) => {
return (args.device.getCapabilityValue('alarm_api_error'));
})
this._flowConditionApiError = this.homey.flow.getConditionCard("alarm_local_storage_full")
.registerRunListener(async (args, state) => {
return (args.device.getCapabilityValue('alarm_local_storage_full'));
})
this._flowConditionCameraOffline = this.homey.flow.getConditionCard("alarm_camera_offline")
.registerRunListener(async (args, state) => {
return (args.device.getCapabilityValue('alarm_camera_offline'));
})
}
// Device/Driver handling =================================================================
getAccountDevices(){
return this.homey.drivers.getDriver('account').getDevices();
}
getAccountDevice(id){
let devices = this.getAccountDevices();
for (let i=0; i<devices.length; i++){
if (devices[i].getData().id == id){
return devices[i];
}
}
return null;
}
async exportSnapshotSmb(args){
// SMB Export of an image token
let tz = this.homey.clock.getTimezone();
let now = new Date().toLocaleString('en-US',
{
hour12: false,
hourCycle: 'h23',
timeZone: tz,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
day: "2-digit",
month: "2-digit",
year: "numeric"
});
let date = now.split(", ")[0];
date = date.split("/")[2] + "-" + date.split("/")[0] + "-" + date.split("/")[1];
let time = now.split(", ")[1];
time = time.split(":")[0] + "-" + time.split(":")[1] + "-" + time.split(":")[2];
let filename = date + "_" + time;
if (args.camera_name){
filename = filename + "_" + args.camera_name;
}
filename = filename + ".jpg";
this.log("Export Image to SMB: "+args.smb_share+"\\"+filename);
// create an SMB2 instance
try{
// let smb2Client = new smb2({
let smb2Client = new (require('@marsaud/smb2'))({
share: args.smb_share,
domain: '',
username: args.smb_user,
password: args.smb_pw,
autoCloseTimeout : 30
});
let stream = await args.droptoken.getStream();
/*
**********************************************************
Buffer
**********************************************************
*/
let buffer = await this.stream2buffer(stream);
await smb2Client.writeFile(filename, buffer );
await smb2Client.disconnect();
/*
**********************************************************
Stream
**********************************************************
*/
// let writeStream = await smb2Client.createWriteStream(filename);
// writeStream
// .on("close", async () => {
// this.log("SMB-Stream closed");
// await smb2Client.disconnect();
// // writeStream = null;
// // smb2Client = null;
// })
// .on("error", async (error) => {
// this.log("SMB-Stream error: "+ error.message);
// });
// stream.pipe(writeStream);
// let writeStream = await smb2Client.createWriteStream(filename);
// stream.pipe(writeStream)
// .on("finish", async () => {
// this.log("Stream ended");
// })
// .on("close", async () => {
// this.log("SMB-Stream closed");
// // writeStream.end();
// // writeStream.destroy();
// await smb2Client.disconnect();
// // await smb2Client.close();
// // writeStream = null;
// // smb2Client = null;
// })
// .on("error", async (error) => {
// this.log("SMB-Stream error: "+ error.message);
// });
}
catch (error){
this.error("Error writing file " + filename + ": " + error.message);
throw error;
}
}
async exportSnapshotFtp(args){
// FTP Export of an image token
let tz = this.homey.clock.getTimezone();
let now = new Date().toLocaleString('en-US',
{
hour12: false,
hourCycle: 'h23',
timeZone: tz,
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
day: "2-digit",
month: "2-digit",
year: "numeric"
});
let date = now.split(", ")[0];
date = date.split("/")[2] + "-" + date.split("/")[0] + "-" + date.split("/")[1];
let time = now.split(", ")[1];
time = time.split(":")[0] + "-" + time.split(":")[1] + "-" + time.split(":")[2];
let filename = date + "_" + time;
if (args.camera_name){
filename = filename + "_" + args.camera_name;
}
filename = filename + ".jpg";
filename = args.ftp_path + filename;
// create an FTP instance
try{
let stream = await args.droptoken.getStream();
/*
**********************************************************
Buffer
**********************************************************
*/
let buffer = await this.stream2buffer(stream);
await this.exportFtp(args, filename, buffer);
}
catch (error){
this.error("Error writing file " + filename + ": " + error.message);
throw error;
}
}
async exportFtp(args, filename, buffer){
this.log("Export Video to FTP: "+args.ftp_host+":"+args.ftp_port+"\\"+filename);
let secure = false;
let secureOptions = {};
switch (args.ftp_secure){
// FTP or FTP over SSL/TLS connection
case 'tls':
secure = true;
secureOptions = {
rejectUnauthorized: false
}
case 'ftp': // continue here for TLS
return new Promise((resolve, reject) => {
let ftpClient = new (require('ftp'));
ftpClient.on('ready', function() {
ftpClient.put(buffer, filename, function(error) {
if (error) reject(error);
ftpClient.end();
resolve(true);
});
});
ftpClient.on('error', (error) => {
reject(error);
});
ftpClient.connect(
{
host: args.ftp_host,
port: args.ftp_port,
user: args.ftp_user,
password: args.ftp_pw,
secure: secure,
secureOptions: secureOptions,
connTimeout: 10000
}
);
});
// break;
// SFTP connection
case 'sftp':
let sftpClient = new (require('ssh2-sftp-client'));
try{
await sftpClient.connect({
host: args.ftp_host,
port: args.ftp_port,
username: args.ftp_user,
password: args.ftp_pw
});
await sftpClient.put(buffer, filename, );
}
catch(err) {
this.error(err, 'catch error');
throw err;
}
finally{
sftpClient.end();
}
break;
default:
break;
}
}
stream2buffer(stream) {
return new Promise((resolve, reject) => {
const _buf = [];
stream.on("data", (chunk) => _buf.push(chunk));
stream.on("end", () => resolve(Buffer.concat(_buf)));
stream.on("error", (err) => reject(err));
});
}
}
module.exports = blinkApp;