2018-10-15 12:10:00 +00:00
|
|
|
"""Transport abstract class"""
|
|
|
|
|
|
|
|
def checksum(data):
|
|
|
|
s = 0
|
|
|
|
for c in data:
|
|
|
|
s += ord(c)
|
|
|
|
return (s & 0xFFFF) ^ 0xFFFF
|
|
|
|
|
|
|
|
|
|
|
|
class BaseTransport(object):
|
|
|
|
DEV01 = 1
|
|
|
|
ESC = 0x20
|
|
|
|
BLE = 0x21
|
|
|
|
BMS = 0x22
|
|
|
|
HOST = 0x3E
|
|
|
|
|
|
|
|
def __init__(self, link):
|
|
|
|
self.link = link
|
|
|
|
|
|
|
|
def recv(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2018-11-19 00:16:19 +00:00
|
|
|
def send(self, src, dst, cmd, arg, data=""):
|
2018-10-15 12:10:00 +00:00
|
|
|
raise NotImplementedError()
|
|
|
|
|
2018-11-19 00:16:19 +00:00
|
|
|
def execute(self, command):
|
|
|
|
self.send(command.request)
|
|
|
|
if not command.has_response:
|
|
|
|
return True
|
|
|
|
#TODO: retry ?
|
|
|
|
rsp = self.recv()
|
|
|
|
return command.handle_response(rsp)
|
|
|
|
|
2018-10-15 12:10:00 +00:00
|
|
|
|
|
|
|
__all__ = ["checksum", "BaseTransport"]
|