307 lines
8.5 KiB
C++
307 lines
8.5 KiB
C++
|
|
/**
|
|||
|
|
* @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<32><37><EFBFBD><EFBFBD>json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
#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)
|
|||
|
|
{
|
|||
|
|
//<2F><>ע<EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Դ<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>cookie<69><65><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
string* str = (string*)stream;
|
|||
|
|
(*str).append((char*)ptr, size * nmemb);
|
|||
|
|
//printf(">>>GetDevice in reply %s\n", (char*)ptr);
|
|||
|
|
//GetCJson(ptr);
|
|||
|
|
return size * nmemb;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
void SendWebAPI_Test(const string strUrl)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
// curl<72><6C>ʼ<EFBFBD><CABC>
|
|||
|
|
CURL* curl = curl_easy_init();
|
|||
|
|
// curl<72><6C><EFBFBD><EFBFBD>ֵ
|
|||
|
|
CURLcode res;
|
|||
|
|
if (curl)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>curl<72><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
|
|||
|
|
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);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊpost<73><74><EFBFBD><EFBFBD>
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>URL<52><4C>ַ
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>post<73><74><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
|
|||
|
|
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<61><6D>
|
|||
|
|
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);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ssl<73><6C>֤
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
|
|||
|
|
|
|||
|
|
//CURLOPT_VERBOSE<53><45>ֵΪ1ʱ<31><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ϸ<EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
|
|||
|
|
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD>պ<EFBFBD>д<EFBFBD>뺯<EFBFBD><EBBAAF>
|
|||
|
|
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);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>ó<EFBFBD>ʱʱ<CAB1><CAB1>
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
|
|||
|
|
|
|||
|
|
printf(">>>TestHuaweiyun Obs Post in curl post\n");
|
|||
|
|
// <20><><EFBFBD><EFBFBD>post<73><74><EFBFBD><EFBFBD>
|
|||
|
|
res = curl_easy_perform(curl);
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
|
|||
|
|
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<72><6C>ʼ<EFBFBD><CABC>
|
|||
|
|
CURL* curl = curl_easy_init();
|
|||
|
|
// curl<72><6C><EFBFBD><EFBFBD>ֵ
|
|||
|
|
CURLcode res;
|
|||
|
|
if (curl)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>curl<72><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
|
|||
|
|
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);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊpost<73><74><EFBFBD><EFBFBD>
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>URL<52><4C>ַ
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>post<73><74><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
|
|||
|
|
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<61><6D>
|
|||
|
|
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);
|
|||
|
|
//string strjson = szjson;
|
|||
|
|
//char* pszEncodeSecret = curl_easy_escape(curl, strclientsecret.c_str(), strclientsecret.length());
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, szjson);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ssl<73><6C>֤
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
|
|||
|
|
|
|||
|
|
//CURLOPT_VERBOSE<53><45>ֵΪ1ʱ<31><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ϸ<EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
|
|||
|
|
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD>պ<EFBFBD>д<EFBFBD>뺯<EFBFBD><EBBAAF>
|
|||
|
|
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);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>ó<EFBFBD>ʱʱ<CAB1><CAB1>
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
|
|||
|
|
|
|||
|
|
printf(">>>TestHuaweiyun Obs Post in curl post\n");
|
|||
|
|
// <20><><EFBFBD><EFBFBD>post<73><74><EFBFBD><EFBFBD>
|
|||
|
|
res = curl_easy_perform(curl);
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
|
|||
|
|
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<72><6C>ʼ<EFBFBD><CABC>
|
|||
|
|
CURL* curl = curl_easy_init();
|
|||
|
|
// curl<72><6C><EFBFBD><EFBFBD>ֵ
|
|||
|
|
CURLcode res;
|
|||
|
|
if (curl)
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>curl<72><6C><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷ
|
|||
|
|
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);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧͷ<D3A6><CDB7><EFBFBD><EFBFBD>0<EFBFBD><30><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> 1<><31><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊpost<73><74><EFBFBD><EFBFBD>
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_POST, 1);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>URL<52><4C>ַ
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>post<73><74><EFBFBD><EFBFBD><EFBFBD>IJ<EFBFBD><C4B2><EFBFBD>
|
|||
|
|
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<61><6D>
|
|||
|
|
cJSON_AddItemToObject(json_param, "object_name", cJSON_CreateString(cloudpath));
|
|||
|
|
cJSON_AddNullToObject(json_param, "localfile_name");
|
|||
|
|
//cJSON_AddItemToObject(json_param, "localfile_name", cJSON_CreateString(localpath));
|
|||
|
|
|
|||
|
|
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);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD>ssl<73><6C>֤
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
|
|||
|
|
|
|||
|
|
//CURLOPT_VERBOSE<53><45>ֵΪ1ʱ<31><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE>ϸ<EFBFBD>ĵ<EFBFBD><C4B5><EFBFBD><EFBFBD><EFBFBD>Ϣ
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
|
|||
|
|
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><DDBD>պ<EFBFBD>д<EFBFBD>뺯<EFBFBD><EBBAAF>
|
|||
|
|
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);
|
|||
|
|
|
|||
|
|
//<2F><><EFBFBD>ó<EFBFBD>ʱʱ<CAB1><CAB1>
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
|
|||
|
|
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
|
|||
|
|
|
|||
|
|
printf(">>>TestHuaweiyun Obs Post in curl post\n");
|
|||
|
|
// <20><><EFBFBD><EFBFBD>post<73><74><EFBFBD><EFBFBD>
|
|||
|
|
res = curl_easy_perform(curl);
|
|||
|
|
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><C7B7>ɹ<EFBFBD>
|
|||
|
|
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
|