# Path: controller/actor.py
# class PoupoolActor(pykka.ThreadingActor):
# 
#     def __init__(self):
#         super().__init__()
#         self._proxy = self.actor_ref.proxy()
#         self.__timer = None
# 
#     def on_failure(self, exception_type, exception_value, traceback):
#         # The actor is going to die
#         logger.fatal(exception_type, exception_value, traceback)
# 
#     def on_stop(self):
#         self.do_cancel()
# 
#     def get_actor(self, name):
#         fsm = pykka.ActorRegistry.get_by_class_name(name)
#         if fsm:
#             return fsm[0].proxy()
#         logger.critical("Actor %s not found!!!" % name)
#         return None
# 
#     def __do_cancel(self):
#         if self.__timer:
#             self.__timer.cancel()
#             self.__timer = None
# 
#     def do_cancel(self):
#         self.__do_cancel()
# 
#     def do_delay(self, delay, method, *args, **kwargs):
#         assert type(method) == str
#         assert delay >= 0
#         # Stop an already running timer
#         self.__do_cancel()
#         func = getattr(self._proxy, method)
#         if delay > 0:
#             self.__timer = Timer(delay, func.defer, *args, **kwargs)
#             self.__timer.start()
#         else:
#             func.defer(*args, **kwargs)
# 
# Path: controller/actor.py
# class PoupoolModel(Machine):
# 
#     def __init__(self, *args, **kwargs):
#         kwargs.setdefault("before_state_change", []).extend(["do_cancel", self.__update_state_time])
#         super().__init__(
#             auto_transitions=False,
#             ignore_invalid_triggers=True,
#             *args,
#             **kwargs
#         )
#         self.__state_time = None
# 
#     def __update_state_time(self):
#         self.__state_time = datetime.now()
# 
#     def get_time_in_state(self):
#         return datetime.now() - self.__state_time
# 
# Path: controller/actor.py
# class StopRepeatException(Exception):
#     pass
# 
# def do_repeat():
#     def wrap(func):
#         @functools.wraps(func)
#         def wrapped_func(self, *args, **kwargs):
#             try:
#                 func(self, *args, **kwargs)
#             except StopRepeatException:
#                 pass
#             else:
#                 method = re.sub("on_enter_", "do_repeat_", func.__name__)
#                 function = getattr(self._proxy, method)
#                 function.defer()
#         return wrapped_func
#     return wrap
# 
# Path: controller/config.py
# class Config:
#     def __init__(self, config_files):
#     def __getitem__(self, pair):
# def as_list(value, type_cast=int):


import logging
import datetime
from .actor import PoupoolActor
from .actor import PoupoolModel
from .actor import StopRepeatException, do_repeat
from .config import config

    hysteresis = int(config["tank", "hysteresis"])
    levels_too_low = int(config["tank", "too_low"])
    levels_eco = {
        "low": int(config["tank", "eco_low"]),
        "high": int(config["tank", "eco_high"]),
    }
    levels_overflow = {
        "low": int(config["tank", "overflow_low"]),
        "high": int(config["tank", "overflow_high"]),
    }

    def __init__(self, encoder, devices):
        super().__init__()
        self.__encoder = encoder
        self.__devices = devices
        self.__force_empty = False
        self.levels = self.levels_eco
        # Initialize the state machine
        self.__machine = PoupoolModel(model=self, states=Tank.states, initial="halt")

        self.__machine.add_transition("low", ["fill", "normal"], "low")
        self.__machine.add_transition("normal", ["fill", "low", "high"], "normal")
        self.__machine.add_transition("high", ["fill", "normal"], "high")
        self.__machine.add_transition("halt", ["fill", "low", "normal", "high"], "halt")
        self.__machine.add_transition("fill", "halt", "fill", unless="is_force_empty")

    def __get_tank_height(self):
        height = self.__devices.get_sensor("tank").value
        logger.debug("Tank level: %d" % height)

# Path: controller/actor.py
# class PoupoolActor(pykka.ThreadingActor):
# 
#     def __init__(self):
#         super().__init__()
#         self._proxy = self.actor_ref.proxy()
#         self.__timer = None
# 
#     def on_failure(self, exception_type, exception_value, traceback):
#         # The actor is going to die
#         logger.fatal(exception_type, exception_value, traceback)
# 
#     def on_stop(self):
#         self.do_cancel()
# 
#     def get_actor(self, name):
#         fsm = pykka.ActorRegistry.get_by_class_name(name)
#         if fsm:
#             return fsm[0].proxy()
#         logger.critical("Actor %s not found!!!" % name)
#         return None
# 
#     def __do_cancel(self):
#         if self.__timer:
#             self.__timer.cancel()
#             self.__timer = None
# 
#     def do_cancel(self):
#         self.__do_cancel()
# 
#     def do_delay(self, delay, method, *args, **kwargs):
#         assert type(method) == str
#         assert delay >= 0
#         # Stop an already running timer
#         self.__do_cancel()
#         func = getattr(self._proxy, method)
#         if delay > 0:
#             self.__timer = Timer(delay, func.defer, *args, **kwargs)
#             self.__timer.start()
#         else:
#             func.defer(*args, **kwargs)
# 
# Path: controller/actor.py
# class PoupoolModel(Machine):
# 
#     def __init__(self, *args, **kwargs):
#         kwargs.setdefault("before_state_change", []).extend(["do_cancel", self.__update_state_time])
#         super().__init__(
#             auto_transitions=False,
#             ignore_invalid_triggers=True,
#             *args,
#             **kwargs
#         )
#         self.__state_time = None
# 
#     def __update_state_time(self):
#         self.__state_time = datetime.now()
# 
#     def get_time_in_state(self):
#         return datetime.now() - self.__state_time
# 
# Path: controller/actor.py
# class StopRepeatException(Exception):
#     pass
# 
# def do_repeat():
#     def wrap(func):
#         @functools.wraps(func)
#         def wrapped_func(self, *args, **kwargs):
#             try:
#                 func(self, *args, **kwargs)
#             except StopRepeatException:
#                 pass
#             else:
#                 method = re.sub("on_enter_", "do_repeat_", func.__name__)
#                 function = getattr(self._proxy, method)
#                 function.defer()
#         return wrapped_func
#     return wrap
# 
# Path: controller/config.py
# class Config:
#     def __init__(self, config_files):
#     def __getitem__(self, pair):
# def as_list(value, type_cast=int):


# Path: controller/tank.py
import logging
import datetime
from .actor import PoupoolActor
from .actor import PoupoolModel
from .actor import StopRepeatException, do_repeat
from .config import config


    hysteresis = int(config["tank", "hysteresis"])
    levels_too_low = int(config["tank", "too_low"])
    levels_eco = {
        "low": int(config["tank", "eco_low"]),
        "high": int(config["tank", "eco_high"]),
    }
    levels_overflow = {
        "low": int(config["tank", "overflow_low"]),
        "high": int(config["tank", "overflow_high"]),
    }

    def __init__(self, encoder, devices):
        super().__init__()
        self.__encoder = encoder
        self.__devices = devices
        self.__force_empty = False
        self.levels = self.levels_eco
        # Initialize the state machine
        self.__machine = PoupoolModel(model=self, states=Tank.states, initial="halt")

        self.__machine.add_transition("low", ["fill", "normal"], "low")
        self.__machine.add_transition("normal", ["fill", "low", "high"], "normal")
        self.__machine.add_transition("high", ["fill", "normal"], "high")
        self.__machine.add_transition("halt", ["fill", "low", "normal", "high"], "halt")
        self.__machine.add_transition("fill", "halt", "fill", unless="is_force_empty")

    def __get_tank_height(self):
        height = self.__devices.get_sensor("tank").value
        logger.debug("Tank level: %d" % height)

        self.__encoder.tank_height(int(round(height)))