Skip to content

controller_proxy

plantimager.webui.controller_proxy Link

Controller Proxy Module

This module provides a bridge between the local controller device functionality and remote RPC communication, allowing applications to interact with controller hardware through a network connection.

Key Features
  • Singleton pattern implementation ensuring only one controller proxy exists
  • Transparent proxying of controller device methods to remote systems
  • ZeroMQ-based RPC communication for reliable client-server interactions
  • Consistent interface matching the local controller device API
Usage Examples
>>> import zmq
>>> from plantimager.webui.controller_proxy import RPCController
>>>
>>> # Initialize the controller proxy
>>> context = zmq.Context()
>>> controller = RPCController(context, "tcp://localhost:14567")
>>>
>>> # Access the singleton instance elsewhere in your code
>>> same_controller = RPCController.instance()
>>>
>>> # Use controller methods as if they were local
>>> controller.some_method()  # This will be executed on the remote system

RPCController Link

RPCController(context, url)

Bases: ControllerDevice, RPCClient

Proxy of controller and RPC server.

A singleton class that serves as a proxy between a controller device and an RPC server. Only one instance of this class can exist at a time, and it can be accessed through the instance class method.

Parameters:

Name Type Description Default
context Context

The ZeroMQ context to use for communication.

required
url str

The URL to connect to for RPC communication.

required

Attributes:

Name Type Description
_instance RPCController or None

Class variable that holds the single instance of the class.

Notes

This class implements the Singleton design pattern. The first time it is instantiated, it creates a new instance and stores it in the _instance class variable. Subsequent instantiations return the existing instance.

The class inherits from both ControllerDevice and RPCClient to provide controller functionality over an RPC connection.

See Also

plantimager.commons.controller_device.ControllerDevice : Base class for controller device functionality. plantimager.commons.RPC.RPCClient : Base class for RPC client functionality.

Examples:

>>> import zmq
>>> from plantimager.webui.controller_proxy import RPCController
>>> context = zmq.Context()
>>> controller = RPCController(context, "tcp://localhost:14567")
>>> # This returns the same instance
>>> controller2 = RPCController(context, "tcp://localhost:14567")
>>> controller is controller2
True
>>> # Get the singleton instance
>>> RPCController.instance()
RuntimeError: Controller proxy not initialized.

Initialize the RPCController.

Parameters:

Name Type Description Default
context Context

The ZeroMQ context to use for communication.

required
url str

The URL to connect to for RPC communication.

required
Source code in plantimager/webui/controller_proxy.py
109
110
111
112
113
114
115
116
117
118
119
120
def __init__(self, context: zmq.Context, url: str):
    """Initialize the RPCController.

    Parameters
    ----------
    context : zmq.Context
        The ZeroMQ context to use for communication.
    url : str
        The URL to connect to for RPC communication.
    """
    RPCClient.__init__(self, context, url)
    self.__class__._instance = self

__new__ Link

__new__(context, url)

Create a new instance or return the existing instance.

Parameters:

Name Type Description Default
context Context

The ZeroMQ context to use for communication.

required
url str

The URL to connect to for RPC communication.

required

Returns:

Type Description
RPCController

The singleton instance of this class.

Source code in plantimager/webui/controller_proxy.py
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
def __new__(cls, context: zmq.Context, url: str):
    """Create a new instance or return the existing instance.

    Parameters
    ----------
    context : zmq.Context
        The ZeroMQ context to use for communication.
    url : str
        The URL to connect to for RPC communication.

    Returns
    -------
    plantimager.commons.controller_device.RPCController
        The singleton instance of this class.
    """
    if cls._instance is None:
        instance = super(RPCController, cls).__new__(cls)
        return instance
    return cls._instance

camera_names abstractmethod Link

camera_names()

Return the list of camera names.

Source code in plantimager/commons/controller_device.py
58
59
60
61
62
@RPCProperty(notify=cameraNamesChanged)
@abstractmethod
def camera_names(self) -> list[str]:
    """Return the list of camera names."""
    pass

instance classmethod Link

instance()

Get the singleton instance of the RPCController.

Returns:

Type Description
RPCController

The singleton instance of this class.

Raises:

Type Description
RuntimeError

If the controller proxy has not been initialized yet.

Source code in plantimager/webui/controller_proxy.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
@classmethod
def instance(cls) -> "RPCController":
    """Get the singleton instance of the RPCController.

    Returns
    -------
    plantimager.commons.controller_device.RPCController
        The singleton instance of this class.

    Raises
    ------
    RuntimeError
        If the controller proxy has not been initialized yet.
    """
    if cls._instance is None:
        raise RuntimeError("Controller proxy not initialized.")
    return cls._instance

max_progress abstractmethod Link

max_progress()

Return the maximum progress of the scan.

Source code in plantimager/commons/controller_device.py
46
47
48
49
50
@RPCProperty(notify=maxProgressChanged)
@abstractmethod
def max_progress(self) -> int:
    """Return the maximum progress of the scan."""
    pass

progress abstractmethod Link

progress()

Return the current progress of the scan.

Source code in plantimager/commons/controller_device.py
40
41
42
43
44
@RPCProperty(notify=progressChanged)
@abstractmethod
def progress(self) -> int:
    """Return the current progress of the scan."""
    pass

ready_to_scan abstractmethod Link

ready_to_scan()

Return whether the controller is ready to start a scan.

Source code in plantimager/commons/controller_device.py
52
53
54
55
56
@RPCProperty(notify=readyToScanChanged)
@abstractmethod
def ready_to_scan(self) -> bool:
    """Return whether the controller is ready to start a scan."""
    pass

run_scan abstractmethod Link

run_scan()

Start the scan.

Source code in plantimager/commons/controller_device.py
35
36
37
38
@abstractmethod
def run_scan(self):
    """Start the scan."""
    pass

set_config abstractmethod Link

set_config(config)

Send a configuration dictionary to the controller.

Source code in plantimager/commons/controller_device.py
25
26
27
28
@abstractmethod
def set_config(self, config: dict):
    """Send a configuration dictionary to the controller."""
    pass

set_dataset_name abstractmethod Link

set_dataset_name(name)

Set the name of the dataset to be created.

Source code in plantimager/commons/controller_device.py
30
31
32
33
@abstractmethod
def set_dataset_name(self, name: str):
    """Set the name of the dataset to be created."""
    pass

set_db_url abstractmethod Link

set_db_url(url)

Set the database URL for the controller.

Source code in plantimager/commons/controller_device.py
20
21
22
23
@abstractmethod
def set_db_url(self, url: str):
    """Set the database URL for the controller."""
    pass