2023-10-19 06:49:13 +00:00
|
|
|
import os
|
|
|
|
import datetime
|
|
|
|
import json
|
|
|
|
from django.apps import AppConfig
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
|
|
from simulation import SimulationThread
|
|
|
|
|
|
|
|
class TrafficlightfrontendConfig(AppConfig):
|
|
|
|
default_auto_field = 'django.db.models.BigAutoField'
|
|
|
|
name = 'trafficlightfrontend'
|
|
|
|
initialized = False
|
|
|
|
simulation_thread = None
|
|
|
|
|
|
|
|
# Note as per documentation, may be called more than once
|
|
|
|
# Therefore needs to be idempotent or use flag
|
|
|
|
def ready(self):
|
|
|
|
# Ensure RUN_MAIN to avoid starting thread on runserver "change watchdog" instance
|
|
|
|
if TrafficlightfrontendConfig.initialized or os.environ.get("RUN_MAIN") is None:
|
|
|
|
return
|
|
|
|
|
|
|
|
print("Traffic Light Frontend: initialization.")
|
|
|
|
print("Current working directory: {}".format(os.getcwd()))
|
|
|
|
|
2023-10-19 07:01:52 +00:00
|
|
|
periods = json.load(open("periods.json"))
|
2023-10-19 06:49:13 +00:00
|
|
|
|
2023-10-20 06:05:24 +00:00
|
|
|
SimulationThread.initialize(periods, start_time=datetime.time.fromisoformat("07:59"), time_factor=4.0)
|
|
|
|
SimulationThread.get_instance().start()
|
2023-10-19 06:49:13 +00:00
|
|
|
|
|
|
|
TrafficlightfrontendConfig.initialized = True
|