From 10823557985e161863bbbe15f93ec23ba854dfee Mon Sep 17 00:00:00 2001 From: Martin Donnelly Date: Sun, 4 Nov 2018 12:41:50 +0000 Subject: [PATCH] added motion detection --- src/service-worker.js | 2 +- src/v1/js/Location.js | 61 ++++++++++++++++++++++++++++++++++++++----- 2 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/service-worker.js b/src/service-worker.js index 8bd8ffc..9ba8f7e 100644 --- a/src/service-worker.js +++ b/src/service-worker.js @@ -11,7 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. -const CACHE_VERSION = { 'version': '0.0.955' }; +const CACHE_VERSION = { 'version': '0.0.960' }; const PRECACHE = `jubileeData-${CACHE_VERSION.version}`; const RUNTIME = 'runtime'; diff --git a/src/v1/js/Location.js b/src/v1/js/Location.js index 1ab0d29..2d6231f 100644 --- a/src/v1/js/Location.js +++ b/src/v1/js/Location.js @@ -56,7 +56,10 @@ const LocationModel = Backbone.Model.extend({ geolocation.on('change', function (position) { console.log('Location update'); - const location = { 'latitude': position.coords.latitude, 'longitude': position.coords.longitude, 'timestamp': position.timestamp }; + 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 }; const now = new Date(); console.log(now.getTime(), now.getTime() - this.throttler); @@ -67,6 +70,53 @@ const LocationModel = Backbone.Model.extend({ else console.log('Throttling location update...'); + 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(); + + motion.prev = Object.assign({}, motion.cur); + motion.cur.lat = latitude; + motion.cur.lon = longitude; + motion.cur.ts = ts; + + const distance = distance(motion.prev.lat, motion.prev.lon, latitude, longitude); + + const timeS = (motion.cur.ts - motion.prev.ts) / 1000.0; + const speedMps = distance / timeS; + const speedKph = (speedMps * 3600.0) / 1000.0; + motion.cur.speed = speedKph; + motion.speed = speedKph; + motion.distance = distance; + console.log(motion); + this.set('motion', motion); + } + // this.set('location', location); }.bind(this)); @@ -81,10 +131,7 @@ const LocationModel = Backbone.Model.extend({ }, 'processPosition': function (pos) { console.log('processPosition'); - let { latitude, longitude, timestamp } = pos; - - latitude = Number.parseFloat(latitude).toFixed(6); - longitude = Number.parseFloat(longitude).toFixed(6); + const { latitude, longitude, timestamp } = pos; // const current = this.get('location'); const current = this.toJSON(); @@ -237,10 +284,10 @@ const LocationView = Backbone.View.extend({ 'initialize': function (options) { this.model.bind('change', this.render, this); }, 'template': _.template(` -
Moving:<%=moving%>
+
Moving:<%=moving.speed%>
`), 'render': function () { - this.$el.html(this.template(this.model.attributes)); + // this.$el.html(this.template(this.model.attributes)); console.log('>> location attributes', this.model.attributes);