forked from SnehalRaj/Bosch_route_optimization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
158 lines (145 loc) · 4.05 KB
/
index.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
/*
The app should use a proper database linked to the account of each user however,
for current presentation,
this is sufficient.
*/
let express = require('express')
let bodyParser = require('body-parser')
let axios = require('axios')
let cors = require('cors')
let fs = require('fs')
const { exec } = require('child_process');
let app = express()
app.use(cors())
let router = express.Router()
app.use(bodyParser.json({
type: 'application/json'
}))
const command = './generateClusters'
const command2 = './generatePaths'
// const command = './generateCluster'
// const command2 = './generatePaths'
function writeToCSVT1(data){
let strx = data.length+'\n';
let ptarr = data.data;
for(var i=0; i<data.length; i++){
strx = strx+ptarr[i][0]+', '+ptarr[i][1]+'\n';
}
fs.writeFileSync('coords.csv', strx);
}
function writeToCSVT2(data){
let strx = data.nclusters+'\n';
let clen = data.clen;
let dist = data.data;
let ptarr = data.clusters;
for(var i=0; i<data.nclusters; i++){
strx = strx+clen[i]+'\n';
for(var j=0; j<clen[i]; j++){
strx = strx+ptarr[i][j][0]+' '+ptarr[i][j][1]+'\n';
}
for(var j=0; j<clen[i]; j++){
for(var k=0; k<clen[i]; k++)
strx = strx+dist[i][j][k]+' ';
//strx = strx.substring(0, strx.length-2)
strx = strx+'\n';
}
}
fs.writeFileSync('distances.csv', strx);
}
function readFromCSV(fname){
let i = 0, x = 0;
let file = String(fs.readFileSync(fname))
let lines = file.split('\n')
let nclusters = parseInt(lines[x])
let line = '', segments = [], lat, lng
let data=[], clstr=[]
for(;i<nclusters;i++){
x++;
let npts = parseInt(lines[x])
clstr=[]
for(let j=0; j<npts; j++){
x++;
line = lines[x];
segments = line.split(', ')
lat = parseFloat(segments[0])
lng = parseFloat(segments[1])
clstr.push([lat, lng])
}
data.push(clstr)
}
// console.log(data)
return data
}
async function getClusters(){
var promiseForClusters = new Promise(function (resolve, reject) {
exec(command, (error, stdout, stderr) => {
if (error) {
reject();
return;
}
else{
resolve();
}
});
})
await promiseForClusters;
return readFromCSV('clusters2.csv');
}
async function getSomethingMoreDone(){
var promiseForClusters = new Promise(function (resolve, reject) {
exec(command2, (error, stdout, stderr) => {
if (error) {
reject();
return;
}
else{
resolve();
}
});
})
await promiseForClusters;
return readFromCSV('ordered.csv');
}
app.post('/uploadPoints', (req, res) => {
var data = req.body;
writeToCSVT1(data);
getClusters().then((dt1) => {
res.json(dt1);
res.end();
}).catch((err)=>{res.json({err});res.end();});
})
app.post('/uploadDistances', (req, res) => {
var data = req.body;
writeToCSVT2(data);
/* getSomethingMoreDone().then((dt1) => {
console.log(dt1);
res.json({"data":dt1});
res.end();
}).catch((err)=>{res.json({err});res.end();}); */
})
app.post('/uploadOrdered',(req,res) => {
//var orderedDistance = readFromCSV('ordered.csv');
//orderedDistance = orderedDistance.data.data;
/* var promiseForClusters = new Promise(function (resolve, reject) {
exec(command2, (error, stdout, stderr) => {
if (error) {
reject();
return;
}
else{
resolve();
}
});
}) */
console.log("Paths generated");
var orderedDistance = readFromCSV('ordered.csv')
res.json({"data":orderedDistance});
res.end();
})
app.get('/nigga', (req, res) => {
let sucker = "CUNT"
res.json(sucker);
res.end();
})
app.use('/', router)
app.listen(8000)