added motion detection

This commit is contained in:
Martin Donnelly 2018-11-04 12:41:50 +00:00
parent 7f7c1f1734
commit 1082355798
2 changed files with 55 additions and 8 deletions

View File

@ -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';

View File

@ -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(`
<div class="">Moving:<%=moving%></div>
<div class="">Moving:<%=moving.speed%></div>
`),
'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);