import org.lsst.ccs.description.groovy.CCSBuilder
import org.lsst.ccs.drivers.reb.ClientFactory
import org.lsst.ccs.subsystem.rafts.RaftsMain
import org.lsst.ccs.subsystem.rafts.REBDevice
import org.lsst.ccs.subsystem.rafts.DacControl
import org.lsst.ccs.subsystem.rafts.BiasControl
import org.lsst.ccs.subsystem.rafts.AspicControl
import org.lsst.ccs.drivers.reb.SlowAdcs
import org.lsst.ccs.drivers.reb.sim.ClientFactorySimulation
import org.lsst.ccs.monitor.Channel
import org.lsst.ccs.daq.utilities.FitsService
import org.lsst.ccs.imagenaming.service.ImageNameService
import org.lsst.ccs.bootstrap.BootstrapResourceUtils;


Properties props = BootstrapResourceUtils.getBootstrapSystemProperties()
def runMode = props.getProperty("org.lsst.ccs.run.mode","normal");
System.out.println("Building ats-wreb subsystem in run mode: "+runMode);

ClientFactory mainClientFactory = runMode.equals("simulation") ? new ClientFactorySimulation() : new ClientFactory();

taskConfig = ["monitor-update/taskPeriodMillis":1000,"monitor-publish/taskPeriodMillis":10000,
              "agentStatusAggregatorService/patternConfigList":[
              "[pattern:.*,predicate:[agentName:ats-wreb]]",
              "[pattern:.*,predicate:[agentName:ats-power]]"]]


CCSBuilder builder = ["ats-wreb"]

