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

source Commons.sh
source Drivers/Arduino8relayModul/driver.sh
source Drivers/GWInstekPSW/driver.sh
source Drivers/TPG262GNOME232/driver.sh


#e.g. ./Dirigent.sh -r Chamber PumpingON()
function RotPumpON(){ RelayON 17; }
function RotPumpOFF(){ RelayOFF 17; }
function Vent1ON(){ RelayON 4; }
function Vent1OFF(){ RelayOFF 4; }
function Vent2ON(){ RelayON 12; }
function Vent2OFF(){ RelayOFF 12; }
function TMP1ON(){ RelayON 5; }
function TMP1OFF(){ RelayOFF 5; }
function TMP2ON(){ RelayON 13; }
function TMP2OFF(){ RelayOFF 13; }



function PrepareDischarge()
{ 
    $LogFunctionPassing;
    python3 -c 'from vacuum.working_gas import fill_chamber; fill_chamber(target_pressure=10,max_time=15)'
    echo OK;
}

function WGtest
{
    $LogFunctionStart
    python3 -c 'from vacuum.working_gas import fill_chamber; fill_chamber(target_pressure=10,max_time=15)'
    sleep 5s
    python3 -c 'from vacuum.working_gas import close_valve; close_valve()'
    $LogFunctionEnd

}

function Arming()
{
    $LogFunctionGoingThrough
    echo OK
}


function PostDisch()
{
    $LogFunctionGoingThrough
    python3 -c 'from vacuum.working_gas import close_valve; close_valve()'
    echo OK
}

function RASPsOpenSession()
{
    VacuumLog
}	


function VacuumLog()
{
    while [ 1 ]; do
        for i in `seq 1 5`; do
            ActualChamberPressurePa=`get_chamber_pressure`
            ActualForVacuumPressurePa=`get_forvacuum_pressure`
            #echo $ActualChamberPressurePa > $SHM/$SUBDIR/$ThisDev/ActualChamberPressurePa #NEJDE, ODSTRELUJE sshfs
            echo -ne $ActualChamberPressurePa > $SHM/SessionLogBook/ActualChamberPressurePa
            echo -ne  $ActualForVacuumPressurePa > $SHM/SessionLogBook/ActualForVacuumPressurePa 
            echo -ne  `echo $ActualChamberPressurePa*1000|bc|xargs printf '%4.2f'|sed 's/,/\./g'` > $SHM/SessionLogBook/ActualChamberPressuremPa
            echo `date '+%H:%M:%S'` "Chamber: `echo $ActualChamberPressurePa*1000|bc|xargs printf '%4.2f'` mPa, ForVacuum: $ActualForVacuumPressurePa Pa">> $SHM/SessionLogBook/PressureLog
            #sleep 1 # neni potreba, je to zatim pomale dost
        done
        LogIt "Chamber: `echo $ActualChamberPressurePa*1000|bc|xargs printf '%4.2f'|sed 's/,/\./g'` mPa, ForVacuum: $ActualForVacuumPressurePa Pa"
    done

}	

function Emergency()
{
    $LogFunctionGoingThrough
    echo OK
}	

function PumpingON(){
    if [ -e $SHM/SessionLogBook/SessionDate ]; then
        $LogFunctionStart
        RotPumpON
        sleep 30
        TMPsON
        sleep 20
        VentsON
        $LogFunctionEnd
    else
        critical_error "Please, first open a session"
    fi
}


function PumpingOFF()	
{ 
    $LogFunctionPassing
    VentsOFF
    TMPsOFF
}


function TMPsON()
{
    $LogFunctionPassing
    TMP1ON
    TMP2ON
}

function VentsON()
{
    $LogFunctionPassing
    Vent1ON
    Vent2ON
}

function TMPsOFF()
{ 
    $LogFunctionPassing
    TMP1OFF
    TMP2OFF
}

function VentsOFF()
{   
    $LogFunctionPassing
    Vent1OFF
    Vent2OFF
}

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

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