forked from TDTP/pantallas-led
123 lines
4.6 KiB
Python
123 lines
4.6 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import requests
|
|
import pytz
|
|
from datetime import datetime, timedelta
|
|
|
|
|
|
class Paradero:
|
|
def __init__(self):
|
|
# Dispositivo
|
|
self.id = "panel-pruebas-2"
|
|
|
|
# Autentificación data
|
|
self.url_auth = 'https://gestion.tdt-dev.ilab.cl/api/auth/'
|
|
self.rut = "11111111-1"
|
|
self.password = "usuario1"
|
|
|
|
# Token obtenido luego del 'login'
|
|
self.token = self.__get_token()
|
|
# print(self.token)
|
|
|
|
# URL de la API para obtener los datos de los recorridos
|
|
self.url_getinfodevice = 'https://gestion.tdt-dev.ilab.cl/api/dispositivos/getInfoDevice/'
|
|
self.data = None
|
|
self.bus_list = []
|
|
self.servicios = ["12Q", "56O"]
|
|
|
|
|
|
def __get_token(self):
|
|
auth = '''{
|
|
"rut": "11111111-1",
|
|
"password": "usuario1"
|
|
}'''
|
|
|
|
response = requests.post(self.url_auth, data=auth)
|
|
|
|
# Estado de la respuesta
|
|
if response.status_code == 200:
|
|
return response.json()['token']
|
|
else:
|
|
# print(response)
|
|
return None
|
|
|
|
def get_data(self):
|
|
# Datos para la solicitud
|
|
data_getinfodevice = {
|
|
"GetInfoDevice": {
|
|
"idDispositivo": self.id,
|
|
"KeyAuthorizacion": "tokenSinUso" #Autentificacion de mentira sisisi
|
|
}
|
|
}
|
|
|
|
if self.token is not None:
|
|
#Aquí se ingresa el token obtenido anteriormente
|
|
headers_getinfodevice = {
|
|
'Authorization': f'Bearer {self.token}',
|
|
'Content-Type': 'application/json'
|
|
}
|
|
|
|
# Request
|
|
response = requests.post(self.url_getinfodevice, json=data_getinfodevice, headers=headers_getinfodevice)
|
|
|
|
self.data = self.__serialize_data(response)
|
|
return self.data
|
|
|
|
def __generate_bus_list(self, info):
|
|
data_main = {}
|
|
zona_horaria_santiago = pytz.timezone('America/Santiago')
|
|
hora_actual_santiago = datetime.now(zona_horaria_santiago).time()
|
|
|
|
for i in range(len(info["GetInfoDeviceResponse"]["DetalleLineas"])):
|
|
|
|
data = info["GetInfoDeviceResponse"]["DetalleLineas"][i]
|
|
if data["Descripcion"] not in self.servicios:
|
|
continue
|
|
|
|
bus_info = {}
|
|
if len(data['Llegadas']) > 0 and data["Llegadas"][0]["DistanciaGPS"] is not None:
|
|
bus_info["distance"] = data["Llegadas"][0]["DistanciaGPS"]
|
|
bus_info["timeLabel"] = data["Llegadas"][0]["EstimadaGPS"]
|
|
bus_info["patente"] = data["Llegadas"][0]["patente"]
|
|
bus_hour = datetime.strptime(bus_info["timeLabel"], "%H:%M:%S").time().hour if datetime.strptime(bus_info["timeLabel"], "%H:%M:%S").time().hour != 0 else 24
|
|
diff = timedelta(
|
|
hours = bus_hour - hora_actual_santiago.hour,
|
|
minutes = datetime.strptime(bus_info["timeLabel"], "%H:%M:%S").time().minute - hora_actual_santiago.minute,
|
|
seconds=datetime.strptime(bus_info["timeLabel"], "%H:%M:%S").time().second - hora_actual_santiago.second
|
|
)
|
|
# print(diff.total_seconds())
|
|
bus_info["timeLabel"] = "m"
|
|
|
|
if data['Llegadas'][0]['textoLlegada'].startswith("Entre"):
|
|
data['Llegadas'][0]['textoLlegada'] = data['Llegadas'][0]['textoLlegada'][5:]
|
|
cut = data['Llegadas'][0]['textoLlegada'].index("min")+3
|
|
bus_info["timeRemaining"] = data['Llegadas'][0]['textoLlegada'][:cut]
|
|
else:
|
|
bus_info["distance"] = "-"
|
|
bus_info["timeLabel"] = "Sin Info"
|
|
bus_info["timeRemaining"] = "Sin Dato"
|
|
|
|
bus_info["route"] = data["Descripcion"][:-1] if data["Descripcion"] is not None else "-"
|
|
bus_info["direction"] = data["Descripcion"][-1] if data["Descripcion"] is not None else "-"
|
|
bus_info["number_background_color"] = "#{}".format(data["colorFondo"])
|
|
bus_info["letter_background_color"] = "#{}".format(data["colorTexto"])
|
|
|
|
data_main[data["Descripcion"]] = bus_info
|
|
|
|
# data_main = sorted(data_main, key=lambda x: x['timeRemaining'])
|
|
salida = []
|
|
for servicio in self.servicios:
|
|
if servicio in data_main:
|
|
salida.append(data_main[servicio])
|
|
|
|
self.bus_list = salida
|
|
|
|
# for d in data_main:
|
|
# print(d['timeRemaining'], d['timeLabel'])
|
|
|
|
def __serialize_data(self, response):
|
|
data = response.json()
|
|
self.__generate_bus_list(data)
|
|
|
|
return self.bus_list
|