From 061234b3012e0a4d917f880ce9e10ed3027ca61a Mon Sep 17 00:00:00 2001 From: root Date: Fri, 14 Aug 2020 12:37:34 +0200 Subject: [PATCH] separate TrackData --- README.md | 2 + app.js | 1 + models/Track.js | 15 +------ routes/api/index.js | 2 +- routes/api/tracks.js | 105 +++++++++++++++++++++++++++---------------- 5 files changed, 73 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index b7fdbfc..659bbab 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,4 @@ # obsAPI OpenBikeSensor Web API + + diff --git a/app.js b/app.js index 9841273..1e730f5 100644 --- a/app.js +++ b/app.js @@ -37,6 +37,7 @@ if(isProduction){ mongoose.set('debug', true); } +require('./models/TrackData'); require('./models/User'); require('./models/Track'); require('./models/Comment'); diff --git a/models/Track.js b/models/Track.js index 08e254f..9876a3c 100644 --- a/models/Track.js +++ b/models/Track.js @@ -2,6 +2,7 @@ var mongoose = require('mongoose'); var uniqueValidator = require('mongoose-unique-validator'); var slug = require('slug'); var User = mongoose.model('User'); +var TrackData = mongoose.model('TrackData'); var TrackSchema = new mongoose.Schema({ slug: {type: String, lowercase: true, unique: true}, @@ -12,18 +13,7 @@ var TrackSchema = new mongoose.Schema({ comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }], author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' }, //Date;Time;Latitude;Longitude;Course;Speed;Right;Left;Confirmed;insidePrivacyArea - points: [ { - date: String, - time : String, - latitude: Number, - longitude: Number, - course: Number, - speed: Number, - d1: Number, - d2: Number, - flag: Number, - private: Number - }] + trackData: { type: mongoose.Schema.Types.ObjectId, ref: 'TrackData' }, }, {timestamps: true}); TrackSchema.plugin(uniqueValidator, {message: 'is already taken'}); @@ -45,7 +35,6 @@ TrackSchema.methods.toJSONFor = function(user){ slug: this.slug, title: this.title, description: this.description, - body: this.body, createdAt: this.createdAt, updatedAt: this.updatedAt, points: this.points, diff --git a/routes/api/index.js b/routes/api/index.js index c7c68fe..6970fab 100644 --- a/routes/api/index.js +++ b/routes/api/index.js @@ -19,4 +19,4 @@ router.use(function(err, req, res, next){ return next(err); }); -module.exports = router; \ No newline at end of file +module.exports = router; diff --git a/routes/api/tracks.js b/routes/api/tracks.js index 6a0e4d5..ac48a32 100644 --- a/routes/api/tracks.js +++ b/routes/api/tracks.js @@ -1,5 +1,6 @@ var router = require('express').Router(); var mongoose = require('mongoose'); +var TrackData = mongoose.model('TrackData'); var Track = mongoose.model('Track'); var Comment = mongoose.model('Comment'); var User = mongoose.model('User'); @@ -7,24 +8,24 @@ var auth = require('../auth'); var currentTracks = new Map(); -function addPointsToTrack(track,trackPart) +function addPointsToTrack(track,body, trackPart) { var num = 0; var start = 0; var end = 0; - //console.log("len"+trackPart.body.length); - while (end < trackPart.body.length) { + console.log("len"+body.length); + while (end < body.length) { start = end; - while (trackPart.body[end] != ";" && trackPart.body[end] != "$" && end < trackPart.body.length) { + while (body[end] != ";" && body[end] != "$" && end < body.length) { 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; } - 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++; if(token.length>0) { @@ -32,14 +33,14 @@ function addPointsToTrack(track,trackPart) //console.log("num:"+num); //console.log("end:"+end); 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) { - track.points[track.points.length - 1].date = token; + track.trackData.points[track.trackData.points.length - 1].date = token; num++; } else if (num == 1) { - track.points[track.points.length - 1].time = token; + track.trackData.points[track.trackData.points.length - 1].time = token; num++; } else if (num == 2) { @@ -48,7 +49,7 @@ function addPointsToTrack(track,trackPart) { 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++; } else if (num == 3) { @@ -57,7 +58,7 @@ function addPointsToTrack(track,trackPart) { 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++; } else if (num == 4) { @@ -66,7 +67,7 @@ function addPointsToTrack(track,trackPart) { 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++; } else if (num == 5) { @@ -75,23 +76,23 @@ function addPointsToTrack(track,trackPart) { 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++; } else if (num == 6) { - track.points[track.points.length - 1].d1 = token; + track.trackData.points[track.trackData.points.length - 1].d1 = token; num++; } else if (num == 7) { - track.points[track.points.length - 1].d2 = token; + track.trackData.points[track.trackData.points.length - 1].d2 = token; num++; } else if (num == 8) { - track.points[track.points.length - 1].flag = token; + track.trackData.points[track.trackData.points.length - 1].flag = token; num++; } else if (num == 9) { - track.points[track.points.length - 1].private = token; + track.trackData.points[track.trackData.points.length - 1].private = token; num++; } } @@ -169,6 +170,7 @@ router.get('/', auth.optional, function(req, res, next) { var tracks = results[0]; var tracksCount = results[1]; var user = results[2]; + console.log(tracks); return res.json({ tracks: tracks.map(function(track){ @@ -247,11 +249,12 @@ router.post('/', auth.required, function(req, res, next) { if (!user) { return res.sendStatus(401); } var track = new Track(req.body.track); + track.trackData = new TrackData(); console.log(track.body); var num=0; var start=0; var end=0; - // console.log("len"+track.body.length); + console.log("len"+track.body.length); while(end < track.body.length) { start = end; @@ -260,60 +263,67 @@ router.post('/', auth.required, function(req, res, next) { end++; } var token = track.body.substr(start,end-start); - //console.log(token); - //console.log("num:"+num); - //console.log("end:"+end); + console.log(token); + console.log("num:"+num); + console.log("end:"+end); end++; if(num == 0) { - track.points.push( {date: "dummy"} ); + track.trackData.points.push( {date: "dummy"} ); } if(num==0) { - track.points[track.points.length - 1].date = token; + track.trackData.points[track.trackData.points.length - 1].date = token; num++; } else if(num==1) { - track.points[track.points.length - 1].time = token; + track.trackData.points[track.trackData.points.length - 1].time = token; num++; } else if(num==2) { - track.points[track.points.length - 1].latitude = token; + track.trackData.points[track.trackData.points.length - 1].latitude = token; num++; } else if(num==3) { - track.points[track.points.length - 1].longitude = token; + track.trackData.points[track.trackData.points.length - 1].longitude = token; num++; } else if(num==4) { - track.points[track.points.length - 1].d1 = token; + track.trackData.points[track.trackData.points.length - 1].d1 = token; num++; } else if(num==5) { - track.points[track.points.length - 1].d2 = token; + track.trackData.points[track.trackData.points.length - 1].d2 = token; num++; } else if(num==6) { - track.points[track.points.length - 1].flag = token; + track.trackData.points[track.trackData.points.length - 1].flag = token; num=0; } } - console.log("TLen"+track.points.length); + console.log("TLen"+track.trackData.points.length); track.author = user; + track.trackData.save(function (err){ + if(err){ + console.log("failed to save trackData"); + return handleError(err); + } + }); return track.save().then(function(){ console.log(track.author); return res.json({track: track.toJSONFor(user)}); }); + return res.json({track: track.toJSONFor(user)}); }).catch(next); }); @@ -328,8 +338,8 @@ router.post('/add', auth.optional, function(req, res, next) { if (currentTracks.has(req.body.id)) track = currentTracks.get(req.body.id); if (track) { - addPointsToTrack(track, req.body.track); - console.log("TLen" + track.points.length); + addPointsToTrack(track, req.body, req.body.track); + console.log("TLen" + track.trackData.points.length); track.author = user; } @@ -350,13 +360,18 @@ router.post('/begin', auth.optional, function (req, res, next) { if(currentTracks.has(req.body.id)) currentTracks.delete(req.body.id); // delete old parts if there are leftovers var track = new Track(req.body.track); + track.trackData = new TrackData(); currentTracks.set(req.body.id, track); - addPointsToTrack(track, track); + console.log("addToTrack"+req.body); + + addPointsToTrack(track, track.body, track); - console.log("TLen" + track.points.length); + console.log("TLen" + track); + console.log("TLen" + track.trackData); + console.log("TLen" + track.trackData.points.length); - //console.log(track.points[0].date); + //console.log(track.trackData.points[0].date); track.author = user; //return track.save().then(function () { @@ -374,11 +389,17 @@ router.post('/end', auth.optional, function (req, res, next) { var track = null; if (currentTracks.has(req.body.id)) + { track = currentTracks.get(req.body.id); + addPointsToTrack(track, req.body, req.body.track); + } else + { track = new Track(req.body.track); + track.trackData = new TrackData(); + addPointsToTrack(track, track.body, req.body.track); + } if (track) { - addPointsToTrack(track, req.body.track); track.author = user; } @@ -388,7 +409,15 @@ router.post('/end', auth.optional, function (req, res, next) { //console.log(track); //console.log("user:"+user); 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:"); return res.sendStatus(200); });