[docs]classAgentServices:""" This class contains all the methods related to the agent services. As well as the asset has services that can be exposed, the agent also has services that can be exposed and used during the execution of the software. """def__init__(self,agent_object):""" The constructor method adds the object of the agent to have access to its information. Args: agent_object (spade.Agent): the SPADE agent object of the AAS Manager agent. """# The SPADE agent object is stored as a variable of the behaviour classself.myagent=agent_object# The services dictionary contains all available services of the agent, with its associated executable methodsself.services={'RAM_memory_function':self.get_software_ram_memory}# TODO no hacerlo asi, rellenarlo de otra forma (p.e. por metodos de extension)# The Lock object is used to manage the access to global service dictionaryself.lock=asyncio.Lock()# ------------------------# Services general methods# ------------------------
[docs]asyncdefget_agent_service_by_id(self,service_id):""" This method gets the agent service by its identifier. It returns None if the service is not found. Args: service_id (str): unique identifier of the agent service. Returns: method: executable method of the agent service. """asyncwithself.lock:ifservice_idnotinself.services:returnNoneelse:returnself.services[service_id]
[docs]asyncdefexecute_agent_service_by_id(self,service_id,**kwargs):""" This method executes the agent service by its identifier. The parameters of the method with their values are available in kwargs. Args: service_id (str): identifier of the agent service. **kwargs: received parameters with the values. Returns: object: the result of the executed agent service ('OK' if the service does not return anything). """# First, the method of the service via its identifier is gotservice_method=awaitself.get_agent_service_by_id(service_id)ifservice_methodisNone:raiseKeyError(f"Agent service with identifier {service_id} does not exist in this DT.")else:# Parameters adapted to the types required in the method are obtainedadapted_params=awaitAgentServiceUtils.get_adapted_service_parameters(service_method,**kwargs)# Eventually, the method with the transformed arguments is called, and it is waited for the resultresult=awaitservice_method(**adapted_params)ifresultisnotNone:returnresultelse:return"OK"
[docs]asyncdefget_software_ram_memory(self):""" This agent service gets the current used RAM memory of the software. Returns: float: the current used RAM memory of the software. """returnpsutil.virtual_memory().percent