# 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

    def process_input_communication(self, ):
        """Checks if there are any commands from client. """

        def parse_msg(msg):
            try:
                msg = msg.split(',')
                _type = msg[0][0]
                _id = int(msg[0][1:])
                value = int(msg[1])
            except:
                return None
            return _type, _id, value

        while not self._comunication_out_buffer.empty():
            msg = self._comunication_out_buffer.get()
            self.logger.debug(msg)
            _type, _id, value = parse_msg(msg)
            if not msg:
                yield None
            yield _type, _id, value, msg

    def _check_elements_values_and_notify(self, ):
        """Check elements new value flags which are set by modbus.
        If there are new values notify interested components and put message to communication thread"""
        for element in self.elements.values():
            if element.new_val_flag:
                self.logger.debug(element)
                element.notify_objects()  # Notifies objects which are interested
                element.new_val_flag = False

# 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


    def process_input_communication(self, ):
        """Checks if there are any commands from client. """

        def parse_msg(msg):
            try:
                msg = msg.split(',')
                _type = msg[0][0]
                _id = int(msg[0][1:])
                value = int(msg[1])
            except:
                return None
            return _type, _id, value

        while not self._comunication_out_buffer.empty():
            msg = self._comunication_out_buffer.get()
            self.logger.debug(msg)
            _type, _id, value = parse_msg(msg)
            if not msg:
                yield None
            yield _type, _id, value, msg

    def _check_elements_values_and_notify(self, ):
        """Check elements new value flags which are set by modbus.
        If there are new values notify interested components and put message to communication thread"""
        for element in self.elements.values():
            if element.new_val_flag:
                self.logger.debug(element)
                element.notify_objects()  # Notifies objects which are interested
                element.new_val_flag = False

                if element.type in (Et.pir, Et.rs, Et.switch, Et.heater, Et.blind):