-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.py
More file actions
97 lines (77 loc) · 2.58 KB
/
main.py
File metadata and controls
97 lines (77 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
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, local_address: str):
"""Performs your actions."""
# open session and create client
session = LoggingClientSession()
client = create(
fingerprint=fingerprint,
access_token=access_token,
client_session=session,
local_address=local_address,
)
# 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 = ""
address = None
opts, args = getopt.getopt(
argv[1:], "hf:t:a:", ["fingerprint=", "token=", "address="]
)
for opt, arg in opts:
if opt in ("-f", "--fingerprint"):
finger = arg
elif opt in ("-t", "--token"):
token = arg
elif opt in ("-a", "--address"):
address = 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, address))