#include <string.h>
#include "zlog.h"
#include "cmt_server.hpp"
#include "local_server.hpp"
#include "jsonparse/communication_cmd.hpp"
#include "common/global.hpp"

extern zlog_category_t *zct;

void LocalServer::HandleTcp_cmd(const char* recv_data,char* send_data,uint8_t& rescmd,int& send_length,int recvbody_length) 
{
    zlog_info(zct,"HandleTcp_cmd");
    PackageHead head;
    memcpy(&head,recv_data,sizeof(PackageHead));
    uint8_t cmd = head.cmd;
    rescmd = head.cmd;
    printf("cmd = %d\n",cmd);
    JsonData jd;
    switch (cmd)
    {
    case 1:{
            Search search;
            search.version = 1;
            memcpy(search.mac,GlobalConfig::MacAddr_G.c_str(),sizeof(search.mac));
            search.gw_type = kGWTDG102;
            send_length = sizeof(Search);
            memcpy(send_data,&search,sizeof(Search));
    }break;
    case kGateWayVersion: {
        jd.CmtCmd_80(send_data,send_length);
    }break;
    case kSensorInfo:{
        char *recv_body = NULL;
        if (recvbody_length > 0){
            recv_body = (char*)malloc(recvbody_length - 4);
            memcpy(recv_body,recv_data + sizeof(PackageHead) + 4,recvbody_length - 4);
        }
        int count = 0;
        memcpy((char*)&count,recv_data + sizeof(PackageHead),4);
        jd.CmtCmd_81(recv_body,count,send_data,send_length);
        if (recv_body){
            free(recv_body);
        }
    }break;
    case kSensorWave:{
        WaveReq wave_req;
        memcpy(&wave_req,recv_data + sizeof(PackageHead),sizeof(WaveReq));
        jd.CmtCmd_82(wave_req.measurement_id,send_data,wave_req.channel,send_length);

    }break;
    case KDownloadConfig:{
        char *recv_body = NULL;
        int count = 0;
        if (recvbody_length > 0){
            recv_body = (char*)malloc(recvbody_length - 4);
            memcpy((char*)&count,(char*)recv_data + sizeof(PackageHead),4);
            memcpy(recv_body,recv_data + sizeof(PackageHead) + 4,recvbody_length - 4);
        }
        jd.CmtCmd_83(recv_body,count,send_data,send_length);
        if (recv_body){
            free(recv_body);
        }
    }break;
    case KUploadConfig:{
        UploadConfigReq upload_config;
        memcpy(&upload_config,recv_data + sizeof(PackageHead),sizeof(UploadConfigReq));
        printf("filename = %s\n",upload_config.filename);
        jd.CmtCmd_84(upload_config.filename,upload_config.md5,send_data,send_length);

    }break;
    case KUpgadeGateway:{
        
        UpgradeGwReq upgrade_gw;
        memcpy(&upgrade_gw,recv_data + sizeof(PackageHead),sizeof(UpgradeGwReq));
        jd.CmtCmd_85(upgrade_gw.filename,upgrade_gw.md5,send_data,send_length);
    }break;
    case KUpgradeSensor:{
        char *recv_body = NULL;
        int count = 0;
        if (recvbody_length > 0){
            recv_body = (char*)malloc(recvbody_length - sizeof(UpgradeSensorReq) - 4);
            memcpy((char*)&count,(char*)recv_data + sizeof(PackageHead) +  sizeof(UpgradeSensorReq) ,4);
            memcpy(recv_body,recv_data + sizeof(PackageHead) + sizeof(UpgradeSensorReq) + 4,recvbody_length - sizeof(UpgradeSensorReq) - 4);
        }
        UpgradeSensorReq upgrade_sensor;
        memcpy(&upgrade_sensor,recv_data + sizeof(PackageHead),sizeof(UpgradeSensorReq));
        jd.CmtCmd_86(recv_body,count,upgrade_sensor.filename,upgrade_sensor.md5,send_data,send_length);
    }break;
    case KEigenvalue:{
        GetEigenvalueReq get_eigenvalue;
        memcpy(&get_eigenvalue,recv_data + sizeof(PackageHead),sizeof(GetEigenvalueReq));
        jd.CmtCmd_87(get_eigenvalue.measurement_id,send_data,send_length);
    }
        break;
    case KUpgradeSensorStop:{
        char *recv_body = NULL;
        if (recvbody_length > 0){
            recv_body = (char*)malloc(recvbody_length - 4);
            memcpy(recv_body,recv_data + sizeof(PackageHead) + 4,recvbody_length - 4);
        }
        int count = 0;
        memcpy((char*)&count,recv_data + sizeof(PackageHead),4);
        jd.CmtCmd_88(recv_body,count,send_data,send_length);
        if (recv_body){
            free(recv_body);
        }
    }
        break;
    default:
        break;
    }
    
}
