From bb0d027bc69db3273118e8d0f857857b9786982a Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Sun, 4 Nov 2018 13:33:47 +0000 Subject: [PATCH] added motion detection --- src/v1/js/Location.js | 76 ++++++++++++++++++++++++++++++++++++++----- 1 file changed, 68 insertions(+), 8 deletions(-) diff --git a/src/v1/js/Location.js b/src/v1/js/Location.js index 2130f18..499ecd0 100644 --- a/src/v1/js/Location.js +++ b/src/v1/js/Location.js @@ -55,21 +55,81 @@ const LocationModel = Backbone.Model.extend({ }); const updateLocation = (position) => { - console.log('New position', position); - const now = new Date(); + const latitude = Number.parseFloat(position.coords.latitude).toFixed(6); + const longitude = Number.parseFloat(position.coords.longitude).toFixed(6); - console.log(now); - console.log(this); + const location = { 'latitude': latitude, 'longitude': longitude, 'timestamp': position.timestamp }; + console.log('*** Updating Loc', location); + + this.processPosition(location); }; - const updatelocationthrottled = _.throttle(updateLocation, 10000); + const updateMotion = (position) => { + const latitude = Number.parseFloat(position.coords.latitude).toFixed(6); + const longitude = Number.parseFloat(position.coords.longitude).toFixed(6); + + if (!this.has('motion')) { + const now = new Date(); + const ts = now.getTime(); + const motion = { + 'cur': { + 'lat': latitude, + 'lon': longitude, + 'speed': 0, + 'ts' : ts + }, + 'prev': { + 'lat': latitude, + 'lon': longitude, + 'speed': 0, + 'ts' : ts + }, + 'speed': 0, + 'distance' : 0 + + }; + + this.set('motion', motion); + } + else { + // + const motion = this.get('motion'); + const now = new Date(); + const ts = now.getTime(); + const timeS = (ts - motion.prev.ts) / 1000.0; + + console.log('time dif', timeS); + + motion.prev = Object.assign({}, motion.cur); + motion.cur.lat = latitude; + motion.cur.lon = longitude; + motion.cur.ts = ts; + + const dist = distance(motion.prev.lat, motion.prev.lon, latitude, longitude); + console.log('Prev', motion.prev); + console.log('Cur', motion.cur); + + const speedMps = dist / timeS; + const speedKph = (speedMps * 3600.0) / 1000.0; + + motion.cur.speed = Number.parseFloat(speedKph).toFixed(3); + motion.speed = Number.parseFloat(speedKph).toFixed(3); + motion.distance = Number.parseFloat(dist).toFixed(3); + console.log(motion); + this.set('motion', motion); + } + }; + + const updatelocationthrottled = _.throttle(updateLocation, 120000); + const updateMotionhrottled = _.throttle(updateLocation, 15000); geolocation.on('change', function (position) { console.log('Location update'); updatelocationthrottled(position); + updateMotionhrottled(position); - const latitude = Number.parseFloat(position.coords.latitude).toFixed(6); + /* const latitude = Number.parseFloat(position.coords.latitude).toFixed(6); const longitude = Number.parseFloat(position.coords.longitude).toFixed(6); const location = { 'latitude': latitude, 'longitude': longitude, 'timestamp': position.timestamp }; @@ -82,9 +142,9 @@ const LocationModel = Backbone.Model.extend({ this.throttler = now.getTime() + 1000; } else - console.log('Throttling location update...'); + console.log('Throttling location update...');*/ - /*if (!this.has('motion')) { + /* if (!this.has('motion')) { const now = new Date(); const ts = now.getTime();