# Path: backend/components/clock.py
# class Clock:
#     """Holds actual time. 
#     Notifies about actual time
#     Notifies about week day"""
# 
#     _is_initialized = False
#     _instance = None
# 
#     def __new__(cls, *args, **kwargs):
#         if not cls._instance:
#             cls._instance = super().__new__(cls, *args, **kwargs)
#         return cls._instance
# 
#     def __init__(self, ):
#         if not Clock._is_initialized:
#             Clock._is_initialized = True
#             self.objects_to_notify_weekday = set()
#             self.objects_to_notify_time = set()
# 
#             self.now = None
#             self.minute = None
#             self.weekday = None
# 
#             self.system_start = time.time()
# 
#     def subscribe_for_weekday(self, who):
#         self.objects_to_notify_weekday.add(who)
# 
#     def subscribe_for_minute(self, who):
#         self.objects_to_notify_time.add(who)
# 
#     def notify_minute(self):
#         self.minute = self.now.minute
#         for _object in self.objects_to_notify_time:
#             _object.notify(self.now)
# 
#     def notify_weekday(self):
#         self.weekday = self.now.weekday()
#         for _object in self.objects_to_notify_weekday:
#             _object.notify(self.weekday)
# 
#     def evaluate_time(self, ):
#         """Evaluates current time. If minutes or day changes it notifies related objects"""
#         self.now = datetime.datetime.now()
#         
#         if self.now.minute != self.minute:
#             self.notify_minute()
# 
#         if self.weekday != self.now.weekday():
#             self.notify_weekday()
# 
#     def get_seconds(self):
#         return time.time() - self.system_start
# 
#     def get_millis(self):
#         return (time.time() - self.system_start)*1000
# 
#     def restart(self):
#         self.system_start = time.time()
# 
# Path: backend/misc/sys_types.py
# class Et(AutoNumber):
#     ds = ()
#     dht = ()
#     dht_temp = ()
#     dht_hum = ()
#     led = ()
#     pir = ()
#     rs = ()
#     heater = ()
#     ls = ()
#     ventilator = ()
#     blind = ()
#     switch = ()


import logging
import queue
import threading
import time
from backend.components.clock import Clock
from backend.misc.sys_types import Et



class LogicManager(threading.Thread):

    clock = Clock()

    def __init__(self, args=(),):
        threading.Thread.__init__(self, group=None, target=None, name='LOGIC')
        self._comunication_out_buffer = args[0]
        self._comunication_in_buffer = args[1]
        system = args[2]
        self.elements = system.elements.all
        self.output_elements = system.elements.output
        self.output_modules = system.modules.output
        self.regulations = system.regulations
        self.dependancies = system.dependancies

        self._client_priority = 5
        self.logger = logging.getLogger('LOGIC')
        self.tasks = queue.Queue()


# Path: backend/components/clock.py
# class Clock:
#     """Holds actual time. 
#     Notifies about actual time
#     Notifies about week day"""
# 
#     _is_initialized = False
#     _instance = None
# 
#     def __new__(cls, *args, **kwargs):
#         if not cls._instance:
#             cls._instance = super().__new__(cls, *args, **kwargs)
#         return cls._instance
# 
#     def __init__(self, ):
#         if not Clock._is_initialized:
#             Clock._is_initialized = True
#             self.objects_to_notify_weekday = set()
#             self.objects_to_notify_time = set()
# 
#             self.now = None
#             self.minute = None
#             self.weekday = None
# 
#             self.system_start = time.time()
# 
#     def subscribe_for_weekday(self, who):
#         self.objects_to_notify_weekday.add(who)
# 
#     def subscribe_for_minute(self, who):
#         self.objects_to_notify_time.add(who)
# 
#     def notify_minute(self):
#         self.minute = self.now.minute
#         for _object in self.objects_to_notify_time:
#             _object.notify(self.now)
# 
#     def notify_weekday(self):
#         self.weekday = self.now.weekday()
#         for _object in self.objects_to_notify_weekday:
#             _object.notify(self.weekday)
# 
#     def evaluate_time(self, ):
#         """Evaluates current time. If minutes or day changes it notifies related objects"""
#         self.now = datetime.datetime.now()
#         
#         if self.now.minute != self.minute:
#             self.notify_minute()
# 
#         if self.weekday != self.now.weekday():
#             self.notify_weekday()
# 
#     def get_seconds(self):
#         return time.time() - self.system_start
# 
#     def get_millis(self):
#         return (time.time() - self.system_start)*1000
# 
#     def restart(self):
#         self.system_start = time.time()
# 
# Path: backend/misc/sys_types.py
# class Et(AutoNumber):
#     ds = ()
#     dht = ()
#     dht_temp = ()
#     dht_hum = ()
#     led = ()
#     pir = ()
#     rs = ()
#     heater = ()
#     ls = ()
#     ventilator = ()
#     blind = ()
#     switch = ()


# Path: backend/managers/logic.py
import logging
import queue
import threading
import time
from backend.components.clock import Clock
from backend.misc.sys_types import Et




class LogicManager(threading.Thread):

    clock = Clock()

    def __init__(self, args=(),):
        threading.Thread.__init__(self, group=None, target=None, name='LOGIC')
        self._comunication_out_buffer = args[0]
        self._comunication_in_buffer = args[1]
        system = args[2]
        self.elements = system.elements.all
        self.output_elements = system.elements.output
        self.output_modules = system.modules.output
        self.regulations = system.regulations
        self.dependancies = system.dependancies

        self._client_priority = 5
        self.logger = logging.getLogger('LOGIC')
        self.tasks = queue.Queue()


    def set_desired_value(self, _type, _id, value, _msg):