separate TrackData

This commit is contained in:
root 2020-08-14 12:37:34 +02:00
parent 934cedd3d0
commit 061234b301
5 changed files with 73 additions and 52 deletions

View file

@ -1,2 +1,4 @@
# obsAPI # obsAPI
OpenBikeSensor Web API OpenBikeSensor Web API

1
app.js
View file

@ -37,6 +37,7 @@ if(isProduction){
mongoose.set('debug', true); mongoose.set('debug', true);
} }
require('./models/TrackData');
require('./models/User'); require('./models/User');
require('./models/Track'); require('./models/Track');
require('./models/Comment'); require('./models/Comment');

View file

@ -2,6 +2,7 @@ var mongoose = require('mongoose');
var uniqueValidator = require('mongoose-unique-validator'); var uniqueValidator = require('mongoose-unique-validator');
var slug = require('slug'); var slug = require('slug');
var User = mongoose.model('User'); var User = mongoose.model('User');
var TrackData = mongoose.model('TrackData');
var TrackSchema = new mongoose.Schema({ var TrackSchema = new mongoose.Schema({
slug: {type: String, lowercase: true, unique: true}, slug: {type: String, lowercase: true, unique: true},
@ -12,18 +13,7 @@ var TrackSchema = new mongoose.Schema({
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }], comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
//Date;Time;Latitude;Longitude;Course;Speed;Right;Left;Confirmed;insidePrivacyArea //Date;Time;Latitude;Longitude;Course;Speed;Right;Left;Confirmed;insidePrivacyArea
points: [ { trackData: { type: mongoose.Schema.Types.ObjectId, ref: 'TrackData' },
date: String,
time : String,
latitude: Number,
longitude: Number,
course: Number,
speed: Number,
d1: Number,
d2: Number,
flag: Number,
private: Number
}]
}, {timestamps: true}); }, {timestamps: true});
TrackSchema.plugin(uniqueValidator, {message: 'is already taken'}); TrackSchema.plugin(uniqueValidator, {message: 'is already taken'});
@ -45,7 +35,6 @@ TrackSchema.methods.toJSONFor = function(user){
slug: this.slug, slug: this.slug,
title: this.title, title: this.title,
description: this.description, description: this.description,
body: this.body,
createdAt: this.createdAt, createdAt: this.createdAt,
updatedAt: this.updatedAt, updatedAt: this.updatedAt,
points: this.points, points: this.points,

View file

@ -1,5 +1,6 @@
var router = require('express').Router(); var router = require('express').Router();
var mongoose = require('mongoose'); var mongoose = require('mongoose');
var TrackData = mongoose.model('TrackData');
var Track = mongoose.model('Track'); var Track = mongoose.model('Track');
var Comment = mongoose.model('Comment'); var Comment = mongoose.model('Comment');
var User = mongoose.model('User'); var User = mongoose.model('User');
@ -7,24 +8,24 @@ var auth = require('../auth');
var currentTracks = new Map(); var currentTracks = new Map();
function addPointsToTrack(track,trackPart) function addPointsToTrack(track,body, trackPart)
{ {
var num = 0; var num = 0;
var start = 0; var start = 0;
var end = 0; var end = 0;
//console.log("len"+trackPart.body.length); console.log("len"+body.length);
while (end < trackPart.body.length) { while (end < body.length) {
start = end; start = end;
while (trackPart.body[end] != ";" && trackPart.body[end] != "$" && end < trackPart.body.length) { while (body[end] != ";" && body[end] != "$" && end < body.length) {
end++; end++;
} }
if(trackPart.body[end] == "$") // $ is replacing \n as newlines are not allowed in json strings if(body[end] == "$") // $ is replacing \n as newlines are not allowed in json strings
{ {
num=0; num=0;
} }
if(end < trackPart.body.length) if(end < body.length)
{ {
var token = trackPart.body.substr(start, end - start); var token = body.substr(start, end - start);
end++; end++;
if(token.length>0) if(token.length>0)
{ {
@ -32,14 +33,14 @@ function addPointsToTrack(track,trackPart)
//console.log("num:"+num); //console.log("num:"+num);
//console.log("end:"+end); //console.log("end:"+end);
if (num == 0) { if (num == 0) {
track.points.push({ date: "dummy", time: "", latitude: "", longitude: "", course: "", speed: "", d1: "", d2: "", flag: "", private: ""}); track.trackData.points.push({ date: "dummy", time: "", latitude: "", longitude: "", course: "", speed: "", d1: "", d2: "", flag: "", private: ""});
} }
if (num == 0) { if (num == 0) {
track.points[track.points.length - 1].date = token; track.trackData.points[track.trackData.points.length - 1].date = token;
num++; num++;
} }
else if (num == 1) { else if (num == 1) {
track.points[track.points.length - 1].time = token; track.trackData.points[track.trackData.points.length - 1].time = token;
num++; num++;
} }
else if (num == 2) { else if (num == 2) {
@ -48,7 +49,7 @@ function addPointsToTrack(track,trackPart)
{ {
f = parseFloat(token.substring(0,10)); f = parseFloat(token.substring(0,10));
} }
track.points[track.points.length - 1].latitude = f; track.trackData.points[track.trackData.points.length - 1].latitude = f;
num++; num++;
} }
else if (num == 3) { else if (num == 3) {
@ -57,7 +58,7 @@ function addPointsToTrack(track,trackPart)
{ {
f = parseFloat(token.substring(0,10)); f = parseFloat(token.substring(0,10));
} }
track.points[track.points.length - 1].longitude = f; track.trackData.points[track.trackData.points.length - 1].longitude = f;
num++; num++;
} }
else if (num == 4) { else if (num == 4) {
@ -66,7 +67,7 @@ function addPointsToTrack(track,trackPart)
{ {
f = parseFloat(token.substring(0,10)); f = parseFloat(token.substring(0,10));
} }
track.points[track.points.length - 1].course = f; track.trackData.points[track.trackData.points.length - 1].course = f;
num++; num++;
} }
else if (num == 5) { else if (num == 5) {
@ -75,23 +76,23 @@ function addPointsToTrack(track,trackPart)
{ {
f = parseFloat(token.substring(0,10)); f = parseFloat(token.substring(0,10));
} }
track.points[track.points.length - 1].speed = f; track.trackData.points[track.trackData.points.length - 1].speed = f;
num++; num++;
} }
else if (num == 6) { else if (num == 6) {
track.points[track.points.length - 1].d1 = token; track.trackData.points[track.trackData.points.length - 1].d1 = token;
num++; num++;
} }
else if (num == 7) { else if (num == 7) {
track.points[track.points.length - 1].d2 = token; track.trackData.points[track.trackData.points.length - 1].d2 = token;
num++; num++;
} }
else if (num == 8) { else if (num == 8) {
track.points[track.points.length - 1].flag = token; track.trackData.points[track.trackData.points.length - 1].flag = token;
num++; num++;
} }
else if (num == 9) { else if (num == 9) {
track.points[track.points.length - 1].private = token; track.trackData.points[track.trackData.points.length - 1].private = token;
num++; num++;
} }
} }
@ -169,6 +170,7 @@ router.get('/', auth.optional, function(req, res, next) {
var tracks = results[0]; var tracks = results[0];
var tracksCount = results[1]; var tracksCount = results[1];
var user = results[2]; var user = results[2];
console.log(tracks);
return res.json({ return res.json({
tracks: tracks.map(function(track){ tracks: tracks.map(function(track){
@ -247,11 +249,12 @@ router.post('/', auth.required, function(req, res, next) {
if (!user) { return res.sendStatus(401); } if (!user) { return res.sendStatus(401); }
var track = new Track(req.body.track); var track = new Track(req.body.track);
track.trackData = new TrackData();
console.log(track.body); console.log(track.body);
var num=0; var num=0;
var start=0; var start=0;
var end=0; var end=0;
// console.log("len"+track.body.length); console.log("len"+track.body.length);
while(end < track.body.length) while(end < track.body.length)
{ {
start = end; start = end;
@ -260,60 +263,67 @@ router.post('/', auth.required, function(req, res, next) {
end++; end++;
} }
var token = track.body.substr(start,end-start); var token = track.body.substr(start,end-start);
//console.log(token); console.log(token);
//console.log("num:"+num); console.log("num:"+num);
//console.log("end:"+end); console.log("end:"+end);
end++; end++;
if(num == 0) if(num == 0)
{ {
track.points.push( {date: "dummy"} ); track.trackData.points.push( {date: "dummy"} );
} }
if(num==0) if(num==0)
{ {
track.points[track.points.length - 1].date = token; track.trackData.points[track.trackData.points.length - 1].date = token;
num++; num++;
} }
else if(num==1) else if(num==1)
{ {
track.points[track.points.length - 1].time = token; track.trackData.points[track.trackData.points.length - 1].time = token;
num++; num++;
} }
else if(num==2) else if(num==2)
{ {
track.points[track.points.length - 1].latitude = token; track.trackData.points[track.trackData.points.length - 1].latitude = token;
num++; num++;
} }
else if(num==3) else if(num==3)
{ {
track.points[track.points.length - 1].longitude = token; track.trackData.points[track.trackData.points.length - 1].longitude = token;
num++; num++;
} }
else if(num==4) else if(num==4)
{ {
track.points[track.points.length - 1].d1 = token; track.trackData.points[track.trackData.points.length - 1].d1 = token;
num++; num++;
} }
else if(num==5) else if(num==5)
{ {
track.points[track.points.length - 1].d2 = token; track.trackData.points[track.trackData.points.length - 1].d2 = token;
num++; num++;
} }
else if(num==6) else if(num==6)
{ {
track.points[track.points.length - 1].flag = token; track.trackData.points[track.trackData.points.length - 1].flag = token;
num=0; num=0;
} }
} }
console.log("TLen"+track.points.length); console.log("TLen"+track.trackData.points.length);
track.author = user; track.author = user;
track.trackData.save(function (err){
if(err){
console.log("failed to save trackData");
return handleError(err);
}
});
return track.save().then(function(){ return track.save().then(function(){
console.log(track.author); console.log(track.author);
return res.json({track: track.toJSONFor(user)}); return res.json({track: track.toJSONFor(user)});
}); });
return res.json({track: track.toJSONFor(user)});
}).catch(next); }).catch(next);
}); });
@ -328,8 +338,8 @@ router.post('/add', auth.optional, function(req, res, next) {
if (currentTracks.has(req.body.id)) if (currentTracks.has(req.body.id))
track = currentTracks.get(req.body.id); track = currentTracks.get(req.body.id);
if (track) { if (track) {
addPointsToTrack(track, req.body.track); addPointsToTrack(track, req.body, req.body.track);
console.log("TLen" + track.points.length); console.log("TLen" + track.trackData.points.length);
track.author = user; track.author = user;
} }
@ -350,13 +360,18 @@ router.post('/begin', auth.optional, function (req, res, next) {
if(currentTracks.has(req.body.id)) if(currentTracks.has(req.body.id))
currentTracks.delete(req.body.id); // delete old parts if there are leftovers currentTracks.delete(req.body.id); // delete old parts if there are leftovers
var track = new Track(req.body.track); var track = new Track(req.body.track);
track.trackData = new TrackData();
currentTracks.set(req.body.id, track); currentTracks.set(req.body.id, track);
addPointsToTrack(track, track); console.log("addToTrack"+req.body);
console.log("TLen" + track.points.length); addPointsToTrack(track, track.body, track);
//console.log(track.points[0].date); console.log("TLen" + track);
console.log("TLen" + track.trackData);
console.log("TLen" + track.trackData.points.length);
//console.log(track.trackData.points[0].date);
track.author = user; track.author = user;
//return track.save().then(function () { //return track.save().then(function () {
@ -374,11 +389,17 @@ router.post('/end', auth.optional, function (req, res, next) {
var track = null; var track = null;
if (currentTracks.has(req.body.id)) if (currentTracks.has(req.body.id))
{
track = currentTracks.get(req.body.id); track = currentTracks.get(req.body.id);
addPointsToTrack(track, req.body, req.body.track);
}
else else
{
track = new Track(req.body.track); track = new Track(req.body.track);
track.trackData = new TrackData();
addPointsToTrack(track, track.body, req.body.track);
}
if (track) { if (track) {
addPointsToTrack(track, req.body.track);
track.author = user; track.author = user;
} }
@ -388,7 +409,15 @@ router.post('/end', auth.optional, function (req, res, next) {
//console.log(track); //console.log(track);
//console.log("user:"+user); //console.log("user:"+user);
return track.save().then(function () { return track.save().then(function () {
console.log("TLen" + track.points.length); console.log("TLen" + track);
console.log("TLen" + track.trackData);
console.log("TLen" + track.trackData.points.length);
track.trackData.save(function (err){
if(err){
console.log("failed to save trackData");
}
});
console.log("successfulSave:"); console.log("successfulSave:");
return res.sendStatus(200); return res.sendStatus(200);
}); });