|
@@ -1,39 +1,41 @@
|
|
#!/usr/bin/env python3
|
|
#!/usr/bin/env python3
|
|
-import threading
|
|
|
|
-from datetime import datetime
|
|
|
|
|
|
+import datetime
|
|
|
|
|
|
from async_realtime_environment import AsyncRealtimeEnvironment
|
|
from async_realtime_environment import AsyncRealtimeEnvironment
|
|
|
|
|
|
-from nicegui import app, ui
|
|
|
|
|
|
+from nicegui import ui
|
|
|
|
|
|
-lblTick = ui.label()
|
|
|
|
-lblColor = ui.label()
|
|
|
|
|
|
+start_time = datetime.datetime.now()
|
|
|
|
|
|
|
|
|
|
def clock(env):
|
|
def clock(env):
|
|
while True:
|
|
while True:
|
|
- lblTick.set_text(datetime.now().strftime("%H:%M:%S"))
|
|
|
|
- # lblTick.set_text(f"{env.now}")
|
|
|
|
|
|
+ simulation_time = start_time + datetime.timedelta(seconds=env.now)
|
|
|
|
+ clock_label.set_text(simulation_time.strftime('%H:%M:%S'))
|
|
yield env.timeout(1)
|
|
yield env.timeout(1)
|
|
|
|
|
|
|
|
|
|
def traffic_light(env):
|
|
def traffic_light(env):
|
|
while True:
|
|
while True:
|
|
- lblColor.set_text("GREEN")
|
|
|
|
|
|
+ lblColor.set_text('GREEN')
|
|
yield env.timeout(30)
|
|
yield env.timeout(30)
|
|
- lblColor.set_text("YELLOW")
|
|
|
|
|
|
+ lblColor.set_text('YELLOW')
|
|
yield env.timeout(5)
|
|
yield env.timeout(5)
|
|
- lblColor.set_text("RED")
|
|
|
|
|
|
+ lblColor.set_text('RED')
|
|
yield env.timeout(20)
|
|
yield env.timeout(20)
|
|
|
|
|
|
|
|
|
|
async def run_simpy():
|
|
async def run_simpy():
|
|
- env = AsyncRealtimeEnvironment(factor=0.1)
|
|
|
|
|
|
+ env = AsyncRealtimeEnvironment(factor=0.1) # fast forward simulation with 1/10th of realtime
|
|
env.process(traffic_light(env))
|
|
env.process(traffic_light(env))
|
|
env.process(clock(env))
|
|
env.process(clock(env))
|
|
- await env.run(until=300)
|
|
|
|
- print("Simulation complete")
|
|
|
|
|
|
+ await env.run(until=300) # run until 300 seconds of simulation time have passed
|
|
|
|
+ ui.notify('Simulation completed')
|
|
|
|
|
|
|
|
+# create the UI
|
|
|
|
+clock_label = ui.label()
|
|
|
|
+lblColor = ui.label()
|
|
|
|
|
|
|
|
+# start the simpy simulation as async task in the background as soon as the UI is ready
|
|
ui.timer(0, run_simpy, once=True)
|
|
ui.timer(0, run_simpy, once=True)
|
|
ui.run()
|
|
ui.run()
|