Cleaned up initialization process for SimulationThread.

This commit is contained in:
Chris Davoren 2023-10-20 16:05:24 +10:00
parent 60ed9fa0c6
commit b8e531af96
2 changed files with 6 additions and 13 deletions

View File

@ -9,14 +9,11 @@ class SimulationThread(threading.Thread):
instance = None instance = None
@classmethod @classmethod
def get_instance(cls, periods=None, start_time=None, run_time=None, time_factor=1.0): def initialize(cls, periods, start_time=None, run_time=None, time_factor=1.0):
if cls.instance is not None:
return cls.instance
if periods is None:
raise ValueError("Periods parameter must be specified on first call")
cls.instance = SimulationThread(periods, start_time, run_time, time_factor) cls.instance = SimulationThread(periods, start_time, run_time, time_factor)
@classmethod
def get_instance(cls, periods=None, start_time=None, run_time=None, time_factor=1.0):
return cls.instance return cls.instance
def __init__(self, periods, start_time, run_time, time_factor): def __init__(self, periods, start_time, run_time, time_factor):

View File

@ -6,11 +6,6 @@ from django.conf import settings
from simulation import SimulationThread from simulation import SimulationThread
def simulation():
while True:
print("Simulation: {}".format(time.time()))
time.sleep(1.0)
class TrafficlightfrontendConfig(AppConfig): class TrafficlightfrontendConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField' default_auto_field = 'django.db.models.BigAutoField'
name = 'trafficlightfrontend' name = 'trafficlightfrontend'
@ -29,6 +24,7 @@ class TrafficlightfrontendConfig(AppConfig):
periods = json.load(open("periods.json")) periods = json.load(open("periods.json"))
SimulationThread.get_instance(periods, start_time=datetime.time.fromisoformat("07:59"), time_factor=4.0).start() SimulationThread.initialize(periods, start_time=datetime.time.fromisoformat("07:59"), time_factor=4.0)
SimulationThread.get_instance().start()
TrafficlightfrontendConfig.initialized = True TrafficlightfrontendConfig.initialized = True