From a4aa97332233a0e4f31c206b662d9bc61871f80b Mon Sep 17 00:00:00 2001 From: JoshuaDodds Date: Fri, 5 Apr 2024 12:48:13 +0200 Subject: [PATCH] [ skip ci ] refactor task scheduling and energybroker code duplication/disabled code --- lib/energy_broker.py | 25 +------------------------ lib/task_scheduler.py | 19 ++++++++++--------- 2 files changed, 11 insertions(+), 33 deletions(-) diff --git a/lib/energy_broker.py b/lib/energy_broker.py index 9f7b0d2..de78cf0 100644 --- a/lib/energy_broker.py +++ b/lib/energy_broker.py @@ -17,11 +17,6 @@ def main(): logging.info("EnergyBroker: Initializing...") - - # main_thread = threading.Thread(target=scheduler_loop) - # main_thread.daemon = True - # main_thread.start() - schedule_tasks() logging.info("EnergyBroker: Initialization complete.") @@ -31,30 +26,12 @@ def schedule_tasks(): scheduler.every().hour.at(":00").do(manage_sale_of_stored_energy_to_the_grid) # Grid Charging Scheduled Tasks - scheduler.every().day.at("13:10").do(publish_mqtt_trigger) # when next day prices are published each day + scheduler.every().day.at("13:10").do(publish_mqtt_trigger) # when next day prices are published each day scheduler.every().day.at("13:30").do(set_charging_schedule, caller="TaskScheduler()", silent=True) scheduler.every().day.at("09:30").do(set_charging_schedule, caller="TaskScheduler()", silent=True) scheduler.every().day.at("00:10").do(set_charging_schedule, caller="TaskScheduler()", silent=True) -def scheduler_loop(): - # ESS Scheduled Tasks - scheduler.every().hour.at(":00").do(manage_sale_of_stored_energy_to_the_grid) - - # Grid Charging Scheduled Tasks - scheduler.every().day.at("13:10").do(publish_mqtt_trigger) # when next day prices are published each day - scheduler.every().day.at("13:30").do(set_charging_schedule, caller="EnergyBroker()", silent=True) - scheduler.every().day.at("09:30").do(set_charging_schedule, caller="EnergyBroker()", silent=True) - scheduler.every().day.at("00:10").do(set_charging_schedule, caller="EnergyBroker()", silent=True) - - for job in scheduler.get_jobs(): - logging.debug(f"TaskScheduler: job: {job}") - - while True: - scheduler.run_pending() - time.sleep(1) - - def retrieve_latest_tibber_pricing(): if dotenv_config('TIBBER_UPDATES_ENABLED') != '1': return None diff --git a/lib/task_scheduler.py b/lib/task_scheduler.py index f48e8aa..0091487 100644 --- a/lib/task_scheduler.py +++ b/lib/task_scheduler.py @@ -7,28 +7,29 @@ from lib.solar_forecasting import get_victron_solar_forecast from lib.energy_broker import retrieve_latest_tibber_pricing - STATE = GlobalStateClient() def TaskScheduler(): logging.info("TaskScheduler: Initializing...") + # General always run tasks + # Pricing and Forecasting Scheduled Tasks + scheduler.every(15).minutes.do(get_victron_solar_forecast) + scheduler.every(10).minutes.do(retrieve_latest_tibber_pricing) + scheduler.every().hour.at(":01").do(retrieve_latest_tibber_pricing) + + job_count = len(scheduler.get_jobs()) + logging.info(f"TaskScheduler: {job_count} jobs found and configured.") + main_thread = threading.Thread(target=scheduler_loop) main_thread.daemon = True main_thread.start() logging.info("TaskScheduler: Started.") -def scheduler_loop(): - # Scheduled Tasks - scheduler.every(15).minutes.do(get_victron_solar_forecast) - scheduler.every(10).minutes.do(retrieve_latest_tibber_pricing) - scheduler.every().hour.at(":01").do(retrieve_latest_tibber_pricing) - - for job in scheduler.get_jobs(): - logging.debug(f"TaskScheduler: job: {job}") +def scheduler_loop(): while True: scheduler.run_pending() time.sleep(1)