Skip to content

Commit

Permalink
Add additional properties to get details of input and output names.
Browse files Browse the repository at this point in the history
  • Loading branch information
foxy82 committed Aug 9, 2024
1 parent 2d4c6ce commit 178a178
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
Change Log
=======================

v 0.19
------------

Add:
outputs_by_id
outputs_by_name
inputs_by_id
inputs_by_name


v 0.18
------------

Expand Down
82 changes: 48 additions & 34 deletions pyblustream/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,29 @@
class Matrix:

def __init__(self, hostname, port):
self.hostname = hostname
self.hostname : str = hostname
self._multiplex_callback = MultiplexingListener()
self._protocol = MatrixProtocol(hostname, port, self._multiplex_callback)
self.output_names = {}
self.input_names = {}
self.mac = None
self.device_name = None
self.firmware_version = None
self.outputs_by_id : dict[int, str] = {}
self.outputs_by_name : dict[str, int] = {}
self.inputs_by_id : dict[int, str] = {}
self.inputs_by_name : dict[str, int] = {}
self.mac : Optional[str] = None
self.device_name : Optional[str] = None
self.firmware_version : Optional[str] = None

@property
def output_names(self):
return list(self.outputs_by_name.keys())

@property
def input_names(self):
return list(self.inputs_by_name.keys())

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)
self._process_meta_data(metadata_json)

def close(self):
self._protocol.close()
Expand Down Expand Up @@ -78,3 +61,34 @@ def register_listener(self, listener):

def unregister_listener(self, listener):
self._multiplex_callback.unregister_listener(listener)

def _process_meta_data(self, metadata_json):
# 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.inputs_by_id[index] = input_name
self.inputs_by_name[input_name] = index

# 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.outputs_by_id[index] = output_name
self.outputs_by_name[output_name] = index

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)
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from setuptools import setup

version = '0.18'
version = '0.19'

with open("README.md", "rb") as f:
long_descr = f.read().decode("utf-8")
Expand Down

0 comments on commit 178a178

Please sign in to comment.