tomato.driverinterface_2_1 package
DriverInterface-2.1
Code author: Peter Kraus
- pydantic model tomato.driverinterface_2_1.Attr
Bases:
BaseModelA Pydantic
BaseModelused to describe device attributes.Show JSON schema
{ "title": "Attr", "type": "object", "properties": { "type": { "default": null, "title": "Type" }, "rw": { "default": false, "title": "Rw", "type": "boolean" }, "status": { "default": false, "title": "Status", "type": "boolean" }, "units": { "default": null, "title": "Units", "type": "string" }, "maximum": { "anyOf": [ { "type": "number" }, { "type": "null" } ], "default": null, "title": "Maximum" }, "minimum": { "anyOf": [ { "type": "number" }, { "type": "null" } ], "default": null, "title": "Minimum" }, "options": { "anyOf": [ { "items": {}, "type": "array", "uniqueItems": true }, { "type": "null" } ], "default": null, "title": "Options" } } }
- Config:
arbitrary_types_allowed: bool = True
- field type: type [Required]
Data type of the attribute
- field rw: bool = False
Is the attribute read-write?
- field status: bool = False
Should the attribute be included in component status?
- field units: str = None
Default units for the attribute, optional.
- field maximum: float | Quantity | None = None
Maximum value for the attribute, optional.
- Constraints:
union_mode = left_to_right
- field minimum: float | Quantity | None = None
Minimum value for the attribute, optional.
- Constraints:
union_mode = left_to_right
- field options: set | None = None
Allowed set of values for the attribute, optional.
- class tomato.driverinterface_2_1.ModelInterface(settings=None)
Bases:
objectAn abstract base class specifying the driver interface.
Individual driver modules should expose a
DriverInterfaceas a top-level import, which inherits from this abstract class. Only the methods of this class are used to interact with drivers and their components.This class contains one abstract method,
DeviceFactory(), that has to be re-implemented by the driver modules.All methods of this class should return
Replyobjects (except theDeviceFactory()function). However, for better readability, a decorator functionto_reply()is provided, so that the types of the return values can be explicitly defined here.- version: str = '2.1'
Version of the
DriverInterface.
- idle_measurement_interval: int | None = None
The interval (in seconds) after which
self.cmp_measure()will be executed, when idle.
- devmap: dict[tuple, ModelDevice]
Map of registered device components, the tuple keys are component = (address, channel)
- constants: dict[str, Any]
A map that should be populated with driver-specific run-time constants.
- settings: dict[str, Any]
A settings map to contain driver-specific settings such as
dllpathfor BioLogic
- retries: dict[tuple, int]
Map of components which failed to register, with number of retries as values.
- abstract DeviceFactory(key: tuple[str, str], **kwargs) ModelDevice
A factory function which is used to pass this instance of the
ModelInterfaceto the newModelDeviceinstance.
- dev_register(**kwargs)
- dev_teardown(**kwargs)
- dev_reset(**kwargs)
- dev_set_attr(**kwargs)
- dev_get_attr(**kwargs)
- dev_status(**kwargs)
- dev_capabilities(**kwargs)
- dev_attrs(**kwargs)
- cmp_register(address: str, channel: str, **kwargs: dict) tuple[bool, str, set]
Register a new device component in this driver.
Creates a
ModelDevicerepresenting a device component, storing it in theself.devmapusing the provided address and channel.Returns the
setof capabilities of the registered component as theReply.data.
- cmp_teardown(key: tuple[str, str], **kwargs: dict) tuple[bool, str, None]
Emergency stop function.
Should set the device component into a documented, safe state. The function is to be only called in case of critical errors, or when the component is being removed, not as part of normal operation (i.e. it is not intended as a clean-up after task completion).
- cmp_reset(key: tuple[str, str], do_run: bool = True, **kwargs: dict) tuple[bool, str, None]
Component reset function.
Should set the device component into a documented, safe state. This function is executed at the end of every job.
- cmp_set_attr(attr: str, val: Val, key: tuple[str, str], **kwargs: dict) tuple[bool, str, Val]
Set value of the
Attrof the specified device component.Pass-through to the
ModelDevice.set_attr()function. No type or read-write validation performed here! Returns the validated or coerced value as theReply.data.
- cmp_get_attr(attr: str, key: tuple[str, str], **kwargs: dict) tuple[bool, str, Val]
Get value of the
Attrfrom the specified device component.Pass-through to the
ModelDevice.get_attr()function. No type coercion is done here. Returns the value as theReply.data.
- cmp_status(key: tuple[str, str], **kwargs: dict) tuple[bool, str, dict]
Get the status report from the specified device component.
Iterates over all
Attrson the component that havestatus=Trueand returns their values in theReply.dataas adict.
- cmp_capabilities(key: tuple[str, str], **kwargs) tuple[bool, str, set]
Returns the capabilities of the device component.
Pass-through to
ModelDevice.capabilities(). Returns thesetof capabilities inReply.data.
- cmp_attrs(key: tuple[str, str], **kwargs: dict) tuple[bool, str, dict]
Query available
Attrson the specified device component.Pass-through to the
ModelDevice.attrs()function. Returns thedictof attributes as theReply.data.
- cmp_constants(key: tuple[str, str], **kwargs: dict) tuple[bool, str, dict]
Query constants on the specified device component and this driver.
Returns the
dictof constants as theReply.data.
- cmp_last_data(key: tuple[str, str], **kwargs: dict) tuple[bool, str, None | Dataset]
Fetch the last stored data on the component.
Passthrough to
ModelDevice.get_last_data(). The data in the form of axarray.Datasetis returned as theReply.data.
- cmp_measure(key: tuple[str, str], **kwargs: dict) tuple[bool, str, None]
Do a single measurement on the component according to its current configuration.
Fails if the component already has a running task / measurement.
- task_start(key: tuple[str, str], task: Task, **kwargs) tuple[bool, str, set | Task]
Submit a
Taskonto the specified device component.Pushes the supplied
Taskinto theQueueof the component, then starts the worker thread (if not already started). Checks that theTaskis among the capabilities of this component.
- task_status(key: tuple[str, str], **kwargs: dict) tuple[bool, str, dict]
Returns the task readiness status of the specified device component.
The running entry in the data slot of the
Replyindicates whether aTaskis running. The can_submit entry indicates whether anotherTaskcan be queued onto the device component already.
- task_stop(key: tuple[str, str], **kwargs) tuple[bool, str, Dataset | None]
Stops a running task and returns any collected data.
Pass-through to
ModelDevice.stop_task()andModelInterface.task_data().If there is any cached data, it is returned as a
xarray.Datasetin theReply.dataand the cache is cleared.
- task_data(key: tuple[str, str], **kwargs) tuple[bool, str, Dataset | None]
Return cached task data on the device component and clean the cache.
Pass-through for
ModelDevice.get_data(), which should return axarray.Datasetthat is fully annotated.This function gets called by the job thread every device.pollrate, it therefore incurs some IPC cost.
- task_validate(key: tuple[str, str], task: Task, **kwargs) tuple[bool, str, None]
Validate the provided
Taskfor submission on the component identified bykey.
- status() Reply
Returns the driver status. Currently that is the names of the components in the devmap.
- reset() Reply
Resets the driver.
Called when the driver process is quitting. Instructs all remaining tasks to stop. Warns when devices linger. Passes through to
dev_reset(). This is not a pass-through todev_teardown().
- class tomato.driverinterface_2_1.ModelDevice(driver, key, **kwargs)
Bases:
objectAn abstract base class specifying a manager for an individual component.
This class should handle determining attributes and capabilities of the component, the reading/writing of those attributes, processing of tasks, and caching and returning of task data.
- driver: ModelInterface
The parent
DriverInterfaceinstance.
- key: tuple[str, str]
The key in
self.driver.devmapreferring to this object.
- task_list: Queue
A
Queueused to passTasksto the workerThread.
- thread: Thread
The worker
Thread.
- running: bool
- datalock: RLock
Lock object for thread-safe data manipulation.
- constants: dict[str, Any]
Constant metadata of this component.
- task_runner() None
Target function for the
self.threadwhen handlingTasks.This function waits for a
Taskpassed usingself.task_list, then handles setting allAttrsusing theprepare_task()function, and finally handles the main loop of the task, periodically running thedo_task()function (using task.sampling_interval) until the maximum task duration (i.e. task.max_duration) is exceeded.The
self.threadis reset to None.
- prepare_task(task: Task, **kwargs: dict) None
Given a
Task, prepare this component for execution by setting allAttrsas specified in the task.task_params dictionary.
- do_task(task: Task, **kwargs: dict) None
Periodically called task execution function.
This function is responsible for updating
self.datawith new data, i.e. performing the measurement. It should also update the value ofself.last_data, so that the component status is consistent with the cached data.
- abstract do_measure(**kwargs: dict) None
One shot execution worker function.
This function is performs a measurement using the current configuration of
self.attrs, and stores the result inself.last_data.
- stop_task(**kwargs: dict) None
Stops the currently running task.
- abstract set_attr(attr: str, val: Val, **kwargs: dict) Val
Sets the specified
Attrtoval.This function should handle any data type coercion and validation using e.g.
Attr.maximumandAttr.minimum.Returns the coerced value corresponding to
val.
- get_data(**kwargs: dict) Dataset
Returns the cached
self.dataas axarray.Datasetbefore clearing the cache.
- get_last_data(**kwargs: dict) Dataset
Returns the
last_dataobject as axarray.Dataset.
- abstract capabilities() set
Returns a
setof all supported techniques.
- status(**kwargs) dict[str, Val]
Compiles a status report from
Attrsmarked as status=True.
Submodules
- tomato.driverinterface_2_1.decorators.in_devmap(func)
- tomato.driverinterface_2_1.decorators.to_reply(func)
Helper decorator for coercing tuples into
Reply.
- tomato.driverinterface_2_1.decorators.log_errors(func)
Helper decorator for logging all kinds of errors.
This decorator should be only used on functions in the API of the
ModelInterface, as the caught exceptions will cause the driver process to exit.
- tomato.driverinterface_2_1.decorators.coerce_val(func)
Decorator for coercing
valinto the correct format based onAttrdata.This decorator should be applied to the
ModelDriver.set_attr()function, in order to check whether the supplied value is allowed (notNone, inoptions, betweenminimumandmaximum) as well as coercing it to the right type and unit.