# 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

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

            self.baudrate = 38400
            self.connected = False
            self.port = ""
            self.t_3_5 = 1e-3  # (1/baudrate)*9*3.5 # 9 bits per charater. 3.5 characters time sleep
            self.sleep_timer = 0
            self.correct_frames = 0
            self.corrupted_frames = 0
            self.consecutive_corrupted_frames = 0

            self.bench = Benchmark(self.logger.level)
            self.bench.start()

            if is_RPI:
                self.port = "/dev/ttyUSB0"
            else:
                self.port = "COM4"

            self.serial_port = None

            self.read_regs_obj = ReadRegsFunction()
            self.write_regs_obj = WriteRegsFunction()
            self.write_coils_obj = WriteCoilsFunction()

    def open(self):

# 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


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

            self.baudrate = 38400
            self.connected = False
            self.port = ""
            self.t_3_5 = 1e-3  # (1/baudrate)*9*3.5 # 9 bits per charater. 3.5 characters time sleep
            self.sleep_timer = 0
            self.correct_frames = 0
            self.corrupted_frames = 0
            self.consecutive_corrupted_frames = 0

            self.bench = Benchmark(self.logger.level)
            self.bench.start()

            if is_RPI:
                self.port = "/dev/ttyUSB0"
            else:
                self.port = "COM4"

            self.serial_port = None

            self.read_regs_obj = ReadRegsFunction()
            self.write_regs_obj = WriteRegsFunction()
            self.write_coils_obj = WriteCoilsFunction()

    def open(self):

        try: