This repository has been archived by the owner on Jan 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
146 lines (112 loc) · 3.74 KB
/
app.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
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
from flask import Flask, request, jsonify, g
from flask_cors import CORS
import sqlite3
from simulador import Simulador
from coordenada import Coordenada
from aeropuerto import Aeropuerto
DATABASE = './airports.db'
sim = Simulador()
app = Flask(__name__)
CORS(app, resources={r"/*": {"origins": "*"}})
def get_db():
db = getattr(g, '_database', None)
if db is None:
db = g._database = sqlite3.connect(DATABASE)
db.row_factory = sqlite3.Row
return db
def query_db(query, args=(), one=False):
cur = get_db().cursor()
cur.execute(query, args)
rv = cur.fetchall()
cur.close()
return (rv[0] if rv else None) if one else rv
@app.teardown_appcontext
def close_connection(exception):
db = getattr(g, '_database', None)
if db is not None:
db.close()
@app.route('/ping', methods=['GET'])
def ping():
status = "200"
message = "pong"
return jsonify(status=status,
message=message)
@app.route('/ubicacion', methods=['GET'])
def ubicacion():
status = "200"
message = sim.ubicacion_actual()
return jsonify(status=status,
message=message)
@app.route('/tiempo_de_vuelo_actual', methods=['GET'])
def tiempo_de_vuelo_actual():
status = "200"
message = sim.tiempo_transcurrido()
return jsonify(status=status,
message=message)
@app.route('/tiempo_de_vuelo_restante', methods=['GET'])
def tiempo_de_vuelo_restante():
status = "200"
message = sim.tiempo_restante()
return jsonify(status=status,
message=message)
@app.route('/temperatura_exterior', methods=['GET'])
def temperatura_exterior():
status = "200"
message = sim.temperatura_exterior()
return jsonify(status=status,
message=message)
@app.route('/temperatura_interior', methods=['GET'])
def temperatura_interior():
status = "200"
message = sim.temperatura_interior()
return jsonify(status=status,
message=message)
@app.route('/altitud', methods=['GET'])
def altitud():
status = "200"
message = sim.altitud()
return jsonify(status=status,
message=message)
@app.route('/velocidad', methods=['GET'])
def velocidad():
status = "200"
message = sim.vel()
return jsonify(status=status,
message=message)
@app.route('/iniciar_vuelo', methods=['POST'])
def iniciar_vuelo():
content = request.get_json()
origen = Coordenada(float(content["lat1"]), float(content["lon1"]))
destino = Coordenada(float(content["lat2"]), float(content["lon2"]))
sim.definir_coordenadas(origen, destino)
sim.definir_velocidad(content["mach"])
sim.iniciar_vuelo()
status = "200"
message = "OK"
return jsonify(status=status,
message=message)
@app.route('/paises', methods=['GET'])
def paises():
message = []
aero = Aeropuerto("", "", "", "", "", "", "", "", "", "", "")
q = query_db(aero.countries_sql())
for row in q:
message.append(row['country'])
return jsonify(message)
@app.route('/aeropuertos/<string:pais>', methods=['GET'])
def aeropuerto(pais):
message = []
aero = Aeropuerto("", "", "", "", "", "", "", "", "", "", "")
q = query_db(aero.airports_in_country_sql(), (pais,))
for row in q:
aero = Aeropuerto(str(row['airport_id']), row['name'],
row['city'], row['country'],
row['iata'], row['icao'],
str(row['latitude']), str(row['longitude']),
str(row['altitude']), str(row['timezone']),
row['timezone_text'])
dictionary = aero.to_dict()
message.append(dictionary)
return jsonify(message)
if __name__ == "__main__":
app.run(host="0.0.0.0")