dglab-kit-python 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,530 @@
1
+ Metadata-Version: 2.4
2
+ Name: dglab-kit-python
3
+ Version: 0.1.0
4
+ Summary: Python SDK for the DG-LAB App V3 and V4 WebSocket protocols.
5
+ Author: LF112
6
+ Author-email: LF112 <lf@lf112.net>
7
+ License-File: LICENSE
8
+ Requires-Dist: websockets>=15.0.1
9
+ Requires-Python: >=3.13
10
+ Project-URL: Homepage, https://github.com/dungeonlab-open/dglab-kit-python
11
+ Project-URL: Issues, https://github.com/dungeonlab-open/dglab-kit-python/issues
12
+ Description-Content-Type: text/markdown
13
+
14
+ <div align="center">
15
+
16
+ <img src="https://raw.githubusercontent.com/dungeonlab-open/dglab-kit-python/main/dglab.webp" alt="DG-LAB Logo" width="128" />
17
+
18
+ # DGLAB KIT - Python
19
+
20
+ English | [简体中文](https://github.com/dungeonlab-open/dglab-kit-python/blob/main/README_CN.md)
21
+
22
+ </div>
23
+
24
+ DGLAB KIT is a Python SDK for the **DG-LAB App**. It uses `asyncio` to pair with the app over WebSocket and control the local devices exposed by the app.
25
+
26
+ The SDK provides two main components:
27
+
28
+ - **Socket SDK**: Connect to V3 / V4 Socket servers for app pairing, device discovery, Channel Intensity control, Pulse delivery, task cleanup, and action feedback.
29
+ - **Pulse SDK**: Built-in Coyote and Opossum Pulse data, ready to use with V3 / V4 Pulse operations.
30
+
31
+ > Use V4 for new integrations. V4 supports one controller connected to multiple app clients, device list synchronization, and request/response handling. V3 is provided for compatibility with legacy controllers.
32
+
33
+ ## Table of Contents
34
+
35
+ - [Requirements](#requirements)
36
+ - [Installation](#installation)
37
+ - [Core Concepts](#core-concepts)
38
+ - [Quick Start: Control the App with V4](#quick-start-control-the-app-with-v4)
39
+ - [Generate an App Pairing QR Code](#generate-an-app-pairing-qr-code)
40
+ - [Socket SDK API](#socket-sdk-api)
41
+ - [Common V4 Operations](#common-v4-operations)
42
+ - [Manual Transport Mode](#manual-transport-mode)
43
+ - [Legacy V3 Protocol](#legacy-v3-protocol)
44
+ - [Pulse SDK](#pulse-sdk)
45
+ - [V4 Protocol Reference](#v4-protocol-reference)
46
+ - [Troubleshooting](#troubleshooting)
47
+
48
+ ## Requirements
49
+
50
+ - Python 3.13 or later.
51
+ - If you already have a WebSocket implementation, use [Manual Transport Mode](#manual-transport-mode).
52
+
53
+ ## Installation
54
+
55
+ Install the SDK in your project:
56
+
57
+ ```bash
58
+ pip install dglab-kit-python
59
+ ```
60
+
61
+ ## Core Concepts
62
+
63
+ | Term | Description |
64
+ |------|-------------|
65
+ | Controller | The Python application running `DglabSocket`, such as a server or desktop controller. |
66
+ | App client | The DG-LAB App attached to the current controller. |
67
+ | `targetId` | The pairing ID assigned when the controller connects. The app uses it to attach. |
68
+ | `clientId` | The connection ID of a specific V4 app client. Required when sending commands to that app. |
69
+ | `slotId` | The ID of a device exposed by the app. Used to identify the device in device operations. |
70
+ | `channel` | The device channel. V4 uses `V4Channel.A` / `V4Channel.B`, or `0` / `1`. |
71
+
72
+ The controller connects to the Socket server, and the app attaches using the controller's `targetId`. For the connection topology, device updates, and complete protocol details, see the [dglab-kit V4 protocol reference](https://github.com/dungeonlab-open/dglab-kit/blob/main/README.md#v4-protocol-reference).
73
+
74
+ ## Quick Start: Control the App with V4
75
+
76
+ This example connects to a V4 server, obtains a pairing ID, waits for an app to attach, discovers devices, and performs Channel Intensity and Pulse operations.
77
+
78
+ ```python
79
+ import asyncio
80
+
81
+ from dglab_kit_python import (
82
+ COYOTE_WAVEFORM,
83
+ COYOTE_WAVEFORMS,
84
+ DGLAB_SOCKET_STATE,
85
+ DglabSocket,
86
+ V4Channel,
87
+ )
88
+
89
+
90
+ async def main() -> None:
91
+ socket = DglabSocket(
92
+ url="ws://127.0.0.1:9998",
93
+ connect_timeout=8_000,
94
+ response_timeout=8_000,
95
+ )
96
+
97
+ socket.on("state", lambda state, previous: print(f"{previous} -> {state}"))
98
+ socket.on("error", lambda error: print("Socket error:", error))
99
+ socket.on("devices", lambda devices, client_id: print(client_id, devices))
100
+
101
+ async def on_client_attached(client_id: str) -> None:
102
+ result = await socket.request_devices(client_id)
103
+ devices = result["devices"]
104
+ if not devices:
105
+ return
106
+
107
+ slot_id = devices[0]["slotId"]
108
+ await socket.reset_intensity(client_id, slot_id, V4Channel.A)
109
+ await socket.add_intensity(client_id, slot_id, V4Channel.A, 5)
110
+ await socket.set_temp_intensity(client_id, slot_id, V4Channel.A, 30, 3_000)
111
+ await socket.send_pulse(
112
+ client_id,
113
+ slot_id,
114
+ V4Channel.A,
115
+ 1_000,
116
+ COYOTE_WAVEFORMS[COYOTE_WAVEFORM.BUBBLE]["raw"],
117
+ )
118
+
119
+ socket.on("client-attached", on_client_attached)
120
+
121
+ result = await socket.connect()
122
+ print("Controller ID:", result["targetId"])
123
+ assert socket.state is DGLAB_SOCKET_STATE.WAITING_FOR_PEER
124
+
125
+ # Keep the connection alive to receive events.
126
+ await asyncio.Event().wait()
127
+
128
+
129
+ asyncio.run(main())
130
+ ```
131
+
132
+ Listeners can be regular functions or `async def` functions. Async listeners are scheduled on the current event loop.
133
+
134
+ ## Generate an App Pairing QR Code
135
+
136
+ Once `connect()` returns a `targetId`, follow the [dglab-kit pairing QR code guide](https://github.com/dungeonlab-open/dglab-kit/blob/main/README.md#generate-an-app-pairing-qr-code) to generate a QR code or pairing link for the app.
137
+
138
+ ## Socket SDK API
139
+
140
+ ### Constructor
141
+
142
+ ```python
143
+ from dglab_kit_python import DGLAB_SOCKET_VERSION, DglabSocket
144
+
145
+ socket = DglabSocket(
146
+ url=None,
147
+ protocols=None,
148
+ connect_timeout=None,
149
+ response_timeout=None,
150
+ version=DGLAB_SOCKET_VERSION.V4,
151
+ )
152
+ ```
153
+
154
+ | Option | Default | Description |
155
+ |--------|---------|-------------|
156
+ | `url` | `None` | WebSocket server URL. Omit to use manual transport mode. |
157
+ | `protocols` | `None` | A WebSocket subprotocol string or list of subprotocols. |
158
+ | `connect_timeout` | `8_000` ms | Maximum time `connect()` waits for the protocol handshake. |
159
+ | `response_timeout` | `8_000` ms | Default V4 response timeout. |
160
+ | `version` | `DGLAB_SOCKET_VERSION.V4` | `V4` / `"v4"` or `V3` / `"v3"`. |
161
+
162
+ The constructor also accepts the camelCase aliases `connectTimeout` and `responseTimeout`. If both naming styles are supplied, the snake_case value takes precedence.
163
+
164
+ ### `connect()` Result
165
+
166
+ ```python
167
+ result = await socket.connect()
168
+ target_id = result["targetId"]
169
+ ```
170
+
171
+ | Field | Description |
172
+ |-------|-------------|
173
+ | `targetId` | The current controller ID. App clients use it to attach. |
174
+
175
+ Calling `connect()` again after a successful connection returns the current connection result immediately.
176
+
177
+ ### Socket States
178
+
179
+ | State | Description |
180
+ |-------|-------------|
181
+ | `DGLAB_SOCKET_STATE.IDLE` | Not connected. |
182
+ | `DGLAB_SOCKET_STATE.CONNECTING` | Establishing the connection. |
183
+ | `DGLAB_SOCKET_STATE.WAITING_FOR_PEER` | Connected and waiting for an app to attach. |
184
+ | `DGLAB_SOCKET_STATE.PAIRED` | At least one V4 app client, or the V3 peer, is paired. |
185
+ | `DGLAB_SOCKET_STATE.DISCONNECTED` | Disconnected. |
186
+
187
+ `DglabSocketState` also provides equivalent PascalCase member names. `DGLAB_SOCKET_STATE` is a compatibility alias for this enum class.
188
+
189
+ ### Common Events
190
+
191
+ `DglabSocket` is an event emitter. `on()`, `once()`, `off()`, and `remove_all_listeners()` return the current socket for chaining. Use `emit()` to publish custom application events.
192
+
193
+ | Event | Listener Arguments | Description |
194
+ |-------|--------------------|-------------|
195
+ | `state` | `(state, previous)` | Socket state changed. |
196
+ | `open` | `(event)` | WebSocket transport opened. |
197
+ | `close` | `(event)` | Transport closed. The event includes `code`, `reason`, `wasClean`, and the original `event`. |
198
+ | `error` | `(error)` | Transport, timeout, or protocol error. |
199
+ | `message` | `(text, raw)` | Raw message received, before parsing. |
200
+ | `frame` | `(frame)` | Parsed JSON frame. |
201
+ | `data` | `(data, client_id=None)` | Application data received. |
202
+ | `action` | `(action)` | App action notification received. |
203
+ | `device` | `(device, client_id)` | A device was added, changed, or removed. |
204
+ | `devices` | `(devices, client_id)` | The current complete V4 device list changed. |
205
+ | `client-attached` | `(client_id)` | An app client attached. |
206
+ | `client-disconnected` | `(client_id)` | An app client disconnected. |
207
+
208
+ ### Common Methods
209
+
210
+ | Method or Property | Returns | Description |
211
+ |--------------------|---------|-------------|
212
+ | `state` | `DglabSocketState` | Current connection state. |
213
+ | `await connect()` | `dict` | Connect and wait for the protocol handshake. |
214
+ | `await disconnect(code=None, reason=None)` | `None` | Disconnect and cancel any pending `connect()` call. |
215
+ | `await destroy(code=None, reason=None)` | `None` | Disconnect and remove all listeners. |
216
+ | `set_sender(sender)` / `setSender(sender)` | Socket | Set the send function for manual transport mode. |
217
+ | `send_raw(data)` / `sendRaw(data)` | `None` | Send raw text or binary data. |
218
+ | `send_frame(frame)` / `sendFrame(frame)` | `None` | JSON-encode a value and send it. |
219
+ | `handle_open(event=None)` / `handleOpen(event)` / `handleSocketOpen(event)` | `None` | Forward a transport open event. |
220
+ | `handle_message(data)` / `handleMessage(data)` / `handleSocketMessage(data)` | `None` | Forward an incoming transport message. |
221
+ | `handle_close(event_or_code=None, reason=None)` / `handleClose(...)` / `handleSocketClose(...)` | `None` | Forward a transport close event. |
222
+ | `handle_error(error)` / `handleError(error)` / `handleSocketError(error)` | `None` | Forward a transport error. |
223
+ | `on(event, listener)` | Socket | Register a listener. |
224
+ | `once(event, listener)` | Socket | Register a listener that runs once. |
225
+ | `off(event, listener)` | Socket | Remove one matching listener. |
226
+ | `remove_all_listeners(event=None)` | Socket | Remove listeners for one event, or all listeners. |
227
+ | `emit(event, *args)` | `bool` | Invoke listeners and return whether any listeners were registered. |
228
+
229
+ ### V4 Properties and Methods
230
+
231
+ V4 supports multiple app clients, so every method targeting an app requires that app's `client_id`.
232
+
233
+ | Method or Property | Returns | Description |
234
+ |--------------------|---------|-------------|
235
+ | `target_id` / `targetId` | `str \| None` | Current controller ID. |
236
+ | `client_ids` / `clientIds` | `list[str]` | IDs of attached app clients. |
237
+ | `clients` | `list[V4Client]` | State objects for attached app clients. |
238
+ | `rpc` | `V4Rpc` | The socket's request/response helper. |
239
+ | `get_client(client_id)` / `getClient(client_id)` | `V4Client \| None` | Get the state of a specific app client. |
240
+ | `await request_devices(client_id)` / `await requestDevices(client_id)` | `dict` | Request the device list, update the cache, and emit `devices`. |
241
+ | `send(client_id, data, options=None)` | `asyncio.Future` | Send a custom V4 request and wait for its response. |
242
+ | `ping(client_id, options=None)` | `asyncio.Future` | Ping the app; returns the app's local timestamp when it received the request. |
243
+ | `reset_intensity(client_id, slot_id, channel, options=None)` / `resetIntensity(...)` | `asyncio.Future` | Reset Channel Intensity to zero. |
244
+ | `add_intensity(client_id, slot_id, channel, value, options=None)` / `addIntensity(...)` | `asyncio.Future` | Increase Channel Intensity. |
245
+ | `reduce_strength(client_id, slot_id, channel, value, options=None)` / `reduceStrength(...)` | `asyncio.Future` | Decrease Channel Intensity. |
246
+ | `set_temp_intensity(client_id, slot_id, channel, value, duration, options=None)` / `setTempIntensity(...)` | `asyncio.Future` | Apply a Temporary Intensity Change. `duration` is in milliseconds. |
247
+ | `send_pulse(client_id, slot_id, channel, duration, frames, options=None)` / `sendPulse(...)` | `asyncio.Future` | Send a Pulse. `frames` is a `list[str]` or `list[list[int]]`. |
248
+ | `clear_pulse(client_id, slot_id, channel)` / `clearPulse(...)` | `asyncio.Future` | Clear all tasks on the specified device channel. Equivalent to calling `clear_operate()` for that channel. |
249
+ | `clear_operate(client_id, options=None)` / `clearOperate(...)` | `asyncio.Future` | Clear all tasks, or tasks for a specific device or channel. |
250
+
251
+ ### V4 Operation Options
252
+
253
+ The `options` argument for `reset_intensity()`, `add_intensity()`, `reduce_strength()`, `set_temp_intensity()`, and `send_pulse()` supports the following fields:
254
+
255
+ | Option | Type | Description |
256
+ |--------|------|-------------|
257
+ | `timeout` | `int` | Response timeout for this request, in milliseconds. |
258
+ | `priority` | `0`, `1`, or `2` | Task priority. |
259
+ | `immediate` | `bool` | Whether to replace matching existing tasks immediately. |
260
+ | `version` | `int` | Pulse format version; `send_pulse()` only. |
261
+ | `seq` | `int` | Pulse sequence number; `send_pulse()` only. |
262
+
263
+ The `options` argument for `send()` and `ping()` supports only `timeout`. For `clear_operate()`, `options` specifies `slot_id` / `slotId` and `channel`; the operation options above do not apply.
264
+
265
+ ### `V4Client`
266
+
267
+ `V4Client` represents a V4 app client. It has a string `clientId` property and a mutable `devices` list, and is typically managed by `DglabSocketV4`.
268
+
269
+ | Method | Description |
270
+ |--------|-------------|
271
+ | `get_device(slot_id)` / `getDevice(slot_id)` | Find a device by ID. |
272
+ | `dispatch(data)` | Merge supported events or responses; returns whether device state changed. |
273
+ | `replace_devices(devices)` / `replaceDevices(devices)` | Replace the device list. |
274
+ | `upsert_device(device)` / `upsertDevice(device)` | Add or replace a device by ID. |
275
+ | `remove_device(slot_id)` / `removeDevice(slot_id)` | Remove a device by ID. |
276
+ | `patch_devices(added=None, removed=None)` / `patchDevices(...)` | Apply device additions and removals. |
277
+ | `patch_slots(slots=None)` / `patchSlots(slots)` | Merge property and device state updates. |
278
+ | `destroy()` | Clear the device list. |
279
+
280
+ ### `V4Rpc`
281
+
282
+ `V4Rpc(send_frame, response_timeout=None)` is the low-level request tracker. Every V4 socket creates an instance at `socket.rpc`. When creating one directly, provide a function that sends outer server frames. The constructor also accepts the `responseTimeout` alias.
283
+
284
+ | Method | Description |
285
+ |--------|-------------|
286
+ | `create_request(method, data=None)` / `createRequest(...)` | Create a V4 request with an automatically assigned request ID. |
287
+ | `send(client_id, data, options=None)` | Send a request and return a Future for its result. |
288
+ | `send_operate(client_id, data, options=None)` / `sendOperate(...)` | Send a device operation request. |
289
+ | `resolve_response(client_id, response)` / `resolveResponse(...)` | Resolve the matching pending response. |
290
+ | `reject_client_pending(client_id)` / `rejectClientPending(...)` | Reject all pending requests for a disconnected app client. |
291
+ | `reject_all_pending(error)` / `rejectAllPending(error)` | Reject all pending requests. |
292
+ | `is_response(data)` / `isResponse(data)` | Check whether data is a V4 response. |
293
+ | `get_request_id(data)` / `getRequestId(data)` | Read the request ID. |
294
+
295
+ ### Exported Types and Errors
296
+
297
+ The following names can be imported directly from `dglab_kit_python`:
298
+
299
+ | Name | Description |
300
+ |------|-------------|
301
+ | `DglabSocket` | Version-selecting facade. Its `raw` property exposes the underlying V3 or V4 socket. |
302
+ | `DglabSocketBase`, `DglabSocketV3`, `DglabSocketV4` | Base class and protocol-specific implementations. |
303
+ | `DglabSocketVersion`, `DGLAB_SOCKET_VERSION` | Version enum and compatibility alias. |
304
+ | `DglabSocketState`, `DGLAB_SOCKET_STATE` | State enum and compatibility alias. |
305
+ | `DglabSocketDeviceType`, `DGLAB_SOCKET_DEVICE_TYPE` | Device type enum and compatibility alias. |
306
+ | `V3Channel`, `V4Channel`, `V4ActionType` | Channel and V4 action type enums. |
307
+ | `DglabError`, `create_named_error` | SDK error class and error factory. |
308
+
309
+ `DglabError.name` starts with `DGLAB-`. Connection or response timeouts, disconnections, missing app clients, invalid sends, and remote errors may raise this exception or cause a Future to fail with it. Use `create_named_error(name, message)` to create an SDK error in the same format.
310
+
311
+ The following `TypedDict` types are available from `dglab_kit_python.types` for type annotations: `DglabSocketConnectResult`, `DglabSocketCloseEvent`, `V4DeviceDescriptor`, `V4DeviceInfo`, `V4DevicesGetResult`, `V4OperateOptions`, `V4AppendPulseDataOptions`, `V4ClearOperateOptions`, and `V3WaveOptions`.
312
+
313
+ ## Common V4 Operations
314
+
315
+ ### Request the Device List
316
+
317
+ ```python
318
+ result = await socket.request_devices(client_id)
319
+
320
+ for device in result["devices"]:
321
+ print(device["slotId"], device["name"], device["type"])
322
+ ```
323
+
324
+ The device list is also maintained automatically through `devices` events. Use `socket.get_client(client_id)` to access the current cache.
325
+
326
+ ### Measure App Round-Trip Latency
327
+
328
+ ```python
329
+ from time import perf_counter
330
+
331
+ started_at = perf_counter()
332
+ received_at = await socket.ping(client_id)
333
+ rtt_ms = (perf_counter() - started_at) * 1_000
334
+
335
+ print("RTT:", rtt_ms, "ms")
336
+ print("App local timestamp when ping was received:", received_at)
337
+ ```
338
+
339
+ `ping()` returns the app's local timestamp when it received the request. Calculate round-trip time on the controller by measuring the elapsed time, as shown above.
340
+
341
+ ### Channel Intensity Control
342
+
343
+ ```python
344
+ await socket.reset_intensity(client_id, slot_id, V4Channel.A)
345
+ await socket.add_intensity(client_id, slot_id, V4Channel.A, 5)
346
+ await socket.reduce_strength(client_id, slot_id, V4Channel.B, 3)
347
+ ```
348
+
349
+ ### Temporary Intensity Change
350
+
351
+ ```python
352
+ await socket.set_temp_intensity(
353
+ client_id,
354
+ slot_id,
355
+ V4Channel.A,
356
+ 30, # Temporary intensity.
357
+ 3_000, # Duration in milliseconds.
358
+ )
359
+ ```
360
+
361
+ ### Send a Pulse
362
+
363
+ ```python
364
+ from dglab_kit_python import COYOTE_WAVEFORM, COYOTE_WAVEFORMS, V4Channel
365
+
366
+ frames = COYOTE_WAVEFORMS[COYOTE_WAVEFORM.BUBBLE]["raw"]
367
+ await socket.send_pulse(client_id, slot_id, V4Channel.A, 1_000, frames)
368
+ ```
369
+
370
+ ### Clear Tasks
371
+
372
+ ```python
373
+ # Clear all tasks for an app client
374
+ await socket.clear_operate(client_id)
375
+
376
+ # Clear tasks on all channels of a specific device
377
+ await socket.clear_operate(client_id, {"slot_id": slot_id})
378
+
379
+ # Clear tasks on a specific device channel
380
+ await socket.clear_operate(client_id, {"slot_id": slot_id, "channel": V4Channel.A})
381
+ ```
382
+
383
+ ### Send a Custom Request
384
+
385
+ Use `send()` for V4 RPCs without a dedicated SDK method. It adds a request ID and waits for a response with the same ID. This example sends `devices.get` directly; for typical use, prefer the equivalent `request_devices()` method:
386
+
387
+ ```python
388
+ result = await socket.send(
389
+ client_id,
390
+ {"t": "req", "m": "devices.get"},
391
+ {"timeout": 5_000},
392
+ )
393
+
394
+ print(result)
395
+ ```
396
+
397
+ ## Manual Transport Mode
398
+
399
+ When `url` is omitted, the SDK does not create a WebSocket connection. Bind a send function and forward transport lifecycle events to the SDK. The SDK handles protocol parsing, state, events, and V4 response tracking; your application handles network transport.
400
+
401
+ ```python
402
+ import asyncio
403
+ from typing import Any
404
+
405
+ from dglab_kit_python import DglabSocket
406
+
407
+
408
+ async def main() -> None:
409
+ socket = DglabSocket()
410
+
411
+ def transport_send(data: str | bytes | bytearray | memoryview) -> None:
412
+ # Forward data to the transport.
413
+ print(data)
414
+
415
+ socket.set_sender(transport_send)
416
+ connect_task = asyncio.create_task(socket.connect())
417
+
418
+ # Call when the transport opens.
419
+ socket.handle_open()
420
+
421
+ # Forward incoming messages to the SDK.
422
+ def on_transport_message(data: Any) -> None:
423
+ socket.handle_message(data)
424
+
425
+ # The connection task returns after the protocol handshake completes.
426
+ result = await connect_task
427
+ print(result["targetId"])
428
+
429
+ def on_transport_close(code: int, reason: str) -> None:
430
+ socket.handle_close(code, reason)
431
+
432
+ def on_transport_error(error: Exception) -> None:
433
+ socket.handle_error(error)
434
+
435
+
436
+ asyncio.run(main())
437
+ ```
438
+
439
+ `send_raw()` accepts `str`, `bytes`, `bytearray`, or `memoryview`. `send_frame()` encodes Python values as compact JSON before sending. A manual transport must preserve the order of incoming and outgoing messages.
440
+
441
+ ## Legacy V3 Protocol
442
+
443
+ Enable the legacy, single-app V3 protocol explicitly with `DGLAB_SOCKET_VERSION.V3`. Use `V3Channel.A` / `V3Channel.B`, or `1` / `2`, for channels. V3 control methods are synchronous: they send data and return `None`.
444
+
445
+ ```python
446
+ import asyncio
447
+
448
+ from dglab_kit_python import (
449
+ COYOTE_WAVEFORM,
450
+ COYOTE_WAVEFORMS,
451
+ DGLAB_SOCKET_VERSION,
452
+ DglabSocket,
453
+ V3Channel,
454
+ )
455
+
456
+
457
+ async def main() -> None:
458
+ socket = DglabSocket(
459
+ version=DGLAB_SOCKET_VERSION.V3,
460
+ url="wss://ws.dungeon-lab.cn/",
461
+ )
462
+
463
+ def on_client_attached(client_id: str) -> None:
464
+ socket.set_strength(V3Channel.A, 20)
465
+ socket.add_strength(V3Channel.A, 3)
466
+ socket.reduce_strength(V3Channel.B, 1)
467
+ socket.send_pulse(
468
+ {
469
+ "channel": "A",
470
+ "time": 5,
471
+ "data": COYOTE_WAVEFORMS[COYOTE_WAVEFORM.BUBBLE]["raw"],
472
+ }
473
+ )
474
+
475
+ socket.on("client-attached", on_client_attached)
476
+ socket.on("action", lambda action: print("Action:", action))
477
+ result = await socket.connect()
478
+ print("Controller ID:", result["targetId"])
479
+ await asyncio.Event().wait()
480
+
481
+
482
+ asyncio.run(main())
483
+ ```
484
+
485
+ ### V3 Methods
486
+
487
+ | Method or Property | Description |
488
+ |--------------------|-------------|
489
+ | `target_id` / `targetId` | Current controller ID. |
490
+ | `paired_client_id` / `pairedClientId` | ID of the paired app client. |
491
+ | `send(data)` | Send custom V3 data as defined by the protocol, after pairing. |
492
+ | `add_strength(channel, step=1)` / `addStrength(...)` | Increase Channel Intensity. Raises `DglabError` if the absolute step exceeds 200. |
493
+ | `reduce_strength(channel, step=1)` / `reduceStrength(...)` | Decrease Channel Intensity. |
494
+ | `set_strength(channel, strength)` / `setStrength(...)` | Set Channel Intensity. |
495
+ | `send_pulse(options)` / `sendPulse(...)` | Send a Pulse. `options` contains `channel`, `time`, and `data`. |
496
+ | `clear_pulse(channel)` / `clearPulse(channel)` | Clear Pulses on the channel. |
497
+
498
+ For V3 usage and message parameters, see the [legacy V3 protocol reference](https://github.com/dungeonlab-open/dglab-kit/blob/main/README.md#legacy-v3-protocol).
499
+
500
+ ## Pulse SDK
501
+
502
+ Each Pulse dictionary contains a localized `label` and raw frames in `raw`. Use members of `COYOTE_WAVEFORM` or `OVC_WAVEFORM` as dictionary keys.
503
+
504
+ ```python
505
+ from dglab_kit_python import COYOTE_WAVEFORM, COYOTE_WAVEFORMS
506
+
507
+ waveform = COYOTE_WAVEFORMS[COYOTE_WAVEFORM.BUBBLE]
508
+ print(waveform["label"]["cn"])
509
+ frames = waveform["raw"]
510
+ ```
511
+
512
+ Import `COYOTE_WAVEFORM`, `COYOTE_WAVEFORMS`, `OVC_WAVEFORM`, and `OVC_WAVEFORMS` from `dglab_kit_python`. For the enum classes themselves, import `CoyoteWaveform` and `OvcWaveform` from `dglab_kit_python.waveform`.
513
+
514
+ ## V4 Protocol Reference
515
+
516
+ See the [dglab-kit V4 protocol reference](https://github.com/dungeonlab-open/dglab-kit/blob/main/README.md#v4-protocol-reference).
517
+
518
+ ## Troubleshooting
519
+
520
+ ### The App Does Not Attach
521
+
522
+ Confirm that the controller's `connect()` call returned the current `targetId`, then pass it to the app as described in the [pairing QR code guide](#generate-an-app-pairing-qr-code). See the [V4 protocol reference](#v4-protocol-reference) for server URLs and pairing parameters.
523
+
524
+ ### The App Attaches, but Device Control Does Not Work
525
+
526
+ Call `await socket.request_devices(client_id)` first and confirm that the response contains a valid `slotId`. Use that ID and the correct channel for V4 operations. Always `await` operation Futures to receive remote errors.
527
+
528
+ ### A V4 Request Times Out or the App Client Disconnects
529
+
530
+ Listen for `error` and `client-disconnected`. Set `response_timeout` in the constructor, or pass `{"timeout": milliseconds}` for an individual request. If an app client disconnects, its pending Futures fail with `DglabError`.
@@ -0,0 +1,17 @@
1
+ dglab_kit_python/__init__.py,sha256=iXhwRSBn5qKslq9G4bkBdwneFZ-iz9iQ1eypEIGyrGo,935
2
+ dglab_kit_python/errors.py,sha256=l5uhCfKUJxtRIaOa6-eu-SlImtdtqhsxtIPL28OrnIo,472
3
+ dglab_kit_python/events.py,sha256=Vi9waXadJfdl_cVlOilxA2nvwtQbiWgmryTFpUsKTAc,1817
4
+ dglab_kit_python/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
5
+ dglab_kit_python/socket/__init__.py,sha256=Xzh5ZDF60CUmvvgux7YKMa7yw9A7i0be0qVrGpQ0jS8,271
6
+ dglab_kit_python/socket/base.py,sha256=6tW8m9AhSQdp6VgppvOf2MaXZu7AZGfekSn5ROhtoxY,15873
7
+ dglab_kit_python/socket/client.py,sha256=k4H7Vh75wW7UIKkKavq1Vr1QYHal4zjQ85GVR3Y3Ivc,3519
8
+ dglab_kit_python/socket/v3.py,sha256=ufgSJwAi7UKUMqaDOZOMS8GW-dzwWcnRNXAkc73PYs4,8669
9
+ dglab_kit_python/socket/v4.py,sha256=VYqWge5-KR6nOeT0tLb6EB2SU5fUFTiwNDJxaCF2epc,26471
10
+ dglab_kit_python/types.py,sha256=OCwm6UcVdKvGYra4CxVlTxSA2W25e410-u2EAnli4fE,2023
11
+ dglab_kit_python/waveform/__init__.py,sha256=DiObpfrCmjWIZgfDsM8MQQZDn-DhN_YCw6MQagFXjEc,272
12
+ dglab_kit_python/waveform/coyote.py,sha256=ITdMR74zcp70E9_mMPUCXOrx7XqvKNX0NXtgKWEk_so,28068
13
+ dglab_kit_python/waveform/ovc.py,sha256=o5MPSKlF9RsVRBOp_pWJxmWJ9nq-7qN_u9bwisTi3s4,17381
14
+ dglab_kit_python-0.1.0.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
15
+ dglab_kit_python-0.1.0.dist-info/WHEEL,sha256=iHtWm8nRfs0VRdCYVXocAWFW8ppjHL-uTJkAdZJKOBM,80
16
+ dglab_kit_python-0.1.0.dist-info/METADATA,sha256=nayP2iyjMDaIw0zbqvwye5QkjpVRypRj0MvntU37K0E,23648
17
+ dglab_kit_python-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: uv 0.9.30
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any