# Path: can4python/exceptions.py
# class CanException(Exception):
# class CanTimeoutException(CanException):
# class CanNotFoundByKernelException(CanException):
# 
# Path: can4python/canbus.py
# class CanBus():
#     def __init__(self, config, interfacename, timeout=None, use_bcm=False):
#     def from_kcd_file(cls, filename, interfacename, timeout=None, busname=None, use_bcm=False, ego_node_ids=None):
#     def __repr__(self):
#     def config(self):
#     def use_bcm(self):
#     def init_reception(self):
#     def recv_next_signals(self):
#     def recv_next_frame(self):
#     def stop_reception(self):
#     def send_signals(self, *args, **kwargs):
#     def start_sending_all_signals(self):
#     def send_frame(self, frame_to_send):
#     def stop_sending(self):
#     def stop(self):
#     def get_descriptive_ascii_art(self):
#     def write_configuration(self, filename):
# 
# Path: can4python/canframe.py
# class CanFrame():
#     def __init__(self, frame_id, frame_data, frame_format=constants.CAN_FRAMEFORMAT_STANDARD):
#     def __repr__(self):
#     def __len__(self):
#     def from_empty_bytes(cls, frame_id, number_of_bytes, frame_format=constants.CAN_FRAMEFORMAT_STANDARD):
#     def from_rawframe(cls, rawframe):
#     def frame_id(self):
#     def frame_id(self, value):
#     def frame_data(self):
#     def frame_data(self, value):
#     def frame_format(self):
#     def frame_format(self, value):
#     def get_signalvalue(self, signaldefinition):
#     def set_signalvalue(self, signaldefinition, physical_value=None):
#     def unpack(self, frame_definitions):
#     def get_rawframe(self):
#     def get_descriptive_ascii_art(self):
# 
# Path: can4python/configuration.py
# class Configuration():
#     def __init__(self, framedefinitions=None, busname=None, ego_node_ids=None):
#     def ego_node_ids(self):
#     def ego_node_ids(self, value):
#     def __repr__(self):
#     def get_descriptive_ascii_art(self):
#     def add_framedefinition(self, framedef):
#     def set_throttle_times(self, inputdict):
#     def set_throttle_times_from_signalnames(self, inputdict):
#     def set_receive_on_change_only(self, inputlist):
#     def set_receive_on_change_only_from_signalnames(self, inputlist):
#     def find_frameid_from_signalname(self, input_signalname):


import contextlib
import copy
import os.path
import subprocess
import sys
import time
import unittest
from can4python import exceptions
from can4python import canbus
from can4python import canframe
from can4python import configuration
    from .test_caninterface_raw import VIRTUAL_CAN_BUS_NAME, enable_virtual_can_bus
    from test_caninterface_raw import VIRTUAL_CAN_BUS_NAME, enable_virtual_can_bus
    from .test_configuration import FRAME_ID_SEND, FRAME_ID_RECEIVE, TESTCONFIG1
    from test_configuration import FRAME_ID_SEND, FRAME_ID_RECEIVE, TESTCONFIG1
    from .test_filehandler_kcd import INPUT_FILENAME, INPUT_FILENAME_NO_BUSDEFINITION
    from test_filehandler_kcd import INPUT_FILENAME, INPUT_FILENAME_NO_BUSDEFINITION
                                                      stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        time.sleep(0.1)

        starttime = time.time()
        for i in range(NUMBER_OF_FRAMES_TO_SEND):
            signalvalues_to_send = {'testsignal1': 1,
                                    'testsignal2': i,
                                    'testsignal3': 256 * 4 + 5}
            self.canbus_bcm.send_signals(signalvalues_to_send)

        try:
            self.simulated_can_process.communicate(timeout=10)
        except subprocess.TimeoutExpired:
            raise exceptions.CanTimeoutException("At least one of the {} sent frames was not seen by candump.".format(
                NUMBER_OF_FRAMES_TO_SEND))
        execution_time = time.time() - starttime

        time_per_loop_ms = 1000 * execution_time / NUMBER_OF_FRAMES_TO_SEND
        frames_per_seconds = NUMBER_OF_FRAMES_TO_SEND / execution_time
        output_string = "\n --> Sent {} frames in {:.1f} s ({:.2f} ms per frame, {:.1f} frames/s). ".format(
            NUMBER_OF_FRAMES_TO_SEND, execution_time, time_per_loop_ms, frames_per_seconds)
        print(output_string)

    def testPeriodicSendingUpdateSignalsAndStop(self):
        RESULT_ALLOWED_DIFFERENCE = 3 # Number of missed frames, or when stopping sending too late.
        TRANSMISSION_INTERVAL_MILLISECONDS = 20
        MEASUREMENT_TIME = 0.5  # seconds

        config = copy.deepcopy(TESTCONFIG1)
        config.framedefinitions[FRAME_ID_SEND].cycletime = TRANSMISSION_INTERVAL_MILLISECONDS

