# by id (independent of plug location, but may not neccessarly be unique)
# to verify just do:
#   ls /dev/serial/by-id
turbo_uri = '/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A601PCGF-if00-port0'
press_uri = '/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_AH07445U-if00-port0'
itc_uri = '/dev/serial/by-id/usb-Prolific_Technology_Inc._USB-Serial_Controller_D-if00-port0'
lsc_uri = '192.168.1.2:7777'
logo_ip = '192.168.0.3'
# by plug location would also be possible (/dev/serial/by-path)


Node('dil5.psi.ch',
     'dil5 on linse-dil5',
     interface='tcp://5000',
     secondary = ['ws://8010']
)

Mod('logo',
    'frappy_psi.logo.IO',
    '',
    ip_address = logo_ip,
    tcap_client = 0x3000,
    tsap_server = 0x2000
)

Mod('V1',
    'frappy_psi.logo.DigitalActuator',
    'Valves',
    io = 'logo',
    feedback_addr ="V1025.0",
    output_addr ="V1064.3"
)

Mod('V2',
    'frappy_psi.logo.DigitalActuator',
    'dil bypass',
    io = 'logo',
    feedback_addr ="V1024.2",
    output_addr ="V1064.0",
)

Mod('V4',
    'frappy_psi.logo.DigitalActuator',
    'compressor to dump',
    io = 'logo',
    # feedback seems not to work
    output_addr ="V1064.7",
    target_addr ="V404.1",
)

Mod('V5',
    'frappy_psi.logo.DigitalActuator',
    'compressor input',
    io = 'logo',
    feedback_addr ="V1024.4",
    output_addr ="V1064.2",
)

Mod('V9',
    'frappy_psi.logo.DelayedActuator',
    'dump output',
    io = 'logo',
    delay_addr = 'VW24',
    feedback_addr ="V1024.3",
    output_addr ="V1064.5",
    target_addr ="V404.3",
)

Mod('forepump',
    'frappy_psi.logo.DigitalActuator',
    'forepump',
    io = 'logo',
    output_addr ="V1064.6",
    target_addr ="V404.4",
)

Mod('compressor',
    'frappy_psi.logo.DigitalActuator',
    '',
    io = 'logo',
    output_addr ="V1064.4",
    target_addr ="V404.2",
)

Mod('p2',
    'frappy_psi.logo.Pressure',
    'pressure after compressor',
    io = 'logo',
    addr ="VW0",
    pollinterval=0.5,
    )

Mod('p1',
    'frappy_psi.logo.Pressure',
    'dump pressure',
    io = 'logo',
    addr ="VW28",
    pollinterval=0.5,
    )

Mod('p5',
    'frappy_psi.logo.Pressure',
    'pressure after forepump',
    io = 'logo',
    addr ="VW4",
    pollinterval = 0.5,
    )

Mod('airpressure',
    'frappy_psi.logo.Comparator',
    'Airpressure state',
    io = 'logo',
    addr ="V1024.7",
    threshold = 0.5,  # TODO: check if we can read the air pressure instead of a flag
    pollinterval = 0.5,
    )

Mod('io_pfeiffer',
    'frappy_psi.pfeiffer_new.PfeifferProtocol',
    '',
    uri=f'serial://{press_uri}?baudrate=9600+parity=none+bytesize=8+stopbits=1',
)

Mod('io_turbo',
    'frappy_psi.pfeiffer_new.PfeifferProtocol',
    '',
    uri=f'serial://{turbo_uri}?baudrate=9600+parity=none+bytesize=8+stopbits=1',
)

Mod('p3',
    'frappy_psi.pfeiffer_new.RPT200',
    'Pressure in HPa',
    io = 'io_pfeiffer',
    address= 2,
)

Mod('p4',
    'frappy_psi.pfeiffer_new.RPT200',
    'Pressure in HPa',
    io = 'io_pfeiffer',
    address= 4
)

Mod('turbopump',
    'frappy_psi.pfeiffer_new.TCP400',
    'Pfeiffer Turbopump',
    io = 'io_turbo',
    address= 1
)

Mod('MV10',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV10'
)

Mod('MV13',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV13'
)

Mod('MV8',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV8'
)

Mod('MVB',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MVB'
)

