Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
51 lines (41 loc) · 1.95 KB
/
Copy pathmain.py
File metadata and controls
51 lines (41 loc) · 1.95 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
"""
Send one command to a shocker and handle the ways the backend can refuse it.
python main.py <shocker-id> [vibrate|sound|shock] [intensity] [duration-ms]
"""
import asyncio
import os
import sys
from uuid import UUID
from openshock import OpenShockApiError, OpenShockClient, ShockerNoPermission, ShockerNotFoundOrNoAccess, ShockerPaused
from openshock.rest.api.shockers import shocker_send_control
from openshock.rest.models import Control, ControlRequest, 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)."
)
async def main() -> None:
if len(sys.argv) < 2:
sys.exit(__doc__)
shocker_id = UUID(sys.argv[1])
control_type = ControlType((sys.argv[2] if len(sys.argv) > 2 else "vibrate").capitalize())
intensity = int(sys.argv[3]) if len(sys.argv) > 3 else 25
duration = int(sys.argv[4]) if len(sys.argv) > 4 else 1000
request = ControlRequest(
shocks=[Control(id=shocker_id, type_=control_type, intensity=intensity, duration=duration)],
custom_name="SDK example",
)
async with OpenShockClient(API_TOKEN, base_url=BASE_URL, app_name=APP_NAME, app_version=APP_VERSION) as client:
try:
await shocker_send_control.asyncio(client=client, body=request)
except ShockerPaused:
sys.exit("That shocker is paused.")
except ShockerNotFoundOrNoAccess:
sys.exit("That shocker does not exist or is not shared with you.")
except ShockerNoPermission:
sys.exit(f"You are not allowed to use {control_type} on that shocker.")
except OpenShockApiError as exc:
sys.exit(f"Request failed: {exc}")
print(f"Sent {control_type} at {intensity}% for {duration} ms")
asyncio.run(main())