Skip to content

Commit

Permalink
Add async_connect as a method
Browse files Browse the repository at this point in the history
  • Loading branch information
foxy82 committed Aug 8, 2024
1 parent 0bec782 commit 38d2968
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 9 deletions.
165 changes: 163 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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/
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Change Log
=======================

v0.11
------------



v0.10
------------

Expand Down
13 changes: 9 additions & 4 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions pyblustream/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
16 changes: 14 additions & 2 deletions pyblustream/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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}"
Expand All @@ -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:
Expand Down
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.10'
version = '0.11'

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

0 comments on commit 38d2968

Please sign in to comment.