Odjezdova-tabule-MHD/server/main.py

64 lines
1.3 KiB
Python

from departures import Departure
from time import sleep
from lora import lora_controller
import threading
class MainLoop:
def __init__(self, stop_id, controller):
self.ended = False
self.stop_id = stop_id
self.controller = controller
self.thread = threading.Thread(target=self.update_loop)
self.thread.start()
def update_loop(self):
refetch = 0
regenerate = 0
while True:
if self.ended:
break
if regenerate == 0:
self.controller.generate_token()
if refetch == 0:
self.controller.time()
Departure.fetch(self.stop_id, 20)
refetch = (refetch + 1) % 3
regenerate = (regenerate + 1) % (6*30)
sleep(.1)
Departure.clear()
sleep(10)
def input_loop(self):
while not self.ended:
print("> ", end="")
command = input()
if command in ["d", "dep", "departures"]:
for did in Departure.storage:
print(Departure.storage[did])
if len(Departure.storage) == 0:
print("No loaded departures")
if command in ["q", "queue"]:
for m in self.controller.message_pool:
print(m)
if len(self.controller.message_pool) == 0:
print("No messages in queue")
if command in ["s", "stop"]:
print("Stopping...")
self.ended = True
self.controller.message_pool = []
main_loop = MainLoop("40", lora_controller)
main_loop.input_loop()