Skip to content

Commit 1deca7c

Browse files
committed
[Server] Adds endpoint to get current location
Does this either based on the coordinates or the ssids (actually it only takes one)
1 parent 88e5c4d commit 1deca7c

1 file changed

Lines changed: 54 additions & 1 deletion

File tree

server/httpserver/server.py

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,56 @@ def get(self):
299299
self.write(json.dumps({'type': 'listKeys','response': 'failure'}\
300300
,indent=4,separators=(',', ': ')))
301301

302+
class GetLocationHandler(tornado.web.RequestHandler):
303+
def get(self):
304+
try:
305+
user=getUserFromToken(self.get_argument("token"))
306+
finalJson={'type': 'getLocation','response': 'success'}
307+
found=False
308+
try:
309+
global locations
310+
lat=self.get_argument("latitude")
311+
longitude=self.get_argument("longitude")
312+
for l in locations:
313+
if l.isInside(lat,longitude):
314+
finalJson['location']=l.getName()
315+
found=True
316+
if not found:
317+
try:
318+
ssid=self.get_argument("ssid")
319+
for l in locations:
320+
if l.isInsideSsid(ssid):
321+
finalJson['location']=l.getName()
322+
found=True
323+
if not found:
324+
self.write(json.dumps({'type': 'getLocation','response': 'failure','reason':'no_location'}\
325+
,indent=4,separators=(',', ': ')))
326+
return
327+
except tornado.web.MissingArgumentError:
328+
self.write(json.dumps({'type': 'getLocation','response': 'failure','reason':'no_location'}\
329+
,indent=4,separators=(',', ': ')))
330+
self.write(json.dumps(finalJson))
331+
except tornado.web.MissingArgumentError:
332+
ssid=self.get_argument("ssid")
333+
for l in locations:
334+
if l.isInsideSsid(ssid):
335+
finalJson['location']=l.getName()
336+
found=True
337+
if not found:
338+
self.write(json.dumps({'type': 'getLocation','response': 'failure','reason':'no_location'}\
339+
,indent=4,separators=(',', ': ')))
340+
return
341+
self.write(json.dumps(finalJson))
342+
except LoginError:
343+
self.write(json.dumps({'type': 'getLocation','response': 'failure','reason':'incorrect_token'}\
344+
,indent=4,separators=(',', ': ')))
345+
except :
346+
traceback.print_exc()
347+
self.write(json.dumps({'type': 'getLocation','response': 'failure'}\
348+
,indent=4,separators=(',', ': ')))
349+
350+
351+
302352
def parseIDs(ids):
303353
return ids.split(",")
304354

@@ -326,6 +376,7 @@ def make_app():
326376
(r"/listMessages", ListMessagesHandler),
327377
(r"/addKey", AddKeyHandler),
328378
(r"/listKeys", ListKeysHandler),
379+
(r"/getLocation",GetLocationHandler)
329380
])
330381

331382
if __name__=="__main__":
@@ -339,7 +390,9 @@ def make_app():
339390
users=[newuser,]
340391
#logging.debug(str(newuser.add_token()))
341392
global locations
342-
locations=[location("RNL")]
393+
locations=[location(name="RNL",latitude=38.73884333333333,longitude=-9.137808333333334,radius=20,ssids=["eduroam"])]
394+
#locations=[location(name="RNL",ssids=["eduroam"])]
395+
343396
if len(sys.argv)>1 :
344397
PORT=int(sys.argv[1])
345398
app.listen(PORT)

0 commit comments

Comments
 (0)