Files
microser/cfg_parse/obs_huaweiyun.cpp

300 lines
8.3 KiB
C++
Raw Permalink Normal View History

2025-01-16 16:17:01 +08:00
/**
* @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"
2025-05-09 16:53:07 +08:00
#include "../json/cjson.h"//WW 2023-08-27新增json解析函数
2025-01-16 16:17:01 +08:00
#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)
{
2025-05-09 16:53:07 +08:00
// curl初始化
2025-01-16 16:17:01 +08:00
CURL* curl = curl_easy_init();
2025-05-09 16:53:07 +08:00
// curl返回值
2025-01-16 16:17:01 +08:00
CURLcode res;
if (curl)
{
2025-05-09 16:53:07 +08:00
//设置curl的请求头
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//不接收响应头数据0代表不接收 1代表接收
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
2025-05-09 16:53:07 +08:00
//设置请求为post请求
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_POST, 1);
2025-05-09 16:53:07 +08:00
//设置请求的URL地址
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
2025-05-09 16:53:07 +08:00
//设置post请求的参数
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//param层
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//设置ssl验证
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
2025-05-09 16:53:07 +08:00
//CURLOPT_VERBOSE的值为1时会显示详细的调试信息
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
2025-05-09 16:53:07 +08:00
//设置数据接收和写入函数
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//设置超时时间
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
printf(">>>TestHuaweiyun Obs Post in curl post\n");
2025-05-09 16:53:07 +08:00
// 开启post请求
2025-01-16 16:17:01 +08:00
res = curl_easy_perform(curl);
2025-05-09 16:53:07 +08:00
// 检查请求是否成功
2025-01-16 16:17:01 +08:00
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)
{
2025-05-09 16:53:07 +08:00
// curl初始化
2025-01-16 16:17:01 +08:00
CURL* curl = curl_easy_init();
2025-05-09 16:53:07 +08:00
// curl返回值
2025-01-16 16:17:01 +08:00
CURLcode res;
if (curl)
{
2025-05-09 16:53:07 +08:00
//设置curl的请求头
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//不接收响应头数据0代表不接收 1代表接收
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
2025-05-09 16:53:07 +08:00
//设置请求为post请求
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_POST, 1);
2025-05-09 16:53:07 +08:00
//设置请求的URL地址
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
2025-05-09 16:53:07 +08:00
//设置post请求的参数
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//param层
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//设置ssl验证
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
2025-05-09 16:53:07 +08:00
//CURLOPT_VERBOSE的值为1时会显示详细的调试信息
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
2025-05-09 16:53:07 +08:00
//设置数据接收和写入函数
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//设置超时时间
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
printf(">>>TestHuaweiyun Obs Post in curl post\n");
2025-05-09 16:53:07 +08:00
// 开启post请求
2025-01-16 16:17:01 +08:00
res = curl_easy_perform(curl);
2025-05-09 16:53:07 +08:00
// 检查请求是否成功
2025-01-16 16:17:01 +08:00
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)
{
2025-05-09 16:53:07 +08:00
// curl初始化
2025-01-16 16:17:01 +08:00
CURL* curl = curl_easy_init();
2025-05-09 16:53:07 +08:00
// curl返回值
2025-01-16 16:17:01 +08:00
CURLcode res;
if (curl)
{
2025-05-09 16:53:07 +08:00
//设置curl的请求头
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//不接收响应头数据0代表不接收 1代表接收
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
2025-05-09 16:53:07 +08:00
//设置请求为post请求
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_POST, 1);
2025-05-09 16:53:07 +08:00
//设置请求的URL地址
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
2025-05-09 16:53:07 +08:00
//设置post请求的参数
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//param层
2025-01-16 16:17:01 +08:00
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);
2025-04-29 15:05:36 +08:00
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, szjson);
2025-05-09 16:53:07 +08:00
//设置ssl验证
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, false);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, false);
2025-05-09 16:53:07 +08:00
//CURLOPT_VERBOSE的值为1时会显示详细的调试信息
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_VERBOSE, 0);
curl_easy_setopt(curl, CURLOPT_READFUNCTION, NULL);
2025-05-09 16:53:07 +08:00
//设置数据接收和写入函数
2025-01-16 16:17:01 +08:00
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);
2025-05-09 16:53:07 +08:00
//设置超时时间
2025-01-16 16:17:01 +08:00
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
printf(">>>TestHuaweiyun Obs Post in curl post\n");
2025-05-09 16:53:07 +08:00
// 开启post请求
2025-01-16 16:17:01 +08:00
res = curl_easy_perform(curl);
2025-05-09 16:53:07 +08:00
// 检查请求是否成功
2025-01-16 16:17:01 +08:00
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