"""
CoolProp Server - Serveur persistant pour éviter le démarrage répété de Python
Supporte les mélanges HEOS via PropsSI
"""

import sys
from CoolProp.CoolProp import PropsSI

def main():
    """Serveur qui lit les commandes depuis stdin et écrit les résultats sur stdout"""
    sys.stdout.write("READY\n")
    sys.stdout.flush()
    
    while True:
        try:
            line = sys.stdin.readline()
            if not line:
                break
            
            line = line.strip()
            if not line:
                continue
            
            if line == "EXIT":
                break
            
            parts = line.split('\t')
            command = parts[0]
            
            if command == "PROP":
                # Format: PROP\toutput\tinput1_name\tinput1_value\tinput2_name\tinput2_value\tfluid
                output_prop = parts[1]
                input1_name = parts[2]
                input1_value = float(parts[3])
                input2_name = parts[4]
                input2_value = float(parts[5])
                fluid = parts[6]
                
                result = PropsSI(output_prop, input1_name, input1_value,
                               input2_name, input2_value, fluid)
                sys.stdout.write(f"{result}\n")
                sys.stdout.flush()
            
            elif command == "PING":
                sys.stdout.write("PONG\n")
                sys.stdout.flush()
                
        except Exception as e:
            sys.stdout.write(f"ERROR: {str(e)}\n")
            sys.stdout.flush()

if __name__ == "__main__":
    main()
