From 6805f2fb5fe1d291ef04388d4a9cb08345fa7881 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Laura=20Kl=C3=BCnder?= Date: Sun, 24 Dec 2017 04:37:05 +0100 Subject: [PATCH] add initil location api and javascript calls to it --- src/c3nav/routing/api.py | 6 ++++ src/c3nav/site/static/site/js/c3nav.js | 35 +++++++++++++++++++ .../site/static/site/js/fakemobileclient.js | 7 ++-- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/src/c3nav/routing/api.py b/src/c3nav/routing/api.py index 9add2dcc..82e25b20 100644 --- a/src/c3nav/routing/api.py +++ b/src/c3nav/routing/api.py @@ -16,6 +16,7 @@ class RoutingViewSet(ViewSet): """ /route/ Get routes. /options/ Get or set route options. + /locate/ Wifi locate. """ @list_route(methods=['get', 'post']) def route(self, request, *args, **kwargs): @@ -76,3 +77,8 @@ class RoutingViewSet(ViewSet): options.save() return Response(options.serialize()) + + @list_route(methods=('POST', )) + def locate(self, request, *args, **kwargs): + # todo: implement wifi location + return Response({'location': None}) diff --git a/src/c3nav/site/static/site/js/c3nav.js b/src/c3nav/site/static/site/js/c3nav.js index a711655a..c49ef5eb 100644 --- a/src/c3nav/site/static/site/js/c3nav.js +++ b/src/c3nav/site/static/site/js/c3nav.js @@ -127,6 +127,10 @@ c3nav = { window.setInterval(c3nav.load_searchable_locations, 42000); + if (window.mobileclient) { + window.setInterval(function() { mobileclient.scanNow(); }, 2000); + } + c3nav.init_completed = true; }, get_csrf_token: function() { @@ -1217,10 +1221,41 @@ c3nav = { var $user = $('header #user'); $user.find('span').text(data.title); $user.find('small').text(data.subtitle || ''); + }, + + _last_wifi_scant: 0, + _wifi_scan_results: function(data) { + if (!JSON.parse(data).length) { + c3nav._set_user_location(null); + } + var now = Date.now(); + if (now-2000 < c3nav._last_wifi_scan) return; + + $.post({ + url: '/api/routing/locate/', + data: data, + dataType: 'json', + contentType: 'application/json', + beforeSend: function(xhrObj){ + xhrObj.setRequestHeader('X-CSRFToken', c3nav.get_csrf_token()); + }, + success: function(data) { + c3nav._set_user_location(data.location); + } + }).fail(function() { + c3nav._set_user_location(null); + }); + }, + _set_user_location: function(location) { + } }; $(document).ready(c3nav.init); +function nearby_stations_available() { + c3nav._wifi_scan_results(mobileclient.getNearbyStations()); +} + LevelControl = L.Control.extend({ options: { diff --git a/src/c3nav/site/static/site/js/fakemobileclient.js b/src/c3nav/site/static/site/js/fakemobileclient.js index 9c9ab232..7d5424b3 100644 --- a/src/c3nav/site/static/site/js/fakemobileclient.js +++ b/src/c3nav/site/static/site/js/fakemobileclient.js @@ -8,12 +8,15 @@ mobileclient = { return JSON.stringify(this.nearbyStations); }, port: 8042, + wait: false, scanNow: function() { console.log('mobileclient: scanNow'); + if (mobileclient.wait) return; + mobileclient.wait = true; $.getJSON('http://localhost:'+String(mobileclient.port)+'/scan', function(data) { mobileclient.setNearbyStations(data.data); - }).fail(function() { - mobileclient.scanNow(); + }).always(function() { + mobileclient.wait = false; }); }, shareUrl: function(url) {