#!/bin/bash

source /opt/tool/pvi_test.sh
source /opt/tool/icm20602.sh
source /opt/tool/get-cpu-temp.sh
source /opt/tool/get-sata-state.sh

PVI_DEFAULT_I=2.06
PVI_DEFAULT_V=12.00
PVI_DEFAULT_P=27.00

PVI_I_THRESHOLD_VAL=1
PVI_V_THRESHOLD_VAL=1
PVI_P_THRESHOLD_VAL=3
PVI_T_THRESHOLD_VAL=2f

ICM_T_THRESHOLD_VAL=55.00
ICM_ACCEL_X_THRESHOLD_VAL=4.00
ICM_ACCEL_Y_THRESHOLD_VAL=4.00
ICM_ACCEL_Z_THRESHOLD_VAL=4.00
ICM_GYRO_X_THRESHOLD_VAL=100.00
ICM_GYRO_Y_THRESHOLD_VAL=100.00
ICM_GYRO_Z_THRESHOLD_VAL=100.00

T_CPU0_VAL=50.00
T_CPU1_VAL=50.00

SATA_ST="PASSED"

pvi_init
icm20602_init

FILE="/opt/storage/log/alarm.log"

#function get_file_size() {
#echo `ls -l ${FILE} | awk '{ print $5 }'`
#}

function pvi_info_handle() {
pvi_temp_get pviTemp
pvi_current_get Isource
pvi_volt_get Vdd
pvi_pratio_get Psource
current_time=$(date "+%Y-%m-%d %H:%M:%S")

Tret=`awk -v T=$pviTemp -v THR=$PVI_T_THRESHOLD_VAL 'BEGIN{
if(T > THR) {
  
  printf("%d", 1)
}
else{
  printf("%d", 0)

}
}'`

if [ $Tret -eq 1 ] ; then 	 
     echo -e "${current_time}\tEMC1701_T: ${pviTemp}" >> $FILE
fi

Iret=`awk -v I=$Isource -v SENS=$PVI_DEFAULT_I -v THR=$PVI_I_THRESHOLD_VAL 'BEGIN{
Iabs=I - SENS
if(Iabs < 0) {
  Iabs=-1*Iabs 
}

if(Iabs > THR) {
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`

if [ $Iret -eq 1 ] ; then 
      echo -e "${current_time}\tI: ${Isource}" >> $FILE
fi
PVI_DEFAULT_I=${Isource}


Vret=`awk -v V=$Vdd -v SENS=$PVI_DEFAULT_V -v THR=$PVI_V_THRESHOLD_VAL 'BEGIN{
Vabs=V - SENS
if(Vabs < 0) {
  Vabs=-1*Vabs 
}

if(Vabs > THR) { 
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`
if [ $Vret -eq 1 ] ; then
    echo -e "${current_time}\tV: ${Vdd}" >> $FILE
fi
PVI_DEFAULT_V=${Vdd}


Pret=`awk -v P=$Psource -v SENS=$PVI_DEFAULT_P -v THR=$PVI_P_THRESHOLD_VAL 'BEGIN{
Pabs=P - SENS
if(Pabs < 0) {
  Pabs=-1*Pabs 
}

if(Pabs > THR) {  
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`
if [ $Pret -eq 1 ] ; then
    echo -e "${current_time}\tP: ${Psource}" >> $FILE
fi
PVI_DEFAULT_P=${Psource}
}

