Skip to content

Instantly share code, notes, and snippets.

@gitcrtn
Last active January 13, 2020 01:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitcrtn/09b7a2aeff7fb86f4ccb4bb8b741af81 to your computer and use it in GitHub Desktop.
Save gitcrtn/09b7a2aeff7fb86f4ccb4bb8b741af81 to your computer and use it in GitHub Desktop.
Install Pebble face from Termux
"""
Install Pebble face from Termux
Requires:
- libpebble2
Usage:
python2 install_pebble_face.py <pbw path>
"""
import sys
import os
import socket
import struct
import fcntl
from libpebble2.communication import PebbleConnection
from libpebble2.communication.transports.websocket import WebsocketTransport, MessageTargetPhone
from libpebble2.communication.transports.websocket.protocol import WebSocketInstallBundle, WebSocketInstallStatus
from libpebble2.exceptions import TimeoutError
class Installer(object):
def __init__(self):
self._ready = False
try:
self._pebble = self.get_pebble()
self._ready = True
except Exception as err:
print(err.message)
def get_ip(self, ifname):
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
return socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', ifname[:15])
)[20:24])
def get_pebble(self):
transport = self.get_transport()
connection = PebbleConnection(transport)
connection.connect()
connection.run_async()
return connection
def get_transport(self):
ip = self.get_ip('wlan0')
if not ip.startswith('192.168.'):
raise RuntimeError('LAN IP not found')
print('Phone IP: %s' % ip)
port = 9000
return WebsocketTransport('ws://{}:{}/'.format(ip, port))
def close(self):
self._pebble.transport.ws.close()
def install(self, pbw):
if not self._ready:
print('Phone connection not found')
return
with open(pbw) as f:
print("Installing app...")
self._pebble.transport.send_packet(
WebSocketInstallBundle(pbw=f.read()), target=MessageTargetPhone())
try:
result = self._pebble.read_transport_message(
MessageTargetPhone, WebSocketInstallStatus, timeout=300)
except TimeoutError:
print("Timed out waiting for install confirmation.")
except Exception as err:
print(err.message)
if result.status != WebSocketInstallStatus.StatusCode.Success:
print("App install failed.")
else:
print("App install succeeded.")
self.close()
def main():
pbw = sys.argv[1]
if not os.path.exists(pbw):
print('pbw path is wrong: %s' % pbw)
return
Installer().install(pbw)
if __name__ == '__main__':
main()
# EOF
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment