class NeblinaDevice(object):
    def __init__(self, address, interface):
        self.address = address

        if interface is Interface.UART:
            self.communication = NeblinaUART(self.address)
        else:
            assert bleSupported
            self.communication = NeblinaBLE(self.address)

    def connect(self):
        self.communication.connect()

    def disconnect(self):
        self.communication.disconnect()

    def isConnected(self):
        return self.communication.isConnected()

    def receivePacket(self):
        if self.isConnected():
            return self.communication.receivePacket()
        else:
            return None

    def sendPacket(self, packet):
        if self.isConnected():
            self.communication.sendPacket(packet)
class DUALIntegrationTest(unittest.TestCase):
    comPort = None
    deviceAddress = None

    def setUp(self):
        if not self.comPort:
            raise unittest.SkipTest("No COM port specified.")
        if not self.deviceAddress:
            raise unittest.SkipTest("No device address specified.")

        # Give it a break between each test
        time.sleep(1)

        self.uart = NeblinaUART()
        self.ble = NeblinaBLE()

    def tearDown(self):
        pass

    def connectBLE(self):
        self.ble.open(self.deviceAddress)
        if not self.ble.isOpened():
            self.fail("Unable to connect to BLE device.")
        self.ble.setStreamingInterface(Interface.BLE)

    def disconnectBLE(self):
        self.ble.motionStopStreams()
        self.ble.close(self.deviceAddress)

    def connectUART(self):
        self.uart.open(self.comPort)
        if not self.uart.isOpened(self.comPort):
            self.fail("Unable to connect to COM port.")
        self.uart.setStreamingInterface(Interface.UART)

    def disconnectUART(self):
        self.uart.motionStopStreams()
        self.uart.close()

    def testFlashPlaybackDump(self):
        self.connectBLE()
        self.ble.eraseStorage()
        self.ble.motionSetDownsample(40)
        self.ble.motionStartStreams(Commands.Motion.IMU)
        self.ble.motionStartStreams(Commands.Motion.MAG)
        self.ble.motionStartStreams(Commands.Motion.Quaternion)
        self.ble.motionStartStreams(Commands.Motion.EulerAngle)
        self.ble.motionStartStreams(Commands.Motion.ExtForce)
        self.ble.flashRecordStart()

        count = 0
        while count < 200:
            self.ble.receivePacket()
            count += 1

        self.ble.flashRecordStop()
        self.ble.motionStopStreams()
        self.disconnectBLE()

        time.sleep(1)

        self.connectUART()
        self.uart.sessionPlayback(0, True)
        self.disconnectUART()