# Path: can4python/exceptions.py
# class CanException(Exception):
# class CanTimeoutException(CanException):
# class CanNotFoundByKernelException(CanException):
# 
# Path: can4python/canbus.py
# class CanBus():
#     def __init__(self, config, interfacename, timeout=None, use_bcm=False):
#     def from_kcd_file(cls, filename, interfacename, timeout=None, busname=None, use_bcm=False, ego_node_ids=None):
#     def __repr__(self):
#     def config(self):
#     def use_bcm(self):
#     def init_reception(self):
#     def recv_next_signals(self):
#     def recv_next_frame(self):
#     def stop_reception(self):
#     def send_signals(self, *args, **kwargs):
#     def start_sending_all_signals(self):
#     def send_frame(self, frame_to_send):
#     def stop_sending(self):
#     def stop(self):
#     def get_descriptive_ascii_art(self):
#     def write_configuration(self, filename):
# 
# Path: can4python/canframe.py
# class CanFrame():
#     def __init__(self, frame_id, frame_data, frame_format=constants.CAN_FRAMEFORMAT_STANDARD):
#     def __repr__(self):
#     def __len__(self):
#     def from_empty_bytes(cls, frame_id, number_of_bytes, frame_format=constants.CAN_FRAMEFORMAT_STANDARD):
#     def from_rawframe(cls, rawframe):
#     def frame_id(self):
#     def frame_id(self, value):
#     def frame_data(self):
#     def frame_data(self, value):
#     def frame_format(self):
#     def frame_format(self, value):
#     def get_signalvalue(self, signaldefinition):
#     def set_signalvalue(self, signaldefinition, physical_value=None):
#     def unpack(self, frame_definitions):
#     def get_rawframe(self):
#     def get_descriptive_ascii_art(self):
# 
# Path: can4python/configuration.py
# class Configuration():
#     def __init__(self, framedefinitions=None, busname=None, ego_node_ids=None):
#     def ego_node_ids(self):
#     def ego_node_ids(self, value):
#     def __repr__(self):
#     def get_descriptive_ascii_art(self):
#     def add_framedefinition(self, framedef):
#     def set_throttle_times(self, inputdict):
#     def set_throttle_times_from_signalnames(self, inputdict):
#     def set_receive_on_change_only(self, inputlist):
#     def set_receive_on_change_only_from_signalnames(self, inputlist):
#     def find_frameid_from_signalname(self, input_signalname):


# Path: tests/test_canbus.py
import contextlib
import copy
import os.path
import subprocess
import sys
import time
import unittest
from can4python import exceptions
from can4python import canbus
from can4python import canframe
from can4python import configuration
    from .test_caninterface_raw import VIRTUAL_CAN_BUS_NAME, enable_virtual_can_bus
    from test_caninterface_raw import VIRTUAL_CAN_BUS_NAME, enable_virtual_can_bus
    from .test_configuration import FRAME_ID_SEND, FRAME_ID_RECEIVE, TESTCONFIG1
    from test_configuration import FRAME_ID_SEND, FRAME_ID_RECEIVE, TESTCONFIG1
    from .test_filehandler_kcd import INPUT_FILENAME, INPUT_FILENAME_NO_BUSDEFINITION
    from test_filehandler_kcd import INPUT_FILENAME, INPUT_FILENAME_NO_BUSDEFINITION

                                                      stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        time.sleep(0.1)

        starttime = time.time()
        for i in range(NUMBER_OF_FRAMES_TO_SEND):
            signalvalues_to_send = {'testsignal1': 1,
                                    'testsignal2': i,
                                    'testsignal3': 256 * 4 + 5}
            self.canbus_bcm.send_signals(signalvalues_to_send)

        try:
            self.simulated_can_process.communicate(timeout=10)
        except subprocess.TimeoutExpired:
            raise exceptions.CanTimeoutException("At least one of the {} sent frames was not seen by candump.".format(
                NUMBER_OF_FRAMES_TO_SEND))
        execution_time = time.time() - starttime

        time_per_loop_ms = 1000 * execution_time / NUMBER_OF_FRAMES_TO_SEND
        frames_per_seconds = NUMBER_OF_FRAMES_TO_SEND / execution_time
        output_string = "\n --> Sent {} frames in {:.1f} s ({:.2f} ms per frame, {:.1f} frames/s). ".format(
            NUMBER_OF_FRAMES_TO_SEND, execution_time, time_per_loop_ms, frames_per_seconds)
        print(output_string)

    def testPeriodicSendingUpdateSignalsAndStop(self):
        RESULT_ALLOWED_DIFFERENCE = 3 # Number of missed frames, or when stopping sending too late.
        TRANSMISSION_INTERVAL_MILLISECONDS = 20
        MEASUREMENT_TIME = 0.5  # seconds

        config = copy.deepcopy(TESTCONFIG1)
        config.framedefinitions[FRAME_ID_SEND].cycletime = TRANSMISSION_INTERVAL_MILLISECONDS

        self.canbus_bcm = canbus.CanBus(config, VIRTUAL_CAN_BUS_NAME, timeout=1.0, use_bcm=True)