# 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
        if self.__machine.get_time_in_state() > datetime.timedelta(hours=6):
            logger.warning("Tank TOO LONG in low state, stopping")
            self.get_actor("Filtration").halt.defer()
            return
        height = self.__get_tank_height()
        if height >= self.levels["low"] + self.hysteresis:
            self._proxy.normal.defer()
            return
        elif height < self.levels_too_low:
            logger.warning("Tank TOO LOW, stopping: %d" % height)
            self.get_actor("Filtration").halt.defer()
            return
        self.do_delay(self.STATE_REFRESH_DELAY / 2, self.do_repeat_low.__name__)

    @do_repeat()
    def on_enter_normal(self):
        logger.info("Entering normal state")
        self.__encoder.tank_state("normal")
        self.__devices.get_valve("main").off()

    def do_repeat_normal(self):
        height = self.__get_tank_height()
        if height < self.levels["low"] - self.hysteresis:
            self._proxy.low.defer()
            return
        elif height >= self.levels["high"] + self.hysteresis:
            self._proxy.high.defer()
            return
        self.do_delay(self.STATE_REFRESH_DELAY, self.do_repeat_normal.__name__)


# 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

        if self.__machine.get_time_in_state() > datetime.timedelta(hours=6):
            logger.warning("Tank TOO LONG in low state, stopping")
            self.get_actor("Filtration").halt.defer()
            return
        height = self.__get_tank_height()
        if height >= self.levels["low"] + self.hysteresis:
            self._proxy.normal.defer()
            return
        elif height < self.levels_too_low:
            logger.warning("Tank TOO LOW, stopping: %d" % height)
            self.get_actor("Filtration").halt.defer()
            return
        self.do_delay(self.STATE_REFRESH_DELAY / 2, self.do_repeat_low.__name__)

    @do_repeat()
    def on_enter_normal(self):
        logger.info("Entering normal state")
        self.__encoder.tank_state("normal")
        self.__devices.get_valve("main").off()

    def do_repeat_normal(self):
        height = self.__get_tank_height()
        if height < self.levels["low"] - self.hysteresis:
            self._proxy.low.defer()
            return
        elif height >= self.levels["high"] + self.hysteresis:
            self._proxy.high.defer()
            return
        self.do_delay(self.STATE_REFRESH_DELAY, self.do_repeat_normal.__name__)


    @do_repeat()