From 9ebc78fc7be036dd691f88f63f1215a42eaab55a Mon Sep 17 00:00:00 2001 From: Martijn The Date: Tue, 27 Aug 2013 21:53:40 -0700 Subject: [PATCH] - Add extra ws_command byte to messages going to the phone. - Cleanup the app install over ws mess --- pebble/WebSocketPebble.py | 21 +++++++++++++++------ pebble/pebble.py | 27 ++++++++++++++------------- 2 files changed, 29 insertions(+), 19 deletions(-) diff --git a/pebble/WebSocketPebble.py b/pebble/WebSocketPebble.py index dc18815..8732876 100644 --- a/pebble/WebSocketPebble.py +++ b/pebble/WebSocketPebble.py @@ -2,16 +2,23 @@ import logging from websocket import * from struct import unpack +from struct import pack # This file contains the libpebble websocket client. # Based on websocket.py from: # https://github.com/liris/websocket-client +WS_CMD_WATCH_TO_PHONE = 0x00 +WS_CMD_PHONE_TO_WATCH = 0x01 +WS_CMD_PHONE_APP_LOG = 0x02 +WS_CMD_SERVER_LOG = 0x03 +WS_CMD_APP_INSTALL = 0x04 + class WebSocketPebble(WebSocket): ######## libPebble Bridge Methods ######### - def write(self, payload, opcode = ABNF.OPCODE_BINARY): + def write(self, payload, opcode = ABNF.OPCODE_BINARY, ws_cmd = WS_CMD_PHONE_TO_WATCH): """ BRIDGES THIS METHOD: def write(self, message): @@ -24,6 +31,8 @@ def write(self, message): log.debug("LightBlue process has shutdown (queue write)") """ + # Append command byte to the payload: + payload = pack("B", ws_cmd) + payload frame = ABNF.create_frame(payload, opcode) if self.get_mask_key: frame.get_mask_key = self.get_mask_key @@ -50,14 +59,14 @@ def read(self): opcode, data = self.recv_data() size, endpoint = unpack("!HH", data[1:5]) resp = data[5:] - direction = unpack('!b',data[0]) - if direction[0]==3: + ws_cmd = unpack('!b',data[0]) + if ws_cmd[0]==WS_CMD_SERVER_LOG: logging.debug("Server: %s" % repr(data[1:])) - if direction[0]==2: + if ws_cmd[0]==WS_CMD_PHONE_APP_LOG: logging.debug("Log: %s" % repr(data[1:])) - if direction[0]==1: + if ws_cmd[0]==WS_CMD_PHONE_TO_WATCH: logging.debug("Phone ==> Watch: %s" % data[1:].encode("hex")) - if direction[0]==0: + if ws_cmd[0]==WS_CMD_WATCH_TO_PHONE: logging.debug("Watch ==> Phone: %s" % data[1:].encode("hex")) return (endpoint, resp, data[1:5]) else: diff --git a/pebble/pebble.py b/pebble/pebble.py index 603312b..cd6785b 100755 --- a/pebble/pebble.py +++ b/pebble/pebble.py @@ -497,20 +497,12 @@ def reinstall_app_by_uuid(self, uuid, pbz_path): self.remove_app_by_uuid(uuid) self.install_app(pbz_path) - def install_app(self, pbz_path, launch_on_install=True): - - """ - Install an app bundle (*.pbw) to the target Pebble. - - This will pick the first free app-bank available. - """ - - # if self.using_ws: - # f = open(pbz_path, 'r') - # data = f.read() - # self._send_message("VERSION",data) #warning: using the version endpoint but any endpoint should work since we're sending it to the phone, would be nice to have a new endpoint - # return; + def _install_app_ws(self, pbz_path): + f = open(pbz_path, 'r') + data = f.read() + self._ser.write(data, ws_cmd=WebSocketPebble.WS_CMD_APP_INSTALL) + def _install_app_pebble_protocol(self, pbz_path): bundle = PebbleBundle(pbz_path) if not bundle.is_app_bundle(): raise PebbleError(self.id, "This is not an app bundle") @@ -553,6 +545,15 @@ def install_app(self, pbz_path, launch_on_install=True): self._add_app(first_free) time.sleep(2) + def install_app(self, pbz_path, launch_on_install=True): + + """Install an app bundle (*.pbw) to the target Pebble.""" + + if self.using_ws: + self._install_app_ws(pbz_path) + else: + self._install_app_pebble_protocol(pbz_path) + if launch_on_install: self.launcher_message(app_metadata['uuid'].bytes, "RUNNING", uuid_is_string=False)