#!../../bin/linux-x86/mksuII

## You may have to change mksuII to something else
## everywhere it appears in this file

< envPaths

cd ${TOP}

## Register all support components
dbLoadDatabase "dbd/mksuII.dbd"
mksuII_registerRecordDeviceDriver pdbbase

# For the Archive Plot edm display
cd(${TOP}/iocBoot/${IOC})
system("./makeFaultArchivePlot.sh")
system("./makeKLYSArchivePlot.sh")
system("./makeTCAVArchivePlot.sh")
cd ${TOP}

# ====================================================================
# Setup some additional environment variables
# ====================================================================
# Setup environment variables
epicsEnvSet("ENGINEER","Shantha Condamoor")

# tag log messages with IOC name
epicsEnvSet("EPICS_IOC_LOG_CLIENT_INET","${IOC}")

# END: Additional environment variables
# ====================================================================

########################################################################
# BEGIN: Initialise all hardware and devices
## Define UDP port to be used by mksuII: 56789

drvMksu_init()

# 172.27.13.71 MKSU-DMP1-RF01 Location: B921-10116 subnet: LCLSINST
drvAsynIPPortConfigure("MksuServer", "172.27.13.71:56789 UDP", 0, 0, 1)
#drvAsynIPPortConfigure("MksuServer", "192.168.0.21:56789 UDP", 0, 0, 1)
#drvAsynIPPortConfigure("MksuServer", "134.79.218.64:56789 UDP", 0, 0, 1)

initMksu("Mksu", "MksuServer")

## Asyn messages
#asynSetTraceMask("MksuServer",-1,0x0F)
#asynSetTraceIOMask("MksuServer",-1,0x6)
#asynSetTraceMask("MksuServer",0,255)
#asynSetTraceIOMask("MksuServer",0,4)

asynSetTraceIOMask("Mksu",-1,0)
asynSetTraceMask("Mksu",-1,0)

#END: Initialisation of the hardware
########################################################################


########################################################################
# BEGIN: Load the record databases
#######################################################################
# =====================================================================
# Load iocAdmin databases to support IOC Health and monitoring
# =====================================================================
dbLoadRecords("db/iocAdminSoft.db","IOC=SIOC:DMP1:RF01")
dbLoadRecords("db/iocAdminScanMon.db","IOC=SIOC:DMP1:RF01")

# The following database is a result of a python parser
# which looks at RELEASE_SITE and RELEASE to discover
# versions of software your IOC is referencing
# The python parser is part of iocAdmin
dbLoadRecords("db/iocRelease.db","IOC=SIOC:DMP1:RF01")

# =====================================================================
# Load database for autosave status
# =====================================================================
dbLoadRecords("db/save_restoreStatus.db", "P=SIOC:DMP1:RF01:")

# =====================================================================
# Load record database for asynRecord
# =====================================================================
dbLoadRecords("db/asynRecord.db", "P=SIOC:DMP1:RF01:,R=ASYNDRIVER,PORT=MksuServer,ADDR=0,IMAX=2048,OMAX=2048")

# =====================================================================
#Load Additional databases:
# =====================================================================
dbLoadRecords("db/mksu.template","AREA=DMP1,LOCATION=1,PORT=Mksu,CHANNEL=0,TIMEOUT=1,SCAN=.1 second")
dbLoadRecords("db/dbFaultSummary.db")
dbLoadRecords("db/mksuFaults.db")

dbLoadRecords("db/1note.vdb","ROOT=KLYS:DMP1:1:NOTE1")
dbLoadRecords("db/1note.vdb","ROOT=KLYS:DMP1:1:NOTE2")
dbLoadRecords("db/1note.vdb","ROOT=KLYS:DMP1:1:NOTE3")
dbLoadRecords("db/1note.vdb","ROOT=KLYS:DMP1:1:NOTE4")
dbLoadRecords("db/1note.vdb","ROOT=KLYS:DMP1:1:NOTE5")

# For the Klystron CUD
dbLoadRecords("db/1STATSTR.vdb","DEVICE=KLYS:DMP1:1")

# For the Archive Plot edm display
< iocBoot/${IOC}/loadFaultArchivePlot.cmd
< iocBoot/${IOC}/loadKLYSArchivePlot.cmd
< iocBoot/${IOC}/loadTCAVArchivePlot.cmd

# For MKSU FAST ADC Waveforms
dbLoadRecords("db/mksuWaveform.db","mksu=KLYS:DMP1:1,ch=BV,freq=0")
dbLoadRecords("db/mksuWaveform.db","mksu=KLYS:DMP1:1,ch=BC,freq=0")
dbLoadRecords("db/mksuWaveform.db","mksu=KLYS:DMP1:1,ch=FP,freq=0")
dbLoadRecords("db/mksuWaveform.db","mksu=KLYS:DMP1:1,ch=RP,freq=0")

# END: Loading the record databases
########################################################################

# Execute generic piece

# < ${TOP}/iocBoot/st.softgeneric.cmd

# Generic piece of startup script
# for LLRF SOFT IOCs

system("pwd")

# For iocAdmin
epicsEnvSet("LOCATION","lcls-daemon1")
epicsEnvSet("ST_CMD","startup.cmd")

# Capture load addresses of all modules (essential for debugging if iocInit crashes...)


#< ${TOP}/iocBoot/init_restore_soft.cmd

# Specify where request and save_restore files can be found
set_requestfile_path("${IOC_DATA}/${IOC}/autosave-req")
set_savefile_path("${IOC_DATA}/${IOC}/autosave")

# Status-PV prefix, so save_restore can find its status PVs.
save_restoreSet_status_prefix("SIOC:DMP1:RF01:")

# Ok to restore a save set that had missing values (no CA connection to PV)?
# Ok to save a file if some CA connections are bad?
save_restoreSet_IncompleteSetsOk(1)

# In the restore operation, a copy of the save file will be written.  The
# file name can look like "auto_settings.sav.bu", and be overwritten every
# reboot, or it can look like "auto_settings.sav_020306-083522" (this is what
# is meant by a dated backup file) and every reboot will write a new copy.
#save_restoreSet_DatedBackupFiles(1)

# Specify what save files should be restored and when.
# Note: up to eight files can be specified for each pass.
set_pass0_restoreFile("info_positions.sav")
set_pass0_restoreFile("info_settings.sav")
set_pass1_restoreFile("info_settings.sav")

iocInit()

# Start EPICS V4 PV Server
pvaSrvStart

#< ${TOP}/iocBoot/start_restore_soft.cmd

# Wait before building autosave files
epicsThreadSleep(1)

# Generate the autosave PV list
# Note we need change directory to 
# where we are saving the restore 
# request file.
cd("${IOC_DATA}/${IOC}/autosave-req")
makeAutosaveFiles()

# Start the save_restore task 
# save changes on change, but no faster
# than every 5 seconds.
# Note: the last arg cannot be set to 0
create_monitor_set("info_positions.req", 60 )
create_monitor_set("info_settings.req" , 60)

cd $(TOP)/iocBoot/sioc-dmp1-rf01

## Start any sequence programs
seq mksuIISetup,"name=mksuIISetup"

##############################################################################################
ffll 0 2
ffll 1 2
ffll 2 2
ffll 3 2 
ffll 4 2
