cut 3.3.0
Loading...
Searching...
No Matches
comms.maladapter.MalAdapter Class Reference

Convenience class to avoid boilerplate code when using MAL Interface. More...

Public Member Functions

 __init__ (self, str uri, Type interface_class=None, str interface_fqn=DEFAULT_INTERFACE_FQN, bool autoconnect=False, bool async_if=False)
 Initializator for the MalAdapter.
 
 set_timeout_connection (self, int timeout)
 Changes the default value for connection timeout.
 
 set_timeout_reply (self, int timeout)
 Changes the default value for reply timeout.
 
 get_interface (self)
 Returns the MAL RR client.
 
 getInterface (self)
 
 get_mal (self)
 Returns the MAL implementation.
 
 getMal (self)
 
 get_factory (self)
 Returns the MAL factory.
 
 getFactory (self)
 
bool is_autoconnection (self)
 Returns value autoconnection flag.
 
 set_autoconnection (self, bool new_value)
 Sets autoconnection flag.
 
bool isConnected (self)
 Indicates if the MalAdapter is currently connected or not.
 
bool is_connected (self)
 Indicates if the MalAdapter is currently connected or not.
 
 setConnected (self, bool state)
 Sets the property "connected" to true, and emits the connectionChanged signal.
 
 set_connection (self, bool new_value, int connection_timeout=DEFAULT_CONNECTION_TIMEOUT, int reply_timeout=DEFAULT_REPLY_TIMEOUT, bool force_connection=False)
 Main interaction point of the class.
 
 on_connection_changed (self, bool new_value)
 Handles reconnection if needed, this is the callback for autoconnection.
 
list[str] query_commands (self)
 Returns a list of strings where each entry is a command name.
 
str get_method_arg_type (self, str method_query)
 Returns the datatype (MAL ICD string that defines the datatype) or the first argument of the method.
 
 get_timeout_millis (self)
 Returns the current timeout value for commands.
 
 set_timeout_millis (self, int timeout)
 Sets the timeout value for execution of commands.
 
 __getattr__ (self, name)
 Overrides.
 
Any command_inout (self, str cmd, list|None params=None)
 Executes a command in the MAL RR Server.
 
Task command_in_task (self, str cmd, list|None params=None, started_slot=None, cancelled_slot=None, progress_slot=None, result_slot=None, error_slot=None, finished_slot=None, no_start=False)
 Executes a command asynchronously via Task.
 

Static Public Attributes

 connectionChanged = Signal(bool)
 Emitted when the connection state changes.
 
 connected = Property(bool, fget=is_connected, fset=_setConnected, notify=connectionChanged)
 Qt Property that indicates if the MalAdapter is "connected" or not.
 

Protected Member Functions

 _setConnected (self, bool state)
 Sets the property "connected" to true, and emits the connectionChanged signal.
 
 _connection_listener (self, bool status)
 Callback method for the MAL RR register listener.
 
 _connect (self, int connection_timeout=DEFAULT_CONNECTION_TIMEOUT, int reply_timeout=DEFAULT_REPLY_TIMEOUT)
 Implements the actual connection to the MAL Request Reply interface.
 
Type|None _replace_sync_for_async (self, Type interface_class)
 Returns the Async version of the MAL interface, given the Sync one.
 

Detailed Description

Convenience class to avoid boilerplate code when using MAL Interface.

The MalAdapter provides a convenience base class to avoid boilerplate code when connecting to MAL Interfaces. The developer creates a new class that inherits from MalAdapter, and the only pending task, is to develop Slots that provide access to Methods in the Interface.

MalAdapter also provides the connected Property, with the connectionChanged(bool) Signal. Please use this Signal to program event based changes for your applications.

See also
Logger
QObject
#!/usr/bin/env python3
from elt.cut.task.task import Task
from elt.cut.comms.maladapter import MalAdapter
from ModDemoserviceif.Demoserviceif.Commands import CommandsAsync
class DemoServiceFacade(MalAdapter):
def __init__(self, uri: str):
MalAdapter.__init__(self, uri, CommandsAsync)
def get_state(self, result_slot=None):
if not self.is_connected():
self.error("Application is not connected to the server. Please connect first.")
return
task = Task(self.get_interface().GetState)
if result_slot:
task.signals.result.connect(result_slot)
task.signals.result.connect(self.print_output)
task.signals.finished.connect(self.task_finished)
task.start()
def print_output(self, s):
self.debug(s)
def task_finished(self):
self.print_output("Task Finished!")
Implements Task and TaskSignals classes.

Constructor & Destructor Documentation

◆ __init__()

comms.maladapter.MalAdapter.__init__ ( self,
str uri,
Type interface_class = None,
str interface_fqn = DEFAULT_INTERFACE_FQN,
bool autoconnect = False,
bool async_if = False )

Initializator for the MalAdapter.

The MalAdapter provides a convenience base class to avoid boilerplate code when connecting to MAL Interfaces.

It accepts either a class passed by the developer, or its FQN name, in which case, the MalAdapter will figure out the class.

Parameters
uriURI of the MAL Reply Request Endpoint
interface_classICD Class generated by MAL. Either Sync or Async
interface_fqnFQN of the interface
autoconnectBoolean that indicates if the object will start and retry automatically its connection.
async_ifBoolean, if True the MalAdapter class will use the Async interface

Member Function Documentation

◆ __getattr__()

comms.maladapter.MalAdapter.__getattr__ ( self,
name )

Overrides.

