/**
* @file:    $RCSfile: obs_huaweiyun.cpp,v $
* @brief:   $huaweiyun obs include
*
* @version: $Revision: 1.00 $
* @date:    $Date: 2023/09/04 18:34:00 $
* @author:  $Author: wangwei $
* @state:   $State: Exp $
*
* @latest:  $Id: obs_huaweiyun.cpp,v 1.00 2023/09/04 18:34:00 wangwei Exp $
*
*/

using namespace std;

#include <stdio.h>

#include <stdlib.h>
#include <string>

#include "../mms/db_interface.h"
#include "../json/cjson.h"//WW 2023-08-27新增json解析函数
#include "../include/curl/curl.h" 
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

char ACCESS_KEY_ID_OBS[2048] = { "J9GS9EA79PZ60OK23LWP" };
char SECRET_ACCESS_KEY[2048] = { "BirGrAFDSLxU8ow5fffyXgZRAmMRb1R1AdqCI60d" };
char HOST_NAME[2048] = { "obs.cn-east-3.myhuaweicloud.com" };
char BUCKET_NAME_OBS[2048] = { "test-8601" };

size_t req_reply_device(void* ptr, size_t size, size_t nmemb, void* stream)
{
	string* str = (string*)stream;
	(*str).append((char*)ptr, size * nmemb);
	return size * nmemb;
}

void SendWebAPI_Test(const string strUrl)
{

	// curl初始化  
	CURL* curl = curl_easy_init();
	// curl返回值 
	CURLcode res;
	if (curl)
	{
		//设置curl的请求头
		struct curl_slist* header_list = NULL;
		header_list = curl_slist_append(header_list, "Content-Type:application/json;");

		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);

		//不接收响应头数据0代表不接收 1代表接收
		curl_easy_setopt(curl, CURLOPT_HEADER, 0);

		//设置请求为post请求
		curl_easy_setopt(curl, CURLOPT_POST, 1);

		//设置请求的URL地址
		curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
		//设置post请求的参数
		cJSON* json_root = cJSON_CreateObject();
		cJSON* json_param = cJSON_CreateObject();

		cJSON_AddItemToObject(json_root, "code", cJSON_CreateString("putObject"));
		cJSON_AddItemToObject(json_root, "param", json_param);
		//param层
		cJSON_AddItemToObject(json_param, "object_name", cJSON_CreateString("comtrade/pq/Device.xml"));
		cJSON_AddItemToObject(json_param, "localfile_name", cJSON_CreateString("/FeProject/etc/Device_Config.xml"));


		char* szjson = cJSON_Print(json_root);
		printf(">>>json %s\n", szjson);
		//string strjson = szjson;
		//char* pszEncodeSecret = curl_easy_escape(curl, strclientsecret.c_str(), strclientsecret.length());
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, szjson);

		//设置ssl验证
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

		//CURLOPT_VERBOSE的值为1时，会显示详细的调试信息
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);

		curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);

		//设置数据接收和写入函数
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply_device);


		string resPost0;
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&resPost0);

		curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);

		//设置超时时间
		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
		curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);

		printf(">>>TestHuaweiyun Obs Post in curl post\n");
		// 开启post请求
		res = curl_easy_perform(curl);
		// 检查请求是否成功
		if (res != CURLE_OK) {
			printf("Huaweiyun Obs failed res code: " );
		}
		else {
			printf("Huaweiyun Obs success,string %s",resPost0.c_str());
		}

		curl_slist_free_all(header_list);
		free(szjson);
		cJSON_Delete(json_root);
	}
	else
	{
		printf(">>> token curl init failed");
	}
	curl_easy_cleanup(curl);
}

void TestOBS()
{
	SendWebAPI_Test("http://127.0.0.1:8091/powerQuality/PQOBS");
}

