Skip to content

Commit

Permalink
Fixed bug where close on a matrix that hadn't been connected would er…
Browse files Browse the repository at this point in the history
…ror. Added ability to get matrix meta data.
  • Loading branch information
foxy82 committed Aug 8, 2024
1 parent 38d2968 commit 24e1daa
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 6 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
------------
Expand Down
6 changes: 6 additions & 0 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
41 changes: 37 additions & 4 deletions pyblustream/matrix.py
Original file line number Diff line number Diff line change
@@ -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()
Expand Down
3 changes: 2 additions & 1 deletion pyblustream/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 """
Expand Down
2 changes: 2 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 24e1daa

Please sign in to comment.