#!/bin/bash
set +x

#检查电子标签检查
source /opt/inspect/inspect_map.sh
CurInspectNum="157"
CurInspectFun="$( GetInspectType $CurInspectNum )"
RESULTFILE="/tmp/tmpResult${CurInspectFun}"
>${RESULTFILE}

LOG_FILE="/var/log/inspect.log"
source /opt/node_type_recognize.sh

function LOG
{
   time=$(date)
   echo [${time}][$$][$CurInspectFun]$@ >> $LOG_FILE
}
function GetProdumctName()
{
    local retryTime=0
    local maxTryTimes=3
    local productName=""
    local productCode=""
    while [ ${retryTime} -lt ${maxTryTimes} ]; do
        productCode=$(ipmitool raw 0x30 0x90 0x05 0x00 0x06 0x01 0x00 0x20)
        if [ -z "${productCode}" ]; then
            LOG "[$FUNCNAME][$LINENO] ERR:Get product code fail, retry time ${retryTime}."
            retryTime=$((retryTime + 1))
            sleep 1
            continue
        fi
        break
    done
    if [ -z "${productCode}" ]; then
        LOG "[$FUNCNAME][$LINENO] ERR:Get product code fail."
        echo ${productName}
        return
    fi
    local index=0
    for code in ${productCode}; do
        if [ ${index} -ne 0 ]; then
            ascii=$(echo $((16#${code})) | awk '{printf("%c", $1)}')
            productName="${productName}${ascii}"
        fi
        index=$((index + 1))
    done
    echo ${productName}
    return
}
function CheckElecLable
{
    local isPass=0
    PRODUCT_NAME=$(GetProdumctName)
    if [ -z "${PRODUCT_NAME}" ]; then
        isPass=1
        LOG "[$FUNCNAME][$LINENO] ERR:Get product name fail."
        echo "[ERR]NodeType:Get product name fail." >>${RESULTFILE} 2>&1
        echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
        return
    fi
    local isRH=0
    local isTS=0
    IsRHNode
    isRH=$?
    IsTSNode
    isTS=$?
    if [ $isRH -ne 0 ] && [ $isTS -ne 0 ];then
        isPass=1
        echo "[ERR]NodeType:$PRODUCT_NAME" >>${RESULTFILE} 2>&1
    else
        echo "NodeType:$PRODUCT_NAME" >>${RESULTFILE} 2>&1
    fi
    echo "${CurInspectFun}_Pass ${isPass}" >>${RESULTFILE} 2>&1
}

CheckElecLable
exit 0