forked from ThatcherC/Terrain2STL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
terrainServer.js
88 lines (73 loc) · 2.85 KB
/
terrainServer.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
//Node server for hgt-to-stl program
//Listens on port 8081
const express = require('express');
const bodyParser = require('body-parser');
const fs = require('fs');
const exec = require('child_process').exec;
const config = require('./config');
const queue = require("queue");
const path = require('path');
var app = express();
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.listen(8080);
var counter = 0;
//initialization from https://www.npmjs.com/package/task-queue
var q = queue()
q.concurrency = 2;
q.timeout=20000;
q.autostart = true;
// if NOSTATIC is not set, set up static file serving
if(!process.env.NOSTATIC) {
console.log("Serving static files")
app.use(express.static(__dirname, {index: "terrain2stl.html"}));
}
app.post("/gen",function(req,res){
var b = req.body;
//lat, long, width, height, verticalscale, rot, waterDrop, baseHeight
var fileNum = counter;
var zipname = "./stls/terrain-"+fileNum;
var filename = "./stls/rawmodel-"+fileNum+".stl";
//b.rotation=0;
var command = "./celevstl "+b.lat+" "+b.lng+" "+b.boxWidth/3+" "
+b.boxHeight/3+" "+b.vScale+" "+b.rotation+" "+b.waterDrop+" "
+b.baseHeight+" "+b.boxScale+" "+filename;
command += "; zip -q "+zipname+" "+filename;
/*
var command = "./elevstl "+b.lat+" "+b.lng+" "+b.boxSize/3+" "
+b.boxSize/3+" "+b.vScale+" "+b.rotation+" "+b.waterDrop+" "+b.baseHeight+" "+b.boxScale+" > "+filename;
command += "; zip -q "+zipname+" "+filename;
var cfilename = "./stls/crawmodel-"+fileNum+".stl";
var czipname = "./stls/cterrain-"+fileNum;
command += "; ./celevstl "+b.lat+" "+b.lng+" "+b.boxSize/3+" "
+b.boxSize/3+" "+b.vScale+" "+b.rotation+" "+b.waterDrop+" "+b.baseHeight+" "+b.boxScale;
command += "; mv out.stl "+cfilename;
command += "; zip -q "+czipname+" "+cfilename;
*/
console.log("> Request for "+b.lat+" "+b.lng);
startTime = Date.now()
paramLog = startTime+"\t"+b.lat+"\t"+b.lng+
"\t"+b.boxHeight+"\t"+b.boxWidth+"\t"+b.boxScale+"\t"+
b.vScale+"\t"+b.rotation+"\t"+b.waterDrop+"\t"+b.baseHeight+"\t";
console.log(command);
q.push(function(cb){
exec(command, function(error,stdout,stderr){
console.log(stderr||"STL "+fileNum+ " created");
res.end(String(fileNum));
//res.type("application/zip");
//res.download(zipname+".zip");
logString = paramLog+Date.now()+"\n";
fs.appendFile("logs/params.log", logString,function(err){
if(err) console.log("> Error!: "+err);
});
fs.appendFile("logs/commands.log", command+"\n", function(err){
if(err) console.log("> Error!: "+err);
});
cb();
})});
counter++;
//res.render("preview.ejs",{filename:"/test.stl",width:b.boxSize/3,height:b.boxSize/3});
});
var datetime = new Date();
console.log("terrainServer.js starting at:");
console.log(datetime);