Spaces:
Sleeping
Sleeping
File size: 4,785 Bytes
ce41917 cf14e32 8bc50ed ce41917 737ad85 cf14e32 ce41917 cf14e32 ce41917 8bc50ed ce41917 8bc50ed ce41917 8bc50ed ce41917 8bc50ed ce41917 cf14e32 ce41917 8bc50ed ce41917 8bc50ed cf14e32 ce41917 8bc50ed cf14e32 ce41917 8bc50ed cf14e32 ce41917 |
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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
from fastapi import FastAPI, HTTPException, Request, Form
from fastapi.responses import HTMLResponse
from fastapi.templating import Jinja2Templates
from datetime import datetime, timedelta
import random
import folium
from folium.plugins import MarkerCluster
import csv
import os
app = FastAPI()
templates = Jinja2Templates(directory="templates")
CSV_FILE = "wifi_signals.csv"
@app.post("/api/generate-data")
def generate_data():
base_latitude = 35.6837
base_longitude = 139.6805
start_date = datetime(2024, 8, 1)
end_date = datetime(2024, 8, 7)
delta = end_date - start_date
with open(CSV_FILE, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['latitude', 'longitude', 'timestamp', 'signal_strength'])
for _ in range(100):
random_days = random.randint(0, delta.days)
random_seconds = random.randint(0, 86400)
random_time = start_date + timedelta(days=random_days, seconds=random_seconds)
random_latitude = base_latitude + random.uniform(-0.01, 0.01)
random_longitude = base_longitude + random.uniform(-0.01, 0.01)
random_signal_strength = random.randint(0, 4)
writer.writerow([
random_latitude,
random_longitude,
random_time.strftime('%Y-%m-%d %H:%M:%S'),
random_signal_strength
])
return {"message": "Demo data generated successfully"}
@app.delete("/api/delete-data")
def delete_data():
try:
if os.path.exists(CSV_FILE):
os.remove(CSV_FILE)
# Create a new empty CSV file with headers
with open(CSV_FILE, mode='w', newline='') as file:
writer = csv.writer(file)
writer.writerow(['latitude', 'longitude', 'timestamp', 'signal_strength'])
return {"message": "Data file deleted and reset successfully"}
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@app.post("/api/upload")
async def upload_data(
latitude: float = Form(...),
longitude: float = Form(...),
timestamp: str = Form(...),
signal_strength: int = Form(...)
):
try:
# Validate timestamp format
datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
with open(CSV_FILE, mode='a', newline='') as file:
writer = csv.writer(file)
writer.writerow([latitude, longitude, timestamp, signal_strength])
return {"message": "Data uploaded successfully"}
except ValueError:
raise HTTPException(status_code=400, detail="Invalid timestamp format. Use 'YYYY-MM-DD HH:MM:SS'")
except Exception as e:
raise HTTPException(status_code=500, detail=f"An error occurred: {str(e)}")
@app.get("/", response_class=HTMLResponse)
def show_map(request: Request, start_date: str = None, end_date: str = None):
signal_data = []
if os.path.exists(CSV_FILE):
with open(CSV_FILE, mode='r') as file:
reader = csv.reader(file)
next(reader) # Skip header row
for row in reader:
lat, lon, timestamp, strength = row
timestamp = datetime.strptime(timestamp, '%Y-%m-%d %H:%M:%S')
if start_date and end_date:
start_datetime = datetime.strptime(start_date, '%Y-%m-%d')
end_datetime = datetime.strptime(end_date, '%Y-%m-%d')
if start_datetime <= timestamp <= end_datetime:
signal_data.append((float(lat), float(lon), int(strength)))
else:
signal_data.append((float(lat), float(lon), int(strength)))
if signal_data:
min_signal = min(s[2] for s in signal_data)
max_signal = max(s[2] for s in signal_data)
else:
min_signal = max_signal = 1
m = folium.Map(location=[35.6837, 139.6805], zoom_start=12)
marker_cluster = MarkerCluster().add_to(m)
for lat, lon, signal_strength in signal_data:
if max_signal - min_signal != 0:
radius = 5 + 10 * ((signal_strength - min_signal) / (max_signal - min_signal))
else:
radius = 15
folium.CircleMarker(
location=[lat, lon],
radius=radius,
popup=f'Signal Strength: {signal_strength}',
color='blue',
fill=True,
fill_opacity=0.6
).add_to(marker_cluster)
map_html = m._repr_html_()
return templates.TemplateResponse("map.html", {"request": request, "map_html": map_html, "start_date": start_date, "end_date": end_date})
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=5000)
|