Command¶
Convenient wrapper around device communication with easy-to-use methods for common queries.
The Command class provides a cleaner API than sending raw command strings, with methods like status(), threshold(), read(), and more.
kazunoko.command.Command
¶
Convenience wrapper for device queries
Provides alias methods for common OSECHI detector commands. Wraps a DeviceProtocol instance to enable clean, discoverable API.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
DeviceProtocol
|
A DeviceProtocol instance (PortDevice or mock) |
required |
Example
from kazunoko import connect, Command
with connect() as device:
cmd = Command(device)
resp = cmd.status()
print(resp.status) # "ok"
print(resp.version) # "1.10.1"
Source code in src/kazunoko/command.py
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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | |
__init__(device)
¶
Initialize Command with device instance
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
device
|
DeviceProtocol
|
A DeviceProtocol instance for sending commands |
required |
Example
device = PortDevice()
cmd = Command(device)
Source code in src/kazunoko/command.py
51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
deadtime(milliseconds)
¶
Set detection deadtime
Configures the deadtime (dead period) during which the detector is insensitive to new detection events after processing a detection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
milliseconds
|
int
|
Deadtime value in milliseconds (device-specific range, typically 0-60000) |
required |
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" and command status |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
resp = cmd.deadtime(100)
print(resp.status) # "ok"
Source code in src/kazunoko/command.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
help()
¶
Get available commands from device
Queries the device for the list of available commands and returns detailed command information.
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with command information |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
resp = cmd.help()
print(resp.commands) # List of available commands
# Use ResponseFormatter for flexible output formatting
fmt = ResponseFormatter(resp)
print(fmt.to_jsonl()) # Flattened JSONL
Source code in src/kazunoko/command.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 | |
led(channel, state)
¶
Control LED on specific channel or all channels
Controls the LED state (on/off) for a specific detector channel or all channels.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel
|
int | Literal['all']
|
Channel number (1-3) or "all" for all channels |
required |
state
|
Literal['on', 'off']
|
LED state, either "on" or "off" |
required |
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" and command status |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
# Turn on LED for channel 1
resp = cmd.led(1, "on")
print(resp.status) # "ok"
# Turn off LED for all channels
resp = cmd.led("all", "off")
print(resp.status) # "ok"
Source code in src/kazunoko/command.py
366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 | |
mac_address()
¶
Get device MAC address
Queries the device for its MAC address.
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" containing MAC address information |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
resp = cmd.mac_address()
print(resp.mac_address) # Device MAC address
Source code in src/kazunoko/command.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 | |
poll_count(count)
¶
Set poll count for detection sampling
Configures the number of events to poll from the detector.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
Poll count value (device-specific range, typically 1-1000) |
required |
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" and command status |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
resp = cmd.poll_count(100)
print(resp.status) # "ok"
Source code in src/kazunoko/command.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 | |
query(command)
¶
Send a raw command to the device
Low-level method for sending arbitrary commands directly to the device. Use this for custom commands or device-specific queries not covered by other convenience methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Command string to send to the device (e.g., "GET_STATUS", "SET_THRESHOLD 1 300") |
required |
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" and command result |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
# Use built-in convenience method
resp = cmd.status()
# Use raw query for custom commands
resp = cmd.query("GET_STATUS")
resp = cmd.query("SET_THRESHOLD 1 300")
resp = cmd.query("CUSTOM_CMD arg1 arg2")
Source code in src/kazunoko/command.py
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 98 99 100 | |
queue_stats()
¶
Get queue statistics from device
Queries the device for queue statistics information.
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" containing queue statistics |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
resp = cmd.queue_stats()
print(resp.queue_count) # Queue statistics
Source code in src/kazunoko/command.py
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
read()
¶
Read a detection event from device
Queries the device for a single detection event. Returns DeviceResponse object with type="event" containing the event data.
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="event" containing detection event fields. |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
event = cmd.read()
print(event.type) # "event"
print(event.timestamp) # Event timestamp
print(event.channel) # Detected channel
# Use ResponseFormatter for flexible output formatting
fmt = ResponseFormatter(event)
print(fmt.to_csv()) # Flattened CSV
Source code in src/kazunoko/command.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 | |
reset()
¶
Reset device configuration to defaults
Resets all device settings to their default values.
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" and command status |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
resp = cmd.reset()
print(resp.status) # "ok"
Source code in src/kazunoko/command.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | |
rtc_time(value=None)
¶
Get or set device RTC (Real-Time Clock) time
If value is None, retrieves the current device time (GET_RTC_TIME). If value is an integer, sets the device time (SET_RTC_TIME).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
int | None
|
Unix timestamp to set, or None to get current time |
None
|
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" and command status. |
DeviceResponse
|
For GET_RTC_TIME: contains current time fields |
DeviceResponse
|
For SET_RTC_TIME: contains confirmation status |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
import time
cmd = Command(device)
# Get current device time
resp = cmd.rtc_time()
print(resp.timestamp) # Current device time
# Set device time to current PC time
unixtime = int(time.time())
resp = cmd.rtc_time(unixtime)
print(resp.status) # "ok"
Source code in src/kazunoko/command.py
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 | |
status()
¶
Get device status and metadata
Queries the device for current status, version, and other metadata. The Response object contains dynamic fields that vary by device firmware.
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" and dynamic fields: |
DeviceResponse
|
|
DeviceResponse
|
|
DeviceResponse
|
|
DeviceResponse
|
|
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
resp = cmd.status()
print(resp.status) # "ok"
print(resp.version) # "1.10.1"
print(resp.poll_count) # 100
Source code in src/kazunoko/command.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
threshold(channel, value=None)
¶
Get or set threshold for a single channel
If value is None, retrieves the current threshold for the channel (GET_THRESHOLD). If value is provided, sets the threshold for the channel (SET_THRESHOLD).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
channel
|
int
|
Channel number (device-specific range) |
required |
value
|
int | None
|
Threshold value (device-specific range), or None to get current threshold |
None
|
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" and command status. |
DeviceResponse
|
For GET_THRESHOLD: contains current threshold value |
DeviceResponse
|
For SET_THRESHOLD: contains confirmation status |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
# Get current threshold for channel 1
resp = cmd.threshold(1)
print(resp.value) # Current threshold value
# Set threshold for channel 1
resp = cmd.threshold(1, 300)
print(resp.status) # "ok"
Source code in src/kazunoko/command.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 | |
thresholds(thresholds=None)
¶
Get or set thresholds for multiple channels
If thresholds is None, retrieves thresholds for all channels (1-3). If thresholds is provided, sets thresholds for each channel in the dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
thresholds
|
dict[int, int] | None
|
Dictionary mapping channel number to threshold value, or None to get all thresholds |
None
|
Returns:
| Type | Description |
|---|---|
list[DeviceResponse]
|
List of DeviceResponse objects, one per channel |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
# Get thresholds for all channels
responses = cmd.thresholds()
for resp in responses:
print(resp.value) # Threshold value for each channel
# Set thresholds for specific channels
responses = cmd.thresholds({1: 300, 2: 400, 3: 500})
for resp in responses:
print(resp.status) # "ok" for each
Source code in src/kazunoko/command.py
440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 | |
uptime()
¶
Get device uptime since power-on
Queries the device for the time elapsed since it was powered on.
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" containing uptime information |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
resp = cmd.uptime()
print(resp.uptime) # Uptime value
Source code in src/kazunoko/command.py
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
usage()
¶
Get list of queryable commands from device
Queries the device for the list of all available commands that can be queried. Returns detailed command information including arguments and descriptions.
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with command list and details |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
resp = cmd.usage()
print(resp.commands) # List of all available commands
# Use ResponseFormatter for flexible output formatting
fmt = ResponseFormatter(resp)
print(fmt.to_table()) # Formatted table
Source code in src/kazunoko/command.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | |
version()
¶
Get device firmware version
Queries the device for firmware version information.
Returns:
| Type | Description |
|---|---|
DeviceResponse
|
DeviceResponse object with type="response" containing version information |
Raises:
| Type | Description |
|---|---|
DeviceError
|
If device connection fails |
CommandError
|
If command transmission fails |
ResponseError
|
If response is invalid |
ResponseTimeout
|
If no response received within timeout |
Example
cmd = Command(device)
resp = cmd.version()
print(resp.version) # "1.10.1"
Source code in src/kazunoko/command.py
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | |