Skip to content
Snippets Groups Projects
patch-control.sh 5.04 KiB
Newer Older
Carmelo Pellegrino's avatar
Carmelo Pellegrino committed
#!/bin/bash
#
# PatchControl
Carmelo Pellegrino's avatar
Carmelo Pellegrino committed
#
# This script helps the shifter to easily operate both the line and
# the DAQ processes until the ControlUnit will be finalised.
Carmelo Pellegrino's avatar
Carmelo Pellegrino committed
#
# Author: Carmelo Pellegrino <carmelo.pellegrino@bo.infn.it>
#

# The configuration of this very script is perfomed via shell variables.
# Here the list of parameters is reported
#
# DM_EXE_DIR  -> the absolute path of the folder which contain DetectorControl.exe
# DM_DATA_DIR -> the absolute path of the folder which contain the detectorfile
# PC_CONF_DIR -> the absolute path of the pc.cfg configuration file
# DAQ_SERVER  -> IP address of the DAQ Server
function put_lock() {
}

function rm_lock() {
}

function test_lock() {
Carmelo Pellegrino's avatar
Carmelo Pellegrino committed
  if [ -e /tmp/pc.lock ]; then
function on_exit() {
  if [ "${LOCK_OWNER}" == "True" ]; then
    rm_lock
  fi

  for pid in ${PROCS[*]}; do
  unset pid
function get_run_setup_file() {
  local LIST=()
  local line
Carmelo Pellegrino's avatar
Carmelo Pellegrino committed

  while IFS=';' read -ra line; do
    LIST+=("FALSE" "${line[@]}")
  done < ${PC_CONF_DIR}/pc.cfg
Carmelo Pellegrino's avatar
Carmelo Pellegrino committed

  zenity --title "Run Setup Selector" \
         --list \
         --text "Select one of the following Run Setups:" \
         --radiolist \
         --width=600 \
         --height=400 \
         --column="Selection" \
         --column="Run Setup file" \
         --column="Run Setup name" \
         --column="Description" \
         --hide-column=2 "${LIST[@]}"
function dm_driver() {
  echo "set session target = run"

  sleep $1

  echo exit
  echo y
}

  [ ! -d ${HOME}/.km3_private ] && mkdir -p ${HOME}/.km3_private
  local logfile="${HOME}/.km3_private/pc.log"
  echo "$(date): ${*}" >>${logfile}
  # print the number of seconds elapsed since Jan 1 1970 00:00 in UTC
  date -u +%s
}

function get_run_number() {
  [ ! -d ~/.km3_private ] && mkdir -p ~/.km3_private
  [ ! -e ~/.km3_private/last_run_number ] && echo 0 >~/.km3_private/last_run_number
  local last_run_number=`cat ~/.km3_private/last_run_number`
  local run_number

  let "run_number = ${last_run_number} + 1"

  echo ${run_number} > ~/.km3_private/last_run_number
  echo ${run_number}
}

Carmelo Pellegrino's avatar
Carmelo Pellegrino committed
# Script begins here

test_lock || { zenity --error --text="There is another instance running of this program."; exit 1;}

put_lock
LOCK_OWNER="True"

trap on_exit EXIT
  rs_file=`get_run_setup_file`
Carmelo Pellegrino's avatar
Carmelo Pellegrino committed
    zenity --error --text="No Run Setup file selected, aborting"
    exit -1
  fi

  # Ask for run duration

  run_duration=`zenity --title "Run duration" \
                       --list \
                       --text "Select one of the following duration [minutes]:" \
                       --radiolist \
                       --column="Selection" \
                       --column="Minutes" \
                       FALSE Cancel FALSE 5 TRUE 10 FALSE 20`

Carmelo Pellegrino's avatar
Carmelo Pellegrino committed
  if [ "${run_duration}" != "Cancel" ]; then
    break
  fi

done

# Wait 10 seconds
i=0;
while [ $i -lt 100 ]; do
  echo $i
  sleep 1
done | zenity --progress \
              --auto-close \
              --text="You have 10 seconds to cancel the current operation:\nrunsetup: ${rs_file}\nduration: ${run_duration} minutes" \

# Peak the run number

run_number=`get_run_number`
let "run_duration_seconds = ${run_duration} * 60"
log "Run ${run_number} ready to start. \
Run start time = ${run_start_time}, \
run setup file = ${rs_file}."

daq_log_file=~/.km3_private/daq_${run_number}.log
JGetMessage -H localhost -T MSG -d 3 >${daq_log_file} &
PROCS=(${PROCS[*]} $!)
# Copy selected run setup file, changing the run number
cat ${rs_file} | sed -e "s/RUNNUMBER/${run_number}/" >${DM_DATA_DIR}/dm.detectorfile
dm_driver ${run_duration_seconds} | mono ${DM_EXE_DIR}/DetectorControl.exe --control --reset &
PROCS=(${PROCS[*]} $!)
log "Launching the DAQ with the following command line: daq.sh ${run_number} ${run_start_time} ${run_duration_seconds}"
daq_driver_log_file=~/.km3_private/daq_driver_${run_number}.log
daq.sh ${run_number} ${run_start_time} ${run_duration_seconds} >${daq_driver_log_file} ${DAQ_SERVER} &
PROCS=(${PROCS[*]} $!)
# Put notification mark in the system tray
Carmelo Pellegrino's avatar
Carmelo Pellegrino committed
zenity --notification --text="Run ${run_number} is on going." &
PROCS=(${PROCS[*]} $!)

# Point the browser to the correct address, if required
zenity --question \
       --text="Would you like to open the DetectorManager GUI?" \
Carmelo Pellegrino's avatar
Carmelo Pellegrino committed
       --cancel-label="No, thanks" && firefox http://localhost:1302/ &
PROCS=(${PROCS[*]} $!)

# Wait the end of the run
end_run_time=`nowUTC`
let "end_run_time=${end_run_time} + ${run_duration_seconds}"
while [ `nowUTC` -lt ${end_run_time} ]; do
  let "eta=${end_run_time} - `nowUTC`"
Carmelo Pellegrino's avatar
Carmelo Pellegrino committed
  notify-send "Run ${run_number} is on going. It will end in ${eta} seconds."
log "Run ${run_number} finished."
zenity --info --text="Run ${run_number} finished! The DAQ log file is located in ${daq_log_file}."