# Path: foos/process.py
# def call_and_log(*args, **kwargs):
#     """Run process, gather output and write to log"""
#     p = subprocess.Popen(*args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
#     stdout, stderr = p.communicate()
#     if len(stdout) > 0:
#         logger.info(stdout.decode("utf-8").strip())
#     if len(stderr) > 0:
#         logger.error(stderr.decode("utf-8").strip())
#     if p.returncode != 0:
#         logger.error("{} returned {}".format(p.args, p.returncode))


import time
import threading
import foos.config as config
import logging
from foos.process import call_and_log


class Plugin:
    def __init__(self, bus, standby_timeout=500):
        self.standby_timeout = config.standby_timeout_secs
        self.bus = bus
        self.activation_events = ["button_event", "movement_detected", "goal_event"]
        self.last_active = time.time()
        self.active = True

        # disable if standby_timeout is 0
        if self.standby_timeout != 0:
            self.bus.subscribe(self.process_event, subscribed_events=self.activation_events)
            threading.Thread(daemon=True, target=self.run).start()

    def run(self):
        while True:
            time.sleep(1)
            if self.active and time.time() > (self.last_active + self.standby_timeout):
                self.turn_off()

    def turn_off(self):
        logger.info("Turning TV off...")
        self.active = False
        call_and_log("echo 'standby 0' | cec-client -s >/dev/null", shell=True)
        self.bus.notify("tv_standby")

    def turn_on(self):
        logger.info("Turning TV on...")
        self.active = True

# Path: foos/process.py
# def call_and_log(*args, **kwargs):
#     """Run process, gather output and write to log"""
#     p = subprocess.Popen(*args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
#     stdout, stderr = p.communicate()
#     if len(stdout) > 0:
#         logger.info(stdout.decode("utf-8").strip())
#     if len(stderr) > 0:
#         logger.error(stderr.decode("utf-8").strip())
#     if p.returncode != 0:
#         logger.error("{} returned {}".format(p.args, p.returncode))


# Path: plugins/standby.py
import time
import threading
import foos.config as config
import logging
from foos.process import call_and_log



class Plugin:
    def __init__(self, bus, standby_timeout=500):
        self.standby_timeout = config.standby_timeout_secs
        self.bus = bus
        self.activation_events = ["button_event", "movement_detected", "goal_event"]
        self.last_active = time.time()
        self.active = True

        # disable if standby_timeout is 0
        if self.standby_timeout != 0:
            self.bus.subscribe(self.process_event, subscribed_events=self.activation_events)
            threading.Thread(daemon=True, target=self.run).start()

    def run(self):
        while True:
            time.sleep(1)
            if self.active and time.time() > (self.last_active + self.standby_timeout):
                self.turn_off()

    def turn_off(self):
        logger.info("Turning TV off...")
        self.active = False
        call_and_log("echo 'standby 0' | cec-client -s >/dev/null", shell=True)
        self.bus.notify("tv_standby")

    def turn_on(self):
        logger.info("Turning TV on...")
        self.active = True

        call_and_log("echo 'on 0' | cec-client -s >/dev/null", shell=True)