Files
microser/cfg_parse/nacos.cpp

660 lines
25 KiB
C++
Raw Permalink Normal View History

2025-01-16 16:17:01 +08:00
/**
* @file: $RCSfile: nacos.cpp,v $
* @brief: $aliyun datahub include
*
* @version: $Revision: 1.00 $
* @date: $Date: 2023/10/24 18:34:00 $
* @author: $Author: wangwei $
* @state: $State: Exp $
*
* @latest: $Id: nacos.cpp,v 1.00 2023/10/24 18:34:00 caizhouyu Exp $
*
*/
using namespace std;
#include <stdio.h>
#include <iostream>
#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 */
2025-04-29 15:05:36 +08:00
size_t req_reply_nacos(void* ptr, size_t size, size_t nmemb, void* stream)
{
string* str = (string*)stream;
(*str).append((char*)ptr, size * nmemb);
return size * nmemb;
}
void read_nacos_param(const char* ptr, char* postgres_uid, char* postgres_pwd, char* web_clientid, char* web_clientsecret) {
//cout << ">>>GetDevice in reply" << (char*)ptr;
int flag = 1;
2025-05-09 16:53:07 +08:00
cJSON* json = cJSON_Parse((char*)ptr); //json格式序列化
2025-04-29 15:05:36 +08:00
cJSON* json_node;
cJSON* json_param;
if (json == NULL) {
printf("nacos error %s\n", cJSON_GetErrorPtr());
2025-01-16 16:17:01 +08:00
}
2025-04-29 15:05:36 +08:00
else {
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "status"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || json_node->valuestring == NULL || json_node->valuestring == "/0" || json_node->valuestring == "" || strcmp(json_node->valuestring, "000000") != 0) {
printf("status error :%s\n", cJSON_GetErrorPtr());
flag = 0;
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || json_node->valuestring == NULL || json_node->valuestring == "/0" || json_node->valuestring == "" || strcmp(json_node->valuestring, "success") != 0) {
printf("errors get falie: %s\n", cJSON_GetErrorPtr());
flag = 0;
}
if (flag) {
printf("read nacos success\n");
2025-05-09 16:53:07 +08:00
json_param = cJSON_GetObjectItem(json, "param"); //获取result
json_node = cJSON_GetObjectItem(json_param, "postgres_uid"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node != NULL && strcmp(json_node->valuestring, "null") != 0) {
strcpy(postgres_uid, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "postgres_pwd"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node != NULL && strcmp(json_node->valuestring, "null") != 0) {
strcpy(postgres_pwd, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "web_clientid"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node != NULL && strcmp(json_node->valuestring, "null") != 0) {
strcpy(web_clientid, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "web_clientsecret"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node != NULL && strcmp(json_node->valuestring, "null") != 0) {
strcpy(web_clientsecret, json_node->valuestring);
2025-01-16 16:17:01 +08:00
}
}
}
2025-04-29 15:05:36 +08:00
cJSON_Delete(json);
}
void releaseMemory(char** ptr) {
if (*ptr != NULL) {
2025-05-09 16:53:07 +08:00
free(*ptr); // 释放内存
2025-01-16 16:17:01 +08:00
}
2025-04-29 15:05:36 +08:00
}
void SendWebAPI_Nacos(const string strUrl, const char* code, char* postgres_uid, char* postgres_pwd, char* web_clientid, char* web_clientsecret)
{
2025-05-09 16:53:07 +08:00
// curl初始化
2025-04-29 15:05:36 +08:00
CURL* curl = curl_easy_init();
2025-05-09 16:53:07 +08:00
// curl返回值
2025-04-29 15:05:36 +08:00
CURLcode res;
if (curl)
2025-01-16 16:17:01 +08:00
{
2025-05-09 16:53:07 +08:00
//设置curl的请求头
2025-04-29 15:05:36 +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-04-29 15:05:36 +08:00
curl_easy_setopt(curl, CURLOPT_HEADER, 0);
2025-05-09 16:53:07 +08:00
//设置请求为post请求
2025-04-29 15:05:36 +08:00
curl_easy_setopt(curl, CURLOPT_POST, 1);
2025-05-09 16:53:07 +08:00
//设置请求的URL地址
2025-04-29 15:05:36 +08:00
curl_easy_setopt(curl, CURLOPT_URL, strUrl.c_str());
2025-05-09 16:53:07 +08:00
//设置post请求的参数
2025-04-29 15:05:36 +08:00
cJSON* json_root = cJSON_CreateObject();
cJSON_AddItemToObject(json_root, "code", cJSON_CreateString(code));
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-04-29 15:05:36 +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-04-29 15:05:36 +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-04-29 15:05:36 +08:00
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply_nacos);
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-04-29 15:05:36 +08:00
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
printf(">>>Test nacos Post in curl post\n");
2025-05-09 16:53:07 +08:00
// 开启post请求
2025-04-29 15:05:36 +08:00
res = curl_easy_perform(curl);
2025-05-09 16:53:07 +08:00
// 检查请求是否成功
2025-04-29 15:05:36 +08:00
if (res != CURLE_OK) {
printf("nacos failed res code: ");
2025-01-16 16:17:01 +08:00
}
2025-04-29 15:05:36 +08:00
else {
printf("nacos success,string %s", resPost0.c_str());
2025-05-09 16:53:07 +08:00
//后期添加webapi返回值判断
2025-04-29 15:05:36 +08:00
read_nacos_param(resPost0.c_str(), postgres_uid, postgres_pwd, web_clientid, web_clientsecret);
2025-01-16 16:17:01 +08:00
}
2025-04-29 15:05:36 +08:00
curl_slist_free_all(header_list);
free(szjson);
cJSON_Delete(json_root);
2025-01-16 16:17:01 +08:00
}
2025-04-29 15:05:36 +08:00
else
2025-01-16 16:17:01 +08:00
{
2025-04-29 15:05:36 +08:00
printf(">>> nacos curl init failed");
}
curl_easy_cleanup(curl);
}
void SendWebAPI_Nacos_Ptr(const string strUrl, const char* code, char** ptr)
{
2025-05-09 16:53:07 +08:00
// curl初始化
2025-04-29 15:05:36 +08:00
CURL* curl = curl_easy_init();
2025-05-09 16:53:07 +08:00
// curl返回值
2025-04-29 15:05:36 +08:00
CURLcode res;
if (curl)
{
char url[100];
sprintf(url, "%s?code=%s", strUrl.c_str(), code);
printf(">>>json %s\n", url);
2025-05-09 16:53:07 +08:00
// 设置URL
2025-04-29 15:05:36 +08:00
curl_easy_setopt(curl, CURLOPT_URL, url);
2025-05-09 16:53:07 +08:00
//设置数据接收和写入函数
2025-04-29 15:05:36 +08:00
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply_nacos);
string resPost0;
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&resPost0);
2025-05-09 16:53:07 +08:00
//设置超时时间
2025-04-29 15:05:36 +08:00
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
2025-05-09 16:53:07 +08:00
// 开启post请求
2025-04-29 15:05:36 +08:00
res = curl_easy_perform(curl);
2025-05-09 16:53:07 +08:00
// 检查请求是否成功
2025-04-29 15:05:36 +08:00
if (res != CURLE_OK) {
printf("nacos failed res code: ");
}
else {
printf(">>> nacos return str:%s \n", resPost0.c_str());
2025-05-09 16:53:07 +08:00
*ptr = (char*)malloc(strlen(resPost0.c_str()) + 1); // 分配足够的内存空间
2025-04-29 15:05:36 +08:00
if (*ptr != NULL) {
strcpy(*ptr, resPost0.c_str());
2025-01-16 16:17:01 +08:00
}
else {
2026-01-06 10:23:43 +08:00
2025-04-29 15:05:36 +08:00
printf("Memory allocation failed!\n");
2025-01-16 16:17:01 +08:00
}
}
}
2025-04-29 15:05:36 +08:00
else
2025-01-16 16:17:01 +08:00
{
2025-04-29 15:05:36 +08:00
printf(">>> nacos curl init failed");
2025-01-16 16:17:01 +08:00
}
2025-04-29 15:05:36 +08:00
curl_easy_cleanup(curl);
}
void Nacos_GetParam(char* postgres_uid, char* postgres_pwd, char* web_clientid, char* web_clientsecret)
{
SendWebAPI_Nacos("http://127.0.0.1:8091/powerQuality/PQNACOS", "200", postgres_uid, postgres_pwd, web_clientid, web_clientsecret);
}
void Nacos_GetParam_Ptr(const char* code, char** ptr)
{
SendWebAPI_Nacos_Ptr("http://127.0.0.1:8091/powerQuality/getProperties", code, ptr);
}
void Read_Nacos_Param_Postgres(char** database_ip, char** database_port, char** postgres_database, char** postgres_username, char** postgres_password, char** postgres_schema, char** postgres_dnsname, char** postgres_tableprefix) {
char* ptr=NULL;
SendWebAPI_Nacos_Ptr("http://127.0.0.1:8091/powerQuality/getProperties", "postgres", &ptr);
int flag = 1;
2025-05-09 16:53:07 +08:00
cJSON* json = cJSON_Parse(ptr); //json格式序列化
2025-04-29 15:05:36 +08:00
cJSON* json_node;
cJSON* json_param;
if (json == NULL) {
printf("nacos error %s\n", cJSON_GetErrorPtr());
2025-01-16 16:17:01 +08:00
}
2025-04-29 15:05:36 +08:00
else {
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "status"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
printf("status error :%s\n", cJSON_GetErrorPtr());
flag = 0;
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "success") != 0) {
printf("errors get falie: %s\n", cJSON_GetErrorPtr());
flag = 0;
2025-01-16 16:17:01 +08:00
}
2025-04-29 15:05:36 +08:00
if (flag) {
printf("read nacos success\n");
2025-05-09 16:53:07 +08:00
json_param = cJSON_GetObjectItem(json, "param"); //获取result
json_node = cJSON_GetObjectItem(json_param, "ip"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*database_ip != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*database_ip);
}
2025-05-09 16:53:07 +08:00
*database_ip = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*database_ip, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "port"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*database_port != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*database_port);
}
2025-05-09 16:53:07 +08:00
*database_port = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*database_port, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "database"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*postgres_database != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*postgres_database);
}
2025-05-09 16:53:07 +08:00
*postgres_database = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*postgres_database, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "username"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*postgres_username != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*postgres_username);
}
2025-05-09 16:53:07 +08:00
*postgres_username = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*postgres_username, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "password"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*postgres_password != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*postgres_password);
}
2025-05-09 16:53:07 +08:00
*postgres_password = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*postgres_password, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "schema"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*postgres_schema != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*postgres_schema);
}
2025-05-09 16:53:07 +08:00
*postgres_schema = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*postgres_schema, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "dnsname"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*postgres_dnsname != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*postgres_dnsname);
}
2025-05-09 16:53:07 +08:00
*postgres_dnsname = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*postgres_dnsname, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "tablePrefix"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*postgres_tableprefix != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*postgres_tableprefix);
}
2025-05-09 16:53:07 +08:00
*postgres_tableprefix = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*postgres_tableprefix, json_node->valuestring);
}
2025-01-16 16:17:01 +08:00
}
}
2025-04-29 15:05:36 +08:00
if (ptr) {
free(ptr);
}
cJSON_Delete(json);
}
void Read_Nacos_Param_Kafka(char** broker_list, char** topic_stat, char** topic_pst, char** topic_plt, char** topic_event, char** topic_alarm, char** topic_sng,char** protocol ,char** mechanisms, char** service_name, char** principal, char** domain_name) {
char* ptr = NULL;
SendWebAPI_Nacos_Ptr("http://127.0.0.1:8091/powerQuality/getProperties", "kafka", &ptr);
int flag = 1;
2025-05-09 16:53:07 +08:00
cJSON* json = cJSON_Parse(ptr); //json格式序列化
2025-04-29 15:05:36 +08:00
cJSON* json_node;
cJSON* json_param;
if (json == NULL) {
printf("nacos error %s\n", cJSON_GetErrorPtr());
}
else {
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "status"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
printf("status error :%s\n", cJSON_GetErrorPtr());
flag = 0;
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "success") != 0) {
printf("errors get falie: %s\n", cJSON_GetErrorPtr());
flag = 0;
}
if (flag) {
printf("read nacos success\n");
2025-05-09 16:53:07 +08:00
json_param = cJSON_GetObjectItem(json, "param"); //获取result
json_node = cJSON_GetObjectItem(json_param, "brokerList"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*broker_list != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*broker_list);
}
2025-05-09 16:53:07 +08:00
*broker_list = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*broker_list, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "hisTopic"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*topic_stat != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*topic_stat);
}
2025-05-09 16:53:07 +08:00
*topic_stat = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*topic_stat, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "pstTopic"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*topic_pst != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*topic_pst);
}
2025-05-09 16:53:07 +08:00
*topic_pst = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*topic_pst, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "pltTopic"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*topic_plt != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*topic_plt);
}
2025-05-09 16:53:07 +08:00
*topic_plt = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*topic_plt, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "eventTopic"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*topic_event != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*topic_event);
}
2025-05-09 16:53:07 +08:00
*topic_event = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*topic_event, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "almTopic"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*topic_alarm != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*topic_alarm);
}
2025-05-09 16:53:07 +08:00
*topic_alarm = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*topic_alarm, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "sngTopic"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*topic_sng != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*topic_sng);
}
2025-05-09 16:53:07 +08:00
*topic_sng = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*topic_sng, json_node->valuestring);
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "protocol"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*protocol != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*protocol);
}
2025-05-09 16:53:07 +08:00
*protocol = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*protocol, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "mechanisms"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*mechanisms != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*mechanisms);
}
2025-05-09 16:53:07 +08:00
*mechanisms = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*mechanisms, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "serviceName"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*service_name != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*service_name);
}
2025-05-09 16:53:07 +08:00
*service_name = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*service_name, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "principal"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*principal != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*principal);
}
2025-05-09 16:53:07 +08:00
*principal = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*principal, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "domainName"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*domain_name != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*domain_name);
}
2025-05-09 16:53:07 +08:00
*domain_name = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*domain_name, json_node->valuestring);
}
2025-01-16 16:17:01 +08:00
}
}
2025-04-29 15:05:36 +08:00
cJSON_Delete(json);
}
void Read_Nacos_Param_Web(char** client_id, char** client_secret, char** token_url, char** device_url, char** grant_type) {
char* ptr = NULL;
SendWebAPI_Nacos_Ptr("http://127.0.0.1:8091/powerQuality/getProperties", "web", &ptr);
int flag = 1;
2025-05-09 16:53:07 +08:00
cJSON* json = cJSON_Parse(ptr); //json格式序列化
2025-04-29 15:05:36 +08:00
cJSON* json_node;
cJSON* json_param;
if (json == NULL) {
printf("nacos error %s\n", cJSON_GetErrorPtr());
}
else {
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "status"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
printf("status error :%s\n", cJSON_GetErrorPtr());
flag = 0;
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "success") != 0) {
printf("errors get falie: %s\n", cJSON_GetErrorPtr());
flag = 0;
}
if (flag) {
printf("read nacos success\n");
2025-05-09 16:53:07 +08:00
json_param = cJSON_GetObjectItem(json, "param"); //获取result
json_node = cJSON_GetObjectItem(json_param, "clientId"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*client_id != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*client_id);
}
2025-05-09 16:53:07 +08:00
*client_id = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*client_id, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "clientSecret"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*client_secret != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*client_secret);
}
2025-05-09 16:53:07 +08:00
*client_secret = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*client_secret, json_node->valuestring);
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "tokenUrl"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*token_url != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*token_url);
}
2025-05-09 16:53:07 +08:00
*token_url = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*token_url, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "deviceUrl"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*device_url != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*device_url);
}
2025-05-09 16:53:07 +08:00
*device_url = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*device_url, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "grantType"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*grant_type != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*grant_type);
}
2025-05-09 16:53:07 +08:00
*grant_type = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*grant_type, json_node->valuestring);
}
2025-01-16 16:17:01 +08:00
}
}
2025-04-29 15:05:36 +08:00
cJSON_Delete(json);
}
void Read_Nacos_Param_Flag(int* file_flag, int* send_flag, int* front_inst, char** front_ip) {
char* ptr = NULL;
SendWebAPI_Nacos_Ptr("http://127.0.0.1:8091/powerQuality/getProperties", "flag", &ptr);
int flag = 1;
2025-05-09 16:53:07 +08:00
cJSON* json = cJSON_Parse(ptr); //json格式序列化
2025-04-29 15:05:36 +08:00
cJSON* json_node;
cJSON* json_param;
if (json == NULL) {
printf("nacos error: %s\n", ptr);
}
else {
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "status"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
printf("status error :%s\n", cJSON_GetErrorPtr());
flag = 0;
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "success") != 0) {
printf("errors get falie: %s\n", cJSON_GetErrorPtr());
flag = 0;
}
if (flag) {
printf("read nacos success\n");
2025-05-09 16:53:07 +08:00
json_param = cJSON_GetObjectItem(json, "param"); //获取result
json_node = cJSON_GetObjectItem(json_param, "fileFlag"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
*file_flag= atoi(json_node->valuestring);
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "sendFlag"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
*send_flag = atoi(json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "frontInst"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
*front_inst = atoi(json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "frontIP"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
//***postgres_pwd = json_node->valuestring;
if (*front_ip != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*front_ip);
}
2025-05-09 16:53:07 +08:00
*front_ip = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*front_ip, json_node->valuestring);
}
2025-01-16 16:17:01 +08:00
}
}
2025-04-29 15:05:36 +08:00
cJSON_Delete(json);
}
void Read_Nacos_Param_Recall(int* recall_len, int* recall_sta, int* recall_daily) {
char* ptr = NULL;
SendWebAPI_Nacos_Ptr("http://127.0.0.1:8091/powerQuality/getProperties", "recall", &ptr);
int flag = 1;
2025-05-09 16:53:07 +08:00
cJSON* json = cJSON_Parse(ptr); //json格式序列化
2025-04-29 15:05:36 +08:00
cJSON* json_node;
cJSON* json_param;
if (json == NULL) {
printf("nacos error %s\n", cJSON_GetErrorPtr());
}
else {
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "status"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
printf("status error :%s\n", cJSON_GetErrorPtr());
flag = 0;
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "success") != 0) {
printf("errors get falie: %s\n", cJSON_GetErrorPtr());
flag = 0;
}
if (flag) {
printf("read nacos success\n");
2025-05-09 16:53:07 +08:00
json_param = cJSON_GetObjectItem(json, "param"); //获取result
json_node = cJSON_GetObjectItem(json_param, "recall_lenth"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
*recall_len = atoi(json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "recall_start"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
*recall_sta = atoi(json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "recall_dailytime"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
*recall_daily = atoi(json_node->valuestring);
2025-01-16 16:17:01 +08:00
}
}
}
2025-04-29 15:05:36 +08:00
cJSON_Delete(json);
}
void Read_Nacos_Param_Uds(char** uds_upload_url, char** uds_download_url, char** uds_delete_url) {
char* ptr = NULL;
SendWebAPI_Nacos_Ptr("http://127.0.0.1:8091/powerQuality/getProperties", "uds", &ptr);
int flag = 1;
2025-05-09 16:53:07 +08:00
cJSON* json = cJSON_Parse(ptr); //json格式序列化
2025-04-29 15:05:36 +08:00
cJSON* json_node;
cJSON* json_param;
if (json == NULL) {
printf("nacos error %s\n", cJSON_GetErrorPtr());
}
else {
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "status"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
printf("status error :%s\n", cJSON_GetErrorPtr());
flag = 0;
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node == NULL || strcmp(json_node->valuestring, "success") != 0) {
printf("errors get falie: %s\n", cJSON_GetErrorPtr());
flag = 0;
}
if (flag) {
printf("read nacos success\n");
2025-05-09 16:53:07 +08:00
json_param = cJSON_GetObjectItem(json, "param"); //获取result
json_node = cJSON_GetObjectItem(json_param, "udsUploadUrl"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*uds_upload_url != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*uds_upload_url);
}
2025-05-09 16:53:07 +08:00
*uds_upload_url = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*uds_upload_url, json_node->valuestring);
2025-01-16 16:17:01 +08:00
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "udsDownloadUrl"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*uds_download_url != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*uds_download_url);
}
2025-05-09 16:53:07 +08:00
*uds_download_url = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*uds_download_url, json_node->valuestring);
}
2025-05-09 16:53:07 +08:00
json_node = cJSON_GetObjectItem(json_param, "UdsDeleteUrl"); //获取result
2025-04-29 15:05:36 +08:00
if (json_node && json_node->type == cJSON_String) {
if (*uds_delete_url != NULL) {
2025-05-09 16:53:07 +08:00
// 如果已经分配了内存,则释放内存
2025-04-29 15:05:36 +08:00
free(*uds_delete_url);
}
2025-05-09 16:53:07 +08:00
*uds_delete_url = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
2025-04-29 15:05:36 +08:00
strcpy(*uds_delete_url, json_node->valuestring);
}
2025-01-16 16:17:01 +08:00
}
}
2025-04-29 15:05:36 +08:00
cJSON_Delete(json);
}
2025-01-16 16:17:01 +08:00
#ifdef __cplusplus
}
#endif