# -*- coding: utf-8 -*- from .processor_utils import get_scl_representation, format_variable_name,get_target_scl_name # TODO: Import necessary functions from processor_utils # Example: from .processor_utils import get_scl_representation, format_variable_name # Or: import processors.processor_utils as utils # TODO: Define constants if needed (e.g., SCL_SUFFIX) or import them SCL_SUFFIX = "_scl" # --- Function code starts --- def process_timer(instruction, network_id, scl_map, access_map, data): """ Genera SCL para Temporizadores (TON, TOF). Requiere datos de instancia (DB o STAT). """ instr_uid = instruction["instruction_uid"] instr_type = instruction["type"] # Será "TON" o "TOF" if instr_type.endswith(SCL_SUFFIX) or "_error" in instr_type: return False # 1. Obtener Inputs in_info = instruction["inputs"].get("IN") # Entrada booleana pt_info = instruction["inputs"].get("PT") # Preset Time (Tipo TIME) scl_in = get_scl_representation(in_info, network_id, scl_map, access_map) scl_pt = get_scl_representation(pt_info, network_id, scl_map, access_map) if scl_in is None or scl_pt is None: return False # Dependencias no listas # 2. Obtener Nombre de Instancia (NECESITA MEJORA EN x1.py) instance_name = instruction.get("instance_db") # Reutilizar campo si x1 lo llena if not instance_name: # Generar placeholder si x1 no extrajo la instancia para Part instance_name = f"#TIMER_INSTANCE_{instr_uid}" # Placeholder para VAR_TEMP o VAR_STAT print(f"Advertencia: No se encontró instancia para {instr_type} UID {instr_uid}. Usando placeholder '{instance_name}'. Ajustar x1.py y declarar en x3.py.") else: instance_name = format_variable_name(instance_name) # Limpiar si viene de x1 # 3. Formatear entradas si son variables scl_in_formatted = format_variable_name(scl_in) if in_info and in_info.get("type") == "variable" else scl_in scl_pt_formatted = format_variable_name(scl_pt) if pt_info and pt_info.get("type") == "variable" else scl_pt # 4. Generar la llamada SCL # Nota: Las salidas Q y ET se acceden directamente desde la instancia. scl_call = f"{instance_name}(IN := {scl_in_formatted}, PT := {scl_pt_formatted}); // TODO: Declarar {instance_name} : {instr_type}; en VAR_STAT o VAR" instruction["scl"] = scl_call instruction["type"] = instr_type + SCL_SUFFIX # 5. Actualizar scl_map para las salidas Q y ET map_key_q = (network_id, instr_uid, "Q") scl_map[map_key_q] = f"{instance_name}.Q" map_key_et = (network_id, instr_uid, "ET") scl_map[map_key_et] = f"{instance_name}.ET" # TON/TOF no tienen un pin ENO estándar en LAD/FBD que se mapee directamente return True # --- Processor Information Function --- def get_processor_info(): """Devuelve la información para los temporizadores TON y TOF (si se usan genéricamente).""" # Esta función manejaría tipos TON/TOF si aparecieran directamente en el JSON # y no fueran manejados por process_sd/process_se (que es lo más común desde LAD). # Incluir por si acaso o si la conversión inicial genera TON/TOF directamente. return [ {'type_name': 'ton', 'processor_func': process_timer, 'priority': 5}, {'type_name': 'tof', 'processor_func': process_timer, 'priority': 5} ]