forked from TDTP/admin_transporte_backend
getInfoDevice lee informacion desde redis
parent
169312b500
commit
8709d39b76
|
@ -1,5 +1,6 @@
|
||||||
|
|
||||||
@server = http://localhost:4000/api
|
@server = http://localhost:4000/api
|
||||||
|
# @server = https://transporte.hz.kursor.cl/api
|
||||||
@token = {{login.response.body.$.token}}
|
@token = {{login.response.body.$.token}}
|
||||||
|
|
||||||
###
|
###
|
||||||
|
|
|
@ -6,6 +6,8 @@ from django.db import connection
|
||||||
from django_filters.rest_framework import DjangoFilterBackend
|
from django_filters.rest_framework import DjangoFilterBackend
|
||||||
from rest_framework.decorators import action
|
from rest_framework.decorators import action
|
||||||
from .. import models, serializers
|
from .. import models, serializers
|
||||||
|
from os import getenv
|
||||||
|
import redis
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
|
@ -55,31 +57,51 @@ class DispositivoViewSet(viewsets.ModelViewSet):
|
||||||
.annotate(nro_paradero=F('id_paradero'), nombre_paradero=F('stop_name')) \
|
.annotate(nro_paradero=F('id_paradero'), nombre_paradero=F('stop_name')) \
|
||||||
.first()
|
.first()
|
||||||
|
|
||||||
query = "SELECT json_data \
|
db_host = getenv('DB_REDIS_HOST')
|
||||||
from gtfs_posiciones_json \
|
db_port = getenv('DB_REDIS_PORT')
|
||||||
where stop_id = %s"
|
r = redis.Redis(host=db_host, port=db_port, decode_responses=True)
|
||||||
|
|
||||||
params = [ record.nro_paradero ]
|
key = f'stop_id:{record.nro_paradero}'
|
||||||
|
json_trayectos = r.get(key)
|
||||||
|
if json_trayectos != None:
|
||||||
|
trayectos = json.loads(json_trayectos)
|
||||||
|
else:
|
||||||
|
trayectos = []
|
||||||
|
|
||||||
with connection.cursor() as cursor:
|
|
||||||
cursor.execute(query, params)
|
|
||||||
row = cursor.fetchone()
|
|
||||||
|
|
||||||
connection.close()
|
lineas = {}
|
||||||
|
lineas_agrupadas = {}
|
||||||
|
for t in trayectos:
|
||||||
|
|
||||||
|
pk_linea = f'{t["route_id"]}-{t["direction_id"]}'
|
||||||
|
if pk_linea not in lineas:
|
||||||
|
lineas[pk_linea] = models.Linea.objects.get(id_linea=pk_linea)
|
||||||
|
|
||||||
|
linea = lineas[pk_linea]
|
||||||
|
|
||||||
|
if pk_linea not in lineas_agrupadas:
|
||||||
|
lineas_agrupadas[pk_linea] = {
|
||||||
|
'Linea': pk_linea,
|
||||||
|
'Descripcion': linea.route_long_name,
|
||||||
|
'TipoLocomocion': linea.route_type.descripcion,
|
||||||
|
'colorFondo': linea.route_color,
|
||||||
|
'colorTexto': linea.route_text_color,
|
||||||
|
'Llegadas': []
|
||||||
|
}
|
||||||
|
|
||||||
|
lineas_agrupadas[pk_linea]['Llegadas'].append({
|
||||||
|
'patente': t['vehicle_license_plate'],
|
||||||
|
'Planificada': None,
|
||||||
|
'EstimadaGPS': t['hora_llegada'],
|
||||||
|
'DistanciaGPS': None,
|
||||||
|
'Mensajelinea': None,
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
detalle_lineas = []
|
detalle_lineas = []
|
||||||
|
for pk_linea in lineas_agrupadas:
|
||||||
|
detalle_lineas.append(lineas_agrupadas[pk_linea])
|
||||||
|
|
||||||
for linea in row[0]:
|
|
||||||
# logging.error(linea['linea'])
|
|
||||||
data_linea = {
|
|
||||||
'Linea': linea['linea'],
|
|
||||||
'Descripcion': linea['Descripcion'],
|
|
||||||
'TipoLocomocion': linea['tipo_locomocion'],
|
|
||||||
'colorFondo': linea['colorFondo'],
|
|
||||||
'colorLetra': linea['colorLetra'],
|
|
||||||
'Llegadas': linea['Llegadas']
|
|
||||||
}
|
|
||||||
detalle_lineas.append(data_linea)
|
|
||||||
|
|
||||||
return JsonResponse({
|
return JsonResponse({
|
||||||
"GetInfoDeviceResponse": {
|
"GetInfoDeviceResponse": {
|
||||||
|
|
Loading…
Reference in New Issue