added motion detection

This commit is contained in:
Martin Donnelly 2018-11-04 13:33:47 +00:00
parent f8f548c4a4
commit bb0d027bc6

View File

@ -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();