Update app.py
Browse files
app.py
CHANGED
|
@@ -5,9 +5,8 @@ from datetime import datetime
|
|
| 5 |
import folium
|
| 6 |
from folium import plugins
|
| 7 |
import webbrowser
|
|
|
|
| 8 |
from geopy.distance import geodesic
|
| 9 |
-
import threading
|
| 10 |
-
from flask import Flask, request, jsonify
|
| 11 |
|
| 12 |
# Configuração do banco de dados
|
| 13 |
def init_db():
|
|
@@ -30,26 +29,6 @@ def init_db():
|
|
| 30 |
|
| 31 |
init_db()
|
| 32 |
|
| 33 |
-
# Criação do servidor Flask para a API
|
| 34 |
-
flask_app = Flask(__name__)
|
| 35 |
-
|
| 36 |
-
@flask_app.route('/api/location', methods=['POST'])
|
| 37 |
-
def api_receive_location():
|
| 38 |
-
data = request.form
|
| 39 |
-
coords = data['coords'].split(",")
|
| 40 |
-
result = add_location(
|
| 41 |
-
data['device_id'],
|
| 42 |
-
data['device_name'],
|
| 43 |
-
coords[0],
|
| 44 |
-
coords[1],
|
| 45 |
-
data.get('accuracy', 10),
|
| 46 |
-
data.get('battery', 100)
|
| 47 |
-
)
|
| 48 |
-
return jsonify({"status": "success" if "✅" in result else "error"})
|
| 49 |
-
|
| 50 |
-
def run_flask():
|
| 51 |
-
flask_app.run(port=8000)
|
| 52 |
-
|
| 53 |
# Função para adicionar localização
|
| 54 |
def add_location(device_id, device_name, latitude, longitude, accuracy=10, battery=100):
|
| 55 |
try:
|
|
@@ -75,29 +54,25 @@ def add_location(device_id, device_name, latitude, longitude, accuracy=10, batte
|
|
| 75 |
except Exception as e:
|
| 76 |
return f"❌ Erro: {str(e)}"
|
| 77 |
|
| 78 |
-
# Função para capturar coordenadas via navegador
|
| 79 |
def start_gps_capture(device_id, device_name):
|
|
|
|
| 80 |
html_content = f"""
|
| 81 |
<!DOCTYPE html>
|
| 82 |
<html>
|
| 83 |
<head>
|
| 84 |
<title>Captura de Localização</title>
|
| 85 |
<script>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
function captureLocation() {{
|
| 87 |
if (navigator.geolocation) {{
|
| 88 |
-
navigator.geolocation.
|
| 89 |
position => {{
|
| 90 |
-
|
| 91 |
-
`${{position.coords.latitude}},${{position.coords.longitude}}`;
|
| 92 |
-
document.getElementById('accuracy').value = position.coords.accuracy;
|
| 93 |
-
if (navigator.getBattery) {{
|
| 94 |
-
navigator.getBattery().then(batt => {{
|
| 95 |
-
document.getElementById('battery').value = Math.round(batt.level * 100);
|
| 96 |
-
document.forms['locForm'].submit();
|
| 97 |
-
}});
|
| 98 |
-
}} else {{
|
| 99 |
-
document.forms['locForm'].submit();
|
| 100 |
-
}}
|
| 101 |
}},
|
| 102 |
error => {{
|
| 103 |
alert("Erro: " + error.message);
|
|
@@ -108,19 +83,12 @@ def start_gps_capture(device_id, device_name):
|
|
| 108 |
alert("Geolocalização não suportada neste navegador.");
|
| 109 |
}}
|
| 110 |
}}
|
| 111 |
-
window.onload = captureLocation;
|
| 112 |
</script>
|
| 113 |
</head>
|
| 114 |
<body>
|
| 115 |
-
<
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
<input type="hidden" id="coords" name="coords">
|
| 119 |
-
<input type="hidden" id="accuracy" name="accuracy" value="10">
|
| 120 |
-
<input type="hidden" id="battery" name="battery" value="100">
|
| 121 |
-
</form>
|
| 122 |
-
<h2>Capturando localização...</h2>
|
| 123 |
-
<p>Permaneça nesta página para atualizações automáticas.</p>
|
| 124 |
</body>
|
| 125 |
</html>
|
| 126 |
"""
|
|
@@ -129,7 +97,7 @@ def start_gps_capture(device_id, device_name):
|
|
| 129 |
f.write(html_content)
|
| 130 |
|
| 131 |
webbrowser.open("gps_capture.html")
|
| 132 |
-
return "✔️ Abrindo captura de GPS no navegador..
|
| 133 |
|
| 134 |
# Função para obter mapa atualizado
|
| 135 |
def get_updated_map():
|
|
@@ -174,7 +142,7 @@ def create_interface():
|
|
| 174 |
with gr.Row():
|
| 175 |
with gr.Column():
|
| 176 |
gr.Markdown("### Captura Automática")
|
| 177 |
-
gps_btn = gr.Button("📡
|
| 178 |
gps_status = gr.Textbox(interactive=False)
|
| 179 |
|
| 180 |
gps_btn.click(
|
|
@@ -213,11 +181,5 @@ def create_interface():
|
|
| 213 |
return demo
|
| 214 |
|
| 215 |
if __name__ == "__main__":
|
| 216 |
-
# Iniciar Flask em uma thread separada
|
| 217 |
-
flask_thread = threading.Thread(target=run_flask)
|
| 218 |
-
flask_thread.daemon = True
|
| 219 |
-
flask_thread.start()
|
| 220 |
-
|
| 221 |
-
# Iniciar interface Gradio
|
| 222 |
demo = create_interface()
|
| 223 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 5 |
import folium
|
| 6 |
from folium import plugins
|
| 7 |
import webbrowser
|
| 8 |
+
import os
|
| 9 |
from geopy.distance import geodesic
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Configuração do banco de dados
|
| 12 |
def init_db():
|
|
|
|
| 29 |
|
| 30 |
init_db()
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Função para adicionar localização
|
| 33 |
def add_location(device_id, device_name, latitude, longitude, accuracy=10, battery=100):
|
| 34 |
try:
|
|
|
|
| 54 |
except Exception as e:
|
| 55 |
return f"❌ Erro: {str(e)}"
|
| 56 |
|
| 57 |
+
# Função para capturar coordenadas via navegador (versão simplificada)
|
| 58 |
def start_gps_capture(device_id, device_name):
|
| 59 |
+
# Cria um arquivo HTML temporário com JavaScript para captura de GPS
|
| 60 |
html_content = f"""
|
| 61 |
<!DOCTYPE html>
|
| 62 |
<html>
|
| 63 |
<head>
|
| 64 |
<title>Captura de Localização</title>
|
| 65 |
<script>
|
| 66 |
+
function sendLocation(lat, lng) {{
|
| 67 |
+
// Esta função será chamada quando o usuário clicar no botão de compartilhar
|
| 68 |
+
window.location.href = `gradio://localhost:7860/?device_id={device_id}&device_name={device_name}&latitude=${{lat}}&longitude=${{lng}}`;
|
| 69 |
+
}}
|
| 70 |
+
|
| 71 |
function captureLocation() {{
|
| 72 |
if (navigator.geolocation) {{
|
| 73 |
+
navigator.geolocation.getCurrentPosition(
|
| 74 |
position => {{
|
| 75 |
+
sendLocation(position.coords.latitude, position.coords.longitude);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 76 |
}},
|
| 77 |
error => {{
|
| 78 |
alert("Erro: " + error.message);
|
|
|
|
| 83 |
alert("Geolocalização não suportada neste navegador.");
|
| 84 |
}}
|
| 85 |
}}
|
|
|
|
| 86 |
</script>
|
| 87 |
</head>
|
| 88 |
<body>
|
| 89 |
+
<h1>Compartilhar Localização</h1>
|
| 90 |
+
<button onclick="captureLocation()">Compartilhar Minha Localização Atual</button>
|
| 91 |
+
<p>Permita o acesso à localização quando solicitado.</p>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 92 |
</body>
|
| 93 |
</html>
|
| 94 |
"""
|
|
|
|
| 97 |
f.write(html_content)
|
| 98 |
|
| 99 |
webbrowser.open("gps_capture.html")
|
| 100 |
+
return "✔️ Abrindo captura de GPS no navegador. Por favor, compartilhe sua localização quando solicitado."
|
| 101 |
|
| 102 |
# Função para obter mapa atualizado
|
| 103 |
def get_updated_map():
|
|
|
|
| 142 |
with gr.Row():
|
| 143 |
with gr.Column():
|
| 144 |
gr.Markdown("### Captura Automática")
|
| 145 |
+
gps_btn = gr.Button("📡 Obter Localização Atual")
|
| 146 |
gps_status = gr.Textbox(interactive=False)
|
| 147 |
|
| 148 |
gps_btn.click(
|
|
|
|
| 181 |
return demo
|
| 182 |
|
| 183 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
demo = create_interface()
|
| 185 |
demo.launch(server_name="0.0.0.0", server_port=7860)
|