forked from gaffner/NoGPSNavigator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
58 lines (40 loc) · 1.7 KB
/
server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
from datetime import datetime
import logging
from typing import List, Dict
from fastapi import FastAPI, Request
from DBSaver.DBSaver import DBSaver
import utils
from geocoding import get_address_from_lating, get_lating_from_address
from wifi import get_location_from_wifi
logging.basicConfig(filename='gps.log', level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s', encoding='utf-16')
app = FastAPI()
# Initialize DBSaver instance
db_saver = DBSaver()
@app.get('/')
def root(request: Request):
logging.info(f"Got keepalive from {request.client.host}")
return {'is_alive': True}
@app.post('/wifi')
def wifi(request: Request, access_points: List[Dict]):
converted_request = utils.convert_nogps_request_to_google_request(access_points)
logging.info(f"Wifi list from: {request.client.host} is: {access_points}")
for ap in access_points:
ap['timestamp'] = datetime.now().isoformat()
db_saver.save_wifi_to_db(ap)
return get_location_from_wifi(converted_request)
@app.get('/geocoding/{address}')
def geocoding(request: Request, address: str):
logging.info(f"{request.client.host} ask coordinates for {address}")
return get_lating_from_address(address)
@app.get('/reverse-geocoding/{lating}')
def reverse_geocoding(request: Request, lating: str):
logging.info(f"{request.client.host} ask Getting street name for {lating}")
return get_address_from_lating(lating)
@app.post('/send-logs')
def send_logs(request: Request):
logging.info(f"{request.client.host} sent logs")
utils.save_user_logs(request)
if __name__ == '__main__':
import uvicorn
logging.info(f"starting NoGPSNavigator server")
uvicorn.run(app, host="0.0.0.0", port=8080)