import org.lsst.ccs.description.groovy.CCSBuilder;

import org.lsst.ccs.subsystem.common.devices.turbopump.*;
import org.lsst.ccs.subsystem.common.devices.refrigeration.*;
import org.lsst.ccs.subsystem.common.devices.refrigeration.data.RefrigerationConfig
import org.lsst.ccs.subsystem.common.devices.vacuum.*;
import org.lsst.ccs.subsystem.common.devices.power.distribution.*;

import org.lsst.ccs.subsystem.comcamvacuum.VacPlutoDevice;
import org.lsst.ccs.subsystem.comcamvacuum.IonPumpDevice;
import org.lsst.ccs.subsystem.comcamvacuum.IonPumpControl;
import org.lsst.ccs.subsystem.comcamvacuum.data.VacSysState;

import org.lsst.ccs.subsystem.comcamvacuum.Mks9xxDevice;

import org.lsst.ccs.subsystem.common.devices.dataforth.Maq20Device

import org.lsst.ccs.subsystem.comcamvacuum.data.*;
import org.lsst.ccs.subsystem.comcamvacuum.*;
import org.lsst.ccs.subsystem.comcamvacuum.alerts.ComCamVacuumAlerts;
import org.lsst.ccs.bootstrap.BootstrapResourceUtils;

import org.lsst.ccs.monitor.Alarm;
import org.lsst.ccs.monitor.Line;
import org.lsst.ccs.monitor.Channel;

CCSBuilder builder = ["comcam-vacuum"]

Properties props = BootstrapResourceUtils.getBootstrapSystemProperties()
def runMode = props.getProperty("org.lsst.ccs.run.mode","normal");

