-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement listening to events endpoint using callbacks
BREAKING CHANGE: adds new method to listen for events in the background
- Loading branch information
Showing
7 changed files
with
293 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import asyncio | ||
import getopt | ||
from sys import argv | ||
|
||
import aiohttp | ||
|
||
from onyx_client.client import create | ||
from onyx_client.data.device_command import DeviceCommand | ||
|
||
|
||
class LoggingClientSession(aiohttp.ClientSession): | ||
"""Used to intercept requests and to be logged.""" | ||
|
||
def __init__(self, enable_logging: bool = True): | ||
"""Initialize the custom logging session.""" | ||
self.enable_logging = enable_logging | ||
super().__init__() | ||
|
||
async def _request(self, method, url, **kwargs): | ||
if self.enable_logging: | ||
print(f"Starting request {method} {url} {kwargs}\n") | ||
return await super()._request(method, url, **kwargs) | ||
|
||
|
||
async def shutter_worker(queue, client, device_id): | ||
"""Worker processing our position commands.""" | ||
while True: | ||
position = await queue.get() | ||
print( | ||
await client.send_command( | ||
device_id, DeviceCommand(properties={"target_position": position}) | ||
) | ||
) | ||
await asyncio.sleep(15) | ||
queue.task_done() | ||
|
||
|
||
async def perform(fingerprint: str, access_token: str): | ||
"""Performs your actions.""" | ||
|
||
# open session and create client | ||
session = LoggingClientSession() | ||
client = create( | ||
fingerprint=fingerprint, access_token=access_token, client_session=session | ||
) | ||
|
||
# verify API | ||
print(await client.verify()) | ||
print() | ||
|
||
# get all devices | ||
devices = await client.devices(include_details=True) | ||
print(devices) | ||
print() | ||
|
||
# call the events API | ||
def received(device): | ||
print(device) | ||
print(device.actual_position) | ||
print(device.actual_position.animation) | ||
|
||
client.set_event_callback(received) | ||
client.start() | ||
|
||
queue = asyncio.Queue() | ||
queue.put_nowait(300) | ||
await queue.join() | ||
await session.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
# process command line args | ||
finger = "" | ||
token = "" | ||
opts, args = getopt.getopt(argv[1:], "hf:t:", ["fingerprint=", "token="]) | ||
for opt, arg in opts: | ||
if opt in ("-f", "--fingerprint"): | ||
finger = arg | ||
elif opt in ("-t", "--token"): | ||
token = arg | ||
|
||
# check if args are not empty | ||
if len(finger) == 0 or len(token) == 0: | ||
print("No fingerprint and/or access token provided.") | ||
exit(1) | ||
|
||
# we are async, so wait until everything completed | ||
loop = asyncio.get_event_loop() | ||
loop.run_until_complete(perform(finger, token)) |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ | |
"Content-Type": "application/json", | ||
} | ||
API_VERSION = "v3" | ||
|
||
MAX_BACKOFF_TIME = 3 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters