egse.control
cgse-core
This code is part of the cgse-core package.
egse.control
¶
This module defines the abstract class for any Control Server and some convenience functions.
Classes:
| Name | Description |
|---|---|
ControlServer |
Base class for all device control servers and for the Storage Manager and Configuration Manager. |
ServiceRegistrationError |
Raised when a control server fails to register itself with the service registry. |
Functions:
| Name | Description |
|---|---|
is_control_server_active |
Checks if the Control Server is running. |
ControlServer
¶
ControlServer(device_id=None)
Base class for all device control servers and for the Storage Manager and Configuration Manager.
A Control Server reads commands from a ZeroMQ socket and executes these commands by calling the execute() method
of the commanding protocol class.
The subclass shall define the following:
- Define the device protocol class ->
self.device_protocol - Bind the command socket to the device protocol ->
self.dev_ctrl_cmd_sock - Register the command socket in the poll set ->
self.poller
Methods:
| Name | Description |
|---|---|
after_serve |
This method needs to be overridden by the subclass if certain actions need to be executed after the control |
before_serve |
This method needs to be overridden by the subclass if certain actions need to be executed before the control |
can_operate_without_registry |
Returns True when this server can still operate without being registered. |
get_average_execution_times |
Returns the average execution times of all functions that have been monitored by this process. |
get_commanding_port |
Returns the commanding port used by the Control Server. |
get_communication_protocol |
Returns the communication protocol used by the Control Server. |
get_device_status |
Returns the state of the device that is controlled by this Control Server. |
get_device_status_for_monitoring |
The device status for the monitoring messages, see |
get_event_handlers |
Override this in the subclass to provide methods to handle each of the subscriptions. |
get_event_subscriptions |
Override this in the subclass to actually subscribe to events. |
get_ip_address |
Returns the IP address of the current host. |
get_monitoring_port |
Returns the monitoring port used by the Control Server. |
get_process_status |
Returns the process status of the Control Server. |
get_service_port |
Returns the service port used by the Control Server. |
get_status_details |
Returns the service or device specific part of the status report. |
get_status_report |
Returns the status report of the Control Server, as requested by clients through the service command |
get_storage_mnemonic |
Returns the storage mnemonics used by the Control Server. |
handle_scheduled_tasks |
Executes or reschedules tasks in the |
is_storage_manager_active |
Checks if the Storage Manager is active. |
propagate_metrics |
Propagates the given housekeeping information to the metrics hub. |
quit |
Interrupts the Control Server. |
register_to_storage_manager |
Registers this Control Server to the Storage Manager. |
schedule_task |
Schedules a task to run in the control server event loop. |
serve |
Activation of the Control Server. |
set_hk_delay |
Sets the delay time for housekeeping. |
set_logging_level |
Sets the logging level to the given level. |
set_mon_delay |
Sets the delay time for monitoring. |
set_scheduled_task_delay |
Sets the delay time between successive executions of scheduled tasks. |
store_housekeeping_information |
Sends housekeeping information to the Storage Manager. |
unregister_from_storage_manager |
Unregisters the Control Server from the Storage Manager. |
Attributes:
| Name | Type | Description |
|---|---|---|
cgse_object |
str | None
|
The |
service_id |
str | None
|
The service ID of this Control Server, as registered in the Service Registry. |
cgse_object
class-attribute
instance-attribute
¶
cgse_object = None
The cgse <object> command that starts, stops and shows the status of this control server, e.g. "puna" for
cgse puna start, None when there is no such command. It is registered in the metadata, so clients can find the
lifecycle command of a service. Subclasses set it to the name of their entry point in cgse.service.device_command
(devices) or cgse.service.core_command (core services).
service_id
instance-attribute
¶
service_id = None
The service ID of this Control Server, as registered in the Service Registry.
after_serve
¶
after_serve()
This method needs to be overridden by the subclass if certain actions need to be executed after the control server has been deactivated.
before_serve
¶
before_serve()
This method needs to be overridden by the subclass if certain actions need to be executed before the control server is activated.
can_operate_without_registry
¶
can_operate_without_registry()
Returns True when this server can still operate without being registered.
The default is strict: registration is required. Subclasses with explicitly configured, externally known endpoints may override this policy hook.
get_average_execution_times
¶
get_average_execution_times()
Returns the average execution times of all functions that have been monitored by this process.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the average execution times of all functions that have been monitored by this process. The dictionary keys are the function names, and the values are the average execution times in ms. |
get_commanding_port
abstractmethod
¶
get_commanding_port()
Returns the commanding port used by the Control Server.
Returns:
| Type | Description |
|---|---|
int
|
Commanding port used by the Control Server, as specified in the settings. |
get_communication_protocol
abstractmethod
¶
get_communication_protocol()
Returns the communication protocol used by the Control Server.
Returns:
| Type | Description |
|---|---|
str
|
Communication protocol used by the Control Server, as specified in the settings. |
get_device_status
¶
get_device_status()
Returns the state of the device that is controlled by this Control Server.
Control Servers of devices shall override this method and return a dictionary with the following entries:
- connected (bool | None): True when the device is connected, None when this is not known;
- sim_mode (bool | None): True when the Control Server talks to a simulator, None when this is not known.
Returns:
| Type | Description |
|---|---|
dict | None
|
None for Control Servers that do not control a device, e.g. the core services. |
get_device_status_for_monitoring
¶
get_device_status_for_monitoring()
The device status for the monitoring messages, see get_device_status(); an error entry when it can't be
retrieved, so the monitoring messages are never stopped by it.
get_event_handlers
¶
get_event_handlers()
Override this in the subclass to provide methods to handle each of the subscriptions.
get_event_subscriptions
¶
get_event_subscriptions()
Override this in the subclass to actually subscribe to events.
get_monitoring_port
abstractmethod
¶
get_monitoring_port()
Returns the monitoring port used by the Control Server.
Returns:
| Type | Description |
|---|---|
int
|
Monitoring port used by the Control Server, as specified in the settings. |
get_process_status
¶
get_process_status()
Returns the process status of the Control Server.
Returns:
| Type | Description |
|---|---|
dict
|
Dictionary with the process status of the Control Server. |
get_service_port
abstractmethod
¶
get_service_port()
Returns the service port used by the Control Server.
Returns:
| Type | Description |
|---|---|
int
|
Service port used by the Control Server, as specified in the settings. |
get_status_details
¶
get_status_details()
Returns the service or device specific part of the status report.
By default, these are the entries that the device protocol adds to the base status in its get_status()
method, i.e. without the timestamp, delay, and process information. Override this method when that status is
too large or not suited to be sent to clients.
get_status_report
¶
get_status_report()
Returns the status report of the Control Server, as requested by clients through the service command
get_status_report.
The report has the same layout for synchronous and asynchronous control servers:
- schema_version: version of the report layout;
- timestamp: ISO 8601 UTC timestamp;
- control_server: name, service type, device identifier, hostname, and ports of the Control Server;
- process: process information with normalized keys, see
ProcessStatus.as_status_dict(); - device: state of the controlled device, see
get_device_status(), or None when there is no device; - details: service or device specific status, see
get_status_details().
When the device state or the details cannot be retrieved, that section contains only an error entry, so the
rest of the report is still returned.
get_storage_mnemonic
¶
get_storage_mnemonic()
Returns the storage mnemonics used by the Control Server.
This is a string that will appear in the filename with the housekeeping information of the device, as a way of identifying the device. If this is not implemented in the subclass, then the class name will be used.
Returns:
| Type | Description |
|---|---|
str
|
Storage mnemonics used by the Control Server, as specified in the settings. |
handle_scheduled_tasks
¶
handle_scheduled_tasks()
Executes or reschedules tasks in the serve() event loop.
is_storage_manager_active
¶
is_storage_manager_active()
Checks if the Storage Manager is active.
This method has to be implemented by the subclass if you need to store information.
Note: You might want to set a specific timeout when checking for the Storage Manager.
Note: If this method returns True, the following methods shall also be implemented by the subclass:
- register_to_storage_manager()
- unregister_from_storage_manager()
- store_housekeeping_information()
Returns:
| Type | Description |
|---|---|
bool
|
True if the Storage Manager is active; False otherwise. |
propagate_metrics
¶
propagate_metrics(hk)
Propagates the given housekeeping information to the metrics hub.
Nothing will be written if the hk dict doesn't contain any metrics
(except for the timestamp), or if the metrics hub sender is not configured.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hk
|
dict
|
Dictionary containing parameter name and value of all device housekeeping. There is also a timestamp that represents the date/time when the HK was received from the device. |
required |
register_to_storage_manager
¶
register_to_storage_manager()
Registers this Control Server to the Storage Manager.
By doing so, the housekeeping information of the device will be sent to the Storage Manager, which will store the information in a dedicated CSV file.
This method has to be overwritten by the subclasses if they have housekeeping information that must be stored.
Subclasses need to overwrite this method if they have housekeeping information to be stored.
The following information is required for the registration:
- origin: Storage mnemonic, which can be retrieved from
self.get_storage_mnemonic() - persistence_class: Persistence layer (one of the TYPES in egse.storage.persistence)
- prep: depending on the type of the persistence class (see respective documentation)
The egse.storage module provides a convenience method that can be called from the method in the subclass:
>>> from egse.storage import register_to_storage_manager # noqa
Note
the egse.storage module might not be available, it is provided by the cgse-core package.
schedule_task
¶
schedule_task(callback, after=0.0, when=None)
Schedules a task to run in the control server event loop.
The callback function will be executed as soon as possible in the serve() event loop.
Some simple scheduling options are available:
- after: the task will only execute 'x' seconds after the time of scheduling. I.e. the task will be rescheduled until time > scheduled time + 'x' seconds.
- when: the task will only execute when the condition is True.
The after and the when arguments can be combined.
Note
- This function is intended to be used in order to prevent a deadlock.
- Since the
callbackfunction is executed in theserve()event loop, it shall not block!
serve
¶
serve()
Activation of the Control Server.
This comprises the following steps:
- Executing the
before_servemethod; - Checking if the Storage Manager is active and registering the Control Server to it;
- Start listening for keyboard interrupts;
- Start accepting (listening to) commands;
- Start sending out monitoring information;
- Start sending out housekeeping information;
- Start listening for quit commands;
- After a quit command has been received:
- Unregister from the Storage Manager;
- Execute the
after_servemethod; - Close all sockets;
- Clean up all threads.
set_hk_delay
¶
set_hk_delay(seconds)
Sets the delay time for housekeeping.
The delay time is the time between two successive executions of the get_housekeeping() function of the device
protocol.
It might happen that the delay time that is set is longer than what you requested. That is the case when the
execution of the get_housekeeping() function takes longer than the requested delay time. That should prevent
the server from blocking when a too short delay time is requested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
Number of seconds between the housekeeping calls |
required |
Returns:
| Type | Description |
|---|---|
float
|
Delay that was set [ms]. |
set_logging_level
¶
set_logging_level(level)
Sets the logging level to the given level.
Allowed logging levels are:
- "CRITICAL" or "FATAL" or 50
- "ERROR" or 40
- "WARNING" or "WARN" or 30
- "INFO" or 20
- "DEBUG" or 10
- "NOTSET" or 0
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
int | str
|
Logging level to use, specified as either a string or an integer |
required |
set_mon_delay
¶
set_mon_delay(seconds)
Sets the delay time for monitoring.
The delay time is the time between two successive executions of the get_status() function of the device
protocol.
It might happen that the delay time that is set is longer than what you requested. That is the case when the
execution of the get_status() function takes longer than the requested delay time. That should prevent the
server from blocking when a too short delay time is requested.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
Number of seconds between the monitoring calls |
required |
Returns:
| Type | Description |
|---|---|
float
|
Delay that was set [ms]. |
set_scheduled_task_delay
¶
set_scheduled_task_delay(seconds)
Sets the delay time between successive executions of scheduled tasks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seconds
|
float
|
the time interval between two successive executions [seconds] |
required |
store_housekeeping_information
¶
store_housekeeping_information(data)
Sends housekeeping information to the Storage Manager.
This method has to be overwritten by the subclasses if they want the device housekeeping information to be saved.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
dict
|
a dictionary containing parameter name and value of all device housekeeping. There is also a timestamp that represents the date/time when the HK was received from the device. |
required |
unregister_from_storage_manager
¶
unregister_from_storage_manager()
Unregisters the Control Server from the Storage Manager.
This method has to be overwritten by the subclasses.
The following information is required for the registration:
- origin: Storage mnemonic, which can be retrieved from
self.get_storage_mnemonic()
The egse.storage module provides a convenience method that can be called from the method in the subclass:
>>> from egse.storage import unregister_from_storage_manager # noqa
Note
the egse.storage module might not be available, it is provided by the cgse-core package.
ServiceRegistrationError
¶
Bases: RuntimeError
Raised when a control server fails to register itself with the service registry.
is_control_server_active
¶
is_control_server_active(endpoint=None, timeout=0.5)
Checks if the Control Server is running.
This function sends a Ping message to the Control Server and expects a Pong answer back within the timeout period.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
Endpoint to connect to, i.e. |
None
|
timeout
|
float
|
Timeout when waiting for a reply [s, default=0.5] |
0.5
|