diff options
Diffstat (limited to 'agl_service_voiceagent/utils')
-rw-r--r-- | agl_service_voiceagent/utils/config.py | 7 | ||||
-rw-r--r-- | agl_service_voiceagent/utils/kuksa_interface.py | 66 | ||||
-rw-r--r-- | agl_service_voiceagent/utils/mapper.py | 22 |
3 files changed, 83 insertions, 12 deletions
diff --git a/agl_service_voiceagent/utils/config.py b/agl_service_voiceagent/utils/config.py index e0b053e..7a5c28a 100644 --- a/agl_service_voiceagent/utils/config.py +++ b/agl_service_voiceagent/utils/config.py @@ -44,9 +44,14 @@ def load_config(): os.makedirs(get_config_value('BASE_LOG_DIR')) logging.basicConfig(filename=get_config_value('BASE_LOG_DIR')+'voiceagent_server.log', level=logging.DEBUG, format='[%(asctime)s] [%(name)s] [%(levelname)s]: (%(filename)s:%(funcName)s) %(message)s', filemode='a') - logger = logging.getLogger() + logger = logging.getLogger("agl_service_voiceagent") logger.info("-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-x-") + # remove unwanted third-party loggers + logging.getLogger("snips_inference_agl").setLevel(logging.WARNING) + logging.getLogger("asyncio").setLevel(logging.WARNING) + logging.getLogger("grpc").setLevel(logging.WARNING) + else: raise Exception("Config file path not provided.") diff --git a/agl_service_voiceagent/utils/kuksa_interface.py b/agl_service_voiceagent/utils/kuksa_interface.py index 0881660..ca1090b 100644 --- a/agl_service_voiceagent/utils/kuksa_interface.py +++ b/agl_service_voiceagent/utils/kuksa_interface.py @@ -52,16 +52,49 @@ class KuksaInterface: # get config values self.ip = str(get_config_value("ip", "Kuksa")) self.port = str(get_config_value("port", "Kuksa")) - self.insecure = get_config_value("insecure", "Kuksa") + self.insecure = bool(int(get_config_value("insecure", "Kuksa"))) self.protocol = get_config_value("protocol", "Kuksa") self.token = get_config_value("token", "Kuksa") + self.tls_server_name = get_config_value("tls_server_name", "Kuksa") self.logger = get_logger() + # validate config + if not self.validate_config(): + exit(1) + print(self.ip, self.port, self.insecure, self.protocol, self.token) # define class methods self.kuksa_client = None + def validate_config(self): + """ + Validate the Kuksa client configuration. + + Returns: + bool: True if the configuration is valid, False otherwise. + """ + if self.ip is None: + print("[-] Error: Kuksa IP address is not set.") + self.logger.error("Kuksa IP address is not set.") + return False + + if self.port is None: + print("[-] Error: Kuksa port is not set.") + self.logger.error("Kuksa port is not set.") + return False + + if self.token is None: + print("[-] Warning: Kuksa auth token is not set.") + self.logger.warning("Kuksa auth token is not set.") + + if self.protocol != "ws" and self.protocol != "grpc": + print("[-] Error: Invalid Kuksa protocol. Only 'ws' and 'grpc' are supported.") + self.logger.error("Invalid Kuksa protocol. Only 'ws' and 'grpc' are supported.") + return False + + return True + def get_kuksa_client(self): """ @@ -97,6 +130,7 @@ class KuksaInterface: "port": self.port, "insecure": self.insecure, "protocol": self.protocol, + "tls_server_name": self.tls_server_name }) self.kuksa_client.start() time.sleep(2) # Give the thread time to start @@ -119,11 +153,17 @@ class KuksaInterface: """ if self.kuksa_client: response = self.kuksa_client.authorize(self.token) - response = json.loads(response) - if "error" in response: + + if self.protocol == "ws" and "error" in json.loads(response): + response = json.loads(response) error_message = response.get("error", "Unknown error") print(f"[-] Error: Authorization failed. {error_message}") self.logger.error(f"Authorization failed. {error_message}") + + elif self.protocol == "grpc" and "error" in response: + print("[-] Error: Authorization failed.") + self.logger.error("Authorization failed.") + else: print("[+] Kuksa client authorized successfully.") self.logger.info("Kuksa client authorized successfully.") @@ -146,17 +186,19 @@ class KuksaInterface: if self.kuksa_client is None: print(f"[-] Error: Failed to send value '{value}' to Kuksa. Kuksa client is not initialized.") self.logger.error(f"Failed to send value '{value}' to Kuksa. Kuksa client is not initialized.") - return + return result if self.get_kuksa_status(): try: response = self.kuksa_client.setValue(path, value) - response = json.loads(response) + if not "error" in response: print(f"[+] Value '{value}' sent to Kuksa successfully.") result = True + else: - error_message = response.get("error", "Unknown error") + response = json.loads(response) + error_message = response.get("error", "{\"message\": \"Unknown error.\"}") print(f"[-] Error: Failed to send value '{value}' to Kuksa. {error_message}") self.logger.error(f"Failed to send value '{value}' to Kuksa. {error_message}") @@ -184,19 +226,23 @@ class KuksaInterface: if self.kuksa_client is None: print(f"[-] Error: Failed to get value at path '{path}' from Kuksa. Kuksa client is not initialized.") self.logger.error(f"Failed to get value at path '{path}' from Kuksa. Kuksa client is not initialized.") - return + return result if self.get_kuksa_status(): try: response = self.kuksa_client.getValue(path) response = json.loads(response) - if not "error" in response: + if self.protocol == "ws" and not "error" in response: result = response.get("data", None) result = result.get("dp", None) result = result.get("value", None) - + + elif self.protocol == "grpc" and not "error" in response: + result = response.get("value", None) + result = result.get("value", None) + else: - error_message = response.get("error", "Unknown error") + error_message = response.get("error", "{\"message\": \"Unknown error.\"}") print(f"[-] Error: Failed to get value at path '{path}' from Kuksa. {error_message}") self.logger.error(f"Failed to get value at path '{path}' from Kuksa. {error_message}") diff --git a/agl_service_voiceagent/utils/mapper.py b/agl_service_voiceagent/utils/mapper.py index f24f44f..e42921a 100644 --- a/agl_service_voiceagent/utils/mapper.py +++ b/agl_service_voiceagent/utils/mapper.py @@ -14,6 +14,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import json from agl_service_voiceagent.utils.config import get_config_value, get_logger from agl_service_voiceagent.utils.common import load_json_file, words_to_number @@ -126,7 +127,7 @@ class Intent2VSSMapper: return result - def parse_intent(self, intent_name, intent_slots = []): + def parse_intent(self, intent_name, intent_slots = [], req_id = ""): """ Parses an intent, extracting relevant VSS signals, actions, modifiers, and values based on the intent and its associated slots. @@ -148,11 +149,13 @@ class Intent2VSSMapper: action = self.determine_action(signal_data, intent_slots) modifier = self.determine_modifier(signal_data, intent_slots) value = self.determine_value(signal_data, intent_slots) + original_value = value if value != None and not self.verify_value(signal_data, value): value = None change_factor = signal_data["default_change_factor"] + log_change_factor = change_factor if action in ["increase", "decrease"]: if value and modifier == "to": @@ -160,6 +163,7 @@ class Intent2VSSMapper: elif value and modifier == "by": execution_list.append({"action": action, "signal": signal_name, "factor": str(value)}) + log_change_factor = value elif value: execution_list.append({"action": action, "signal": signal_name, "value": str(value)}) @@ -173,6 +177,20 @@ class Intent2VSSMapper: if action == "set" and value != None: execution_list.append({"action": action, "signal": signal_name, "value": str(value)}) + + + # log the mapping data + mapping_log_data = { + "Signal": signal_name, + "Action": action, + "Modifier": modifier, + "OriginalValue": original_value, + "ProcessedValue": value, + "ChangeFactor": log_change_factor + } + mapping_log_data = json.dumps(mapping_log_data) + print(f"[+] Mapper Log: {mapping_log_data}") + self.logger.info(f"[ReqID#{req_id}] Mapper Log: {mapping_log_data}") return execution_list @@ -259,6 +277,8 @@ class Intent2VSSMapper: Returns: bool: True if the value is valid, False otherwise. """ + value = int(value) if value.isnumeric() else float(value) if value.replace('.', '', 1).isnumeric() else value + if value in signal_data["values"]["ignore"]: return False |