From 38d2968ed5d84991c38322fd07fa716dd9e1f69f Mon Sep 17 00:00:00 2001 From: Foxy82 Date: Thu, 8 Aug 2024 17:36:46 +0100 Subject: [PATCH] Add async_connect as a method --- .gitignore | 165 +++++++++++++++++++++++++++++++++++++++- CHANGELOG.md | 5 ++ example.py | 13 +++- pyblustream/matrix.py | 3 + pyblustream/protocol.py | 16 +++- setup.py | 2 +- 6 files changed, 195 insertions(+), 9 deletions(-) diff --git a/.gitignore b/.gitignore index 1432e35..05aeda3 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,164 @@ -pyblustream.egg-info/* +# Byte-compiled / optimized / DLL files +__pycache__/ +*.py[cod] +*$py.class + +# C extensions +*.so + +# Distribution / packaging +.Python +build/ +develop-eggs/ +dist/ +downloads/ +eggs/ +.eggs/ +lib/ +lib64/ +parts/ +sdist/ +var/ +wheels/ +share/python-wheels/ +*.egg-info/ +.installed.cfg +*.egg MANIFEST -dist/* + +# PyInstaller +# Usually these files are written by a python script from a template +# before PyInstaller builds the exe, so as to inject date/other infos into it. +*.manifest +*.spec + +# Installer logs +pip-log.txt +pip-delete-this-directory.txt + +# Unit test / coverage reports +htmlcov/ +.tox/ +.nox/ +.coverage +.coverage.* +.cache +nosetests.xml +coverage.xml +*.cover +*.py,cover +.hypothesis/ +.pytest_cache/ +cover/ + +# Translations +*.mo +*.pot + +# Django stuff: +*.log +local_settings.py +db.sqlite3 +db.sqlite3-journal + +# Flask stuff: +instance/ +.webassets-cache + +# Scrapy stuff: +.scrapy + +# Sphinx documentation +docs/_build/ + +# PyBuilder +.pybuilder/ +target/ + +# Jupyter Notebook +.ipynb_checkpoints + +# IPython +profile_default/ +ipython_config.py + +# pyenv +# For a library or package, you might want to ignore these files since the code is +# intended to run in multiple environments; otherwise, check them in: +# .python-version + +# pipenv +# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control. +# However, in case of collaboration, if having platform-specific dependencies or dependencies +# having no cross-platform support, pipenv may install dependencies that don't work, or not +# install all needed dependencies. +#Pipfile.lock + +# poetry +# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control. +# This is especially recommended for binary packages to ensure reproducibility, and is more +# commonly ignored for libraries. +# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control +#poetry.lock + +# pdm +# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control. +#pdm.lock +# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it +# in version control. +# https://pdm.fming.dev/latest/usage/project/#working-with-version-control +.pdm.toml +.pdm-python +.pdm-build/ + +# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm +__pypackages__/ + +# Celery stuff +celerybeat-schedule +celerybeat.pid + +# SageMath parsed files +*.sage.py + +# Environments +.env +.venv +env/ +venv/ +ENV/ +env.bak/ +venv.bak/ + +# Spyder project settings +.spyderproject +.spyproject + +# Rope project settings +.ropeproject + +# mkdocs documentation +/site + +# mypy +.mypy_cache/ +.dmypy.json +dmypy.json + +# Pyre type checker +.pyre/ + +# pytype static type analyzer +.pytype/ + +# Cython debug symbols +cython_debug/ + +# PyCharm +# JetBrains specific template is maintained in a separate JetBrains.gitignore that can +# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore +# and can be added to the global gitignore or merged into this file. For a more nuclear +# option (not recommended) you can uncomment the following to ignore the entire idea folder. +# +# foxy82: For now we will ignore .idea +.idea/ \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f06fc1..408c209 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,11 @@ Change Log ======================= +v0.11 +------------ + + + v0.10 ------------ diff --git a/example.py b/example.py index 2965b55..c5f15a3 100644 --- a/example.py +++ b/example.py @@ -49,19 +49,24 @@ async def main(): matrix.register_listener(MyListener(connected_event)) # You always need to connect to the matrix - best to do this after # adding your listener to avoid missing the initial status that is returned on start up - matrix.connect() - await connected_event.wait() + + # Either + #matrix.connect() + #await connected_event.wait() + + # Or the async way + await matrix.async_connect() await asyncio.sleep(20000) # Programmatically change the source for output 2 to input 3. - matrix.change_source(3, 5) + matrix.change_source(input_id=6, output_id=5) print("--- All Outputs --- ") all_outputs = matrix.status_of_all_outputs() print(all_outputs) print("--- Input for Zone 1 --- ") - input_for_zone_one = matrix.status_of_output(1) + input_for_zone_one = matrix.status_of_output(output_id=1) print(input_for_zone_one) # Force the matrix to refresh its status diff --git a/pyblustream/matrix.py b/pyblustream/matrix.py index 4c9881c..8b705f8 100644 --- a/pyblustream/matrix.py +++ b/pyblustream/matrix.py @@ -13,6 +13,9 @@ def __init__(self, hostname, port): def connect(self): self._protocol.connect() + async def async_connect(self): + await self._protocol.async_connect() + def close(self): self._protocol.close() diff --git a/pyblustream/protocol.py b/pyblustream/protocol.py index 32f20bb..9a27be5 100644 --- a/pyblustream/protocol.py +++ b/pyblustream/protocol.py @@ -66,11 +66,17 @@ def connect(self): connection_task = self._loop.create_connection(lambda: self, host=self._hostname, port=self._port) self._loop.create_task(connection_task) + async def async_connect(self): + transport, protocol = await self._loop.create_connection( + lambda: self, host=self._hostname, port=self._port + ) + def close(self): self._reconnect = False self._transport.close() def connection_made(self, transport): + """ Method from asyncio.Protocol """ self._connected = True self._transport = transport self.peer_name = transport.get_extra_info("peername") @@ -86,12 +92,17 @@ async def _heartbeat(self): self._logger.debug('heartbeat') self._data_send("\n") - async def wait_to_reconnect(self): + async def _wait_to_reconnect(self): + # TODO with the new async_connect I think we can make this much easier - but I can't test right now + # so not changing yet: + # await self.async_connect() + # using old sync connect. while not self._connected and self._reconnect: await asyncio.sleep(self._reconnect_time) self.connect() def connection_lost(self, exc): + """ Method from asyncio.Protocol """ self._connected = False self._heartbeat_task.cancel() disconnected_message = f"Disconnected from {self._hostname}" @@ -104,10 +115,11 @@ def connection_lost(self, exc): self._logger.info(disconnected_message) self._source_change_callback.disconnected() if self._reconnect: - self._loop.create_task(self.wait_to_reconnect()) + self._loop.create_task(self._wait_to_reconnect()) pass def data_received(self, data): + """ Method from asyncio.Protocol """ self._logger.debug(f"data_received client: {data}") for letter in data: diff --git a/setup.py b/setup.py index cebc311..16276aa 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,6 @@ from setuptools import setup -version = '0.10' +version = '0.11' with open("README.md", "rb") as f: long_descr = f.read().decode("utf-8")