-
Notifications
You must be signed in to change notification settings - Fork 7
/
data.js
141 lines (124 loc) · 4.11 KB
/
data.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
// adapted from https://github.com/mscdex/node-mariasql
var inspect = require('util').inspect;
var Client = require('mariasql');
var client = new Client();
exports.connect = function() {
client.connect
(
{
host: '127.0.0.1',
user: 'root',
password: 'password1',
db: 'running'
}
);
client.on
(
'connect', function() { console.log('Client connected'); }
).on
(
'error', function(err) { console.log('Client error: ' + err); }
).on
(
'close', function(hadError) { console.log('Client closed'); }
);
}
exports.insertPoint = function(lon, lat) {
// console.log('INSERT INTO trackpoints (gpsPoint) VALUES (PointFromText(:loc))', { loc: 'POINT(' + lon + " " + lat + ')' });
client.query('INSERT INTO trackpoints (gpsPoint) VALUES (PointFromText(:loc))', { loc: 'POINT(' + lon + " " + lat + ')' })
.on('result', function(res) {
res.on('row', function(row) {
console.log('Result row: ' + inspect(row));
})
.on('error', function(err) {
console.log('Result error: ' + inspect(err));
})
.on('end', function(info) {
console.log('Result finished successfully');
});
})
.on('end', function() {
console.log('Done with all results');
});
}
exports.getTrackPoints = function(trackId, httpRes) {
var xys = [];
client.query('SELECT X(GeomFromText(AsText(gpsPoint))) AS X, Y(GeomFromText(AsText(gpsPoint))) AS Y FROM trackpoints WHERE trackId = :pTrackId', { pTrackId: trackId })
.on('result', function(res) {
res.on('row', function(row) {
xys.push(row);
})
.on('error', function(err) {
console.log('Result error: ' + inspect(err));
})
.on('end', function(info) {
console.log('Result finished successfully');
});
})
.on('end', function() {
httpRes.send(xys);
});
}
exports.getXMLTracks = function(httpRes) {
var data = { tracks: {} };
client.query('SELECT filename, lon, lat FROM GPXSource ORDER BY time')
.on('result', function(res) {
res.on('row', function(row) {
if (data.tracks[row['filename']] == undefined)
data.tracks[row['filename']] = [];
data.tracks[row['filename']].push({ lon: row['lon'], lat: row['lat'] });
})
.on('error', function(err) {
console.log('Result error: ' + inspect(err));
})
.on('end', function(info) {
console.log('Result finished successfully');
});
})
.on('end', function() {
httpRes.json(data);
});
}
exports.getXMLTracksDuration = function(httpRes) {
var data = [];
client.query('SELECT filename, TIMEDIFF(MAX(time), MIN(time)) AS duration FROM GPXSource GROUP BY filename ORDER BY filename;')
.on('result', function(res) {
res.on('row', function(row) {
data.push(row);
})
.on('error', function(err) {
console.log('Result error: ' + inspect(err));
})
.on('end', function(info) {
console.log('Result finished successfully');
});
})
.on('end', function() {
httpRes.json(data);
});
}
exports.getDistance = function(trackId, httpRes) {
var result = [];
client.query("SELECT SUM(earth_circle_distance(t1.gpsPoint, t2.gpsPoint)) AS distance \
FROM trackpoints t1 INNER JOIN trackpoints t2 ON t2.pointId = (t1.pointId + 1) WHERE t1.tr\
ackId = :pTrackId", { pTrackId: trackId })
.on('result', function(res) {
console.log("Result: " + inspect(result));
res.on('row', function(row) {
result.push(row);
console.log("Result row: " + inspect(row));
})
.on('error', function(err) {
console.log('Result error: ' + inspect(err));
})
.on('end', function(info) {
console.log('Result finished successfully');
});
})
.on('end', function() {
httpRes.send(result);
});
}
exports.disconnect = function() {
client.end();
}