/******************************************************************
** Copyright (c) 2009  Wuhan xiyun Technology Co., Ltd .
** FileName:	handle.ack.c
** Author:	 	chw
** Mail:		
** Editor:		chw
** Date:		2020-12-03 13:34
** Version: 	2020-12-03
** File Description:  处理mqtt的消息确认 包含:1.控制类消息确认 2.配置类消息确认
** Param explain: char* mac : 网关MAC地址
   char* subMac             ：子节点MAC地址
  MQTTAsync_message *pubmsg : 消息结构体
  MYSQL *_db                ：数据库连接
  char *topicName           : 消息主题
******************************************************************/

//------  控制指令确认  ------------------------------------------------------

//远程开关控制确认
int  do_ACK_CTR_RSC(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);

    //校验长度
    if(CheckMsgLength(topicName, pubmsg,20))
        return 1;

    char update_sql[256] = {0};
    char update_sql2[256] = {0};
    char mac[25] = {0};     //网关mac
    char subMac[13] = {0}; //节点mac

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    Hex2Str((const char*)(data+12),subMac,6);
    


    if(strcmp(subMac,"0F0000000001")==0)//网关
    {
        log("↑ 收到远程开关控制确认,网关:%s,类型:%d,动作:%d \n",mac,data[18],data[19]);

        sprintf(update_sql,"update dev_info_gateway set ack_switch = 1 where device_mac = '%s'",mac);

        //更新dev_status表网关开关状态
        sprintf(update_sql2,"update dev_status set current_on_off = %d,update_time = now() where device_mac = '%s'",data[19],mac);
    }
    else //节点
    {
        log("↑ 收到远程开关控制确认,网关:%s,节点:%s,节点类型:%d,动作:%d \n",mac,subMac,data[18],data[19]);

        sprintf(update_sql,"update dev_info_node set ack_switch = 1 where device_mac = '%s'",subMac);

        //更新dev_status表节点开关状态
        sprintf(update_sql2,"update dev_status set current_on_off = %d,update_time = now() where device_mac = '%s'",data[19],subMac);
    }
    excuteSql(_db,update_sql);
    excuteSql(_db,update_sql2);
    
    return 0;
}

//远程开关编组控制确认
int  do_ACK_CTR_RGC(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);

    //校验长度
    if(CheckMsgLength(topicName, pubmsg,15))
        return 1;

    char update_sql[256] = {0};
    char update_sql2[256] = {0};
    char mac[25] = {0};     //网关mac

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);

    log("↑ 收到远程开关编组控制确认,网关:%s,组号:%d,类型:%d,动作:%d \n",mac,data[12],data[13],data[14]);

    //更新dev_status表节点开关状态
    sprintf(update_sql,"update dev_status set current_on_off = %d,update_time = now() where "
            "device_mac in(select device_mac from dev_info_node where gateway_mac = '%s' and device_group = %d)",data[14],mac,data[12]);

    sprintf(update_sql2,"update dev_info_gateway set ack_group_switch = 1 where device_mac = '%s'",mac);

    excuteSql(_db,update_sql);
    excuteSql(_db,update_sql2);
    
    return 0;
}


//------  配置指令确认  ------------------------------------------------------


//备份服务器配置确认
 int  do_ACK_CFG_BKS(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
 {
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);

    //校验长度
    if(CheckMsgLength(topicName, pubmsg,18))
        return 1;

    char update_sql[512] = {0};
    char mac[25] = {0};
    char ip[4] = {0};
    int port = 0;


    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    memcpy(ip,data+12,4);
    
    port = (data[16]<<16) + (data[17]<<24);
    log("↑ 收到备份服务器配置确认,网关:%s \n",mac);

    sprintf(update_sql,"update dev_info_gateway set ack_ip = 1 where device_mac = '%s'",
            mac);
    excuteSql(_db,update_sql);

    return 0;
 }

//上传间隔配置确认
int  do_ACK_CFG_ULI(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);

    //校验长度
    if(CheckMsgLength(topicName, pubmsg,14))
        return 1;

    char update_sql[512] = {0};
    char mac[25] = {0};

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    log("↑ 收到上传间隔配置确认,网关:%s,单位:%d,数值:%d \n",mac,data[12],data[13]);

    sprintf(update_sql,"update dev_info_gateway set ack_upload_interval = 1 where device_mac = '%s'",
            mac);
    excuteSql(_db,update_sql);

    return 0;
}


//开关时间段确认
int  do_ACK_CFG_STS(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);

    //校验长度
    if(CheckMsgLength(topicName, pubmsg,23))
        return 1;

    char update_sql[512] = {0};
    char mac[25] = {0};     //网关mac
    char subMac[13] = {0}; //节点mac   

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    Hex2Str((const char*)(data+12),subMac,6);
    
    if(strcmp(subMac,"0F0000000001")==0)//网关
    {
        log("↑ 收到开关时间段确认,网关:%s,类型:%d \n",mac,data[18]);
        sprintf(update_sql,"update dev_info_gateway set ack_switch_time = 1 where device_mac = '%s'",
            mac);
    }
    else //节点
    {
        log("↑ 收到开关时间段确认,网关:%s,节点:%s,节点类型:%d \n",mac,subMac,data[18]);
        sprintf(update_sql,"update dev_info_node set ack_switch_time = 1 where device_mac = '%s'",
            subMac);
    }
    excuteSql(_db,update_sql);
    
    return 0;
}

//额定电压设置确认
int  do_ACK_CFG_SRV(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);
    
    //校验长度
    if(CheckMsgLength(topicName, pubmsg,21))
        return 1;

    char update_sql[512] = {0};
    char mac[25] = {0};     //网关mac
    char subMac[13] = {0}; //节点mac
    int rate_voltage = 0;

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    Hex2Str((const char*)(data+12),subMac,6);
    ((char*)&rate_voltage)[0] = data[20];
    ((char*)&rate_voltage)[1] = data[19];

    if(strcmp(subMac,"0F0000000001")==0)//网关
    {
        log("↑ 收到额定电压设置确认,网关:%s,类型:%d,阈值:%d \n",mac,data[18],rate_voltage);
        sprintf(update_sql,"update dev_info_gateway set ack_rated_voltage = 1 where device_mac = '%s'",mac);
    }
    else //节点
    {
        log("↑ 收到额定电压设置确认,网关:%s,节点:%s,节点类型:%d,阈值:%d \n",mac,subMac,data[18],rate_voltage);
        sprintf(update_sql,"update dev_info_node set ack_rated_voltage = 1 where device_mac = '%s'",subMac);
    }
    excuteSql(_db,update_sql);
    
    return 0;
}


//额定电流配置确认
int  do_ACK_CFG_SRC(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);

    //校验长度
    if(CheckMsgLength(topicName, pubmsg,23))
        return 1;

    char update_sql[512] = {0};
    char mac[25] = {0};     //网关mac
    char subMac[13] = {0}; //节点mac
    int rate_current = 0;

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    Hex2Str((const char*)(data+12),subMac,6);
    ((char*)&rate_current)[0] = data[22];
    ((char*)&rate_current)[1] = data[21];
    ((char*)&rate_current)[2] = data[20];
    ((char*)&rate_current)[3] = data[19];
    
    if(strcmp(subMac,"0F0000000001")==0)//网关
    {
        log("↑ 收到额定电流设置确认,网关:%s,类型:%d,阈值:%d \n",mac,data[18],rate_current);
        sprintf(update_sql,"update dev_info_gateway set ack_rated_current = 1 where device_mac = '%s'",mac);
    }
    else //节点
    {
        log("↑ 收到额定电流设置确认,网关:%s,节点:%s,节点类型:%d,阈值:%d \n",mac,subMac,data[18],rate_current);
        sprintf(update_sql,"update dev_info_node set ack_rated_current = 1 where device_mac = '%s'",subMac);
    }
    excuteSql(_db,update_sql);
    
    return 0;
}


//开关控制模式确认
int  do_ACK_CFG_SCM(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);

    //校验长度
    if(CheckMsgLength(topicName, pubmsg,20))
        return 1;

    char update_sql[512] = {0};
    char mac[25] = {0};     //网关mac
    char subMac[13] = {0}; //节点mac

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    Hex2Str((const char*)(data+12),subMac,6);

    if(strcmp(subMac,"0F0000000001")==0)//网关
    {
        log("↑ 收到开关控制模式设置确认,网关:%s,类型:%d,控制模式:%d \n",mac,data[18],data[19]);
        sprintf(update_sql,"update dev_info_gateway set ack_switch_mode = 1 where device_mac = '%s'",mac);
    }
    else //节点
    {
        log("↑ 收到开关控制模式设置确认,网关:%s,节点:%s,节点类型:%d,控制模式:%d \n",mac,subMac,data[18],data[19]);
        sprintf(update_sql,"update dev_info_node set ack_switch_mode = 1 where device_mac = '%s'",subMac);
    }
    excuteSql(_db,update_sql);
    
    return 0;
}


//传感器阈值配置确认
int  do_ACK_CFG_SAT(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    UNUSED(topicName);
    char update_sql[512] = {0};
    char mac[25] = {0};

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    log("↑ 收到传感器阈值配置确认,网关:%s \n",mac);

    sprintf(update_sql,"update dev_info_gateway set ack_sensor_threshold = 1 where device_mac = '%s'",
            mac);
    excuteSql(_db,update_sql);

    return 0;
}

//调光节点分组设置确认
int  do_ACK_CFG_DGS(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);

    //校验长度
    if(CheckMsgLength(topicName, pubmsg,34))
        return 1;

    char update_sql[512] = {0};
    char mac[25] = {0};     //网关mac
    char subMac[13] = {0}; //节点mac

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    Hex2Str((const char*)(data+12),subMac,6);
    log("↑ 收到调光节点分组设置确认,网关:%s,节点mac:%s,组号:%d \n",mac,subMac,data[19]);

    sprintf(update_sql,"update dev_info_node set ack_group_set = 1 where device_mac = '%s'",subMac);

    excuteSql(_db,update_sql);
    
    return 0;
}

//子节点版本号回复
int  do_ACK_CFG_GNV(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);

    //校验长度
    if(CheckMsgLength(topicName, pubmsg,23))
        return 1;

    char update_sql[512] = {0};
    char mac[25] = {0};     //网关mac
    char subMac[13] = {0}; //节点mac
    char node_version[10] = {0};

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    Hex2Str((const char*)(data+12),subMac,6);
    memcpy(node_version,data+18,5);
    log("↑ 收到子节点版本号回复,网关:%s,节点:%s,版本号:%s \n",mac,subMac,node_version);

    sprintf(update_sql,"update dev_info_node set device_version = '%s',version_update_time = now() where device_mac = '%s'",node_version,subMac);

    excuteSql(_db,update_sql);
    
    return 0;
}

//远程手动调光确认
int  do_ACK_CTR_RDC(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);

    //校验长度
    if(CheckMsgLength(topicName, pubmsg,15))
        return 1;

    char update_sql[512] = {0};
    char mac[25] = {0};     //网关mac
    int group,luminuance,dimming_type;

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    group = data[12];
    luminuance = data[13];
    dimming_type = data[14];
    log("↑ 收到远程手动调光确认,网关:%s,组号:%d,亮度:%d,调光类型:%d  \n",mac,group,luminuance,dimming_type);

    sprintf(update_sql,"update dev_dimming_group set current_brightness = %d,ack_group_dimming = 1 where gateway_mac = '%s' and device_group = %d",luminuance,mac,group);

    excuteSql(_db,update_sql);
    
    return 0;
}

//远程升级返回确认
int  do_ACK_OTA(char *topicName, MQTTAsync_message *pubmsg, MYSQL *_db)
{
    //debug("topicName=%s payloadlen=%d\n", topicName, pubmsg->payloadlen);
    //校验长度
    UNUSED(topicName);
    
    char update_sql[512] = {0};
    char mac[25] = {0};     //网关mac
    char error_code;    //错误码 00：文件接收OK，FE:旧文件不对，01:文件长度不对，02：文件crc32值不对
    char app_name[256] = {0};   //本地当前APP 名称 只有在旧文件错误（FE）时，才有这部分

    int status = 0;
    char comment[256] = {0};

    unsigned char* data = pubmsg->payload;
    Hex2Str((const char*)data,mac,12);
    error_code = (char)data[12];
    status = (int)error_code;
    if(error_code == (char)0xFE)//旧文件不对
    {
        strcpy(app_name,(char*)data+13);
        sprintf(comment,"error_code:0xFE,app_name:%s",app_name);
        status = 3;
    }
    else if(error_code == (char)0x00)//文件接收OK
    {
        sprintf(comment,"error_code:0x00,文件接收ok");
        status = 2;
    }
    else if(error_code == (char)0x01)//文件长度不对
    {
        sprintf(comment,"error_code:0x01,文件长度不对");
        status = 3;
    }
    else if(error_code == (char)0x02)//文件crc32值不对
    {
        sprintf(comment,"error_code:0x02,文件crc32值不对");
        status = 3;
    }
    log("↑ 收到远程升级返回确认,网关:%s,状态or错误码:%02x,本地当前APP:%s  \n",mac,error_code,app_name);

    sprintf(update_sql,"update dev_status_upgrade set status = %d,comment = '%s',update_time = now() where gateway_mac = '%s'",status,comment,mac);
    log(update_sql);
    excuteSql(_db,update_sql);
    
    return 0;
}