#!/bin/bash
SUBDIR=Infrastructure
ThisDev=Charger

source Commons.sh
source Drivers/UniPi/driver.sh
TheOscilloscope=RigolMSO5104Charger
source Drivers/RigolMSO5/driver.sh
#192.168.2.34


#./Dirigent.sh -r "Charger RelayON 1"
#./Charger.sh -r "RelayON 1"

function BtShortCircuitsDisEngage() { RelayON 3; }	
function BtShortCircuitsEngage() { RelayOFF 3; }
function CdShortCircuitsDisEngage() { RelayON 1; }	
function CdShortCircuitsEngage() { RelayOFF 1; }
function HVlighON() { RelayON 7; }
function HVlighOFF() { RelayOFF 7; }	
#****************************************
function PowerSupplyHVcontactorON() { RelayON 16; }	
function PowerSupplyHVcontactorOFF() { RelayOFF 16; }
#function PowerSupplyHVcontactorON() { RelayON 13; } #Test na Gabove Zdroji	
#function PowerSupplyHVcontactorOFF() { RelayOFF 13; } #Test na Gabove Zdroji
#****************************************

function BtHVrelayON() { RelayON 4 ; }
function BtHVrelayOFF() { RelayOFF 4 ; }
function CdHVrelayON() { RelayON 2 ; }
function CdHVrelayOFF() { RelayOFF 2 ; }


function ChargingResistor_I_DisEngage() { RelayON 15 ; }
function ChargingResistor_I_Engage() { RelayOFF 15 ; }


#function ChargingResistor_II_DisEngage() { RelayON 6 ; }
#function ChargingResistor_II_Engage() { RelayOFF 6 ; }


ChargingFlag=0
ChargingResistors=0

function RASPsOpenSession()
{
#	mkdir -p $SHM
#	sshfs golem@Dirigent:$SHM/ $SHM/
    LoadSetup "C:\RigolDS1.stp"
    echo OK
}

#./Charger.sh -r "PrepareDischarge 10 15"
#./Charger.sh -r "PostDischarge"

function PrepareDischarge()
{
    $LogFunctionStart
    UBtRequest=$1;UCdRequest=$2
    OpenOscDVMchannel  1
     if [ $UBtRequest -gt 0 ]; then 
        BtStatus=Charging;
        BtShortCircuitsDisEngage;
        BtHVrelayON;
        Relax;HVon;Relax
        LogIt "Bt capacitor charging started"
        else
        LogIt "Bt capacitor charging not requested"
        BtStatus=Charged;
    fi 
    if [ $UCdRequest -gt 0 ]; then 
        CdStatus=Charging;
        CdShortCircuitsDisEngage; 
        CdHVrelayON;
        Relax;HVon;Relax
        LogIt "Cd capacitor charging started"
        else
        LogIt "Cd capacitor charging not requested"
        CdStatus=Charged;
    fi
    if [ $UBtRequest -gt 200 ]; then  ChargingResistorsAllOFF; fi
    #BtSlowLine=`echo "0.01725*$UBtRequest + 76.43956"|bc|xargs printf '%4.0f\n'`
    BtSlowLine=98
    LogIt "U_Bt: $UBtRequest, Hrana: $BtSlowLine"
    CdSlowLine=98 
    LogIt "U_Et: $UCdRequest, Hrana: $CdSlowLine"
        
    while [ $BtStatus == Charging ] || [ $CdStatus == Charging ]; do
        PowSupVoltage=`ReadDVMOscilloscope RigolMSO5104Charger`
        LogIt "Charging ... HV=$PowSupVoltage V"
        if [ $PowSupVoltage -ge $UBtRequest ] && [ "$BtStatus" = "Charging" ]; then 
            BtHVrelayOFF;
            BtStatus=Charged;
            LogIt "Bt capacitor charging finished @ $PowSupVoltage V"
        fi
        if [ $PowSupVoltage -ge $UCdRequest ] && [ "$CdStatus" = "Charging" ]; then 
            CdStatus=Charged;
            CdHVrelayOFF;
            LogIt "Cd capacitor charging finished @ $PowSupVoltage V"
        fi
        if [ $PowSupVoltage -gt $(($UBtRequest*$BtSlowLine/100)) ] && [           $ChargingResistors == 0  ] && [ "$BtStatus" = "Charging" ]; then LogIt "Bt slow PowSup";ChargingResistorsAllON;
        fi
        if [ $CdStatus == Charging ] && [ $PowSupVoltage -gt $(($UCdRequest*$CdSlowLine/100)) ] && [ $ChargingResistors == 0  ] && [ "$CdStatus" = "Charging" ]; then LogIt "Cd slow PowSup";ChargingResistorsAllON;fi
        if [ "$CdStatus" = "Charged" ] && [ $ChargingResistors == 1  ] && [ $PowSupVoltage -lt $(($UBtRequest*$BtSlowLine/100)) ]; then LogIt "Bt boost PowSup";ChargingResistorsAllOFF;fi
        sleep 0.1
    done    
    HVoff
    $LogFunctionEnd
    echo OK
}    




function PostDisch()
{
    GoToSafeState
    $LogFunctionGoingThrough
    echo OK
}

function PostDischargeFinals()
{
    $LogFunctionGoingThrough
    DVMDisable
    GetOscScreenShot > $SHM0/$SUBDIR/$ThisDev/ScreenShot.png
    killall netcat 
    echo OK
}

function GoToSafeState()
{
    HVoff; 
    CdHVrelayOFF;
    BtHVrelayOFF;
    Relax;
    CdShortCircuitsEngage;
    BtShortCircuitsEngage;
}

function Emergency()
{
    GoToSafeState
    $LogFunctionStart
    echo OK
}	

function HVon()
{
    $LogFunctionPassing
    HVlighON
    PowerSupplyHVcontactorON
    Relax
}	

function HVoff()
{
    PowerSupplyHVcontactorOFF
    $LogFunctionPassing
    HVlighOFF
    mRelax
	ChargingResistorsAllON
	
}


function ChargingResistorsAllOFF()
{
    $LogFunctionStart
    ChargingResistors=0
    ChargingResistor_I_DisEngage
    #ChargingResistor_II_DisEngage
    echo "Fast">$SHM/Charging
}	

function ChargingResistorsAllON()
{
    $LogFunctionStart
    ChargingResistors=1
    ChargingResistor_I_Engage
    #ChargingResistor_II_Engage
    echo "Slow">$SHM/Charging
}



TASK=$1
COMMANDLINE=`echo $@|sed 's/-r //g'`

case "$TASK" in
   "")
      RETVAL=1
      ;;
      --raw_command|-r)
      $COMMANDLINE
      ;;
      --rsync)
      RsyncDeviceFromDirigent
      ;;
esac
