Controllers

PyLabware.controllers.in_simulation_device_returns(value)[source]

Decorator that patched the device send() method to return the value passed.

class PyLabware.controllers.LabDevice(device_name=None, connection_mode=None, connection_parameters=None)[source]

Bases: AbstractLabDevice

Base controller class for all labware devices.

Default constructor. This function performs object initialization. All device-specific hardware initialization procedures should be inside the initialise_device() method.

This method has to be redefined in child classes.

Parameters:
  • device_name (str) – Device name (for logging purposes).

  • connection_mode (str) – Physical connection mode (defines the connection adapter used).

  • connection_parameters (Dict) – Dictionary with connection-specific settings. These vary depending on the connection_mode.

cast_reply_type(cmd, reply)[source]

Casts reply type based on the type provided in command definition.

Parameters:
  • reply (Any) – Reply string.

  • cmd (Dict) – Command definition to look the type value in.

Returns:

Reply casted to the correct type.

Return type:

(any)

check_value(cmd, value)[source]

Checks the value provided against the definitions in command dict. Then does any value conversion/formatting/type casting as needed.

This method may be redefined in child classes.

Parameters:
  • cmd (Dict) – Device command definition.

  • value (Any) – Command parameter to check.

Returns:

Processed value.

Return type:

(Any)

connect()[source]

Connects to the device.

This method normally shouldn’t be redefined in child classes.

disconnect()[source]

Disconnects from the device.

This method normally shouldn’t be redefined in child classes.

execute_when_ready(action, *args, check_ready=None)[source]

Acquires device lock, waits till device is ready and runs specified action. If no method is provided for checking, PyLabware.models.AbstractLabDevice._is_idle() is used.

Parameters:
  • action (Callable) –

    A method to run when the device is ready

    args: List of positional arguments for the method to run

    check_ready: A method to use for checking whether the device is ready or not. (keyword-only)

  • check_ready (Callable | None) –

Returns:

The return value of action.

Return type:

(any)

get_all_tasks()[source]

Returns internal list of tasks.

parse_reply(cmd, reply)[source]

This function takes a LabDeviceReply object and does all necessary processing to return a reply string back. Parsing is done according to command specification.

This method may be redefined in child classes.

Parameters:
  • reply (Any) – Reply from the device

  • cmd (Dict) – Command definition to look the parsing workflow in.

Returns:

Processed reply

Return type:

(any)

prepare_message(cmd, value)[source]

This function does all preparations needed to make the command compliant with device protocol, e.g. type casts the parameters, checks that their values are in range, adds termination sequences, etc.

This method may be redefined in child classes.

Parameters:
  • cmd (Dict) – Device command definition.

  • value (Any) – Command parameter to set, if any.

Returns:

Checked & prepared command string.

Return type:

(str)

send(cmd, value=None)[source]

This method takes the command to be sent and runs all necessary checks on the command parameter if present and required. Then the command get wrapped with the necessary prefix/terminator and connection lock is acquired. The command string is sent to the device using appropriate connection adapter and _recv() is invoked if a reply is expected. Only after that the connection lock is released.

This method normally shouldn’t be redefined in child classes.

Parameters:
  • cmd – The command to send.

  • value – Command parameter, if any.

property simulation: bool

Determines whether the device behaves as as a real or simulated one. Simulated device just logs all the commands.

start_task(interval, method, args=None)[source]

Creates a LabDeviceTask object, starts it and appends the reference to the task instance to the list of tasks.

Parameters:
  • interval (int) – How often the method should be executed, in seconds

  • method (Callable) – The function to run.

  • args – Arguments for the function, if any.

Returns:

Created task object for further reference.

Return type:

(LabDeviceTask)

stop_all_tasks()[source]

Stops all tasks currently running and resets internal list of tasks.

stop_task(task_to_stop=None)[source]

Stops the LabDeviceTask object and remove the reference to it from the list of tasks. If no argument is provided and only a single task is running - stops that one.

Arg:

task_to_stop: LabDeviceTask object to stop.

wait_until_ready(check_ready=None)[source]

Acquires device lock, waits till device is ready and returns. If no method is provided for checking, self._is_idle is used.

Parameters:

check_ready (Optional[Callable]) – A method to use for checking whether the device is ready or not.

class PyLabware.controllers.LabDeviceTask(interval, method, args=None)[source]

Bases: Thread

Simple class to implement periodically running device actions.

Default constructor

Parameters:
  • interval (int) –

  • method (Callable) –

run()[source]

Starts task activity.

stop()[source]

Sets the stop flag to signal for the thread to exit.