builder.

    main (ComCamVacuumSubsystem, cryoPressChan:"VQMonitor/vqmpressure", turboPressChan:"VQMonitor/vqmpressure",
              turboSpeedChan:"turbo/rpm",
        switches:[VacSysState.SW_CRYO_TURBO, VacSysState.SW_CRYO_ION_PUMP1, VacSysState.SW_CRYO_VALVE,
                    VacSysState.SW_CRYOTEL_CRYO, VacSysState.SW_CRYOTEL_COLD1, VacSysState.SW_CRYOTEL_COLD2]) {

    AlarmCryoHighTempLimit  (Alarm, alertToBeRaised:ComCamVacuumAlerts.CRYO_PLATE_TEMPERATURE_TOO_HIGH.getAlert())

    AlarmCryoLowTempLimit   (Alarm, alertToBeRaised:ComCamVacuumAlerts.CRYO_PLATE_TEMPERATURE_TOO_LOW.getAlert())
    
    AlarmColdHighTempLimit  (Alarm, alertToBeRaised:ComCamVacuumAlerts.COLD_PLATE_TEMPERATURE_TOO_HIGH.getAlert())

    AlarmColdLowTempLimit   (Alarm, alertToBeRaised:ComCamVacuumAlerts.COLD_PLATE_TEMPERATURE_TOO_LOW.getAlert())

    AlarmPressureHighLimit  (Alarm, alertToBeRaised:ComCamVacuumAlerts.PRESSURE_TOO_HIGH.getAlert())

    AlarmPressureLowLimit   (Alarm, alertToBeRaised:ComCamVacuumAlerts.PRESSURE_TOO_LOW.getAlert())

    AlarmTurboStatusFail    (Alarm, alertToBeRaised:ComCamVacuumAlerts.TURBO_PUMP_FAIL.getAlert())

    AlarmTurboTempHighLimit (Alarm, alertToBeRaised:ComCamVacuumAlerts.TURBO_PUMP_OVERTEMP.getAlert())

    AlarmCoolerTempTooLowLimit (Alarm, alertToBeRaised:ComCamVacuumAlerts.COOLER_TEMPERATURE_TOO_LOW.getAlert())
    AlarmCoolerTempTooHighLimit (Alarm, alertToBeRaised:ComCamVacuumAlerts.COOLER_TEMPERATURE_TOO_High.getAlert())
    AlarmCoolerPwrSettingTooLowLimit (Alarm, alertToBeRaised:ComCamVacuumAlerts.COOLER_PWR_SETTING_TOO_LOW.getAlert())
    AlarmCoolerPwrSettingTooHighLimit (Alarm, alertToBeRaised:ComCamVacuumAlerts.COOLER_PWR_SETTING_TOO_High.getAlert())
    AlarmCoolerPwrTooHighLimit (Alarm, alertToBeRaised:ComCamVacuumAlerts.COOLER_PWR_TOO_HIGH.getAlert())
    AlarmCoolerAutoShutOffLimit (Alarm, alertToBeRaised:ComCamVacuumAlerts.COOLER_AUTO_SHUTOFF.getAlert())
    AlarmCoolerAutoOffTooHighLimit (Alarm, alertToBeRaised:ComCamVacuumAlerts.COOLER_AUTO_OFF_T_TOO_HIGH.getAlert())
    AlarmCoolerAutoOnTooHighLimit (Alarm, alertToBeRaised:ComCamVacuumAlerts.COOLER_AUTO_ON_T_TOO_HIGH.getAlert())
    AlarmCoolerRejectTTooHighLimit (Alarm, alertToBeRaised:ComCamVacuumAlerts.COOLER_REJECT_T_TOO_HIGH.getAlert())

    AlarmIonPumpVoltageOff  (Alarm, alertToBeRaised:ComCamVacuumAlerts.GENERIC.getAlert())

    AlarmGeneric  (Alarm, alertToBeRaised:ComCamVacuumAlerts.GENERIC.getAlert())
    AlarmHDWC  (Alarm, alertToBeRaised:ComCamVacuumAlerts.GENERIC.getAlert())



      rtds       (Maq20Device, modules: ["RTD", "RTD", "RTD", "IS", "IS", "RTD", "RTD"]) {

      		"temperatureCold2" (Channel, description: "Cold Plate A Temperature", units: "\u00b0C", format: ".1f",
                                devcName: "rtds", hwChan: 0, type: "6", subtype: "R200",
                                checkLo:"alarm", alarmLo:"AlarmColdLowTempLimit",
			        checkHi:"alarm", alarmHi:"AlarmColdHighTempLimit")
      		"temperatureCold1" (Channel, description: "Cold Plate B Temperature", units: "\u00b0C", format: ".1f",
                                devcName: "rtds", hwChan: 2, type: "6", subtype: "R200",
                                checkLo:"alarm", alarmLo:"AlarmColdLowTempLimit",
			        checkHi:"alarm", alarmHi:"AlarmColdHighTempLimit")
      		"temperatureCryo" (Channel, description: "Cryo Plate Temperature", units: "\u00b0C", format: ".1f",
                                devcName: "rtds", hwChan: 1, type: "6", subtype: "R200",
                                checkLo:"alarm", alarmLo:"AlarmCryoLowTempLimit",
			        checkHi:"alarm", alarmHi:"AlarmCryoHighTempLimit")
     }

    cryo  (AVCCryoDevice, serialdev:"/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A107I7UW-if00-port0", baud:9600, devType:RefrigerationConfig.CryoTelType.CRYO) {


        "temperature"  (Channel,
                    description:"Cooler temperature", units:"\u00b0C",
                           hwChan:0, type:"TEMP", subtype:"T", offset:0.0, scale:1.0,
                           checkLo:"alarm", alarmLo:"AlarmCoolerTempTooLowLimit",
                           checkHi:"alarm", alarmHi:"AlarmCoolerTempTooHighLimit")

        "setpoint"  (Channel,
                    description:"Cooler setpoint temperature", units:"\u00b0C",
                           hwChan:1, type:"TEMP", subtype:"T", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"flag", alarmHi:null)

        "setPower"  (Channel,
                    description:"Cooler power setting", units:"Watt",
                           hwChan:2, type:"WATTS", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerPwrSettingTooHighLimit")
        "power"  (Channel,
                    description:"Cooler power", units:"Watt",
                           hwChan:3, type:"WATTS", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerPwrTooHighLimit")
        "autoOffEnabled"  (Channel,
                    description:"Cooler auto shutoff state (1 = auto off)", units:"unitless",
                           hwChan:4, type:"UNKNOWN", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerAutoShutOffLimit")
        "autoOffTemperature"  (Channel,
                    description:"Cooler auto shutoff temperature setting", units:"\u00b0C",
                           hwChan:5, type:"TEMP", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerAutoOffTooHighLimit")
        "autoOnTemperature"  (Channel,
                    description:"Cooler auto shutoff cancellation temperature setting", units:"\u00b0C",
                           hwChan:6, type:"TEMP", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerAutoOnTooHighLimit")
        "rejectTemperature"  (Channel,
                    description:"Cooler reject temperature reading", units:"\u00b0C",
                           hwChan:7, type:"TEMP", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerRejectTTooHighLimit")

    }


    cold1  (AVCCryoDevice, serialdev:"/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A107I7US-if00-port0", baud:9600, devType:RefrigerationConfig.CryoTelType.COLD1) {

        "temperature"  (Channel,
                    description:"Cooler temperature", units:"\u00b0C",
                           hwChan:0, type:"TEMP", subtype:"T", offset:0.0, scale:1.0,
                           checkLo:"alarm", alarmLo:"AlarmCoolerTempTooLowLimit",
                           checkHi:"alarm", alarmHi:"AlarmCoolerTempTooHighLimit")
        "setpoint"  (Channel,
                    description:"Cooler setpoint temperature", units:"\u00b0C",
                           hwChan:1, type:"TEMP", subtype:"T", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"flag", alarmHi:null)

        "setPower"  (Channel,
                    description:"Cooler power setting", units:"Watt",
                           hwChan:2, type:"WATTS", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerPwrSettingTooHighLimit")
        "power"  (Channel,
                    description:"Cooler power", units:"Watt",
                           hwChan:3, type:"WATTS", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerPwrTooHighLimit")
        "autoOffEnabled"  (Channel,
                    description:"Cooler auto shutoff state (1 = auto off)", units:"unitless",
                           hwChan:4, type:"UNKNOWN", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerAutoShutOffLimit")
        "autoOffTemperature"  (Channel,
                    description:"Cooler auto shutoff temperature setting", units:"\u00b0C",
                           hwChan:5, type:"TEMP", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerAutoOffTooHighLimit")
        "autoOnTemperature"  (Channel,
                    description:"Cooler auto shutoff cancellation temperature setting", units:"\u00b0C",
                           hwChan:6, type:"TEMP", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerAutoOffTooHighLimit")
        "rejectTemperature"  (Channel,
                    description:"Cooler reject temperature reading", units:"\u00b0C",
                           hwChan:7, type:"TEMP", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerRejectTTooHighLimit")

    }


    cold2  (AVCCryoDevice, serialdev:"/dev/serial/by-id/usb-FTDI_FT232R_USB_UART_A107I7UU-if00-port0", baud:9600, devType:RefrigerationConfig.CryoTelType.COLD2) {

        "temperature"  (Channel,
                    description:"Cooler temperature", units:"\u00b0C",
                           hwChan:0, type:"TEMP", subtype:"T", offset:0.0, scale:1.0,
                           checkLo:"alarm", alarmLo:"AlarmCoolerTempTooLowLimit",
                           checkHi:"alarm", alarmHi:"AlarmCoolerTempTooHighLimit")
        "setpoint"  (Channel,
                    description:"Cooler setpoint temperature", units:"\u00b0C",
                           hwChan:1, type:"TEMP", subtype:"T", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"flag", alarmHi:null)

        "setPower"  (Channel,
                    description:"Cooler power setting", units:"Watt",
                           hwChan:2, type:"WATTS", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerPwrSettingTooHighLimit")
        "power"  (Channel,
                    description:"Cooler power", units:"Watt",
                           hwChan:3, type:"WATTS", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerPwrTooHighLimit")
        "autoOffEnabled"  (Channel,
                    description:"Cooler auto shutoff state (1 = auto off)", units:"unitless",
                           hwChan:4, type:"UNKNOWN", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerAutoShutOffLimit")
        "autoOffTemperature"  (Channel,
                    description:"Cooler auto shutoff temperature setting", units:"\u00b0C",
                           hwChan:5, type:"TEMP", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerAutoOffTooHighLimit")
        "autoOnTemperature"  (Channel,
                    description:"Cooler auto shutoff cancellation temperature setting", units:"\u00b0C",
                           hwChan:6, type:"TEMP", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerAutoOffTooHighLimit")
        "rejectTemperature"  (Channel,
                    description:"Cooler reject temperature reading", units:"\u00b0C",
                           hwChan:7, type:"TEMP", subtype:"1", offset:0.0, scale:1.0,
                           checkLo:"flag", alarmLo:null,
                           checkHi:"alarm", alarmHi:"AlarmCoolerRejectTTooHighLimit")

    }

    
    turbo  (CryoTurboDevice) {


    "current"      (Channel, description:"TurboPump current", units:"mA",
                       type:"Numeric", subtype:"CURRENT",
                       checkLo:"none", checkHi:"none", format:"%.0f")

    "voltage"      (Channel, description:"TurboPump voltage", units:"Volt",
                       type:"Numeric", subtype:"VOLTAGE",
                       checkLo:"none", checkHi:"none", format:"%.0f")

    "power"        (Channel, description:"TurboPump power", units:"Watt",
                       type:"Numeric", subtype:"POWER",
                       checkLo:"none", checkHi:"alarm", format:"%.0f",
                       alarmHi:"AlarmGeneric")

    "driveFrequency"    (Channel, description:"TurboPump drive frequency", units:"Hz",
                       type:"Numeric", subtype:"DRIVEFREQ",
                       checkLo:"none", checkHi:"none", format:"%.0f")

    "pumpTemperature"     (Channel, description:"TurboPump pump temperature", 
                       units:"\u00b0C", 
                       type:"Numeric", subtype:"PUMP_TEMP", format:"%.0f",
                       checkLo:"flag", 
                       checkHi:"alarm", 
                       alarmHi:"AlarmTurboTempHighLimit")

    "cntrlrSinkTemperature" (Channel, description:"TurboPump controller sink temp.", 
                       units:"\u00b0C", 
                       type:"Numeric", subtype:"CONT_TEMP_SINK", format:"%.0f",
                       checkLo:"flag", 
                       checkHi:"alarm", 
                       alarmHi:"AlarmTurboTempHighLimit")

    "cntrlrAirTemperature"  (Channel, description:"TurboPump controller air temp.", 
                       units:"\u00b0C", 
                       type:"Numeric", subtype:"CONT_TEMP_AIR", format:"%.0f",
                       checkLo:"flag", 
                       checkHi:"alarm", 
                       alarmHi:"AlarmTurboTempHighLimit")

    "rpm"          (Channel, description:"TurboPump speed in RPM", units:"unitless",
                       type:"Numeric", subtype:"RPM",
                       checkLo:"none", checkHi:"none", format:"%.0f")

    "status"       (Channel, description:"TurboPump status: 5=normal, 6=fail", units:"unitless", 
                       type:"Numeric", subtype:"STATUS", 
                       format:"%.0f",
                       checkLo:"flag",
                       checkHi:"alarm", alarmHi:"AlarmTurboStatusFail")




    }


     VQMonitor (Mks9xxDevice) {


        vqmpressure  (Channel, description:"Pressure Reading", units:"Torr", format:".3G",
                      hwChan:0,
                    checkLo:"alarm", alarmLo:"AlarmPressureLowLimit",
                    checkHi:"alarm", alarmHi:"AlarmPressureHighLimit")

    }




     /*
         Its name, used to identify it in databases, plots, etc.
    Its longer, more descriptive, name.
    The units the value is expressed in, e.g. "Volt".
    The device used for obtaining its value.
    The hardware channel number on the device.
    The channel type, e.g. temperature, pressure, etc, which is needed by some devices.
    The subtype, which supplies hardware configuration information.
    The offset and scale values needed for converting a raw hardware value to a physical one.
    Parameters used for checking the value against a limit.  There is one set for the low limit and one for the high one:
        The checking option: NONE, FLAG or ALARM
        The limit value.
        The alarm to be activated upon status transitions when ALARM is specified.
        The deadband value which delays a potential alarm action during a transition back to good status.
    Its id, which is its index in the list of all channels.

The limit values are maintained by the configuration system and can be changed while running.  Any such change causes a status message to be broadcast, which can be used to update the trending database or to update any console displays.
*/







    VacPluto (VacPlutoDevice, 
              latchesUsed:[VacSysState.LATCH_CR_GATE_AO, VacSysState.LATCH_CR_GATE_NFC,
                           VacSysState.LATCH_CR_PUMP, VacSysState.LATCH_CR_VACUUM],
              condsUsed:[VacSysState.COND_CR_TRB_PRESS_10,
                         VacSysState.COND_CR_TRB_PUMP_OFF, VacSysState.COND_CR_VACUUM_001,
                         VacSysState.COND_CR_VACUUM_01])

//    VacPluto (VacPlutoDevice, node:"192.168.1.131",
//              condsUsed:[VacSysState.COND_CR_GATE_AO, VacSysState.COND_CR_GATE_NFC,
//                         VacSysState.COND_CR_PUMP])

    int cip1 = IonPumpDevice.CHAN_CIP1
    int hip1 = IonPumpDevice.CHAN_HIP1
    int hip2 = IonPumpDevice.CHAN_HIP2
    int oip  = IonPumpDevice.CHAN_OIP

    IonPumps (IonPumpDevice) {

        CIP1C (IonPumpControl, hwChan:cip1)

        "current" (Channel, description:"Cryo ion pump current", units:"mA",
                hwChan:cip1, type:"CURRENT", scale:1000.0, )

        "voltage" (Channel, description:"Cryo ion pump voltage", units:"Volt", format:".1f",
                hwChan:cip1, type:"VOLTAGE",
		checkLo:"alarm", alarmLo:"AlarmIonPumpVoltageOff",
		checkHi:"flag")

    }




}