#include    "goahead.h"
#include    "libipmi_IPM.h"
#include    "com_IPMIDefs.h"
#include    "ResultUtils.h"
#include    "cJSON.h"
#include    "libipmi_storlead_OEM.h"
#include    "libsensor.h"
#include 	"libipmi_sdr.h"
#include 	"libipmi_sensor.h"
#include    <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>

void setThreshold(Webs *wp){
    uint16_t    wRet = LIBIPMI_E_SUCCESS;
    IPMI20_UDS_SESSION_T    UDSSession;
    char *strUNC = websGetVar(wp, "UNC", NULL);
    char *strUC = websGetVar(wp, "UC", NULL);
    char *strUNR = websGetVar(wp, "UNR", NULL);
    char *strLNC = websGetVar(wp, "LNC", NULL);
    char *strLC = websGetVar(wp, "LC", NULL);
    char *strLNR = websGetVar(wp, "LNR", NULL);
    char *strSensorNumber = websGetVar(wp, "SensorNumber", NULL);
    float UNC = atof(strUNC);
    float UC = atof(strUC);
    float UNR = atof(strUNR);
    float LNC = atof(strLNC);
    float LC = atof(strLC);
    float LNR = atof(strLNR);
    int   SensorNumber = atoi(strSensorNumber);

    printf(" %s, %s %s %s, %s %s %s = %d, %f %f %f %f %f %f \n", strSensorNumber, strUNC, strUC, strUNR, strLNC, strLC, 
    	strLNR,  SensorNumber, UNC, UC, UNR, LNC, LC, LNR );

    uint8_t  *data_buffer = (uint8_t*)walloc(255);
    FullSensorRec_T	*sdr_record = (FullSensorRec_T*) data_buffer;
    uint32_t data_buff_size = 255;//sizeof(data_buffer);
    double val;
    SetSensorThresholdReq_T* pReqSetSensorThreshold;
    SetSensorThresholdRes_T* pResSetSensorThreshold;
    
    pReqSetSensorThreshold = (SetSensorThresholdReq_T*)walloc(sizeof(SetSensorThresholdReq_T));
    if(pReqSetSensorThreshold == NULL)
    {
    	printf("Malloc error!\n");
        wRet = -1;
        websError(wp, 404, "Malloc error!");
    }
    memset(pReqSetSensorThreshold, 0, sizeof(SetSensorThresholdReq_T));

    pResSetSensorThreshold = (SetSensorThresholdRes_T*)walloc(sizeof(SetSensorThresholdRes_T));
    if(pResSetSensorThreshold == NULL)
    {
    	printf("Malloc error!\n");
        wRet = -1;
        websError(wp, 404, "Malloc error!");
    }
    memset(pResSetSensorThreshold, 0, sizeof(pResSetSensorThreshold));
    
    //Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);

    wRet = LIBIPMI_HL_GetSpecificSDR(&UDSSession, data_buffer, &data_buff_size, SensorNumber, DEFAULT_TIMEOUT);
    
    pReqSetSensorThreshold->SensorNum = SensorNumber;
    
    // Follow IPMI Spec. 2.0 Session 35.8, 0x3F for all thresholds
    pReqSetSensorThreshold->SetFlags = 0x3f;    
    pReqSetSensorThreshold->UpperNonCritical = sdr_convert_sensor_value_to_raw(sdr_record, UNC);    
    pReqSetSensorThreshold->UpperCritical = sdr_convert_sensor_value_to_raw(sdr_record, UC);    
    pReqSetSensorThreshold->UpperNonRecoverable = sdr_convert_sensor_value_to_raw(sdr_record, UNR);    
    pReqSetSensorThreshold->LowerNonCritical = sdr_convert_sensor_value_to_raw(sdr_record, LNC);
    pReqSetSensorThreshold->LowerCritical = sdr_convert_sensor_value_to_raw(sdr_record, LC);
    pReqSetSensorThreshold->LowerNonRecoverable = sdr_convert_sensor_value_to_raw(sdr_record, LNR);
    
    // printf("---> Num %d, Name: %s, raw: %d, %d, %d, %d, %d, %d\n", 
    // 	pReqSetSensorThreshold->SensorNum, sdr_record->IDStr,
    // 	pReqSetSensorThreshold->UpperNonCritical, pReqSetSensorThreshold->UpperCritical, 
    // 	pReqSetSensorThreshold->UpperNonRecoverable, pReqSetSensorThreshold->LowerNonCritical, 
    // 	pReqSetSensorThreshold->LowerCritical, pReqSetSensorThreshold->LowerNonRecoverable);

    wRet = IPMICMD_SetSensorThreshold( &UDSSession, pReqSetSensorThreshold, pResSetSensorThreshold, DEFAULT_TIMEOUT);

    //Close session
    LIBIPMI_CloseSession(&UDSSession );

	char *pStr;
    cJSON * root =  cJSON_CreateObject();
    cJSON * data =  cJSON_CreateObject();
    cJSON_AddItemToObject(root, "data", data);//根节点下添加
    cJSON_AddStringToObject(root, "msg", "");
    cJSON_AddNumberToObject(root, "code", 200);
    
   
    pStr = cJSON_PrintUnformatted(root);

//    printf("---> cJSON Str:\n%s\n", pStr);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp); 
    websWrite(wp,"%s", pStr);
    websFlush(wp, 0);
    websDone(wp);

    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);



    if(pReqSetSensorThreshold != NULL)
        wfree(pReqSetSensorThreshold);
    if(pResSetSensorThreshold != NULL)
        wfree(pResSetSensorThreshold);
    if(data_buffer)
    	wfree(data_buffer);

}

