Skip to content
This repository has been archived by the owner on Jan 5, 2024. It is now read-only.

Commit

Permalink
Make connect() return the credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
rccoleman committed Dec 27, 2020
1 parent 3605cc3 commit 948fb13
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 53 deletions.
34 changes: 18 additions & 16 deletions README.md
@@ -1,21 +1,23 @@
# lmdirect

## Local API access to network-connected La Marzocco espresso machines

This is a prototype library for interacting with the local network API of a La Marzocco espresso machine. It's still in the beginning stages, but currently it's able to retrieve configuration and status information from the machine and turn it on/off. I've reverse-engineered this from decoding the network traffic between the mobile app and the machine, so it's incomplete, imperfect, and may have significant bugs. It works on my La Marzocco GS/3, but I can't guarantee that you'll have a similar experience with yours.
This is a prototype library for interacting with the local network API of a La Marzocco espresso machine. It's still in the beginning stages, but currently it's able to retrieve configuration and status information from the machine and turn it on/off. I've reverse-engineered this from decoding the network traffic between the mobile app and the machine, so it's incomplete, imperfect, and may have significant bugs. It works on my La Marzocco GS/3, but I can't guarantee that you'll have a similar experience with yours.

**You take any and all risks from using this on your machine!**

### Preparation & Installation

Using this library is an advanced exercise. You'll need to do the following:
* Find the `client_id` and `client_secret` for your machine by sniffing the network traffic while operating the mobile app (`mitmproxy` is good for this). You'll need to capture a token request to https://cms.lamarzocco.io/oauth/v2/token and find the `client_id` and `client_secret` in the request.
* Find the username & password that you used to register with La Marzocco when you set up remote access. The username is most likely the email address that you used for registration.
Using this library is an advanced exercise. You'll need to do the following:

- Find the `client_id` and `client_secret` for your machine by sniffing the network traffic while operating the mobile app (`mitmproxy` is good for this). You'll need to capture a token request to https://cms.lamarzocco.io/oauth/v2/token and find the `client_id` and `client_secret` in the request.
- Find the username & password that you used to register with La Marzocco when you set up remote access. The username is most likely the email address that you used for registration.

Once you have the client ID, client secret, username, and passowrd, construct a file called `config.json` with these contents and put it in the directory along with `test.py`:

```
{
"ip_addr": "ip_address",
"host": "host",
"client_id": "a_long_string",
"client_secret": "another_long_string",
"username": "email@address.com",
Expand All @@ -29,23 +31,23 @@ Now, run `python test.py` and you should get a prompt that looks like this:

`1 = ON, 2 = OFF, 3 = Status, Other = quit:`

You can hit `1` to turn the machine on, `2` to turn it off, `3` to dump a dict of all the config and status items that it's received from your machine, and any other key to quit. The app requests all status & config information every 5 seconds, so you should see the values change when the state of the machine changes.
You can hit `1` to turn the machine on, `2` to turn it off, `3` to dump a dict of all the config and status items that it's received from your machine, and any other key to quit. The app requests all status & config information every 5 seconds, so you should see the values change when the state of the machine changes.

The machine can only accept a single connection at a time, but the library keeps the connection open long enough only to send commands and receive the responses. Both the mobile app and this library will attempt to reconnect if the port is in use, but you may need to wait a bit while using the mobile app for it to try again.
The machine can only accept a single connection at a time, but the library keeps the connection open long enough only to send commands and receive the responses. Both the mobile app and this library will attempt to reconnect if the port is in use, but you may need to wait a bit while using the mobile app for it to try again.

### Notes

So far, these are the commands that I’ve found:

* A command that I call `D8` that appears to request the same info that you get from the “status” cloud endpoint. This is a read starting with “R”, and has a preamble of `40000020`.
* A command that I call `E9` that appears to request the same info that you get from the “configuration” cloud endpoint. This is a read starting with “R”, and has a preamble of `0000001F`
* A command that I call `EB` that requests the auto on/off schedule. This is a read starting with “R”, and has a preamble of `0310001D`
* A command that writes a value, and it starts with “W” and has a preamble of `00000001`. I’ve only used this to turn the machine on and off so far, so there may be other preambles.
- A command that I call `D8` that appears to request the same info that you get from the “status” cloud endpoint. This is a read starting with “R”, and has a preamble of `40000020`.
- A command that I call `E9` that appears to request the same info that you get from the “configuration” cloud endpoint. This is a read starting with “R”, and has a preamble of `0000001F`
- A command that I call `EB` that requests the auto on/off schedule. This is a read starting with “R”, and has a preamble of `0310001D`
- A command that writes a value, and it starts with “W” and has a preamble of `00000001`. I’ve only used this to turn the machine on and off so far, so there may be other preambles.

The responses have a matching “R” or “W” and mostly-matching preamble based on the command and these are the types that I’ve found and decoded (more or less):

* A periodic short status broadcast that gives the current brew/steam boiler temps. I don’t need to send a command to get this - it may just be sent to whoever has an active socket connection. Preamble is `401C0004`
* A response to the `D8` command with status info
* A response to the `E9` command with config info
* A response to the `EB` command with the auto on/off schedule and what’s enabled
* A “confirmation” response to a “write” command from the app (message to the machine starting with “W”). This includes the string “OK” if it succeeded.
- A periodic short status broadcast that gives the current brew/steam boiler temps. I don’t need to send a command to get this - it may just be sent to whoever has an active socket connection. Preamble is `401C0004`
- A response to the `D8` command with status info
- A response to the `E9` command with config info
- A response to the `EB` command with the auto on/off schedule and what’s enabled
- A “confirmation” response to a “write” command from the app (message to the machine starting with “W”). This includes the string “OK” if it succeeded.
10 changes: 5 additions & 5 deletions lmdirect/__init__.py
@@ -1,5 +1,5 @@
"""lmdierct"""
from lmdirect.const import DISABLED, ENABLED
from lmdirect.const import DISABLED, ENABLED, MACHINE_NAME, MODEL_NAME, SERIAL_NUMBER
from .connection import Connection
from .msgs import (
DOSE_TEA,
Expand Down Expand Up @@ -38,17 +38,17 @@ def current_status(self):
@property
def machine_name(self):
"""Return the name of the machine"""
return self._machine_name
return self._creds[MACHINE_NAME]

@property
def serial_number(self):
"""Return serial number"""
return self._serial_number
return self._creds[SERIAL_NUMBER]

@property
def model_name(self):
"""Return model name"""
return self._model_name
return self._creds[MODEL_NAME]

def register_callback(self, callback):
"""Register callback for updates"""
Expand All @@ -68,7 +68,7 @@ def deregister_raw_callback(self, key):
self._raw_callback_list.remove(key)

async def connect(self):
await self._connect()
return await self._connect()

async def request_status(self):
"""Request all status elements"""
Expand Down
61 changes: 36 additions & 25 deletions lmdirect/connection.py
Expand Up @@ -9,6 +9,7 @@
from functools import partial

_LOGGER = logging.getLogger(__name__)
_LOGGER.setLevel(logging.DEBUG)

from .msgs import Msg, MSGS, AUTO_BITFIELD_MAP, Elem
from .const import *
Expand All @@ -18,40 +19,40 @@
class Connection:
def __init__(self, creds):
"""Init LMDirect"""
self._reader = self._writer = None
self._reader = None
self._writer = None
self._read_response_task = None
self._reaper_task = None
self._current_status = {}
self._responses_waiting = []
self._run = False
self._run = True
self._callback_list = []
self._raw_callback_list = []
self._cipher = None
self._creds = creds
self._key = None
self._start_time = None
self._connected = False
self._lock = asyncio.Lock()
self._first_time = True

async def retrieve_key(self):
async def retrieve_key(self, creds):
"""Machine data inialization"""
client = AsyncOAuth2Client(
client_id=self._creds[CLIENT_ID],
client_secret=self._creds[CLIENT_SECRET],
client_id=creds[CLIENT_ID],
client_secret=creds[CLIENT_SECRET],
token_endpoint=TOKEN_URL,
)

headers = {
"client_id": self._creds[CLIENT_ID],
"client_secret": self._creds[CLIENT_SECRET],
"client_id": creds[CLIENT_ID],
"client_secret": creds[CLIENT_SECRET],
}

try:
await client.fetch_token(
url=TOKEN_URL,
username=self._creds[USERNAME],
password=self._creds[PASSWORD],
username=creds[USERNAME],
password=creds[PASSWORD],
headers=headers,
)
except OAuthError:
Expand All @@ -66,54 +67,57 @@ async def retrieve_key(self):
cust_info = await client.get(CUSTOMER_URL)
if cust_info is not None:
fleet = cust_info.json()["data"]["fleet"][0]
self._key = fleet["communicationKey"]
self._serial_number = fleet["machine"]["serialNumber"]
self._machine_name = fleet["name"]
self._model_name = fleet["machine"]["model"]["name"]
self._cipher = AESCipher(self._key)
creds[KEY] = fleet["communicationKey"]
creds[SERIAL_NUMBER] = fleet["machine"]["serialNumber"]
creds[MACHINE_NAME] = fleet["name"]
creds[MODEL_NAME] = fleet["machine"]["model"]["name"]

"""Done with the cloud API"""
await client.aclose()

return creds

async def _connect(self):
"""Conmnect to espresso machine"""
if self._connected:
return
return self._creds

_LOGGER.debug("Connecting")

if not self._key:
if not self._creds.get(KEY):
try:
await self.retrieve_key()
self._creds.update(await self.retrieve_key(self._creds))
except Exception as err:
_LOGGER.debug("Exception retrieving key: {}".format(err))
raise

if not self._cipher:
self._cipher = AESCipher(self._creds[KEY])

TCP_PORT = 1774

"""Connect to the machine"""
try:
self._reader, self._writer = await asyncio.wait_for(
asyncio.open_connection(self._creds[IP_ADDR], TCP_PORT), timeout=3
asyncio.open_connection(self._creds[HOST], TCP_PORT), timeout=3
)
except asyncio.TimeoutError:
_LOGGER.warning("Connection Timeout, skipping")
return False
return None

except Exception as err:
_LOGGER.error("Cannot connect to machine: {}".format(err))
return False
return None

"""Start listening for responses & sending status requests"""
await self.start_read_task()

self._connected = True
_LOGGER.debug("Finished Connecting")
return True

async def start_read_task(self):
self._run = True
return self._creds

async def start_read_task(self):
"""Start listening for responses & sending status requests"""
loop = asyncio.get_event_loop()
self._read_response_task = loop.create_task(
Expand All @@ -130,14 +134,18 @@ async def _close(self):

if self._writer is not None:
self._writer.close()

if self._read_response_task:
self._read_response_task.cancel()

self._reader = self._writer = None
self._connected = False
_LOGGER.debug("Finished closing")

async def reaper(self):
_LOGGER.debug("Starting reaper")
try:
await asyncio.gather(*[self._read_response_task])
await asyncio.gather(self._read_response_task)
except Exception as err:
_LOGGER.error(f"Exception in read_response_task: {err}")

Expand Down Expand Up @@ -293,6 +301,9 @@ def checksum(buffer):
"""Connect if we don't have an active connection"""
await self._connect()

if not self._writer:
_LOGGER.error(f"self._writer={self._writer}")

"""Add read/write and check bytes"""
plaintext = msg.msg_type + msg.msg

Expand Down
14 changes: 9 additions & 5 deletions lmdirect/const.py
Expand Up @@ -6,8 +6,12 @@
TOKEN_URL = "https://cms.lamarzocco.io/oauth/v2/token"
CUSTOMER_URL = "https://cms.lamarzocco.io/api/customer"

IP_ADDR = "IP_ADDR"
CLIENT_ID = "CLIENT_ID"
CLIENT_SECRET = "CLIENT_SECRET"
USERNAME = "USERNAME"
PASSWORD = "PASSWORD"
HOST = "host"
CLIENT_ID = "client_id"
CLIENT_SECRET = "client_secret"
USERNAME = "username"
PASSWORD = "password"
KEY = "key"
SERIAL_NUMBER = "serial_number"
MACHINE_NAME = "machine_name"
MODEL_NAME = "model_name"
10 changes: 8 additions & 2 deletions test.py
Expand Up @@ -27,7 +27,7 @@ def read_config(self):
exit(1)

creds = {
IP_ADDR: data["ip_addr"],
HOST: data["host"],
CLIENT_ID: data["client_id"],
CLIENT_SECRET: data["client_secret"],
USERNAME: data["username"],
Expand Down Expand Up @@ -125,7 +125,13 @@ def check_args(num_args):
break

self._run = False
await asyncio.gather(*[self._poll_status_task])

self._poll_status_task.cancel()

try:
await asyncio.gather(self._poll_status_task)
except asyncio.CancelledError:
pass

await self.lmdirect.close()

Expand Down

0 comments on commit 948fb13

Please sign in to comment.