create track points from csv

This commit is contained in:
root 2020-04-14 03:05:07 +02:00
parent 5b99d0fb8a
commit a709c4a8e2
2 changed files with 132 additions and 3 deletions

View file

@ -9,7 +9,16 @@ var TrackSchema = new mongoose.Schema({
description: String, description: String,
body: String, body: String,
numEvents: {type: Number, default: 0}, numEvents: {type: Number, default: 0},
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' } comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }],
author: { type: mongoose.Schema.Types.ObjectId, ref: 'User' },
points: [ {
date: String,
time : String,
latitude: Number,
longitude: Number,
d1: Number,
d2: Number,
flag: Number }]
}, {timestamps: true}); }, {timestamps: true});
TrackSchema.plugin(uniqueValidator, {message: 'is already taken'}); TrackSchema.plugin(uniqueValidator, {message: 'is already taken'});
@ -34,6 +43,7 @@ TrackSchema.methods.toJSONFor = function(user){
body: this.body, body: this.body,
createdAt: this.createdAt, createdAt: this.createdAt,
updatedAt: this.updatedAt, updatedAt: this.updatedAt,
points: this.points,
visibleForAll: user ? user.areTracksVisibleForAll : false, visibleForAll: user ? user.areTracksVisibleForAll : false,
author: this.author.toProfileJSONFor(user) author: this.author.toProfileJSONFor(user)
}; };

View file

@ -153,6 +153,66 @@ 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);
console.log(track.body);
var num=0;
var start=0;
var end=0;
// console.log("len"+track.body.length);
while(end < track.body.length)
{
start = end;
while(track.body[end]!=";" && track.body[end]!="\n" && end < track.body.length)
{
end++;
}
var token = track.body.substr(start,end-start);
//console.log(token);
//console.log("num:"+num);
//console.log("end:"+end);
end++;
if(num == 0)
{
track.points.push( {date: "dummy"} );
}
if(num==0)
{
track.points[track.points.length - 1].date = token;
num++;
}
else if(num==1)
{
track.points[track.points.length - 1].time = token;
num++;
}
else if(num==2)
{
track.points[track.points.length - 1].latitude = token;
num++;
}
else if(num==3)
{
track.points[track.points.length - 1].longitude = token;
num++;
}
else if(num==4)
{
track.points[track.points.length - 1].d1 = token;
num++;
}
else if(num==5)
{
track.points[track.points.length - 1].d2 = token;
num++;
}
else if(num==6)
{
track.points[track.points.length - 1].flag = token;
num=0;
}
}
console.log("TLen"+track.points.length);
track.author = user; track.author = user;
@ -167,13 +227,72 @@ router.post('/add', auth.optional, function(req, res, next) {
console.log("Add"); console.log("Add");
if(true) if(true)
{ {
console.log(req.body); //console.log(req.payload);
console.log(req.payload);
User.findById(req.body.id).then(function(user){ User.findById(req.body.id).then(function(user){
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);
console.log(track.body);
var num=0;
var start=0;
var end=0;
// console.log("len"+track.body.length);
while(end < track.body.length)
{
start = end;
while(track.body[end]!=";" && track.body[end]!="\n" && end < track.body.length)
{
end++;
}
var token = track.body.substr(start,end-start);
//console.log(token);
//console.log("num:"+num);
//console.log("end:"+end);
end++;
if(num == 0)
{
track.points.push( {date: "dummy"} );
}
if(num==0)
{
track.points[track.points.length - 1].date = token;
num++;
}
else if(num==1)
{
track.points[track.points.length - 1].time = token;
num++;
}
else if(num==2)
{
track.points[track.points.length - 1].latitude = token;
num++;
}
else if(num==3)
{
track.points[track.points.length - 1].longitude = token;
num++;
}
else if(num==4)
{
track.points[track.points.length - 1].d1 = token;
num++;
}
else if(num==5)
{
track.points[track.points.length - 1].d2 = token;
num++;
}
else if(num==6)
{
track.points[track.points.length - 1].flag = token;
num=0;
}
}
console.log("TLen"+track.points.length);
//console.log(track.points[0].date);
track.author = user; track.author = user;
return track.save().then(function(){ return track.save().then(function(){