void SendWebAPI(const string strUrl, char* localpath, char* cloudpath,const char* code)
{
	// curl初始化  
	CURL* curl = curl_easy_init();
	// curl返回值 
	CURLcode res;
	if (curl)
	{
		//设置curl的请求头
		struct curl_slist* header_list = NULL;
		header_list = curl_slist_append(header_list, "Content-Type:application/json;");

		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);

		//不接收响应头数据0代表不接收 1代表接收
		curl_easy_setopt(curl, CURLOPT_HEADER, 0);

		//设置请求为post请求
		curl_easy_setopt(curl, CURLOPT_POST, 1);

		//设置请求的URL地址
		curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
		//设置post请求的参数
		cJSON* json_root = cJSON_CreateObject();
		cJSON* json_param = cJSON_CreateObject();
		cJSON_AddItemToObject(json_root, "code", cJSON_CreateString(code));
		cJSON_AddItemToObject(json_root, "param", json_param);
		//param层
		cJSON_AddItemToObject(json_param, "object_name", cJSON_CreateString(cloudpath));
		cJSON_AddItemToObject(json_param, "localfile_name", cJSON_CreateString(localpath));

		char* szjson = cJSON_Print(json_root);
		printf(">>>json %s\n", szjson);
		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, szjson);

		//设置ssl验证
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

		//CURLOPT_VERBOSE的值为1时，会显示详细的调试信息
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);

		curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);

		//设置数据接收和写入函数
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply_device);


		string resPost0;
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&resPost0);

		curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);

		//设置超时时间
		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
		curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);

		printf(">>>TestHuaweiyun Obs Post in curl post\n");
		// 开启post请求
		res = curl_easy_perform(curl);
		// 检查请求是否成功
		if (res != CURLE_OK) {
			printf("Huaweiyun Obs failed res code: ");
		}
		else {
			printf("Huaweiyun Obs success,string %s", resPost0.c_str());
		}

		curl_slist_free_all(header_list);
		free(szjson);
		cJSON_Delete(json_root);
	}
	else
	{
		printf(">>> token curl init failed");
	}
	curl_easy_cleanup(curl);
}

void OBSFile(char* localpath,char* cloudpath,const char* code)
{
	SendWebAPI("http://127.0.0.1:8091/powerQuality/PQOBS",localpath,cloudpath,code);
}

void SendWebAPI_del(const string strUrl, char* cloudpath, const char* code)
{
	// curl初始化  
	CURL* curl = curl_easy_init();
	// curl返回值 
	CURLcode res;
	if (curl)
	{
		//设置curl的请求头
		struct curl_slist* header_list = NULL;
		header_list = curl_slist_append(header_list, "Content-Type:application/json;");

		curl_easy_setopt(curl, CURLOPT_HTTPHEADER, header_list);

		//不接收响应头数据0代表不接收 1代表接收
		curl_easy_setopt(curl, CURLOPT_HEADER, 0);

		//设置请求为post请求
		curl_easy_setopt(curl, CURLOPT_POST, 1);

		//设置请求的URL地址
		curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
		//设置post请求的参数
		cJSON* json_root = cJSON_CreateObject();
		cJSON* json_param = cJSON_CreateObject();
		cJSON_AddItemToObject(json_root, "code", cJSON_CreateString(code));
		cJSON_AddItemToObject(json_root, "param", json_param);
		//param层
		cJSON_AddItemToObject(json_param, "object_name", cJSON_CreateString(cloudpath));
		cJSON_AddNullToObject(json_param, "localfile_name");

		char* szjson = cJSON_Print(json_root);
		printf(">>>json %s\n", szjson);

		curl_easy_setopt(curl, CURLOPT_POSTFIELDS, szjson);

		//设置ssl验证
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
		curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);

		//CURLOPT_VERBOSE的值为1时，会显示详细的调试信息
		curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);

		curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);

		//设置数据接收和写入函数
		curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply_device);


		string resPost0;
		curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&resPost0);

		curl_easy_setopt(curl, CURLOPT_NOSIGNAL, 1);

		//设置超时时间
		curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
		curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);

		printf(">>>TestHuaweiyun Obs Post in curl post\n");
		// 开启post请求
		res = curl_easy_perform(curl);
		// 检查请求是否成功
		if (res != CURLE_OK) {
			printf("Huaweiyun Obs failed res code: ");
		}
		else {
			printf("Huaweiyun Obs success,string %s", resPost0.c_str());
		}

		curl_slist_free_all(header_list);
		free(szjson);
		cJSON_Delete(json_root);
	}
	else
	{
		printf(">>> token curl init failed");
	}
	curl_easy_cleanup(curl);
}

void OBSFile_del(char* cloudpath, const char* code)
{
	SendWebAPI_del("http://127.0.0.1:8091/powerQuality/PQOBS", cloudpath, code);
}
#ifdef __cplusplus
}
#endif
