From ecb868f1451d8a5c998bb72a57b97cf75b15c809 Mon Sep 17 00:00:00 2001 From: Jean Bertrand Date: Wed, 23 Nov 2016 11:57:43 +0100 Subject: [PATCH 1/7] Gibe better syntax to a function --- lib/geometry.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/geometry.js b/lib/geometry.js index 958d730..d7b0866 100644 --- a/lib/geometry.js +++ b/lib/geometry.js @@ -78,7 +78,9 @@ var randomPointInPolygon = function(polygon, bbox) { }; var randomPointInBbox = function(bbox) { - return { lat: Math.random() * (bbox._northEast.lat - bbox._southWest.lat) + bbox._southWest.lat, lng: Math.random() * (bbox._northEast.lng - bbox._southWest.lng) + bbox._southWest.lng } + var lat = Math.random() * (bbox._northEast.lat - bbox._southWest.lat) + bbox._southWest.lat; + var lng = Math.random() * (bbox._northEast.lng - bbox._southWest.lng) + bbox._southWest.lng; + return { lat: lat, lng: lng }; }; var insidePolygon = function(point, polygon) { From 0981f3d58601e6b5ad17b2a9f0ef39f247ac764b Mon Sep 17 00:00:00 2001 From: Jean Bertrand Date: Wed, 23 Nov 2016 12:04:53 +0100 Subject: [PATCH 2/7] Fix indentation for a function --- public/javascripts/index.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/public/javascripts/index.js b/public/javascripts/index.js index 5dfcf98..0fd8ae3 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -302,11 +302,11 @@ function display_trips() { } function display_points() { - socket.on('trip', function(data) { - data.ride.forEach(function(step){ - L.marker(L.latLng(step.lat, step.lng)).addTo(map); - }); - }); + socket.on('trip', function(data) { + data.ride.forEach(function(step){ + L.marker(L.latLng(step.lat, step.lng)).addTo(map); + }); + }); } function delete_bot(id) { From 12f1ad75d08ea275f12e990a5c7f3f912ab81ae8 Mon Sep 17 00:00:00 2001 From: Jean Bertrand Date: Wed, 23 Nov 2016 15:04:33 +0100 Subject: [PATCH 3/7] Fix issue with job and require --- lib/job.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/job.js b/lib/job.js index 8bfa00a..e7f2923 100644 --- a/lib/job.js +++ b/lib/job.js @@ -10,6 +10,7 @@ queue.process('destroy_bot', function(job, done) { }); var job_destroy_bot = function(id_bot, done) { + var io = require('./socket'); var Bot = require('../models/bot'); Bot.remove({ _id: id_bot }) .then(function(result) { From f5d709e0dffcaeb1d71afba7ad190c007a296e36 Mon Sep 17 00:00:00 2001 From: gabriel vareilles Date: Wed, 23 Nov 2016 16:43:01 +0100 Subject: [PATCH 4/7] #24 accuracy added to form + front validation --- public/javascripts/index.js | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/public/javascripts/index.js b/public/javascripts/index.js index 5dfcf98..7cd85af 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -78,16 +78,25 @@ $('#add').click(function() { title: 'Generate the bot', html: '' + - '', + '' + + '', confirmButtonText: 'Generate', showCancelButton: true, preConfirm: function() { - return new Promise(function(resolve) { - resolve({ - name: $('#bot_name').val(), - nb_driver: parseInt($('#nb_driver').val()), - zone: polygon, - }) + return new Promise(function(resolve, reject) { + var name = $('#bot_name').val(); + var nb_driver = parseInt($('#nb_driver').val()); + var accuracy = parseInt($('#accuracy').val()); + + if (parseInt($('#nb_driver').val()) < 50) { + resolve({ + name: name, + nb_driver: nb_driver, + zone: polygon, + }) + } else { + reject('50 drivers maximum!') + } }) } } From 1990002fc075807fae497b7782ebfc57fa56b570 Mon Sep 17 00:00:00 2001 From: Jean Bertrand Date: Wed, 23 Nov 2016 16:54:46 +0100 Subject: [PATCH 5/7] Fix function that add point --- lib/geometry.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/geometry.js b/lib/geometry.js index d7b0866..ccaaace 100644 --- a/lib/geometry.js +++ b/lib/geometry.js @@ -126,7 +126,7 @@ var getIntermediatePoints = function(firstPosition, secondPosition, distance) { var latArray = getSteps(firstPosition.lat, secondPosition.lat, nbSteps); var res = []; - for (var i = 0; i <= nbSteps; i++) { + for (var i = 0; i < nbSteps-1; i++) { res[i] = { lat: latArray[i], lng: lngArray[i] }; } return res; @@ -135,8 +135,8 @@ var getIntermediatePoints = function(firstPosition, secondPosition, distance) { var getSteps = function(firstPosition, secondPosition, nbSteps) { var res = []; var interval = (secondPosition - firstPosition)/nbSteps; - for (var i = 0; i <= nbSteps; i++) { - res[i] = firstPosition + interval * i; + for (var i = 0; i < nbSteps-1; i++) { + res[i] = firstPosition + interval * (i+1); } return res; }; From 1da677544eac9148461edd06b8327a4cc57f5d27 Mon Sep 17 00:00:00 2001 From: Jean Bertrand Date: Wed, 23 Nov 2016 16:55:57 +0100 Subject: [PATCH 6/7] Use polyline from MAPS API instead of steps, we have now better position for the driver --- lib/trip.js | 24 +++++++++--------------- package.json | 5 ++++- 2 files changed, 13 insertions(+), 16 deletions(-) diff --git a/lib/trip.js b/lib/trip.js index 09be149..24ec0b7 100644 --- a/lib/trip.js +++ b/lib/trip.js @@ -1,4 +1,5 @@ var maps = require('@google/maps'); +var polylineLib = require('polyline'); var config = require('dotenv').config(); var googleMapsClient = maps.createClient({ key: process.env.GOOGLE_API_KEY @@ -29,16 +30,16 @@ var generate_trip = function(bot, origin, callback) { var coords = { origin: start_points, destination: end_points }; generate_directions(coords, function(directions) { // generate_road(directions, callback); - var steps = geometry.splitTrip(directions, bot.speed); - steps.forEach(function(step, index) { + // var steps = geometry.splitTrip(directions, 40); + directions.forEach(function(step, index) { if(index != 0) { - var brng = geometry.calcBearing(step, steps[index-1]); + var brng = geometry.calcBearing(step, directions[index-1]); } else { var brng = 0; } step.brng = brng; }); - callback(steps); + callback(directions); }); }; @@ -48,18 +49,11 @@ var generate_directions = function(coords, callback) { console.log(err) return false; // Should also return err } else { - var steps = response.json.routes[0].legs[0].steps; + var encodePolyline = response.json.routes[0].overview_polyline.points; + var polyline = polylineLib.decode(encodePolyline); tripPoints = []; - steps.forEach(function(step, index) { - var lat, lng; - if (index == steps.length) { - lat = step.end_location.lat; - lng = step.end_location.lng; - } else { - lat = step.start_location.lat; - lng = step.start_location.lng; - } - var stepLatLng = { lat: lat, lng: lng }; + polyline.forEach(function(step) { + var stepLatLng = { lat: step[0], lng: step[1] }; tripPoints.push(stepLatLng); }); callback(tripPoints); diff --git a/package.json b/package.json index 0753e18..5ae2469 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,9 @@ "bugs": { "url": "https://github.com/Ecotaco/bumblebee-bot/issues" }, - "engines" : { "node" : ">=7.0.0" }, + "engines": { + "node": ">=7.0.0" + }, "homepage": "https://github.com/Ecotaco/bumblebee-bot#readme", "scripts": { "start": "nodemon ./bin/www", @@ -33,6 +35,7 @@ "mongoose-timestamp": "^0.6.0", "morgan": "~1.7.0", "node-sass-middleware": "0.9.8", + "polyline": "^0.2.0", "pug": "~2.0.0-beta6", "serve-favicon": "~2.3.0", "socket.io": "^1.5.1" From 4eee06610ec2388d945ff2ef73a8ca9422cd3646 Mon Sep 17 00:00:00 2001 From: Jean Bertrand Date: Wed, 23 Nov 2016 17:13:36 +0100 Subject: [PATCH 7/7] Set url of backend in frontend from window location --- public/javascripts/index.js | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/public/javascripts/index.js b/public/javascripts/index.js index 5c0a181..8c3bdb4 100644 --- a/public/javascripts/index.js +++ b/public/javascripts/index.js @@ -7,7 +7,8 @@ var styleLayer = L.mapbox.styleLayer('mapbox://styles/ecotaco/civ00flry01gx2jl8d var featureGroup = L.featureGroup().addTo(map); var polygon = null; var drivers = []; -var socket = io('http://localhost:3000'); +var host_back = window.location.origin; +var socket = io(host_back); var drawControl = new L.Control.Draw({ edit: { featureGroup: featureGroup, @@ -141,7 +142,7 @@ $('#list').click(function() { }) $.ajax({ - url : 'http://localhost:3000/bots', + url : host_back + '/bots', success : function(data){ var html = ''; data.forEach(function(bot){ @@ -321,7 +322,7 @@ function display_points() { function delete_bot(id) { $.ajax({ type: 'DELETE', - url : 'http://localhost:3000/bots/' + id + url : host_back + '/bots/' + id }); swal.close(); } @@ -329,7 +330,7 @@ function delete_bot(id) { function pause_bot(id) { $.ajax({ type: 'POST', - url : 'http://localhost:3000/bots/' + id + '/deactivate' + url : host_back + '/bots/' + id + '/deactivate' }); swal.close(); } @@ -337,7 +338,7 @@ function pause_bot(id) { function active_bot(id) { $.ajax({ type: 'POST', - url : 'http://localhost:3000/bots/' + id + '/active' + url : host_back + '/bots/' + id + '/active' }); swal.close(); }