Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
60 lines (45 loc) · 2.09 KB
/
Copy pathmain.py
File metadata and controls
60 lines (45 loc) · 2.09 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
"""
Ramp a shocker's vibration up and down through live control.
python main.py <shocker-id>
The shocker needs the "live" permission for your account, and its hub must be online.
"""
import asyncio
import math
import os
import sys
from uuid import UUID
from openshock import OpenShockClient, unwrap
from openshock.realtime import LiveControlClient, LiveResponseType
from openshock.rest.api.shockers import shocker_get_shocker_by_id
from openshock.rest.models import ControlType
APP_NAME = "OpenShock.SDK.Python.Examples"
APP_VERSION = "1.0.0"
BASE_URL = os.environ.get("OPENSHOCK_API_URL", "https://api.openshock.app")
API_TOKEN = os.environ.get("OPENSHOCK_API_TOKEN") or sys.exit(
"Set OPENSHOCK_API_TOKEN to an API token from https://openshock.app (Settings > API Tokens)."
)
PEAK = 30 # percent
SECONDS = 6
async def main() -> None:
if len(sys.argv) < 2:
sys.exit(__doc__)
shocker_id = UUID(sys.argv[1])
async with OpenShockClient(API_TOKEN, base_url=BASE_URL, app_name=APP_NAME, app_version=APP_VERSION) as client:
# Live control connects per hub, so find the hub this shocker belongs to.
shocker = unwrap(await shocker_get_shocker_by_id.asyncio(shocker_id, client=client)).data
live = LiveControlClient(shocker.device, client=client, app_name=APP_NAME, app_version=APP_VERSION)
live.on_latency(lambda own, hub: print(f"latency {own} ms"))
live.on_hub_not_connected(lambda: print("The hub went offline"))
@live.on_rejected
def on_rejected(reason: LiveResponseType, data: object) -> None:
print(f"Frame rejected: {reason.name}")
async with live:
print("Waiting for the hub's gateway...")
await live.wait_connected()
print(f"Connected to {live.gateway}, sending at {live.tps} ticks per second")
steps = SECONDS * 20
for step in range(steps):
intensity = round(PEAK * math.sin(math.pi * step / steps))
live.intake_frame(shocker_id, ControlType.VIBRATE, intensity)
await asyncio.sleep(0.05)
asyncio.run(main())