mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
* It's HUB ID * Rename file * Working with official doc * Some progress * AttachedIO msg * device action impl * some better device alert impl * restructuring * Some port commands handled * Some command feedback waiting * Some more request-reply things * Some more request-reply things * Reworked msg classes * Getting back to UTs * Getting back to UTs * Facing sync lock problems * Facing sync lock problems * Testing it * Covering more with tests * handle actions * Hub class is almost covered * done coverage for Hub * done coverage for MoveHub * Button is tested * remove debug server from examples * Current and voltage tested * color sensor basic test * cover tilt sensor * motor sensor tested * constant motor * motor is tested * hold_speed impl for motor * motor commands recorded * some cleanup * some cleanup * some cleanup * debug * debug * FIX a bug * acc/dec profiles figured out * UT motor ops * UT motor ops * Get rid of weird piggyback * fix UT * Fix encoding? * fix test mb * More robust * Checked demo works * cosmetics * cosmetics * Maybe better test * fetching and decoding some device caps * describing devs * describing devs works * Applying modes we've learned * Simple and extensible dev attach * Reworking peripherals based on modes * Applying modes we've learned * implemented getting sensor data * fixed port subscribe * Added led out cmds on vision sensor * Worked on color-distance sensor * Introduce some locking for consistency * Improved it all * Travis flags * improve * improve * improve docs
70 lines
2.0 KiB
Python
70 lines
2.0 KiB
Python
import unittest
|
|
|
|
import pygatt
|
|
import serial
|
|
from pygatt import BLEAddressType
|
|
from pygatt.backends.bgapi.bgapi import MAX_CONNECTION_ATTEMPTS
|
|
from pygatt.backends.bgapi.device import BGAPIBLEDevice
|
|
from pygatt.backends.bgapi.util import USBSerialDeviceInfo
|
|
|
|
from pylgbst.comms.cpygatt import GattoolConnection
|
|
from tests import log
|
|
|
|
|
|
class SerialMock(serial.Serial):
|
|
def write(self, data):
|
|
self.is_open = True
|
|
log.debug("Write data to serial: %s", data)
|
|
return len(data)
|
|
|
|
def flush(self, *args, **kwargs):
|
|
pass
|
|
|
|
def close(self):
|
|
pass
|
|
|
|
def read(self, size=1):
|
|
return bytes()
|
|
|
|
|
|
class BGAPIBLEDeviceMock(BGAPIBLEDevice):
|
|
def subscribe(self, uuid, callback=None, indication=False):
|
|
log.debug("Mock subscribing")
|
|
|
|
def char_write_handle(self, char_handle, value, wait_for_response=False):
|
|
log.debug("Mock write: %s", value)
|
|
|
|
|
|
class BlueGigaBackendMock(pygatt.BGAPIBackend):
|
|
def _open_serial_port(self, max_connection_attempts=MAX_CONNECTION_ATTEMPTS):
|
|
log.debug("Mock open serial port")
|
|
self._ser = SerialMock()
|
|
|
|
def expect(self, expected, *args, **kargs):
|
|
log.debug("Mock expect")
|
|
data = {
|
|
"packet_type": 0x04,
|
|
"sender": "abcdef".encode('ascii'),
|
|
"data": [1, 2, 3],
|
|
"rssi": 1
|
|
}
|
|
self._ble_evt_gap_scan_response(data)
|
|
|
|
def connect(self, address, timeout=5, address_type=BLEAddressType.public, interval_min=60, interval_max=76,
|
|
supervision_timeout=100, latency=0):
|
|
log.debug("Mock connect")
|
|
device = BGAPIBLEDeviceMock("address", 0, self)
|
|
return device
|
|
|
|
def _detect_device_port(self):
|
|
return USBSerialDeviceInfo().port_name
|
|
|
|
|
|
class BlueGigaTests(unittest.TestCase):
|
|
def test_1(self):
|
|
obj = GattoolConnection()
|
|
obj.backend = BlueGigaBackendMock
|
|
obj.connect(u'66:65:64:63:62:61')
|
|
obj.write(0, "test".encode('ascii'))
|
|
obj.set_notify_handler(lambda x: None)
|