From 24e1daa5834b80301f72c3baf1d90819f0e651ee Mon Sep 17 00:00:00 2001 From: Foxy82 Date: Thu, 8 Aug 2024 21:52:14 +0100 Subject: [PATCH] Fixed bug where close on a matrix that hadn't been connected would error. Added ability to get matrix meta data. --- CHANGELOG.md | 9 ++++++++- example.py | 6 ++++++ pyblustream/matrix.py | 41 +++++++++++++++++++++++++++++++++++++---- pyblustream/protocol.py | 3 ++- setup.py | 2 ++ 5 files changed, 55 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 408c209..d60eaa3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,17 @@ Change Log ======================= -v0.11 +v0.12 ------------ +Don't error if close() is called on a matrix that hasn't connected successfully. +Added mtrix metadata but you must use async_connect for this to be populated. + + +v0.11 +------------ +Add async_connect method v0.10 ------------ diff --git a/example.py b/example.py index c5f15a3..2351c74 100644 --- a/example.py +++ b/example.py @@ -57,6 +57,12 @@ async def main(): # Or the async way await matrix.async_connect() + print(matrix.output_names) + print(matrix.input_names) + print(matrix.mac) + print(matrix.device_name) + print(matrix.firmware_version) + await asyncio.sleep(20000) # Programmatically change the source for output 2 to input 3. diff --git a/pyblustream/matrix.py b/pyblustream/matrix.py index 8b705f8..daaac32 100644 --- a/pyblustream/matrix.py +++ b/pyblustream/matrix.py @@ -1,20 +1,53 @@ +import aiohttp from typing import Optional from pyblustream.listener import MultiplexingListener from pyblustream.protocol import MatrixProtocol - +import xmltodict class Matrix: def __init__(self, hostname, port): + self._hostname = hostname self._multiplex_callback = MultiplexingListener() self._protocol = MatrixProtocol(hostname, port, self._multiplex_callback) - - def connect(self): - self._protocol.connect() + self.output_names = {} + self.input_names = {} + self.mac = None + self.device_name = None + self.firmware_version = None async def async_connect(self): await self._protocol.async_connect() + metadata_json = await self._get_matrix_metadata() + + # Extract required fields + webserver = metadata_json.get('MATRIX', {}).get('webserver', {}) + mxsta = metadata_json.get('MATRIX', {}).get('mxsta', {}) + + self.mac = webserver.get('mac', '') + self.device_name = mxsta.get('devname', '') + self.firmware_version = mxsta.get('softver', '') + + # Extract input names + inputs = metadata_json.get('MATRIX', {}).get('input', []) + input_names = [input_item.get('name', '').replace("_", " ") for input_item in inputs] + for (index, input_name) in enumerate(input_names, start=1): + self.input_names[index] = input_name + + # Extract output names + outputs = metadata_json.get('MATRIX', {}).get('output', []) + output_names = [output_item.get('name', '').replace("_", " ") for output_item in outputs] + for (index, output_name) in enumerate(output_names, start=1): + self.output_names[index] = output_name + + async def _get_matrix_metadata(self) -> str: + url = f"http://{self._hostname}/cgi-bin/getxml.cgi?xml=mxsta" + + async with aiohttp.ClientSession() as session: + async with session.get(url) as response: + response_text = await response.text() + return xmltodict.parse(response_text) def close(self): self._protocol.close() diff --git a/pyblustream/protocol.py b/pyblustream/protocol.py index 9a27be5..3e24f6c 100644 --- a/pyblustream/protocol.py +++ b/pyblustream/protocol.py @@ -73,7 +73,8 @@ async def async_connect(self): def close(self): self._reconnect = False - self._transport.close() + if self._transport: + self._transport.close() def connection_made(self, transport): """ Method from asyncio.Protocol """ diff --git a/setup.py b/setup.py index 16276aa..168482e 100644 --- a/setup.py +++ b/setup.py @@ -19,6 +19,8 @@ download_url=f'https://github.com/foxy82/designer-living/archive/{version}.tar.gz', keywords=['Blustream', 'Elan', 'HDBaseT'], install_requires=[ + "aiohttp==3.8.4", + "xmltodict==0.13.0" ], classifiers=[ 'Development Status :: 5 - Production/Stable',