-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
140 lines (110 loc) · 4.98 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
//energyplus.io
//DEPENDENCIES
var app = require('express.io')();
var express = require('express.io'); //Upgrades existing Express functionality
var path = require('path');
//SETUP EXPRESS.IO APP
app.http().io();
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.logger('dev'));
app.use(express.bodyParser());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
app.use(express.directory('public'));
//SHOW FOLDERS & FILES (like Apache)
app.use('/runs', express.directory('../runs', {icons:true}));
app.use('/runs', express.static('../runs'));
express.static.mime.define({'text/plain': ['idf', 'osm', 'epw', 'err', 'idd', 'eio','audit','bnd','end', 'eso','mdd','mtd','mtr','rdd','shd']});
express.static.mime.default_type = "text/plain"; //to render files without an extention, i.e. stdout, stderr
//WEBPAGES
app.get('/', function(req, res){res.render('index');});
app.get('/index', function(req, res){res.render('index');});
app.get('/idf-creator', function(req, res){res.render('idf-creator');});
app.get('/websockets', function(req, res){res.render('websockets');});
//WEBSOCKET EVENTS
app.io.route('ready', function(req) {
req.io.emit('connect', {
message: 'io event connected from an io route on the server'
});
});
app.io.route('execute', function(req) {
var spawn = require('child_process').spawn;
//var file = 'progress.txt';
var command = spawn("ls", ["-al"]);
command.stdout.pipe(process.stdout);
command.stdout.on("data", function (data) {
var line = data.toString();
req.io.emit('new-line', {message: line});
});
});
app.io.route('run', function(req) {
var string;
string = 'EnergyPlus.io starting up on server...';
req.io.emit('form-data', {message: string});
string = 'FORM DATA...';
req.io.emit('form-data', {message: string});
console.log(req.data);
var querystring = require('querystring');
var building = querystring.parse(req.data);
console.log(building);
string = JSON.stringify(building);
req.io.emit('form-data', {message: string});
//CREATE unique simulation Name & Timestamp ID
string = "Creating unique Building Name & Folder...";
req.io.emit('form-data', {message: string});
var buildingName = building.buildingName.replace(/\s+/g, '') || "NoName";
var timestamp = require("./routes/timestamp.js").createTimestamp();
var simulationID = buildingName+timestamp;
//CREATE unique simulation Folder
var outputPath = "../runs/" + simulationID +"/"; //CREATE or UPDATE "runs" for your local setup
var fs = require("fs");
fs.mkdirSync(outputPath, function(error) {if (error) throw error;});
//SAVE idf file
var idfName = simulationID+'_input.idf'
fs.writeFile(outputPath+idfName, building.inputDataFile, function(error){if (error) throw error;});
//FORMAT request.body json to match buildingData2.json
var buildingJSONName = outputPath + simulationID +'_input.json';
string = "BUILDING DATA...";
req.io.emit('form-data', {message: string});
var buildingJSON =
{
"simulationID": simulationID,
"buildingName": buildingName,
"weather": building.weather,
"idfName": idfName,
"outputPath": outputPath
};
console.log(buildingJSON);
//SAVE formatted json to outputPath with name buildingNameTimestamp_input.json
var fileString = JSON.stringify(buildingJSON, null, 4);
req.io.emit('form-data', {message: fileString});
fs.writeFileSync(buildingJSONName, fileString);
string = 'Input file saved at energyplus.io/runs/'+simulationID;
req.io.emit('form-data', {message: string});
var fork = require('child_process').fork;
var command = fork("./routes/openstudio-run.js", [buildingJSONName], {silent: true }); //silent because http://stackoverflow.com/questions/22275556/node-js-forked-pipe
command.stdout.pipe(process.stdout);
command.stdout.pipe(process.stderr);
command.stdout.on("data", function (data) {
var line = data.toString();
req.io.emit('form-data', {message: line});
});
command.stderr.on("data", function (data) {
var line = data.toString();
req.io.emit('form-data', {message: line});
});
string = "<a target='_blank' href='http://energyplus.io/runs/"+simulationID+"/1-EnergyPlus-0/eplustbl.htm'>Summary of Building Energy Consumption</a><br>";
req.io.emit('output-data', {message: string});
string = "<a target='_blank' href='http://energyplus.io/runs/"+simulationID+"/1-EnergyPlus-0/'>All Files</a>";
req.io.emit('output-data', {message: string});
});
//ENERGYPLUS as a SERVICE (over POST)
var simulate = require('./routes/run.js');
app.post('/simulate', simulate.energyplus);
//START SERVER
app.listen(app.get('port'), function(){
console.log('Express & Socket listening on port ' + app.get('port'));
});