#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    <stdio.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include "libipmi_ChassisDevice.h"


/*控制远程开关机及复位*/
/* 
	192.168.0.207/action/?pwrAction=on
	192.168.0.207/action/?pwrAction=off
	192.168.0.207/action/?pwrAction=reset
	192.168.0.207/action/?pwrAction=cycle
	192.168.0.207/action/?pwrAction=diagnostic
	192.168.0.207/action/?pwrAction=soft
*/
void chassisPwrCtrl(Webs *wp){

	IPMI20_UDS_SESSION_T    UDSSession;
	char cmd = -1;
	char *pwrAction = NULL;
	int ret;
	pwrAction = websGetVar(wp, "pwrAction", NULL);

	if(strcmp(pwrAction, "on") == 0)
		cmd = 1;
	else if(strcmp(pwrAction, "off") == 0)
		cmd = 0;
	else if(strcmp(pwrAction, "reset") == 0)
		cmd = 3;
	else if(strcmp(pwrAction, "cycle") == 0)
		cmd = 2;
	else if(strcmp(pwrAction, "diagnostic")==0)
		cmd = 4;
	else if(strcmp(pwrAction, "soft") == 0)
		cmd = 5;

	printf("Get pwrAction: %s, cmd: %d\n", pwrAction, cmd);

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

	if((cmd >= 0) && (cmd <= 5) )
	{
		//Create session
	    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);
	    IPMICMD_ChassisControl( &UDSSession, DEFAULT_TIMEOUT, cmd);
	    //Close session
	    LIBIPMI_CloseSession(&UDSSession );
	    cJSON_AddStringToObject(data, "status", "success");
	}
	else
	{
		printf("Invalid command %d\n", cmd);
		cJSON_AddStringToObject(data, "status", "fail");
	}

    pStr = cJSON_PrintUnformatted(root);

//    printf("---> cJSON Str: %d\n%s\n", strlen(pStr), pStr);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);
    websWriteEndHeaders(wp);
    ret = websWrite(wp, "%s", pStr);
    if(ret < 0)
    	printf("websWrite error\n");
    
    ret = websFlush(wp, 1);	//wait for all data to be written to the socket 
  	printf("websFlush ret = %d\n", ret);
    websDone(wp);

    // if(pwrAction)
    // 	wfree(pwrAction);
    if(pStr)
        wfree(pStr);
    if(root)
        cJSON_Delete(root);
}


/*控制远程开关机及复位*/
void getChassisStatus(Webs *wp)
{
	chassis_status_T chassisSta;
	IPMI20_UDS_SESSION_T    UDSSession;
	int ret;

	//Create session
    LIBIPMI_CreateSession(&UDSSession, DEFAULT_TIMEOUT);
    LIBIPMI_HL_GetChassisStatus(&UDSSession, DEFAULT_TIMEOUT, &chassisSta);
    //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);
    if(chassisSta.power_state == 0)
    {
    	cJSON_AddStringToObject(data, "powerStatus", "off" );
    }
    else
    {
    	cJSON_AddStringToObject(data, "powerStatus", "on" );	
    }

    pStr = cJSON_PrintUnformatted(root);

//    printf("---> cJSON Str: %d \n%s\n", strlen(pStr), pStr);
    websSetStatus(wp, 200);
    websWriteHeaders(wp, -1, 0);	//Set length to -1 if unknown and transfer-chunk-encoding will be employed.
    websWriteEndHeaders(wp);

    ret = websWrite(wp, "%s", pStr);
    if(ret < 0)
    	printf("websWrite error\n");
    
    ret = websFlush(wp, 1);	//wait for all data to be written to the socket 
    if(ret != 1)
    	printf("websFlush error, ret = %d\n", ret);
    
    websDone(wp);

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