mirror of
https://github.com/undera/pylgbst.git
synced 2020-11-18 19:37:26 -08:00
73 lines
2.4 KiB
Python
73 lines
2.4 KiB
Python
import logging
|
|
import time
|
|
import unittest
|
|
from threading import Thread
|
|
|
|
from demo import demo_all
|
|
from pylgbst import MoveHub
|
|
from pylgbst.comms import Connection
|
|
|
|
logging.basicConfig(level=logging.DEBUG)
|
|
|
|
log = logging.getLogger('test')
|
|
|
|
|
|
class ConnectionMock(Connection):
|
|
"""
|
|
For unit testing purposes
|
|
"""
|
|
|
|
def __init__(self):
|
|
super(ConnectionMock, self).__init__()
|
|
self.notifications = []
|
|
self.notification_handler = None
|
|
self.running = True
|
|
self.finished = False
|
|
|
|
def set_notify_handler(self, handler):
|
|
self.notification_handler = handler
|
|
thr = Thread(target=self.notifier)
|
|
thr.setDaemon(True)
|
|
thr.start()
|
|
|
|
def notifier(self):
|
|
while self.running:
|
|
if self.notification_handler:
|
|
while self.notifications:
|
|
handle, data = self.notifications.pop(0)
|
|
self.notification_handler(handle, data.replace(' ', '').decode('hex'))
|
|
time.sleep(0.1)
|
|
|
|
self.finished = True
|
|
|
|
def write(self, handle, data):
|
|
log.debug("Writing to %s: %s", handle, data.encode("hex"))
|
|
|
|
def read(self, handle):
|
|
log.debug("Reading from: %s", handle)
|
|
return None # TODO
|
|
|
|
|
|
class GeneralTest(unittest.TestCase):
|
|
def test_capabilities(self):
|
|
conn = ConnectionMock()
|
|
hub = MoveHub(conn)
|
|
time.sleep(1)
|
|
conn.notifications.append((14, '1b0e00 0f00 04 01 0125000000001000000010'))
|
|
conn.notifications.append((14, '1b0e00 0f00 04 02 0126000000001000000010'))
|
|
conn.notifications.append((14, '1b0e00 0f00 04 37 0127000100000001000000'))
|
|
conn.notifications.append((14, '1b0e00 0f00 04 38 0127000100000001000000'))
|
|
conn.notifications.append((14, '1b0e00 0900 04 39 0227003738'))
|
|
conn.notifications.append((14, '1b0e00 0f00 04 32 0117000100000001000000'))
|
|
conn.notifications.append((14, '1b0e00 0f00 04 3a 0128000000000100000001'))
|
|
conn.notifications.append((14, '1b0e00 0f00 04 3b 0115000200000002000000'))
|
|
conn.notifications.append((14, '1b0e00 0f00 04 3c 0114000200000002000000'))
|
|
conn.notifications.append((14, '1b0e00 0f00 8202 01'))
|
|
conn.notifications.append((14, '1b0e00 0f00 8202 0a'))
|
|
time.sleep(1)
|
|
#demo_all(hub)
|
|
conn.running = False
|
|
|
|
while not conn.finished:
|
|
time.sleep(0.1)
|