#!/bin/sh

######################################################################
# 
# Script for power cycling OTRDMP GigE camera (GX1050) and reapply
# SIOC (sioc-dmp1-pm10) settings (no reboot). SIOC settings must be
# restored because the camera comes back from the power cycle with
# firmware default values.
#
######################################################################

done="false"
i=0
p=0

# Time to wait between camera OFF and camera ON
waitTime=5
remainingTime=$waitTime
step=0
connectCount=0
connectTimeout=30
iocReboot=0

(while [ $done != "true" ]
do
# Disable camera
  if [ $i == "0" ]; then
      echo "# Disabling OTRDMP camera"; 
      caput OTRS:DMP1:695:Acquisition 0
      caput OTRS:DMP1:695:Acquisition.DISA 1
      step=$[$step + 1]
# Disconnect ASYN
  elif [ $i == "1" ]; then
      caput OTRS:DMP1:695:AsynIO.CNCT 0
      echo "# Disconnecting ASYN";
      step=$[$step + 1]
# Turn camera off
  elif [ $i == "2" ]; then
      echo "# Powering OTRDMP camera OFF";
      caput ACSW:DMP1:NW01:3POWEROFF 1;
      sleep 1
      step=$[$step + 1]
# Give a few seconds for the camera
  elif [ $i -lt $[$waitTime + $step] ]; then
      echo "# Cooling down camera, waiting $remainingTime seconds ... ";
      remainingTime=`expr $remainingTime - 1`
      sleep 1
# Turn it back on
  elif [ $i == "$[$waitTime + $step]" ]; then
      echo "# Powering OTRDMP camera ON";
      caput ACSW:DMP1:NW01:3POWERON 1;
      sleep 1
# Connect the SIOC to camera
  elif [ $i == $[$waitTime + $step + 1] ]; then
      connected=`caget -nt OTRS:DMP1:695:AsynIO.CNCT`
      while [ "$connected" == "0" ]; do
	  echo "# Waiting SIOC to reconnect ($connectCount seconds elapsed)";
	  connected=`caget -nt OTRS:DMP1:695:AsynIO.CNCT`
	  connectCount=$[$connectCount + 1]
	  if [ $connectCount -gt $connectTimeout ]; then
	      if [ "$iocReboot" == 0 ]; then
		  iocReboot=1
		  echo "# SIOC is failing to autoconnect, rebooting SIOC";
		  caput SIOC:DMP1:PM10:SYSRESET 1;
		  sleep 5
		  echo "# Waiting for SIOC-DMP1-PM10...";
		  
		  isSIOCReady=`caget -tn SIOC:DMP1:PM10:HEARTBEATSUM`
		  while [ $isSIOCReady != 0 ]; do
		      isSIOCReady=`caget -tn SIOC:DMP1:PM10:HEARTBEATSUM`
		  done
	      fi
	  fi
	  sleep 1
      done
      echo "# SIOC reconnected"
      caput OTRS:DMP1:695:AsynIO.CNCT 1
      sleep 1
# Restore exposure time, data type, trigger mode (Sync in 1)
# and ROI configuration
  elif [ $i == $[$waitTime + $step + 2] ]; then

      echo "# Disabling OTRDMP camera"; 
      caput OTRS:DMP1:695:Acquisition 0
      
      echo "# Disabling acquisition"
      caput OTRS:DMP1:695:Acquisition.DISA 1

      acquireTime=`caget -t OTRS:DMP1:695:AcquireTime`
      echo "# Restoring acquire time to $acquireTime"
      caput OTRS:DMP1:695:AcquireTime $acquireTime
      sleep 1

      # Force TriggerMode to 'Sync in 1'
      caput OTRS:DMP1:695:TriggerMode 1

      dataType=`caget -t OTRS:DMP1:695:DataType`
      caput OTRS:DMP1:695:DataType $dataType

      minX=`caget -t OTRS:DMP1:695:MinX`
      minY=`caget -t OTRS:DMP1:695:MinY`
      sizeX=`caget -t OTRS:DMP1:695:SizeX`
      sizeY=`caget -t OTRS:DMP1:695:SizeY`

      echo "# Restoring ROI $sizeX x $sizeY"
      caput OTRS:DMP1:695:MinX $minX
      caput OTRS:DMP1:695:MinY $minY
      caput OTRS:DMP1:695:SizeX $sizeX
      caput OTRS:DMP1:695:SizeY $sizeY
      caput OTRS:DMP1:695:BinX.PROC 1
      caput OTRS:DMP1:695:BinY.PROC 1
      sleep 1

#      caput SIOC:DMP1:PM10:SYSRESET 1;
# done! Re-enable acquisition
  else
      caput OTRS:DMP1:695:Acquisition.DISA 0

      echo "# Enabling acquisition... ";
      caget OTRS:DMP1:695:Acquisition &> /dev/null;
      while [ "$?" == "1" ]; do
	  caget OTRS:DMP1:695:Acquisition &> /dev/null;
      done
      caput OTRS:DMP1:695:Acquisition 1;
      done="true"
  fi
  i=$[$i + 1]
  p=$[$p + 1]
done)
# | zenity --progress --pulsate --auto-close --title "OTRDMP camera only reboot " --text="test"

# Increase the number this script has been called
# at SIOC:SYS0:ML03:AO001
cnt=`caget -t SIOC:SYS0:ML03:AO999`
cnt=$((cnt+1))
caput SIOC:SYS0:ML03:AO999 $cnt > /dev/null

hst=`hostname`
dat=`date`
str="OTRDMP rebooted at $dat from $hst"
caput -S SIOC:SYS0:ML03:CA999 $str > /dev/null

exit 0
