aalcala07 / home_dashboard

Home dashboard built with Pygame
MIT License
0 stars 1 forks source link

Provide a generic mechanism for updating background data #6

Closed snoyes closed 2 years ago

snoyes commented 2 years ago

At the moment, weather updates are hard coded into main.py. Even if I'm not using the weather component, the main loop tries to fetch weather data every 5 minutes.

Instead, each component should optionally expose an update callback function for such activity.

aalcala07 commented 2 years ago

Yeah, I'm thinking the user should be able to opt-out of using the weather API and also add their own API integrations.

You could call these services. I think they should also work independently of the components so you can have multiple components using the same API or web service, whether that involves using a callback or something else.

Have to think about this.

snoyes commented 2 years ago

See https://github.com/snoyes/home_dashboard/tree/generic_timed_callback for one approach

aalcala07 commented 2 years ago

I like how you are setting events dynamically and allowing each component to have an optional update function.

The downside is that this would tightly couple each component to the service or API it's consuming.

  1. The clock and weather panel would both have to have an update function so they could be used independently of each other, and
  2. Additional logic would have to be added to prevent duplicate API requests when they are used together.

So maybe update.py could have list_active_services instead of list_active_components and this could load all modules in a services folder:

/services
  weather.py

Each service module would have:

def update():
  # Fetch data from web service/API and cache the data

def get():
  # Return cached data

Any component could then use the get function for whichever service it wants to use.

snoyes commented 2 years ago

I like the idea of separating the interface component from the service.

I don't think I like running each background service just because the file exists. I think I'm happy with it finding the update function for every service that exists, but not actually executing unless it has been enabled in the configuration file.

I like encouraging a cache mechanism. If someone creates a module where caching doesn't make sense, they can always just make get() simply call update() or even an alias for update().

aalcala07 commented 2 years ago

I've moved weather.py to a new services folder in the dev branch. I also added services.json for configuring services. If a user doesn't want a service to run they can remove it from that file or set update_interval_seconds to 0.

If you'd like to add any of your code from main.py and update.py, now would be a good time to make a pull request to dev.