""" File to group useful methods for accessing and managing the AAS Archive."""importcalendarimportjsonimportloggingimportosimporttimefromutilities.AASarchiveInfoimportAASarchiveInfo_logger=logging.getLogger(__name__)# ------------------------# Methods related to files# ------------------------
[docs]defcreate_status_file():"""This method creates the status file of the AAS Manager and sets it to "initializing"."""initial_status_info={'name':'AAS_Manager','status':'Initializing','timestamp':calendar.timegm(time.gmtime())}with(open(AASarchiveInfo.MANAGER_STATUS_FILE_PATH,'x')asstatus_file):json.dump(initial_status_info,status_file)status_file.close()
[docs]defcreate_interaction_files():"""This method creates the necessary interaction files to exchange information between AAS Core and AAS Manager."""# First interaction folders are createdos.mkdir(AASarchiveInfo.CORE_INTERACTIONS_FOLDER_PATH)os.mkdir(AASarchiveInfo.MANAGER_INTERACTIONS_FOLDER_PATH)# Then the interaction files are added in each folderwith(open(AASarchiveInfo.CORE_INTERACTIONS_FOLDER_PATH+AASarchiveInfo.SVC_REQUEST_FILE_SUBPATH,'x')ascore_requests_file,open(AASarchiveInfo.CORE_INTERACTIONS_FOLDER_PATH+AASarchiveInfo.SVC_RESPONSE_FILE_SUBPATH,'x')ascore_responses_file,open(AASarchiveInfo.MANAGER_INTERACTIONS_FOLDER_PATH+AASarchiveInfo.SVC_REQUEST_FILE_SUBPATH,'x')asmanager_requests_file,open(AASarchiveInfo.MANAGER_INTERACTIONS_FOLDER_PATH+AASarchiveInfo.SVC_RESPONSE_FILE_SUBPATH,'x')asmanager_responses_file):core_requests_file.write('{"serviceRequests": []}')core_requests_file.close()manager_requests_file.write('{"serviceRequests": []}')manager_requests_file.close()core_responses_file.write('{"serviceResponses": []}')core_responses_file.close()manager_responses_file.write('{"serviceResponses": []}')manager_responses_file.close()
[docs]defcreate_log_files():"""This method creates the necessary log files to save services information."""# First the log folder is createdos.mkdir(AASarchiveInfo.LOG_FOLDER_PATH)# The folder for services log is also createdos.mkdir(AASarchiveInfo.SVC_LOG_FOLDER_PATH)# Then the log files are added in each folderall_svc_log_file_names=[AASarchiveInfo.ASSET_RELATED_SVC_LOG_FILENAME,AASarchiveInfo.AAS_INFRASTRUCTURE_SVC_LOG_FILENAME,AASarchiveInfo.AAS_SERVICES_LOG_FILENAME,AASarchiveInfo.SUBMODEL_SERVICES_LOG_FILENAME]forlog_file_nameinall_svc_log_file_names:withopen(AASarchiveInfo.SVC_LOG_FOLDER_PATH+'/'+log_file_name,'x')aslog_file:log_file.write('[]')log_file.close()
# -------------------------# Methods related to status# -------------------------
[docs]defchange_status(new_status):""" This method updated the status of an AAS Manager instance. Args: new_status (str): the new status of the AAS Manager instance. """status_file_json=file_to_json(AASarchiveInfo.MANAGER_STATUS_FILE_PATH)status_file_json['status']=new_statusstatus_file_json['timestamp']=calendar.timegm(time.gmtime())update_json_file(AASarchiveInfo.MANAGER_STATUS_FILE_PATH,status_file_json)
[docs]defget_status(entity):""" This method gets the status of the requested entity. Args: entity (str): The entity to get the status for. Returns: dict: status in JSON format. """status_file_json=Noneifentity=="Manager":status_file_json=file_to_json(AASarchiveInfo.MANAGER_STATUS_FILE_PATH)elifentity=="Core":status_file_json=file_to_json(AASarchiveInfo.CORE_STATUS_FILE_PATH)returnstatus_file_json['status']
[docs]defcheck_core_initialization():"""This method checks if the core has initialized so the Manager can be started."""whileTrue:ifos.path.isfile(AASarchiveInfo.CORE_STATUS_FILE_PATH)isTrue:iffile_to_json(AASarchiveInfo.CORE_STATUS_FILE_PATH)['status']!="Initializing":breaktime.sleep(1)# waits 1s_logger.info('AAS Core has initialized, so the AAS Manager is starting.')
# ------------------------# Methods related to JSON# ------------------------
[docs]deffile_to_json(file_path):""" This method gets the content of a JSON file. Args: file_path (str): the path of the JSON file. Returns: dict: content of the file in JSON format."""f=open(file_path)try:content=json.load(f)f.close()exceptjson.JSONDecodeErrorase:_logger.error("Invalid JSON syntax:"+str(e))returnNonereturncontent
[docs]defupdate_json_file(file_path,content):""" This method updates the content of a JSON file. Args: file_path (str): the path to the JSON file. content (dict): the content of the JSON file. """withopen(file_path,"w")asoutfile:json.dump(content,outfile)
# ------------------------# Methods related to XML# ------------------------
[docs]defxml_to_file(file_path,xml_content):""" This method writes the content of an XML in a file. Args: file_path (str): the path to the XML file. xml_content (bytes): the content of the XML file. """withopen(file_path,'wb')asxml_file:xml_file.write(xml_content)