"""Background tasks, polled every second.

Original author: Mark Zvilius, 2-August-2019
For: The Sexton Corp
"""
import time

class BackgroundTasks:
    """Background tasks, polled every second."""

    #
    #   Update camera temperature.
    #   Update recording time.
    #   Check camera recording status.
    #   Clear error message after some time has elapsed.
    #

    # Background task interval in seconds.
    BACKGROUND_INTERVAL_SECONDS = 1.0

    def __init__(self, commands):
        """Constructor."""
        self.commands = commands
        self.last_time = time.perf_counter()
        self.tasks = [
            #self.update_temperature
        ]

    def poll(self):
        """Perform tasks on 1 second intervals."""

        # Check if interval has elapsed.
        now = time.perf_counter()
        if now - self.last_time < BackgroundTasks.BACKGROUND_INTERVAL_SECONDS:
            return

        self.last_time = now

        # Run tasks.
        for task in self.tasks:
            task()


    def update_temperature(self):
        """Read camera temperature and update on display."""

        # TODO: currently only gets master camera temperature,
        # might really want max of all cameras.

        self.commands.update_camera_temperature()
