Skip to content

Commit

Permalink
Close #178
Browse files Browse the repository at this point in the history
  • Loading branch information
dostuffthatmatters committed Nov 8, 2023
1 parent 5460f91 commit 99de333
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions packages/core/threads/helios_thread.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,9 @@ def main(headless: bool = False) -> None:
)

# evaluate sun state only if list is filled
new_state: Optional[bool] = current_state
if edge_fraction_history.is_full():
new_state: Optional[bool] = current_state

average_edge_fraction = float(
edge_fraction_history.sum() /
edge_fraction_history.get_max_size()
Expand All @@ -396,7 +397,7 @@ def main(headless: bool = False) -> None:

upper_ef_threshold = config.helios.edge_pixel_threshold
lower_ef_threshold = upper_ef_threshold * 0.7
if current_state is None:
if new_state is None:
new_state = average_edge_fraction >= upper_ef_threshold
else:
# if already running and below lower threshold -> stop
Expand All @@ -410,36 +411,43 @@ def main(headless: bool = False) -> None:
) and (average_edge_fraction >= upper_ef_threshold):
new_state = True

logger.debug(
f"New state: {'GOOD' if (new_state == True) else 'BAD'}"
)
if current_state != new_state:
# only do state change if last_state_change is long ago in the past
seconds_since_last_state_change: float = config.helios.min_seconds_between_state_changes
if last_state_change is not None:
seconds_since_last_state_change = (
datetime.datetime.now() - last_state_change
).total_seconds()

if seconds_since_last_state_change >= config.helios.min_seconds_between_state_changes:
logger.info(
f"State change: {'BAD -> GOOD' if (new_state == True) else 'GOOD -> BAD'}"
)
interfaces.StateInterface.update_state(
helios_indicates_good_conditions=(
"inconclusive" if (
new_state is None
) else "yes" if new_state else "no"
logger.debug(
f"New state: {'GOOD' if (new_state == True) else 'BAD'}"
)
if current_state != new_state:
# only do state change if last_state_change is long ago in
# the past see https://github.com/tum-esm/pyra/issues/195
seconds_since_last_state_change: float = config.helios.min_seconds_between_state_changes
if last_state_change is not None:
seconds_since_last_state_change = (
datetime.datetime.now() - last_state_change
).total_seconds()

if seconds_since_last_state_change >= config.helios.min_seconds_between_state_changes:
logger.info(
f"State change: {'BAD -> GOOD' if (new_state == True) else 'GOOD -> BAD'}"
)
)
current_state = new_state
last_state_change = datetime.datetime.now()
else:
logger.debug(
"Not changing state because last state change was" +
f" too recent ({seconds_since_last_state_change} " +
"second(s) ago)"
)
interfaces.StateInterface.update_state(
helios_indicates_good_conditions=(
"inconclusive" if (
new_state is None
) else "yes" if new_state else "no"
)
)
current_state = new_state
last_state_change = datetime.datetime.now()
else:
logger.debug(
"Not changing state because last state change was"
+
f" too recent ({seconds_since_last_state_change} "
+ "second(s) ago)"
)
else:
logger.debug(
"Not evaluating sun state because " +
"Helios buffer is still filling up"
)

# wait rest of loop time
elapsed_time = time.time() - start_time
Expand Down

0 comments on commit 99de333

Please sign in to comment.