-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
102 lines (81 loc) · 2.86 KB
/
Copy pathserver.js
File metadata and controls
102 lines (81 loc) · 2.86 KB
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
const express = require('C:/Users/User/AppData/Roaming/npm/node_modules/express');
const path = require('C:/Users/User/AppData/Roaming/npm/node_modules/path');
const {PythonShell} = require('C:/Users/User/AppData/Roaming/npm/node_modules/python-shell');
const app = express();
const router = express.Router();
const port = process.env.PORT || 3000;//port to which the server start listening
const createCsvWriter = require('C:/Users/User/AppData/Roaming/npm/node_modules/csv-writer').createObjectCsvWriter;
router.get('/', function (req, res) {
res.sendFile(path.join(__dirname + '/index.html'));
});
//the express server hosts the html file
app.use('/', router);
app.use(express.json());//indispensabile sennò il body della request è undefinied
const mode = 'all';
const N = 20
if (mode == 'system') {
const csvWriter = createCsvWriter({
path: 'Data.csv',
header: [
{ id: 'E', title: 'Energy' },
{ id: 'T', title: 'Temp' },
{ id: 'P', title: 'Press' },
{ id: 'V', title: 'Volume' },
{ id: 'N', title: 'Num' },
],
append: true // to append to the file, without clearing
});
csvWriter.writeRecords([{ E: 'Energy', V: 'Volume', N: 'Num', T: 'Temp', P: 'Press' }]) // returns a promise
.then(() => {
console.log('Data.csv created');
});
PythonShell.run('system.py', {}, (err, res) => {
if (err) console.log(err);
if(res) console.log(res);
}) //to run the python script
let data_counter = 0;
app.post('/system', (req, res) => {
const data = req.body;
res.json({
status: 'success',
})
csvWriter.writeRecords([data]) // returns a promise
.then(() => {
console.log(data);
});
data_counter += 1;
})
}
else if (mode == 'all') {
const csvWriter = createCsvWriter({
path: 'distribution.csv',
header: [
{ id: 'vx', title: 'vx' },
{ id: 'vy', title: 'vy' },
],
append : false
});
const experimental_data = [];
PythonShell.run('distribution.py', {}, (err, res) => {
if (err) console.log(err);
if (res) console.log(res);
}) //to run the python script
app.post('/all', (req, res) => {
const data = req.body.data;
for (let item of data) {
experimental_data.push(item);
}
csvWriter.writeRecords(experimental_data) // returns a promise
.then(() => {
console.log('...Done');
});
res.json({
status: 'success',
})
})
}
app.listen(port, () => console.log('listening to port ' + port));
//TO REMOVE ELEMENTS FROM THE DATABASE
// db.remove({ name: 'leo' }, { multi: true }, (err, numRemoved) => {
// console.log(numRemoved);
// });