Skip to content

Commit

Permalink
[ skip ci ] refactor task scheduling and energybroker code duplicatio…
Browse files Browse the repository at this point in the history
…n/disabled code
  • Loading branch information
JoshuaDodds committed Apr 5, 2024
1 parent 1edc365 commit a4aa973
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 33 deletions.
25 changes: 1 addition & 24 deletions lib/energy_broker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.")

Expand All @@ -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
Expand Down
19 changes: 10 additions & 9 deletions lib/task_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit a4aa973

Please sign in to comment.