-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpilapse.js
119 lines (99 loc) · 3.09 KB
/
pilapse.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
const fs = require('fs');
const path = require('path');
const config = require('./config');
const moment = require('moment');
const spawn = require('child_process').spawn;
const Promise = require('bluebird');
const AWS = require('aws-sdk');
AWS.config.loadFromPath(path.join(__dirname,'credentials.json'));
const s3 = new AWS.S3();
function Picture(){
this.filepath;
this.keyname;
this.url;
this.getUrl = function(expires){
expires = expires ? expires : 60;
var params = {Bucket: config.aws_s3_bucket, Key: this.keyname, Expires: expires};
this.url = s3.getSignedUrl('getObject', params);
return this.url;
}
}
var captureProc = null;
function takePicture() {
return new Promise(function(resolve){
if(captureProc != null) {
captureProc.kill();
captureProc = null;
}
var date = moment.utc();
var datestr = date.format('YYYYMMDDHHmmss');
var filename = datestr+".jpg";
var filepath = path.join(config.capture_dir, filename);
var keyname = date.format("YYYY/MM/DD/") + filename;
var args = ["-awb", "auto", "-ex", "auto", "-w", config.capture_width, "-h", config.capture_height, "-vf", "-o", filepath];
console.info(keyname + "\tcapture started")
captureProc = spawn('raspistill', args);
console.info(keyname + "\tcapture complete")
var picture = new Picture();
picture.filepath = filepath;
picture.keyname = keyname;
captureProc.on('close', function(code){
resolve(picture);
});
});
}
function uploadPicture(picture) {
return new Promise(function(resolve, reject){
fs.readFile(picture.filepath, function(err, data) {
if(err) throw err;
var s3bucket = new AWS.S3({params: {Bucket: config.aws_s3_bucket }});
var params = {
Key: picture.keyname,
Body: data
};
console.info(picture.keyname + "\tupload started");
s3bucket.upload(params, function (err, data) {
if (err) {
reject(err);
} else {
console.info(picture.keyname + "\tupload complete");
resolve(picture);
}
});
});
});
}
function deleteFile(picture) {
return new Promise(function(resolve, reject){
fs.unlink(picture.filepath, function(err){
if (err) throw err;
console.info(picture.keyname + "\tdeleted local file\t" + picture.filepath);
resolve(picture);
});
});
}
function capture(){
takePicture()
.then(uploadPicture)
.then(deleteFile)
.then(function(picture){
console.info(picture.keyname + "\turl created\t" + picture.getUrl(60));
});
}
var interval;
function start(){
console.info("process\tstarting timelapse\t" + config.capture_frequency + " seconds");
capture(); // take picture on start
interval = setInterval(function(){
capture();
}, config.capture_frequency * 1000)
}
function stop(){
if(interval) clearInterval(interval);
console.info("process\thalting timelapse");
}
// MAIN :::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
// Let's get this party started, right?
start();
process.on('SIGTERM', stop);
process.on('SIGINT', stop);