builder.main (RaftsMain, clientFactory:mainClientFactory, nodeTags:taskConfig) {
     
    imageNameService (ImageNameService, 
        dbURL: props.getProperty("org.lsst.ccs.dbUrl", "jdbc:h2:mem:test;MODE=MYSQL"), 
        source: "AuxTel",
        controller: "CCS"
    )
    
    def reb = "WREB"

    "$reb" (REBDevice, hdwType: "daq2", id: 0, ifcName: "ats", ccdMask: 1, clientFactory: mainClientFactory, 
            dataSegmentMap: [15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0], useScienceCCD: true) {

        "${reb}.DAC"  (DacControl)
        //"${reb}.DAC"  (DacControl, raw: true)

        fitsService (FitsService, 
            headerFilesList:["ats-primary:primary", "extended", "ats-reb_cond", "ats-test_cond"]            
        )


        for (int j = 0; j < 2; j++) {
            "${reb}.ASPIC$j" (AspicControl, hwChan: j)
        }

        for (int j = 0; j < 1; j++) {
            "${reb}.Bias$j" (BiasControl, hwChan: j)
            //"${reb}.Bias$j" (BiasControl, hwChan: j, raw: true)
        }
    }

    String title = "${reb} temperatures\\"
    for (int j = 1; j <= 6; j++) {
        "${reb}.Temp$j" (Channel, description: "${title}Board temperature $j", units: "\u00b0C",
                         devcName: "${reb}", hwChan: j - 1, type: "TEMP")
        title = ""
    }

    "${reb}.Atemp0U" (Channel, description: "ASPIC 0 upper temp", units: "\u00b0C",
                      devcName: "${reb}", hwChan: 0, type: "ATEMP")

    "${reb}.Atemp0L" (Channel, description: "ASPIC 0 lower temp", units: "\u00b0C",
                      devcName: "${reb}", hwChan: 1, type: "ATEMP")

    "${reb}.CCDtemp0" (Channel, description: "CCD 0 temperature", units: "\u00b0C",
                      devcName: "${reb}", hwChan: 0, type: "RTD")

    "${reb}.RTDtemp" (Channel, description: "RTD temperature", units: "\u00b0C",
                      devcName: "${reb}", hwChan: 3, type: "RTD")

    "${reb}.DigPS_V" (Channel, description: "${reb} board power\\Digital PS voltage", units: "Volts",
                     devcName: "${reb}", hwChan: 0, type: "POWER")

    "${reb}.DigPS_I" (Channel, description: "Digital PS current", format: ".1f", units: "mA",
                     devcName: "${reb}", hwChan: 1, type: "POWER", scale: 1000)

    "${reb}.AnaPS_V" (Channel, description: "Analog PS voltage", units: "Volts",
                     devcName: "${reb}", hwChan: 2, type: "POWER")

    "${reb}.AnaPS_I" (Channel, description: "Analog PS current", format: ".1f", units: "mA",
                     devcName: "${reb}", hwChan: 3, type: "POWER", scale: 1000)

    "${reb}.ClkHPS_V" (Channel, description: "CLK_H PS voltage", units: "Volts",
                     devcName: "${reb}", hwChan: 4, type: "POWER")

    "${reb}.ClkHPS_I" (Channel, description: "CLK_H PS current", format: ".1f", units: "mA",
                     devcName: "${reb}", hwChan: 5, type: "POWER", scale: 1000)

    "${reb}.ODPS_V" (Channel, description: "OD PS voltage", units: "Volts",
                     devcName: "${reb}", hwChan: 6, type: "POWER")

    "${reb}.ODPS_I" (Channel, description: "OD PS current", format: ".1f", units: "mA",
                     devcName: "${reb}", hwChan: 7, type: "POWER", scale: 1000)

    "${reb}.HtrPS_V" (Channel, description: "Heater PS voltage", units: "Volts",
                     devcName: "${reb}", hwChan: 8, type: "POWER")

    "${reb}.HtrPS_I" (Channel, description: "Heater PS current", format: ".1f", units: "mA",
                     devcName: "${reb}", hwChan: 9, type: "POWER", scale: 1000)

    "${reb}.Power"   (Channel, description: "Total power", format: ".2f", units: "Watts",
                      devcName: "$reb", hwChan: REBDevice.CHAN_TOTAL_POWER, type: "POWER")

    "${reb}.CKPSH_V" (Channel, description: "${reb} clock rails\\CKP_SH voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_CKP_SH, type: "CRVOLT")

    "${reb}.CKS_V"  (Channel, description: "CKS voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_CKS, type: "CRVOLT")

    "${reb}.SCKU_V" (Channel, description: "SCK_U voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_SCK_U, type: "CRVOLT")

    "${reb}.SCKL_V" (Channel, description: "SCK_L voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_SCK_L, type: "CRVOLT")

    "${reb}.RG_V"   (Channel, description: "RG voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_RG, type: "CRVOLT")

    "${reb}.RGU_V"  (Channel, description: "RG_U voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_RG_U, type: "CRVOLT")

    "${reb}.RGL_V"  (Channel, description: "RG_L voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_RG_L, type: "CRVOLT")

    "${reb}.CKP0V"  (Channel, description: "CKP 0 voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_CKP_0, type: "CRVOLT")

    "${reb}.CKS0V"  (Channel, description: "CKS 0 voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_CKS_0, type: "CRVOLT")

    "${reb}.RG0V"   (Channel, description: "RG 0 voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_RG_0, type: "CRVOLT")

    "${reb}.OD0V"   (Channel, description: "${reb} bias voltages\\OD 0 voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_OD_0, type: "CRVOLT")

    "${reb}.OG0V"   (Channel, description: "OG 0 voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_OG_0, type: "CRVOLT")

    "${reb}.RD0V"   (Channel, description: "RD 0 voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_RD_0, type: "CRVOLT")

    "${reb}.GD0V"   (Channel, description: "GD 0 voltage", format: ".2f", units: "Volts",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_GD_0, type: "CRVOLT")

    "${reb}.OD0I"   (Channel, description: "OD 0 current", format: ".1f", units: "mA",
                     devcName: "${reb}", hwChan: SlowAdcs.CHAN_ODI_0, type: "CRVOLT", scale: 1000)

}
