Odjezdova-tabule-MHD/server/departures.py

86 lines
2 KiB
Python
Raw Normal View History

2022-11-16 14:51:20 +01:00
from math import floor
2022-12-08 20:59:11 +01:00
from datetime import datetime
from dateutil import parser
2022-11-15 16:09:38 +01:00
class Departure:
2022-12-14 14:41:39 +01:00
id_pool = {}
2022-11-15 16:09:38 +01:00
storage = dict()
2022-12-08 22:17:26 +01:00
@staticmethod
def get(stop_id: str):
resp = []
for id in Departure.storage:
d = Departure.storage[id]
2022-12-14 19:28:38 +01:00
if d.stop_id == stop_id:
2022-12-14 14:41:39 +01:00
resp.append(d)
2022-12-08 22:17:26 +01:00
return resp
2022-11-16 14:51:20 +01:00
@staticmethod
2022-12-14 14:41:39 +01:00
def get_id(stop_id: str):
if stop_id not in Departure.id_pool:
Departure.id_pool[stop_id] = []
2022-11-16 14:51:20 +01:00
i = 0
while True:
i += 1
2022-12-14 14:41:39 +01:00
if i not in Departure.id_pool[stop_id]:
Departure.id_pool[stop_id].append(i)
2022-11-16 14:51:20 +01:00
return i
2022-11-15 16:09:38 +01:00
@staticmethod
def clear():
remove = []
for did in Departure.storage:
d = Departure.storage[did]
2022-12-08 22:17:26 +01:00
if d.get_departure() < -3.5:
2022-12-14 14:41:39 +01:00
Departure.id_pool[d.stop_id].remove(d.id)
2022-11-15 16:09:38 +01:00
remove.append(did)
for did in remove:
Departure.storage.pop(did)
2022-12-14 14:41:39 +01:00
def __init__(self, did, stop_id, type, line, last_stop, departure, delay):
2022-11-16 14:51:20 +01:00
departure = (parser.parse(departure)).timestamp()
if -(datetime.now().timestamp() - (departure + delay*60))/60 <= -1:
return
self.did = did
2022-12-08 22:17:26 +01:00
self.stop_id = stop_id
2022-12-14 14:41:39 +01:00
self.id = Departure.get_id(stop_id)
2022-11-15 16:09:38 +01:00
self.line = line
self.type = type
2022-11-15 16:09:38 +01:00
self.last_stop = last_stop
2022-11-16 14:51:20 +01:00
self.departure = departure
self.delay = delay
2022-12-14 14:41:39 +01:00
self.updated = 1
2022-12-08 22:17:26 +01:00
Departure.storage[did] = self
2022-11-15 16:09:38 +01:00
def update(self, delay):
if delay != self.delay:
2022-12-14 14:41:39 +01:00
self.updated += 1
print(f"Updated | {self.id} | {self.delay} -> {delay}")
2022-11-15 16:09:38 +01:00
self.delay = delay
2022-11-16 14:51:20 +01:00
def get_departure(self):
2022-12-08 22:17:26 +01:00
return -(floor(((datetime.now().timestamp() - (self.departure + self.delay*60))/60)*10)/10)
2022-12-08 15:05:35 +01:00
2022-12-14 14:41:39 +01:00
def data(self):
if self.updated != 0:
self.updated = 0
return f"{self}"
else:
return None
2022-11-16 14:51:20 +01:00
def __repr__(self):
2022-12-14 19:28:38 +01:00
last_stop = self.last_stop
if len(last_stop) >= 21:
last_stop = last_stop[:20].strip() + "..."
return f"{self.id}|{self.type}|{self.line}|{last_stop}|{(self.get_departure()*10):.0f}"
2022-12-08 15:05:35 +01:00
def json(self):
return {
'id': self.id,
'line': self.line,
'type': self.type,
'last_stop': self.last_stop,
2022-12-08 22:17:26 +01:00
'departure': self.get_departure(),
2022-12-08 20:02:58 +01:00
'delay': self.delay
2022-12-08 15:05:35 +01:00
}