From e130a18d3c46f311d095819ab901b2fca57fcea3 Mon Sep 17 00:00:00 2001 From: Marek Siarkowicz Date: Mon, 15 Jun 2015 00:13:44 +0200 Subject: Create message class. --- ParrotProtocol.py | 20 -------------------- message.py | 30 ++++++++++++++++++++++++++++++ resource_manager.py | 10 +++------- setup.py | 2 +- 4 files changed, 34 insertions(+), 28 deletions(-) delete mode 100644 ParrotProtocol.py create mode 100644 message.py diff --git a/ParrotProtocol.py b/ParrotProtocol.py deleted file mode 100644 index a4e1677..0000000 --- a/ParrotProtocol.py +++ /dev/null @@ -1,20 +0,0 @@ -def generateRequest(requestString): - message = bytearray() - message.extend(generateHeader(requestString)) - message.extend(bytearray(requestString)) - return message - - -def generateHeader(requestString): - header = bytearray([0]) - header.append(len(requestString) + 3) - header.append("\x80") - return header - - -def getRequest(resource): - return generateRequest("GET " + resource) - - -def setRequest(resource, args): - return generateRequest("SET " + resource + "?arg=" + args) diff --git a/message.py b/message.py new file mode 100644 index 0000000..214b6f9 --- /dev/null +++ b/message.py @@ -0,0 +1,30 @@ +class Message: + def __init__(self, resource, method, arg=None): + self.method = method + self.resource = resource + self.arg = arg + + def __str__(self): + return str(self.request) + + @property + def request(self): + message = bytearray() + message.extend(self.header) + message.extend(bytearray(self.request_string)) + return message + + @property + def header(self): + header = bytearray([0]) + header.append(len(self.request_string) + 3) + header.append("\x80") + return header + + @property + def request_string(self): + if self.method == 'set': + return 'SET {}/{}?arg={}'.format(self.resource, self.method, + str(self.arg).lower()) + else: + return 'GET {}/{}'.format(self.resource, self.method) diff --git a/resource_manager.py b/resource_manager.py index 752efe4..fb3b5f3 100644 --- a/resource_manager.py +++ b/resource_manager.py @@ -1,8 +1,8 @@ from operator import itemgetter import sys from BeautifulSoup import BeautifulSoup -import ParrotProtocol +from message import Message class ResourceManagerBase(object): resources = [ @@ -37,12 +37,8 @@ class ResourceManagerBase(object): def _create_message(self, resource, method, arg=None): assert resource in self.resources, 'Unknown resource {}'.format(resource) - assert method in self.resources[resource], 'Unhandled method {} for {}'.format( - method, resource) - if method == 'set': - return ParrotProtocol.setRequest(resource + '/' + method, str(arg).lower()) - else: - return ParrotProtocol.getRequest(resource + '/' + method) + assert method in self.resources[resource], 'Unhandled method {} for {}'.format(method, resource) + return Message(resource, method, arg) def send_message(self, message): try: diff --git a/setup.py b/setup.py index de58f17..9bb3ae7 100644 --- a/setup.py +++ b/setup.py @@ -43,7 +43,7 @@ setup( 'BeautifulSoup', 'bluetooth' ], - py_modules=['ParrotZik', 'ParrotProtocol', 'resource_manager'], + py_modules=['ParrotZik', 'message', 'resource_manager'], scripts=["ParrotZikTray"] ) -- cgit v1.2.1