Note
This is necessary due to the TaurusCommandButton requiring both certain command be available at the modelObj level.

◆ _connect()

comms.maladapter.MalAdapter._connect ( self,
int connection_timeout = DEFAULT_CONNECTION_TIMEOUT,
int reply_timeout = DEFAULT_REPLY_TIMEOUT )
protected

Implements the actual connection to the MAL Request Reply interface.

This method is started in another thread, so certain member need to be protected by using threading.RLock.

◆ _connection_listener()

comms.maladapter.MalAdapter._connection_listener ( self,
bool status )
protected

Callback method for the MAL RR register listener.

Parameters
[in]statusbool with the new status of connection

◆ _replace_sync_for_async()

Type|None comms.maladapter.MalAdapter._replace_sync_for_async ( self,
Type interface_class )
protected

Returns the Async version of the MAL interface, given the Sync one.

Parameters
[in]interface_classClass Type of the Sync MAL interface
Returns
Class Type of the Async MAL interface

◆ command_in_task()

Task comms.maladapter.MalAdapter.command_in_task ( self,
str cmd,
list|None params = None,
started_slot = None,
cancelled_slot = None,
progress_slot = None,
result_slot = None,
error_slot = None,
finished_slot = None,
no_start = False )

Executes a command asynchronously via Task.

Future results, result/error is handled through signals.

Parameters
[in]cmdCommand name
[in]paramsParameters list for the command to be executed.
[in]started_slotOptional slot called when the Task starts.
[in]cancelled_slotOptional slot called when the Task is cancelled.
[in]progress_slotOptional slot called with AMI (multiple replies) values.
[in]result_slotOptional slot called with the resolved result.
[in]error_slotOptional slot called on error.
[in]finished_slotOptional slot called when the task finishes.
[in]no_startReturns an unstarted Task. Useful for further configuration or testing.
Returns
Task The Task instance executing the command.

◆ command_inout()

Any comms.maladapter.MalAdapter.command_inout ( self,
str cmd,
list|None params = None )

Executes a command in the MAL RR Server.

The command in question is identified by #cmd and uses #params as the parameters for the command execution.

Parameters
[in]cmdCommand name
[in]paramsParameters list for the command to be executed.
Returns
Any object. It depends on the underlying command.

◆ get_factory()

comms.maladapter.MalAdapter.get_factory ( self)

Returns the MAL factory.

Use it to create structures.

◆ get_method_arg_type()

str comms.maladapter.MalAdapter.get_method_arg_type ( self,
str method_query )

Returns the datatype (MAL ICD string that defines the datatype) or the first argument of the method.

Parameters
[in]method_queryMethod name as a string
Returns
string name of the parameter, one of 'string' 'boolean' 'double' 'uint8_t', etc

◆ getFactory()

comms.maladapter.MalAdapter.getFactory ( self)

◆ getInterface()

comms.maladapter.MalAdapter.getInterface ( self)

◆ getMal()

comms.maladapter.MalAdapter.getMal ( self)

◆ on_connection_changed()

comms.maladapter.MalAdapter.on_connection_changed ( self,
bool new_value )

Handles reconnection if needed, this is the callback for autoconnection.

Parameters
[in]new_valueBoolean, new connection state

◆ query_commands()

list[str] comms.maladapter.MalAdapter.query_commands ( self)

Returns a list of strings where each entry is a command name.

Values on the list are only available if the MalAdapter class was created with a fully qualified name for its interface.

◆ set_connection()

comms.maladapter.MalAdapter.set_connection ( self,
bool new_value,
int connection_timeout = DEFAULT_CONNECTION_TIMEOUT,
int reply_timeout = DEFAULT_REPLY_TIMEOUT,
bool force_connection = False )

Main interaction point of the class.

Developers should invoke this to start a connection attempt, or request disconnection.

It does decision logic based on the state of connection.

If connection is requested, and is already connected, it will do nothing. If disconnection is requested, and is already disconnected, it will do nothing.

Parameters
[in]new_valueBoolean. True to connect, False to disconnect
[in]connection_timeoutint. Timeout for connection attemp in milliseconds
[in]reply_timeoutint. Timeout for replies in milliseconds
[in]force_connectionbool. Set to True to ignore an ongoing connection request and try anyways.

◆ set_timeout_millis()

comms.maladapter.MalAdapter.set_timeout_millis ( self,
int timeout )

Sets the timeout value for execution of commands.

Todo
: This is a workaround. The MAL RR Client does not allow to change the reply timeout, so the implementation reconnected to trigger the reconfiguration of the QOS.

◆ setConnected()

comms.maladapter.MalAdapter.setConnected ( self,
bool state )

Sets the property "connected" to true, and emits the connectionChanged signal.

Deprecated
MalAdapter.setConnected() deprecated; it is now a private method.
Todo
this method should be private

Member Data Documentation

◆ connected

comms.maladapter.MalAdapter.connected = Property(bool, fget=is_connected, fset=_setConnected, notify=connectionChanged)
static

Qt Property that indicates if the MalAdapter is "connected" or not.

A bool that is true if connected, false when not. The MAL RR Client provides a connectionListener callback that informs of status changes. When status changes the connectionChanged signal is fired. It carries as argument the new status.

◆ connectionChanged

comms.maladapter.MalAdapter.connectionChanged = Signal(bool)
static

Emitted when the connection state changes.

Provides two states: Connected (True) or Disconnected (False).

Parameters
boolNew state of connection: True = connected; False = disconnected

The documentation for this class was generated from the following file: