Skip to content

Commit

Permalink
- Add extra ws_command byte to messages going to the phone.
Browse files Browse the repository at this point in the history
- Cleanup the app install over ws mess
  • Loading branch information
martijnthe committed Aug 28, 2013
1 parent 628b6dd commit 9ebc78f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
21 changes: 15 additions & 6 deletions pebble/WebSocketPebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand All @@ -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:
Expand Down
27 changes: 14 additions & 13 deletions pebble/pebble.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 9ebc78f

Please sign in to comment.