function icm20602_info_handle() {
temp_get  icmTemp
accel_get AccelX AccelY AccelZ
gyro_get  GyroX  GyroY  GyroZ 
current_time=$(date "+%Y-%m-%d %H:%M:%S")

Tret=`awk -v T=$icmTemp -v THR=$ICM_T_THRESHOLD_VAL 'BEGIN{
if(T > THR) { 
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`
if [ $Tret -eq 1 ] ; then 	 
    echo -e "${current_time}\tT: ${icmTemp}" >> $FILE
fi

AXret=`awk -v A=$AccelX -v THR=$ICM_ACCEL_X_THRESHOLD_VAL 'BEGIN{
if(A < 0) {
  A=-1*A
}

if(A > THR) {
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`
if [ $AXret -eq 1 ] ; then
    echo -e "${current_time}\tAccelX: ${AccelX}" >> $FILE
fi

AYret=`awk -v A=$AccelY -v THR=$ICM_ACCEL_Y_THRESHOLD_VAL 'BEGIN{
if(A < 0) {
  A=-1*A
}

if(A > THR) {
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`
if [ $AYret -eq 1 ] ; then 
    echo -e "${current_time}\tAccelY: ${AccelY}" >> $FILE
fi

AZret=`awk -v A=$AccelZ -v THR=$ICM_ACCEL_Z_THRESHOLD_VAL 'BEGIN{
if(A < 0) {
  A=-1*A
}

if(A > THR) {
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`
if [ $AZret -eq 1 ] ; then
    echo -e "${current_time}\tAccelZ: ${AccelZ}" >> $FILE
fi

GXret=`awk -v G=$GyroX -v THR=$ICM_GYRO_X_THRESHOLD_VAL 'BEGIN{
if(G < 0) {
  G=-1*G
}

if(G > THR) { 
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`
if [ $GXret -eq 1 ] ; then
    echo -e "${current_time}\tGyroX: ${GyroX}" >> $FILE
fi

GYret=`awk -v G=$GyroY -v THR=$ICM_GYRO_Y_THRESHOLD_VAL 'BEGIN{
if(G < 0) {
  G=-1*G
}

if(G > THR) {
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`
if [ $GYret -eq 1 ] ; then
    echo -e "${current_time}\tGyroY: ${GyroY}" >> $FILE
fi

GZret=`awk -v G=$GyroZ -v THR=$ICM_GYRO_Z_THRESHOLD_VAL 'BEGIN{
if(G < 0) {
  G=-1*G
}

if(G > THR) {
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`
if [ $GZret -eq 1 ] ; then 
     echo -e "${current_time}\tGyroZ: ${GyroZ}" >> $FILE
fi
	
}

function cpu_tmp_info_handle() {
cpu_temp_get TempCpu0 TempCpu1
current_time=$(date "+%Y-%m-%d %H:%M:%S")

Tcpu0=`awk -v T=$TempCpu0 -v THR=$T_CPU0_VAL 'BEGIN{
if(T > THR) {  
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`
if [ $Tcpu0 -eq 1 ] ; then 
      echo -e "${current_time}\tTempCpu0: ${TempCpu0}" >> $FILE
fi

Tcpu1=`awk -v T=$TempCpu1 -v THR=$T_CPU1_VAL 'BEGIN{
if(T > THR) { 
  printf("%d", 1)
}
else{
  printf("%d", 0)
}
}'`	
if [ $Tcpu1 -eq 1 ] ; then 
     echo -e "${current_time}\tTempCpu1: ${TempCpu1}" >> $FILE
fi
}

function collect_info_handle() {
pvi_info_handle
icm20602_info_handle
cpu_tmp_info_handle

for arg in $*
do
#echo -e "${arg} collect information:" >> $FILE
ST=`sata_health_get ${arg}`

if [ ! "${ST}" = "${SATA_ST}" ] ; then 
	  current_time=$(date "+%Y-%m-%d %H:%M:%S")
      echo -e "${current_time}\t${arg}\tSataHealthSt: ${ST}" >> $FILE
fi

done
}

SataName="/dev/sda /dev/sdb /dev/sdc /dev/sdd /dev/sde"

if [ $# -eq 1 ]; then
    while [ true ]                                                                  
    do                                                                                             
    collect_info_handle ${SataName}
    sleep $1
    done
else
	echo "Usage:saturn-collect-alarm.sh times"
fi



 
