# -*- 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_sd(instruction, network_id, scl_map, access_map, data): """ Genera SCL para Temporizador On-Delay (Sd -> TON). Requiere datos de instancia (DB o STAT/TEMP). """ instr_uid = instruction["instruction_uid"] instr_type = "Sd" # Tipo original LAD if instruction["type"].endswith(SCL_SUFFIX) or "_error" in instruction["type"]: return False # 1. Obtener Inputs: s (start), tv (time value) # El pin 'r' (reset) no tiene equivalente directo en TON, se ignora aquí. s_info = instruction["inputs"].get("s") tv_info = instruction["inputs"].get("tv") timer_instance_info = instruction["inputs"].get("timer") # Esperando que x1 lo extraiga scl_s = get_scl_representation(s_info, network_id, scl_map, access_map) scl_tv = get_scl_representation(tv_info, network_id, scl_map, access_map) scl_instance_name = get_scl_representation(timer_instance_info, network_id, scl_map, access_map) if scl_s is None or scl_tv is None: return False # Dependencias no listas # 2. Validar y obtener Nombre de Instancia instance_name = None if timer_instance_info and timer_instance_info.get("type") == "variable": instance_name = scl_instance_name elif timer_instance_info: print(f"Advertencia: Pin 'timer' de {instr_type} UID {instr_uid} conectado a algo inesperado: {timer_instance_info.get('type')}") instance_name = f"#TON_INSTANCE_{instr_uid}" else: instance_name = f"#TON_INSTANCE_{instr_uid}" print(f"Advertencia: No se encontró conexión al pin 'timer' para {instr_type} UID {instr_uid}. Usando placeholder '{instance_name}'. ¡Revisar x1.py y XML!") # 3. Formatear entradas si son variables scl_s_formatted = format_variable_name(scl_s) if s_info and s_info.get("type") == "variable" else scl_s scl_tv_formatted = format_variable_name(scl_tv) if tv_info and tv_info.get("type") == "variable" else scl_tv # 4. Generar la llamada SCL (TON usa IN, PT, Q, ET) scl_call = f"{instance_name}(IN := {scl_s_formatted}, PT := {scl_tv_formatted}); // TODO: Declarar {instance_name} : TON; en VAR_STAT o VAR" instruction["scl"] = scl_call instruction["type"] = instr_type + SCL_SUFFIX # 5. Actualizar scl_map para las salidas Q y RT (mapeado a ET de TON) map_key_q = (network_id, instr_uid, "q") scl_map[map_key_q] = f"{instance_name}.Q" map_key_rt = (network_id, instr_uid, "rt") scl_map[map_key_rt] = f"{instance_name}.ET" return True # --- NUEVO: Procesador de Agrupación (Refinado) --- # --- Function code ends --- # --- Processor Information Function --- def get_processor_info(): """Devuelve la información para el temporizador On-Delay (Sd -> TON).""" # Asumiendo que el tipo en el JSON es 'Sd' return {'type_name': 'sd', 'processor_func': process_sd, 'priority': 5}