-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
213 lines (172 loc) · 6.04 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
"use strict";
const express = require("express");
const app = express();
const request = require('request');
const fs = require("fs");
const dotenv = require("dotenv").config();
const debug = process.env.DEBUG === "true";
const { exec } = require("child_process");
const path_to_photos = "./photos";
const path_to_opensfm = "./OpenSfM";
const CALIBRATION_ONLY = true;
let options;
if (!debug) {
options = {
key: fs.readFileSync(process.env.KEY_PATH),
cert: fs.readFileSync(process.env.CERT_PATH)
};
}
const https = require("https").createServer(options, app);
// default -- pingInterval: 1000 * 25, pingTimeout: 1000 * 60
// low latency -- pingInterval: 1000 * 5, pingTimeout: 1000 * 10
let io, http;
const ping_interval = 1000 * 5;
const ping_timeout = 1000 * 10;
const port_http = process.env.PORT_HTTP;
const port_https = process.env.PORT_HTTPS;
const port_ws = process.env.PORT_WS;
const WebSocket = require("ws");
const ws = new WebSocket.Server({ port: port_ws, pingInterval: ping_interval, pingTimeout: ping_timeout }, function() {
console.log("\nNode.js listening on websocket port " + port_ws);
});
if (!debug) {
http = require("http");
http.createServer(function(req, res) {
res.writeHead(301, { "Location": "https://" + req.headers['host'] + req.url });
res.end();
}).listen(port_http);
io = require("socket.io")(https, {
pingInterval: ping_interval,
pingTimeout: ping_timeout
});
} else {
http = require("http").Server(app);
io = require("socket.io")(http, {
pingInterval: ping_interval,
pingTimeout: ping_timeout
});
}
app.use(express.static("public"));
app.get("/", function(req, res) {
res.sendFile(__dirname + "/public/index.html");
});
if (!debug) {
https.listen(port_https, function() {
console.log("\nNode.js listening on https port " + port_https);
});
} else {
http.listen(port_http, function() {
console.log("\nNode.js listening on http port " + port_http);
});
}
deleteImages();
io.on("connection", function(socket) {
console.log("A socket.io user connected.");
socket.on("disconnect", function(event) {
console.log("A socket.io user disconnected.");
});
socket.on("download_files", function(event) {
if (CALIBRATION_ONLY) deleteImages();
let date = Date.now();
for (let i=0; i<event.fileList.length; i++) {
console.log("Downloading file " + (i+1) + " of " + event.fileList.length + "...");
saveBase64(event.fileList[i], "./photos/images/" + event.hostnameList[i] + "_" + date + ".jpg");
if (i === event.fileList.length-1) {
console.log("DOWNLOAD COMPLETE");
//socket.emit("download_complete", "hello");
doOpenSfmBatch();
} else {
console.log("...Received file " + (i+1) + " of " + event.fileList.length + ".");
}
}
});
socket.on("test_run", function(event) {
doOpenSfmBatch();
});
});
ws.on("connection", function(socket) {
console.log("A user connected.");
socket.onclose = function(event) {
console.log("A user disconnected.");
};
socket.onmessage = function(event) {
//
};
});
function deleteImages() {
runCmd("rm " + path_to_photos + "/images/*.jpg");
}
function saveBase64(data, filename) {
let buffer = new Buffer(data, "base64");
fs.writeFileSync(filename, buffer);
}
// https://stackoverflow.com/questions/11944932/how-to-download-a-file-with-node-js-without-using-third-party-libraries
function download(url, dest, cb) {
let file = fs.createWriteStream(dest);
let sendReq = request.get(url);
// verify response code
sendReq.on('response', (response) => {
if (response.statusCode !== 200) {
return cb('Response status was ' + response.statusCode);
}
sendReq.pipe(file);
});
// close() is async, call cb after close completes
file.on('finish', () => file.close(cb));
// check for request errors
sendReq.on('error', (err) => {
fs.unlink(dest);
return cb(err.message);
});
file.on('error', (err) => { // Handle errors
fs.unlink(dest); // Delete the file async. (But we don't check the result)
return cb(err.message);
});
}
function runCmd(cmd) {
exec(cmd, function(err, stdout, stderr) {
if (err) {
console.error(`exec error: ${err}`);
return;
}
console.log(`result: ${stdout}`);
});
}
function doOpenSfm(cmd) {
runCmd(path_to_opensfm + "/bin/" + cmd + " " + path_to_photos);
}
function doOpenSfmBatch() {
console.log ("Running OpenSfM...");
let cmd1 = path_to_opensfm + "/bin/" + "opensfm_run_all" + " " + path_to_photos;
let cmd2 = path_to_opensfm + "/bin/" + "opensfm undistort" + " " + path_to_photos;
let cmd3 = path_to_opensfm + "/bin/" + "opensfm compute_depthmaps" + " " + path_to_photos;
exec(cmd1, function(err, stdout, stderr) {
if (err) {
console.error(`exec error: ${err}`);
return;
}
console.log(`result: ${stdout}`);
exec("cp " + path_to_photos + "/reconstruction.json ./public/");
if (!CALIBRATION_ONLY) {
exec(cmd2, function(err, stdout, stderr) {
if (err) {
console.error(`exec error: ${err}`);
return;
}
console.log(`result: ${stdout}`);
exec(cmd3, function(err, stdout, stderr) {
if (err) {
console.error(`exec error: ${err}`);
return;
}
console.log(`result: ${stdout}`);
console.log("OpenSfM: completed depth maps. (3/3)");
});
console.log("OpenSfM: completed undistortion. (2/3)");
});
console.log("OpenSfM: completed calibration. (1/3)");
} else {
console.log("OpenSfM: completed calibration.");
}
});
}