Mod('MV2',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV2'
)

Mod('MV1',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV1'
)


Mod('MV3a',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV3a'
)

Mod('MV3b',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV3b'
)

Mod('GV1',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve GV1'
)

Mod('GV2',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve GV2'
)

Mod('MV14',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV14'
)

Mod('MV12',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV12'
)

Mod('MV11',

    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV11'
)

Mod('MV9',
    'frappy_psi.manual_valves.ManualValve',
    'Manual Valve MV9'
)

Mod('itc',
    'frappy_psi.mercury.IO',
    'connection to MercuryiTC',
    uri=f'serial://{itc_uri}?baudrate=115200+parity=none+bytesize=8+stopbits=1',
)

Mod('T_still_wup',
    'frappy_psi.mercury.TemperatureLoop',
    'still warmup temperature',
    slot='MB1.T1',
    io='itc',
)

Mod('T_one_K',
    'frappy_psi.mercury.TemperatureLoop',
    '1 K plate warmup temperature',
    slot='DB5.T1',
    io='itc',
)

Mod('T_mix_wup',
    'frappy_psi.mercury.TemperatureLoop',
    'mix. chamber warmup temperature',
    slot='DB6.T1',
    io='itc',
)

Mod('T_ivc_wup',
    'frappy_psi.mercury.TemperatureLoop',
    'IVC warmup temperature',
    slot='DB7.T1',
    io='itc',
)

Mod('T_cond',
    'frappy_psi.mercury.TemperatureLoop',
    'condenser temperature',
    slot='DB8.T1',
    io='itc',
)

Mod('safety',
    'frappy_psi.dilution.Interlock',
    'interlock mechanism',
    io='logo',
    dil='dil',
)

Mod('dil',
    'frappy_psi.dilution.DIL5',
    'dilution state machine and parameters',

    condenseline_pressure = "p2",
    condense_valve = "V9",
    dump_valve = "V4",
    forepump = "forepump",
    compressor = "compressor",

    turbopump = "turbopump",
    condenseline_valve = "V1",
    circuitshort_valve = "V2",
    still_pressure = "p4",
    still_pressure_turbo = "p3",
    #ls372 = "res1",
    dump_pressure = "p1",
    condensing_p_low  = 1200,
    condensing_p_high = 1500,
)

## Dilution lakeshore Temperature controller

Mod('io_ls',
    'frappy_psi.ls372.StringIO',
    'io for Ls372',
    uri=lsc_uri,
    )

Mod('sw',
    'frappy_psi.ls372.Switcher',
    'channel switcher',
    io = 'io_ls',
)

Mod('T_ivc',
    'frappy_psi.ls372.TemperatureChannel',
    'mix temperature chan 2',
    channel = 2,
    switcher = 'sw',
)

Mod('T_still',
    'frappy_psi.ls372.TemperatureChannel',
    'mix temperature chan 3',
    channel = 3,
    switcher = 'sw',
)

Mod('T_sorb',
    'frappy_psi.ls372.TemperatureChannel',
    'mix temperature chan 1',
    channel = 1,
    switcher = 'sw',
)

Mod('T_cp',
    'frappy_psi.ls372.TemperatureChannel',
    'mix temperature chan 4',
    channel = 4,
    switcher = 'sw',
)

Mod('T_mix',
    'frappy_psi.ls372.TemperatureLoop',
    'mix temperature chan 5',
    channel = 5,
    htrrng = '1mA',
    switcher = 'sw',
    meaning = ['temperature', 30],
)

Mod('htr_sorb',
    'frappy_psi.ls372.SorbHeater',
    'sorb heater',
    io = 'io_ls',
    output = 1,
    fullpower = 5,
)

Mod('htr_still',
    'frappy_psi.ls372.SorbHeater',
    'sorb heater',
    io = 'io_ls',
    output = 2,
    fullpower = 0.2,
)

#Mod('T_sorb_reg',
#    'frappy_psi.picontrol.PIctrl',
#    'controlled mix ch. temperature',
#    input_module = 'T_sorb',
#    output_module = 'htr_sorb',
#    output_min = 0,
#    output_max = 3.6,
#    p = 0.05,
#    itime = 120,
#   )
