214 lines
8.1 KiB
Python
214 lines
8.1 KiB
Python
|
|
from tpmcqr_service import redis_client
|
|
from tpmcqr_service.models.gtfs import QRDev, Shapes, Paraderos, Trips, Stops, find_shape_position
|
|
import time
|
|
import pickle
|
|
import math
|
|
|
|
def parada_ruta_expediciones(redis_id, parada_id, lineacode):
|
|
|
|
info_linea = dict()
|
|
|
|
paradadb = obtiene_datos_parada(parada_id)
|
|
if not paradadb:
|
|
info_linea['debug'] = 'Invalid QR'
|
|
return 400, info_linea
|
|
|
|
lineadb = obtiene_datos_linea(lineacode)
|
|
if not lineadb:
|
|
info_linea['debug'] = 'Invalid Lineas'
|
|
return 400, info_linea
|
|
|
|
rt_linea = pickle.loads(redis_client.get(lineadb.id_linea))
|
|
|
|
if len(rt_linea['servicios']) == 0:
|
|
info_linea['debug'] = 'No data'
|
|
info_linea['servicios'] = 0
|
|
return 400, info_linea
|
|
|
|
rt_ppus = pickle.loads(redis_client.get('ppus'))
|
|
|
|
for key in ['stop_name', 'stop_lat', 'stop_lon']:
|
|
info_linea[key] = getattr(paradadb, key)
|
|
|
|
for key in ['route_short_name', 'route_long_name', 'route_color', 'route_text_color', 'lur']:
|
|
info_linea[key] = rt_linea[key]
|
|
|
|
shape, paradas = obtiene_shape_paradas(rt_linea['id_shape'], rt_ppus[rt_linea['servicios'][0][1]]['id_trip'])
|
|
|
|
info_linea['route_shape'] = shape
|
|
info_linea['route_stops'] = paradas
|
|
parada_pos, parada_distance = find_shape_position(rt_linea['id_shape'], paradadb.stop_lat, paradadb.stop_lon)
|
|
info_linea['servicios'] = []
|
|
info_linea['recientes'] = []
|
|
|
|
for item in rt_linea['servicios']:
|
|
|
|
if item[0] < parada_distance:
|
|
expediciones_enruta.append(item)
|
|
else:
|
|
expediciones_pasadas.append(item)
|
|
|
|
if len(expediciones_enruta) > 0:
|
|
expedicion = expediciones_enruta.pop(-1)
|
|
expedicion_pos = expedicion[4]
|
|
trip_shape = [{'lat': expedicion[2], 'lon': expedicion[3]}]
|
|
if expedicion_pos != parada_pos:
|
|
trip_shape += shape[expedicion_pos:parada_pos-1]
|
|
trip_shape += [{'lat': info_linea['stop_lat'], 'lon': info_linea['stop_lon']}]
|
|
|
|
info_linea['servicios'].append( {'ppu': expedicion[1], 'trip_shape': trip_shape} )
|
|
|
|
if len(expediciones_enruta) > 0:
|
|
expedicion = expediciones_enruta.pop(-1)
|
|
expedicion_pos = expedicion[4]
|
|
trip_shape = [{'lat': expedicion[2], 'lon': expedicion[3]}]
|
|
if expedicion_pos != parada_pos:
|
|
trip_shape += shape[expedicion_pos:parada_pos-1]
|
|
trip_shape += [{'lat': info_linea['stop_lat'], 'lon': info_linea['stop_lon']}]
|
|
|
|
info_linea['servicios'].append( {'ppu': expedicion[1], 'trip_shape': trip_shape} )
|
|
|
|
if len(expediciones_pasadas) > 0:
|
|
|
|
expedicion = expediciones_enruta.pop(0)
|
|
expedicion_pos = expedicion[4]
|
|
trip_shape = [{'lat': info_linea['stop_lat'], 'lon': info_linea['stop_lon']}]
|
|
if expedicion_pos != parada_pos:
|
|
trip_shape += shape[parada_pos:expedicion_pos-1]
|
|
trip_shape += [{'lat': expedicion[2], 'lon': expedicion[3]}]
|
|
|
|
info_linea['recientes'].append( {'ppu': expedicion[1], 'trip_shape': trip_shape} )
|
|
|
|
if len(expediciones_pasadas) > 0:
|
|
expedicion = expediciones_enruta.pop(0)
|
|
expedicion_pos = expedicion[4]
|
|
trip_shape = [{'lat': info_linea['stop_lat'], 'lon': info_linea['stop_lon']}]
|
|
if expedicion_pos != parada_pos:
|
|
trip_shape += shape[parada_pos:expedicion_pos-1]
|
|
trip_shape += [{'lat': expedicion[2], 'lon': expedicion[3]}]
|
|
|
|
info_linea['recientes'].append( {'ppu': expedicion[1], 'trip_shape': trip_shape} )
|
|
|
|
|
|
|
|
def calcula_distancias_parada(redis_id, parada_id):
|
|
salida_parada = dict()
|
|
|
|
devdb = QRDev.query.filter(QRDev.id_dispositivo==parada_id).one_or_none()
|
|
if not devdb:
|
|
salida_parada['debug'] = 'Invalid Request'
|
|
return 400, salida_parada
|
|
|
|
paradadb = obtiene_datos_parada(devdb.id_paradero)
|
|
if not paradadb:
|
|
salida_parada['debug'] = 'Parada {}'.format(devdb.id_paradero)
|
|
return 200, salida_parada
|
|
|
|
|
|
for key in ['stop_name', 'stop_lat', 'stop_lon']:
|
|
salida_parada[key] = getattr(paradadb, key)
|
|
|
|
salida_parada['lineas'] = []
|
|
|
|
for id_linea in obtiene_lineas_parada(devdb.id_paradero):
|
|
rt_linea = pickle.loads(redis_client.get(id_linea))
|
|
|
|
info_linea = dict()
|
|
|
|
for key in ['route_short_name', 'route_long_name', 'route_color', 'route_text_color', 'lur']:
|
|
info_linea[key] = rt_linea[key]
|
|
|
|
info_linea['servicios'] = []
|
|
info_linea['recientes'] = []
|
|
|
|
if 'id_shape' in rt_linea:
|
|
parada_pos, parada_distance = find_shape_position(rt_linea['id_shape'], salida_parada['stop_lat'], salida_parada['stop_lon'])
|
|
|
|
expediciones_enruta = []
|
|
expediciones_pasadas = []
|
|
for item in rt_linea['servicios']:
|
|
if item[0] < parada_distance:
|
|
expediciones_enruta.append(item)
|
|
else:
|
|
expediciones_pasadas.append(item)
|
|
|
|
if len(expediciones_enruta) > 0:
|
|
info_linea['servicios'].append( estima_llegada(parada_distance, expediciones_enruta.pop(-1)) )
|
|
if len(expediciones_enruta) > 0:
|
|
info_linea['servicios'].append( estima_llegada(parada_distance, expediciones_enruta.pop(-1)) )
|
|
|
|
if len(expediciones_pasadas) > 0:
|
|
info_linea['recientes'].append( estima_llegada(parada_distance, expediciones_pasadas.pop(0)) )
|
|
if len(expediciones_pasadas) > 0:
|
|
info_linea['recientes'].append( estima_llegada(parada_distance, expediciones_pasadas.pop(0)) )
|
|
|
|
salida_parada['lineas'].append(info_linea)
|
|
|
|
return 200, salida_parada
|
|
|
|
|
|
def estima_llegada(parada_distance, expedicion):
|
|
ts = int(time.time())
|
|
data_keys = ['trip_traveled', 'ppu', 'trip_lat', 'trip_lon', 'trip_pos', 'ts']
|
|
trip_info = dict(zip(data_keys, expedicion))
|
|
trip_info['drift'] = int(trip_info['ts']) - ts
|
|
trip_info['trip_distance'] = parada_distance - int(trip_info['trip_traveled'])
|
|
estimator = int(trip_info['trip_distance'] / 5) # 18 Km/h promedio -> 5 m/s
|
|
trip_info['debug_estimator'] = estimator
|
|
|
|
if estimator > 1570: # 26:10 minutos
|
|
trip_info['trip_estimator'] = '25-30 minutos'
|
|
elif estimator > 1260: # #21 minutos
|
|
trip_info['trip_estimator'] = '20-25 minutos'
|
|
elif estimator > 950: #15:50 minutos
|
|
trip_info['trip_estimator'] = '15-20 minutos'
|
|
elif estimator > 640: #10:40 minutos
|
|
trip_info['trip_estimator'] = '10-15 minutos'
|
|
elif estimator > 330: # 5:30
|
|
trip_info['trip_estimator'] = '5-10 minutos'
|
|
elif estimator > 200: # 3:20 sec
|
|
trip_info['trip_estimator'] = '3 minutos'
|
|
elif estimator > 70: #
|
|
trip_info['trip_estimator'] = 'Llegando'
|
|
elif estimator > 0: #
|
|
trip_info['trip_estimator'] = 'En parada'
|
|
else: #
|
|
salida = math.ceil(-estimator/60)
|
|
trip_info['trip_estimator'] = 'Hace {} minutos'.format(salida)
|
|
|
|
return trip_info
|
|
|
|
def obtiene_shape_paradas(id_shape, id_trip):
|
|
shape = []
|
|
paradas = []
|
|
|
|
for item in Shapes.query.filter(Shapes.id_shape==id_shape).order_by(Shape.shape_pt_sequence.asc()).all():
|
|
shape.append({'lat': item.shape_pt_lat, 'lon': item.shape_pt_lon})
|
|
|
|
for item in Paraderos.query.join(Stops).filter(Stops.id_trip==id_trip).distinct(Paraderos.id_paradero).order_by(Stops.stop_sequence.asc()).all():
|
|
paradas.append({'name': item.stop_name, 'lat': item.stop_lat, 'lon': item.stop_lon})
|
|
|
|
return shape, paradas
|
|
|
|
def obtiene_datos_parada(QRCode):
|
|
devdb = QRDev.query.filter(QRDev.id_dispositivo==QRCode).one_or_none()
|
|
if not devdb:
|
|
return None
|
|
|
|
paradadb = Paraderos.query.filter(Paraderos.id_paradero==devdb.id_paradero).one_or_none()
|
|
|
|
return paradadb
|
|
|
|
def obtiene_datos_linea(codigo_linea):
|
|
parada = Lineas.query.filter(Lineas.route_short_name==id_paradero).one_or_none()
|
|
if parada is None:
|
|
return None
|
|
return parada
|
|
|
|
def obtiene_lineas_parada(id_paradero):
|
|
lineas = []
|
|
for linea in Trips.query.join(Stops).filter(Stops.id_paradero == id_paradero).distinct(Trips.id_linea).all():
|
|
lineas.append(linea.id_linea)
|
|
return lineas
|