#include "mqtt_infor_handle.h"
#include "mqtt_init.h"
#include "ip_reader.h"
#include "mqtt_verify.h"
#include "device_fileopen.h"
#include "device_identity.h"
#include "temperature.h"
#include "common.h"
#include "mylog.h"

bool g_verified_mode = TRUE;//控制是否需要验证

int g_heartbeat_count=0;

unsigned char g_valt[4];//存放mqtt接收的tpye，mode等

//心跳发送格式*5/2
void heartbeat_send() {
    float voltage = 0;//INA226_readBusVoltage();
    float current = 0;//INA226_readCurrent();
    //my_zlog_debug("%.2f\n", voltage);
    // if(voltage<=0.5){
    //     voltage=ads1115_read_channel(0)*5/2;
    //     current=ads1115_read_channel(1)*5/2;
    //     //my_zlog_debug("%.2f\n", voltage);
    //     current=(current-voltage)*100;
    // }
    char voltage_str[20];  // 足够存储转换后的字符串的缓冲区
    sprintf(voltage_str, "%.2f", voltage); 
    char current_str[20];  // 足够存储转换后的字符串的缓冲区
    sprintf(current_str, "%.2f", current); 
   
    //alarm_control(voltage);//判断电压警报,在不动时候才判断
    
    /*读取程序版本号*/
    char *version_num = program_version();

    if(heat_tem() !=0 ) {//获取CPU温度
        my_zlog_debug("获取cpu温度失败");
    }
    ipaddr_obtain();//获取ip
    int message=1;

    cJSON *root = cJSON_CreateObject();
    cJSON *body = cJSON_CreateObject();
    cJSON *head = cJSON_CreateObject();

    cJSON_AddStringToObject(body, "ip", g_ip_address);//发送设备ip
    cJSON_AddStringToObject(body, "ID", mqtt_topic_pure_number());//发送设备号
    cJSON_AddStringToObject(body, "V", voltage_str);//心跳发送电压
    cJSON_AddStringToObject(body, "I", current_str);//心跳发送电流
    cJSON_AddStringToObject(body,"Tem",g_temperature);//发送温度
    cJSON_AddStringToObject(body, "version", version_num);//版本号

    cJSON_AddNumberToObject(head, "message_type",message);

    cJSON_AddItemToObject(root, "body", body);
    cJSON_AddItemToObject(root, "head",head);

    char *payload = cJSON_PrintUnformatted(root);
    
    my_zlog_debug("%s",payload);
    mosquitto_publish(mosq, NULL, mqtt_topic_dev2app_number(), strlen(payload), payload, 0, false); 
    mosquitto_publish(mosq, NULL, mqtt_topic_pure_number(), strlen(payload), payload, 0, false); 
    
    cJSON_Delete(root);  // 释放 cJSON 对象
}

//mqtt设备行走驱动函数
void device_driver(){
    // if(AppExit_pin_pwm == 101) speed_change(valt) ;
    // if(AppExit_pin_pwm == 102) car0102_speed_change(valt) ;
    // if(AppExit_pin_pwm == 103 ) car0103_change(valt) ;
    // if(AppExit_pin_pwm == 104 ) car0104_change(valt) ;
    // if(AppExit_pin_pwm == 202) tank0202_change(valt);
    // if(AppExit_pin_pwm == 301) ship_speed_change(valt) ;
    // if(AppExit_pin_pwm == 401) PTZ_pwm_change(valt);
}

//角度发送
void angle_mqtt_send() { 
    // float angle_shot=ads1115_read_channel(2);
    // if(angle_shot>0){  
    //  angle_shot=angle_shot*360/5;
    //  //my_zlog_debug("%.2f",angle_shot);
    //  topic_middle_value();
    // cJSON *root = cJSON_CreateObject();
    // char TOPIC_send_angle[26];
    // double rounded_angle = round(angle_shot * 100) / 100;
    // sprintf(TOPIC_send_angle,"dev_rtinfo/%s",TOPIC3);
    // cJSON_AddStringToObject(root, "type","tank_angle");
    // cJSON_AddNumberToObject(root, "angle",rounded_angle);
    // char *payload = cJSON_PrintUnformatted(root);
    // my_zlog_debug("%s",payload);
    // mosquitto_publish(mosq, NULL, TOPIC_send_angle, strlen(payload), payload, 0, false); 
    // cJSON_Delete(root);  // 释放 cJSON 对象
    // }
   
}

//心跳格式，每5s一次心跳
void mqtt_wirte(){

     if( g_heartbeat_count >= 30) {
        heartbeat_send();
        g_heartbeat_count=0;
     }
    // if( AppExit_pin_pwm == 202) angle_mqtt_send();  

}

//message_type为3，控制pwm  
void message_3(cJSON *body){
    cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl");
    cJSON *pin_setctrl = cJSON_GetObjectItem(body, "pin_setctrl");
    if(pwm_ctrl==NULL||pin_setctrl==NULL){ 
        return ;
    }
    cJSON *mode = cJSON_GetObjectItem(pwm_ctrl, "mode");   //mode=1 速度，mode=2 转向(unsigned char)
    cJSON *type = cJSON_GetObjectItem(pwm_ctrl, "type");
    cJSON *val =   cJSON_GetObjectItem(pwm_ctrl, "val");  //val为pwm的值 0~100(unsigned char)(unsigned char)

    unsigned char modeTemp= mode->valueint;
    unsigned char typeTemp=type->valueint;
    unsigned char valTemp= val->valueint;

    g_valt[0]=typeTemp;
    g_valt[1]=modeTemp;
    g_valt[2]=valTemp;
    gPwmCount = 0;

    my_zlog_debug("typeTemp:%d",g_valt[0]);
    my_zlog_debug("modeTemp:%d",g_valt[1]);
    my_zlog_debug("valTemp:%d",g_valt[2]);

    device_driver();
        
}

void message_4(cJSON *body){//message 为4时候

    cJSON *pwm_ctrl = cJSON_GetObjectItem(body, "pwm_ctrl");
    cJSON *pin_setctrl = cJSON_GetObjectItem(body, "pin_setctrl");
    if(pwm_ctrl==NULL||pin_setctrl==NULL){ 
        return ;
    }
    cJSON *pin = cJSON_GetObjectItem(pin_setctrl, "pin");
    cJSON *val = cJSON_GetObjectItem(pin_setctrl, "val");  //val为pwm的值 0~100(unsigned char)(unsigned char)
    
    unsigned char pinTemp =pin->valueint;
    unsigned char valTemp = val->valueint;

    if(valTemp>=1){
        valTemp=1;
    }

    gPwmCount=0;
    g_valt[0]=0;
    g_valt[1]=pinTemp;
    g_valt[2]=valTemp;

    my_zlog_debug("pinTemp:%d",g_valt[1]);
    my_zlog_debug("valTemp:%d",g_valt[2]);

    //pin_value(g_valt[1],g_valt[2]);
    //pwm_value(g_valt[1],g_valt[2]);
    //if(AppExit_pin_pwm == 202 ) tank_shot_back_stop(g_valt[1],g_valt[2]);
}

//当接收到2时候验证
void message_2_judyverify(cJSON *body){
    if(g_verified_mode == FALSE) {
        //refresh_cam();
        my_zlog_warn("不使用验证");
        return ;
    }

    receive_jwt(body);
   if(g_verify_index == 0) {
    //refresh_cam();   
   } else {
    my_zlog_warn("验证不通过");
   }
}

//当接收到3时候验证
void message_3_judyverify(cJSON *body){
    if(g_verified_mode == FALSE) {
        message_3(body);
        my_zlog_warn("不使用验证");
        return ;
    }
    
    receive_jwt(body);
   if(g_verify_index == 0) {
    message_3(body);
   } else {
    my_zlog_warn("验证不通过");
   }
}

//当接收到4时候验证
void message_4_judyverify(cJSON *body){
    if(g_verified_mode == FALSE) {
        message_4(body);
        my_zlog_warn("不使用验证");
        return ;
    }
   
   receive_jwt(body);
   if(g_verify_index == 0) {
    message_4(body);
   } else {
    my_zlog_warn("验证不通过");
   }
}


//接收到设备改名函数
int device_mqttchange_name(cJSON *device_id){
    // cJSON *id = cJSON_GetObjectItem(device_id, "id");// 提取ID   
    // cJSON *date = cJSON_GetObjectItem(device_id, "date");// 提取日期
    // char *device_change_id=id->valuestring;
    // char *device_change_date=date->valuestring;
    // device_changename_back(device_change_date,device_change_id);
    // // 输出提取的数据
    // my_zlog_debug("设备ID: %s", device_change_id);
    // my_zlog_debug("日期: %s", device_change_date);

    return 0;
}

int device_message_receive(cJSON *json){//接收到的控制设备的mqtt消息
    
    cJSON *head = cJSON_GetObjectItem(json, "head");
    cJSON *message_type = cJSON_GetObjectItem(head, "message_type");

    if (cJSON_IsObject(head)&&cJSON_IsNumber(message_type)) {;
            
        cJSON *body = cJSON_GetObjectItem(json, "body");//提取 body数据段
        if (!cJSON_IsObject(body)) {
            return 1;
        }

        gmessage_type=message_type->valueint;
        my_zlog_debug("message_type: %d",message_type->valueint);
        switch(gmessage_type){
            case 2:
                //message_2_judyverify(body); 
                my_zlog_debug("进入刷新");
                break;
            case 3:
                message_3_judyverify(body);
                my_zlog_debug("进入pwm控制");
                break;
            case 4:
                message_4_judyverify(body);
                my_zlog_debug("进入引脚控制");
                break;
            case 2001: 
                //audioplay_mqtt_receive(body);
                my_zlog_debug("进入音频播放");
                break;
            case 2002:
                //device_mqttchange_name(body);
                my_zlog_debug("进入修改设备号");
                break;
            case 2003:
                //scan_wifi_json();
                my_zlog_debug("进入查询WiFi");
                break;
            case 2004:
                //wifi_change_recmqtt(body);
                my_zlog_debug("进入修改WiFi");
                break;
            case 2006:
                message2006_verify(body);
                my_zlog_debug("进入消息为2006验证");
                break;
            case 2011:
                system("sudo reboot");//重启香橙派
                my_zlog_debug("重启成功");
                break;
            case 2012:
                //refresh_cam();    
                my_zlog_debug("刷新成功");
                break;
            default:
                break;
        }
    }else return 3;     
 
    return 0;
}
