# Path: backend/misc/check_host.py

# 
# Path: backend/misc/benchmark.py
# class Benchmark():
#     """Calculates number of loops in a second. Useful for testing communication speed"""
#     def __init__(self, logging_level):
#         self.logger = logging.getLogger('BENCHMARK')
#         self.logger.disabled = False
#         self.logger.setLevel(logging_level)
#         self.counter = 0
#         self.snipet_timer = 0   
#         self.lps_timer = 0  # loops per second timer
#         self.lt_timer = 0   # loop time timer
#         self.min_lt = 10000
#         self.max_lt = -1
#         self.min_loops_ps = 10000000
#         self.max_loops_ps = 0
# 
#     def start(self,):
#         t()
# 
#     def start_timing_snippet(self, ):
#         self.snipet_timer = t()
# 
#     def get_snippet_time(self, ):
#         return t()-self.snipet_timer
# 
#     def loop_time(self):
#         lt = t() - self.lt_timer
#         self.lt_timer = t()
#         return lt
# 
#     def loops_per_second(self,):
#         self.counter += 1
#         if t() - self.lps_timer >= 1:
#             self.lps_timer = t()
#             lps = self.counter
#             self.counter = 0
#             #print('Loops per second: {} '.format(lps))       
#             self.logger.debug('Loops per second: {} '.format(lps))
#             return True
#         else:
#             return False


from timeit import default_timer as t
from functools import wraps
from backend.misc.check_host import is_RPI
from backend.misc.benchmark import Benchmark
import serial
import logging
import time

            if len(out_bytes) % 2:
                out_bytes += bytes([byte])  # add pending byte
            else:
                out_bytes += bytes([byte, 0])  # add pennding byte and zero byte for odd len bytes
        else:  # iterated through whole byte
            if len(out_bytes) % 2:  # even number of whole bytes
                out_bytes += bytes([0])  # add pending byte

        return out_bytes


class ModbusNetwork:
    """Handles opening and closing of serial port.
       Gives API to modbus functions"""

    _instance = None
    _is_initialized = False

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super().__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self):
        if not ModbusNetwork._is_initialized:
            ModbusNetwork._is_initialized = True
            self.logger = logging.getLogger('MODBUS')

            self.baudrate = 38400

# Path: backend/misc/check_host.py

# 
# Path: backend/misc/benchmark.py
# class Benchmark():
#     """Calculates number of loops in a second. Useful for testing communication speed"""
#     def __init__(self, logging_level):
#         self.logger = logging.getLogger('BENCHMARK')
#         self.logger.disabled = False
#         self.logger.setLevel(logging_level)
#         self.counter = 0
#         self.snipet_timer = 0   
#         self.lps_timer = 0  # loops per second timer
#         self.lt_timer = 0   # loop time timer
#         self.min_lt = 10000
#         self.max_lt = -1
#         self.min_loops_ps = 10000000
#         self.max_loops_ps = 0
# 
#     def start(self,):
#         t()
# 
#     def start_timing_snippet(self, ):
#         self.snipet_timer = t()
# 
#     def get_snippet_time(self, ):
#         return t()-self.snipet_timer
# 
#     def loop_time(self):
#         lt = t() - self.lt_timer
#         self.lt_timer = t()
#         return lt
# 
#     def loops_per_second(self,):
#         self.counter += 1
#         if t() - self.lps_timer >= 1:
#             self.lps_timer = t()
#             lps = self.counter
#             self.counter = 0
#             #print('Loops per second: {} '.format(lps))       
#             self.logger.debug('Loops per second: {} '.format(lps))
#             return True
#         else:
#             return False


# Path: backend/components/modbus_network.py
from timeit import default_timer as t
from functools import wraps
from backend.misc.check_host import is_RPI
from backend.misc.benchmark import Benchmark
import serial
import logging
import time


            if len(out_bytes) % 2:
                out_bytes += bytes([byte])  # add pending byte
            else:
                out_bytes += bytes([byte, 0])  # add pennding byte and zero byte for odd len bytes
        else:  # iterated through whole byte
            if len(out_bytes) % 2:  # even number of whole bytes
                out_bytes += bytes([0])  # add pending byte

        return out_bytes


class ModbusNetwork:
    """Handles opening and closing of serial port.
       Gives API to modbus functions"""

    _instance = None
    _is_initialized = False

    def __new__(cls, *args, **kwargs):
        if not cls._instance:
            cls._instance = super().__new__(cls, *args, **kwargs)
        return cls._instance

    def __init__(self):
        if not ModbusNetwork._is_initialized:
            ModbusNetwork._is_initialized = True
            self.logger = logging.getLogger('MODBUS')

            self.baudrate = 38400

            self.connected = False