Skip to content

Commit

Permalink
Updated for python 3.10
Browse files Browse the repository at this point in the history
  • Loading branch information
foxy82 committed Jan 22, 2022
1 parent d3f5fb7 commit 2e6161b
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 43 deletions.
69 changes: 44 additions & 25 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,39 +6,58 @@

class MyListener(SourceChangeListener):

def __init__(self, connected_event: asyncio.Event = None):
self._connected_event = connected_event

def source_changed(self, output_id, input_id):
# Your code to run when the source changes
print("Source Changed Output ", output_id, " input ", input_id)
pass

def connected(self):
# Your code to run on a successful connection to the matrix
pass
# For now we set event to say the matrix is connected
if self._connected_event is not None:
self._connected_event.set()
print("Connected")

def disconnected(self):
# Your code to run when disconnected from the matrix
# Note: the library will try to reconnect so you don't need to
pass
# Note: the library will try to reconnect, so you don't need to
print("Disconnected")


async def stop_in(seconds, event: asyncio.Event):
await asyncio.sleep(seconds)
event.set()


async def main():
# Set to your details
ip = "192.168.1.160"
port = 23
# Create a matrix
matrix = Matrix(ip, port)
# Register a listener so you can handle state changes
connected_event = asyncio.Event()
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()

# Programmatically change the source for output 2 to input 3.
matrix.change_source(2, 3)

all_outputs = matrix.status_of_all_outputs()
input_for_zone_one = matrix.status_of_output("01")

# Force the matrix to refresh its status
# This is done automatically on startup/reconnect, so you shouldn't need to do this
matrix.update_status()

await asyncio.sleep(20)
print("Done")

# Set to your details
ip = "127.0.0.1"
port = 23
# Use an asyncio event loop - you will need to make sure this runs
my_loop = asyncio.get_event_loop()
# Create a matrix
matrix = Matrix(ip, port, loop=my_loop)
# Register a listenerer so you can handle state changes
matrix.register_listener(MyListener())
# You always need to connect to the matrix - best to do this after
# adding your listener to avoid missing the inital status that is returned on start up
matrix.connect()

# Programmatically change the source for output 2 to input 3.
matrix.change_source(2, 3)

all_outputs = matrix.status_of_all_outputs()
input_for_zone_one = matrix.status_of_output("01")
# Force the matrix to refresh it's status
# This is done automatically on startup/reconnect so you shouldn't need to do this
matrix.update_status()
my_loop.run_forever()
if __name__ == '__main__':
asyncio.run(main())
36 changes: 24 additions & 12 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
from pyblustream.listener import LoggingListener
from pyblustream.matrix import Matrix

STOP = asyncio.Event()


def print_usage():
print('main.py -h <matrix ip address> [-p <port> -s <output_id> -o <output_id> -i <input_id> -a -l]')
Expand All @@ -17,7 +19,7 @@ def print_usage():
print('\t-l\t\t\t\t\t\t\tContinue running and listen for source changes')


def main(argv):
async def main(argv):
ip = None
port = 23
input_id = None
Expand Down Expand Up @@ -57,30 +59,36 @@ def main(argv):

logging.info("starting up..")

my_loop = asyncio.get_event_loop()

matrix = Matrix(ip, port, loop=my_loop)
matrix = Matrix(ip, port)
if listen:
matrix.register_listener(LoggingListener())
matrix.connect()

delay = 1
if output_id is not None and input_id is not None:
my_loop.create_task(change_source(delay, output_id, input_id, matrix))
asyncio.create_task(change_source(delay, output_id, input_id, matrix))
delay += 1

if status_output_id is not None:
my_loop.create_task(status(delay, status_output_id, matrix))
asyncio.create_task(status(delay, status_output_id, matrix))
delay += 1

if get_statuses:
my_loop.create_task(statuses(delay, matrix))
asyncio.create_task(statuses(delay, matrix))
delay += 1

if listen:
my_loop.run_forever()
else:
my_loop.run_until_complete(done(delay))
if not listen:
await asyncio.create_task(schedule_exit(delay))
await STOP.wait()


def ask_exit(*args):
STOP.set()


async def schedule_exit(delay):
await asyncio.sleep(delay)
ask_exit()


async def done(delay):
Expand All @@ -106,4 +114,8 @@ async def statuses(delay, matrix):

if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
main(sys.argv[1:])
try:
asyncio.run(main(sys.argv[1:]))
except KeyboardInterrupt:
ask_exit()
logging.info("Finished")
4 changes: 2 additions & 2 deletions pyblustream/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

class Matrix:

def __init__(self, hostname, port, loop=None):
def __init__(self, hostname, port):
self._multiplex_callback = MultiplexingListener()
self._protocol = MatrixProtocol(hostname, port, self._multiplex_callback, loop=loop)
self._protocol = MatrixProtocol(hostname, port, self._multiplex_callback)

def connect(self):
self._protocol.connect()
Expand Down
6 changes: 2 additions & 4 deletions pyblustream/protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ class MatrixProtocol(asyncio.Protocol):

_source_change_callback: SourceChangeListener

def __init__(self, hostname, port, callback: SourceChangeListener, loop=None, heartbeat_time=5, reconnect_time=10):
def __init__(self, hostname, port, callback: SourceChangeListener, heartbeat_time=5, reconnect_time=10):
self._logger = logging.getLogger(__name__)
self._heartbeat_time = heartbeat_time
self._reconnect_time = reconnect_time
self._hostname = hostname
self._port = port
self._source_change_callback = callback
self._loop = loop
if self._loop is None:
self._loop = asyncio.get_event_loop()
self._loop = asyncio.get_event_loop()

self._connected = False
self._transport = None
Expand Down

0 comments on commit 2e6161b

Please sign in to comment.