device
frog.hardware.device
¤
Provides base classes for all types of devices.
The Device class is the top-level base class from which all devices ultimately inherit. Concrete classes for devices must not inherit directly from this class, but instead should inherit from a device base class.
Classes¤
AbstractDevice
¤
Bases: ABC
An abstract base class for devices.
Functions¤
__init_subclass__(parameters={}, async_open=None)
¤
Initialise a device class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
parameters
|
Mapping[str, str | tuple[str, Sequence]]
|
Extra device parameters that this class requires |
{}
|
async_open
|
bool | None
|
Whether the device should be opened in the background |
None
|
Source code in src/frog/hardware/device.py
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 | |
close()
abstractmethod
¤
Close the connection to the device.
Source code in src/frog/hardware/device.py
201 202 203 | |
get_device_base_type_info()
classmethod
¤
Get information about the base type for this device type.
Source code in src/frog/hardware/device.py
210 211 212 213 | |
get_device_parameters()
classmethod
¤
Get the parameters for this device class.
Source code in src/frog/hardware/device.py
205 206 207 208 | |
get_device_type_info()
classmethod
¤
Get information about this device type.
Source code in src/frog/hardware/device.py
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 | |
pubsub_broadcast(func, success_topic_suffix, *kwarg_names)
¤
Broadcast success or failure of function via pubsub.
If the function returns without error, the returned values are sent as arguments to the success_topic message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
The function to wrap |
required |
success_topic_suffix
|
str
|
The topic name on which to broadcast function results |
required |
kwarg_names
|
str
|
The names of each of the returned values |
()
|
Source code in src/frog/hardware/device.py
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 | |
pubsub_errors(func)
¤
Catch exceptions and broadcast via pubsub.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
The function to wrap |
required |
Source code in src/frog/hardware/device.py
232 233 234 235 236 237 238 239 240 241 242 243 244 245 | |
Device(name=None)
¤
Bases: AbstractDevice
A base class for device types.
This class is the base class for device base types and (indirectly) concrete device type classes. Unlike AbstractDevice, it provides an init_subclass method to initialise the its subclasses differently depending on whether or not they are defined as device base types or not.
Create a new Device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str | None
|
A name to distinguish devices of the same type. |
None
|
Source code in src/frog/hardware/device.py
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 | |
Attributes¤
name = name
instance-attribute
¤
The (optional) name of this device to use in pubsub messages.
topic = f'device.{self._device_base_type_info.name}'
instance-attribute
¤
The name of the root pubsub topic on which this device will broadcast.
Functions¤
__init_subclass__(class_type=None, **kwargs)
¤
Initialise a device type class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
class_type
|
DeviceClassType | None
|
Optionally override the default heuristic for determining whether this is a base type, device type or neither |
None
|
**kwargs
|
Any
|
Class arguments for either base type or device type initialisation |
{}
|
Source code in src/frog/hardware/device.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 | |
close()
¤
Close the device and clear any pubsub subscriptions.
Source code in src/frog/hardware/device.py
440 441 442 443 | |
get_instance_ref()
¤
Get the DeviceInstanceRef corresponding to this device.
Source code in src/frog/hardware/device.py
445 446 447 | |
send_error_message(error)
¤
Send an error message for this device.
Source code in src/frog/hardware/device.py
491 492 493 494 495 496 497 498 499 500 501 502 503 | |
send_message(topic_suffix, **kwargs)
¤
Send a pubsub message for this device.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
topic_suffix
|
str
|
The part of the topic name after self.topic |
required |
**kwargs
|
Any
|
Extra arguments to include with pubsub message |
{}
|
Source code in src/frog/hardware/device.py
482 483 484 485 486 487 488 489 | |
signal_is_opened()
¤
Signal that the device is now open.
Source code in src/frog/hardware/device.py
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 | |
subscribe(func, topic_name_suffix, success_topic_suffix=None, *kwarg_names)
¤
Subscribe to a pubsub topic using the pubsub_* helper functions.
Errors will be broadcast with the message "device.error.{THIS_INSTANCE}". If success_topic_suffix is provided, a message will also be sent on success (see pubsub_broadcast).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable
|
Function to subscribe to |
required |
topic_name_suffix
|
str
|
The suffix of the topic to subscribe to |
required |
success_topic_suffix
|
str | None
|
The topic name on which to broadcast function results |
None
|
kwarg_names
|
str
|
The names of each of the returned values |
()
|
Source code in src/frog/hardware/device.py
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 | |
DeviceClassType
¤
Bases: Enum
The type of a class inheriting directly or indirectly from Device.
Attributes¤
BASE_TYPE = 0
class-attribute
instance-attribute
¤
A base device type (e.g. stepper motor)
DEVICE_TYPE = 1
class-attribute
instance-attribute
¤
A device type (e.g. ST10 stepper motor controller)
IGNORE = 2
class-attribute
instance-attribute
¤
An intermediate class type that should not be added to either registry
DeviceError
¤
Bases: Exception
Base class for errors raised by device plugins.
RetryFailedError
¤
Bases: DeviceError
Indicates that an operation failed after multiple retries.
Functions¤
get_device_types()
¤
Return info about device types grouped according to their base type.
Source code in src/frog/hardware/device.py
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 | |
retry_request(func, max_attempts, catch)
¤
Attempt to call func up to max_attempts times, retrying on caught exceptions.
The raised exception must be of type catch. If func raises an exception type
other than catch, it will not be caught and the request will not be retried.
On each failed attempt a warning is logged. If all attempts are exhausted a RetryFailedError is raised.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[[], T]
|
The callable to invoke on each attempt. |
required |
max_attempts
|
int
|
Maximum number of attempts (must be >= 1). |
required |
catch
|
type[Exception]
|
The exception type to catch and retry on. |
required |
Returns:
| Type | Description |
|---|---|
T
|
The return value of func on success. |
Raises:
| Type | Description |
|---|---|
RetryFailedError
|
All attempts resulted in the |
Source code in src/frog/hardware/device.py
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 | |