/*
 * kk_PTPO.cpp
 *
 *  Created on: Dec 27, 2018
 *      Author: sds
 */


#include "kk_PTPO.h"

using namespace std;

kk_PTPO* kk_PTPO::ms_pInstance = NULL;

kk_PTPO* kk_PTPO::GetInstance() {
	if (ms_pInstance == NULL) {
		ms_pInstance = new kk_PTPO();
	}
	return ms_pInstance;
}

kk_PTPO::kk_PTPO() {

}
bool kk_PTPO::init(string gw_id){
    this->gw_id= gw_id;

	pthread_t tid;
	pthread_create(&tid, NULL, run0, this);
	return true;
}
void* kk_PTPO::run0(void* opt) {
	kk_PTPO* client = (kk_PTPO*) opt;
	client->run(opt);
	return NULL ;
}
int  kk_PTPO::run(void* opt){
	LOGD("konkeyunzhiyi OTA 升级检测\n");
    while (true) {
        time_t t = time(0);                                                                    \
        struct tm ptm;                                                                         \
        memset(&ptm, 0, sizeof(ptm));                                                          \
        localtime_r(&t, &ptm);  
        LOGD("当前时间小时点:%d \n",ptm.tm_hour);
        if(ptm.tm_hour == 2){ //是否是两点 //
            string respTopic = string("$xlink/ptp/")+gw_id+string("/firmware/check");
            EH::Json::Value Panel_2;
            Panel_2["msg_id"] = 15;
            Panel_2["mac"] = gw_id;
            Panel_2["product_id"] = "1603bcc0b2ca03e91603bcc0b2cab201";
            Panel_2["type"] = 1 ;                           // type 为 wifi，mcu，子设备三个类型（1，2，3）
            Panel_2["current_version"] = MQ_VERSION;
            Panel_2["identify"] = 120;                      //识别码
            MQ_CCU_Adapter::GetInstance()->sendtoRoma(Panel_2,respTopic);  //17. 查询最新的升级任务
        }
        sleep(3602); //一个小时多2秒检测一次，

	}
	return 1;
}
//处理运维Topic 25. 运维功能调用
int kk_PTPO::handle_Ptpo_optcall(string payload,string Topic){
	try{
		LOGI("payload is: %s \n",payload.c_str());
        string respTopic = Topic+"/firmware/check";

		EH::Json::Value jsonObject;
		EH::Json::Reader reader;
		if (!reader.parse(payload, jsonObject) || !jsonObject.isObject()) {
			LOGW("Message format is wrong.primary msg is: %s\n",payload.c_str());
			return -1;
		}

		if(jsonObject["callcode"].asInt() == 1){ //1 执行升级
            LOGI("payload is:111111111111111111111111111111 \n");
            EH::Json::Value Panel_2;
            Panel_2["msg_id"] = jsonObject["callcode"].asInt()+ 1;
            Panel_2["mac"] = jsonObject["mac"];
            Panel_2["product_id"] = jsonObject["product_id"];
            Panel_2["type"] = 1 ;                           // type 为 wifi，mcu，子设备三个类型（1，2，3）
            Panel_2["current_version"] = MQ_VERSION;
            Panel_2["identify"] = 120;                      //待更改
            MQ_CCU_Adapter::GetInstance()->sendtoRoma(Panel_2,respTopic);  //17. 查询最新的升级任务

		}else if(jsonObject["callcode"].asInt() == 2){ 	//2 :更新升级参数，其中param1是pkey，param2是deviceid和当前固件版本
            LOGI("payload is:22222222222222222222222222222 \n");
		}else{
			// LOGW("recv unknown Topic:%s.\n",Sub_Topic.c_str());
		}
	} catch (exception &e) {
		LOGW("%s.\n", e.what());
	}
	return 0;
}

//判定处理是否进行升级
int kk_PTPO::handle_check_result(string payload){
	try{
		LOGI("payload is: %s \n",payload.c_str());

		EH::Json::Value jsonObject;
		EH::Json::Reader reader;
		if (!reader.parse(payload, jsonObject) || !jsonObject.isObject()) {
			LOGW("Message format is wrong.primary msg is: %s\n",payload.c_str());
			return -1;
		}
		if(jsonObject["from_version"].asInt() != jsonObject["target_version"].asInt()
        && jsonObject["from_version_md5"].asString() != jsonObject["target_version_md5"].asString() ){ //版本与MD5都不一样才可以进行升级否则就不升级
            LOGI("payload is:可以进行升级 \n");
            KK_OTA::GetInstance()->init(jsonObject["target_version_url"].asString());
		}else{
			LOGW("信息不对，不进行升级.\n");
		}
	} catch (exception &e) {
		LOGW("%s.\n", e.what());
	}
	return 0;
}