void webGetSensorHistory(Webs *wp)
{
    IPMI20_UDS_SESSION_T    UDSSession;
    int i = 0,j=0;
    int wRet = 0;
    sensor_history_st *pSensorHistory = (sensor_history_st *)walloc(sizeof(sensor_history_st));
    char tmpBuf[10];
    char * const historyBuf = walloc(5000);
    char *strBuf = historyBuf;
    uint8_t CARDE_INDEX = 0;
    uint8_t IPMB_ADDR = 0;


    char *strSensorNumber = websGetVar(wp, "SensorNumber", NULL);
    int   SensorNumber = atoi(strSensorNumber);
    
    //Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);

    wRet = LIBIPMI_HL_GetSensorHistory(&UDSSession, pSensorHistory, CARDE_INDEX, SensorNumber, IPMB_ADDR);
    
    if(wRet != 0){
        printf("---> LIBIPMI_HL_GetSensorHistory error, return %#x\n", wRet);
         websError(wp, 404, "Get Sensor History error!");
        return ;
    }

    //Close session
    LIBIPMI_CloseSession(&UDSSession );
    
    for(i=0;i<HISTORY_DATA_SIZE;i++)
    {
        sprintf(tmpBuf, "%0.3f", pSensorHistory->sensor_history[i]);
        j = 0;
        while(tmpBuf[j] != '\0')
        {
            j++;
            if(j>=10)
                break;
        }
        tmpBuf[j] = ',';    //replace '\0' by ','
        j++;        //point to next empty
        sprintf(strBuf, "%s", tmpBuf);
        strBuf += j;
    }
    strBuf--;
    *strBuf = '\0';

//    printf("---> strBuf: %s\n", strBuf);

    char *pStr;
    cJSON * root =  cJSON_CreateObject();

    cJSON_AddNumberToObject(root, "SensorNum", pSensorHistory->sensor_num);
    cJSON_AddStringToObject(root, "SensorName", pSensorHistory->sensor_name);
    cJSON_AddStringToObject(root, "history", historyBuf);
    
   
    pStr = cJSON_PrintUnformatted(root);

//    printf("---> cJSON Str:\n%s\n", pStr);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp); 
    websWrite(wp, pStr);
    websFlush(wp, 0);
    websDone(wp);

    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);


    if(historyBuf)
        wfree(historyBuf);
    if(pSensorHistory)
        wfree(pSensorHistory);


}











