743 lines
26 KiB
C++
743 lines
26 KiB
C++
/**
|
||
* @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>
|
||
|
||
/*lnk10-12*/
|
||
#include <iostream>
|
||
|
||
#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 */
|
||
|
||
|
||
size_t req_reply_nacos(void* ptr, size_t size, size_t nmemb, void* stream)
|
||
{
|
||
//((std::string*)userp)->append((char*)contents, size * nmemb);
|
||
//在注释的里面可以打印请求流,cookie的信息
|
||
string* str = (string*)stream;
|
||
(*str).append((char*)ptr, size * nmemb);
|
||
//printf(">>>GetDevice in reply %s\n", (char*)ptr);
|
||
|
||
//GetCJson(ptr);
|
||
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;
|
||
cJSON* json = cJSON_Parse((char*)ptr); //json格式序列化
|
||
cJSON* json_node;
|
||
cJSON* json_param;
|
||
|
||
if (json == NULL) {
|
||
printf("nacos error %s\n", cJSON_GetErrorPtr());
|
||
}
|
||
else {
|
||
json_node = cJSON_GetObjectItem(json, "status"); //获取result
|
||
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;
|
||
}
|
||
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
|
||
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");
|
||
|
||
json_param = cJSON_GetObjectItem(json, "param"); //获取result
|
||
json_node = cJSON_GetObjectItem(json_param, "postgres_uid"); //获取result
|
||
if (json_node != NULL && strcmp(json_node->valuestring, "null") != 0) {
|
||
strcpy(postgres_uid, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "postgres_pwd"); //获取result
|
||
if (json_node != NULL && strcmp(json_node->valuestring, "null") != 0) {
|
||
//***postgres_pwd = json_node->valuestring;
|
||
strcpy(postgres_pwd, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "web_clientid"); //获取result
|
||
if (json_node != NULL && strcmp(json_node->valuestring, "null") != 0) {
|
||
strcpy(web_clientid, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "web_clientsecret"); //获取result
|
||
if (json_node != NULL && strcmp(json_node->valuestring, "null") != 0) {
|
||
strcpy(web_clientsecret, json_node->valuestring);
|
||
}
|
||
|
||
}
|
||
}
|
||
cJSON_Delete(json);
|
||
}
|
||
|
||
void releaseMemory(char** ptr) {
|
||
if (*ptr != NULL) {
|
||
free(*ptr); // 释放内存
|
||
}
|
||
}
|
||
|
||
void SendWebAPI_Nacos(const string strUrl, const char* code, char* postgres_uid, char* postgres_pwd, char* web_clientid, char* web_clientsecret)
|
||
{
|
||
|
||
// 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_AddItemToObject(json_root, "code", cJSON_CreateString(code));
|
||
|
||
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_nacos);
|
||
|
||
|
||
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(">>>Test nacos Post in curl post\n");
|
||
// 开启post请求
|
||
res = curl_easy_perform(curl);
|
||
// 检查请求是否成功
|
||
if (res != CURLE_OK) {
|
||
printf("nacos failed res code: ");
|
||
}
|
||
else {
|
||
printf("nacos success,string %s", resPost0.c_str());
|
||
//后期添加webapi返回值判断
|
||
read_nacos_param(resPost0.c_str(), postgres_uid, postgres_pwd, web_clientid, web_clientsecret);
|
||
}
|
||
|
||
curl_slist_free_all(header_list);
|
||
free(szjson);
|
||
cJSON_Delete(json_root);
|
||
}
|
||
else
|
||
{
|
||
printf(">>> nacos curl init failed");
|
||
}
|
||
curl_easy_cleanup(curl);
|
||
}
|
||
|
||
void SendWebAPI_Nacos_Ptr(const string strUrl, const char* code, char** ptr)
|
||
{
|
||
// curl初始化
|
||
CURL* curl = curl_easy_init();
|
||
// curl返回值
|
||
CURLcode res;
|
||
if (curl)
|
||
{
|
||
char url[100];
|
||
sprintf(url, "%s?code=%s", strUrl.c_str(), code);
|
||
printf(">>>json %s\n", url);
|
||
|
||
// 设置URL
|
||
curl_easy_setopt(curl, CURLOPT_URL, url);
|
||
|
||
|
||
//设置数据接收和写入函数
|
||
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, req_reply_nacos);
|
||
|
||
|
||
string resPost0;
|
||
curl_easy_setopt(curl, CURLOPT_WRITEDATA, (void*)&resPost0);
|
||
//设置超时时间
|
||
curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10);
|
||
curl_easy_setopt(curl, CURLOPT_TIMEOUT, 10);
|
||
|
||
|
||
// 开启post请求
|
||
res = curl_easy_perform(curl);
|
||
|
||
// 检查请求是否成功
|
||
if (res != CURLE_OK) {
|
||
printf("nacos failed res code: ");
|
||
}
|
||
else {
|
||
printf(">>> nacos return str:%s \n", resPost0.c_str());
|
||
//*ptr = strdup(resPost0.c_str());
|
||
*ptr = (char*)malloc(strlen(resPost0.c_str()) + 1); // 分配足够的内存空间
|
||
if (*ptr != NULL) {
|
||
strcpy(*ptr, resPost0.c_str());
|
||
}
|
||
else {
|
||
printf("Memory allocation failed!\n");
|
||
}
|
||
//printf(">>>json %s\n", *ptr);
|
||
//后期添加webapi返回值判断
|
||
}
|
||
|
||
//free(url);
|
||
}
|
||
else
|
||
{
|
||
printf(">>> nacos curl init failed");
|
||
}
|
||
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);
|
||
//cout << ">>>GetDevice in reply" << (char*)ptr;
|
||
int flag = 1;
|
||
cJSON* json = cJSON_Parse(ptr); //json格式序列化
|
||
cJSON* json_node;
|
||
cJSON* json_param;
|
||
|
||
if (json == NULL) {
|
||
printf("nacos error %s\n", cJSON_GetErrorPtr());
|
||
}
|
||
else {
|
||
json_node = cJSON_GetObjectItem(json, "status"); //获取result
|
||
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
|
||
printf("status error :%s\n", cJSON_GetErrorPtr());
|
||
flag = 0;
|
||
}
|
||
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
|
||
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");
|
||
|
||
json_param = cJSON_GetObjectItem(json, "param"); //获取result
|
||
json_node = cJSON_GetObjectItem(json_param, "ip"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*database_ip != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*database_ip);
|
||
}
|
||
*database_ip = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*database_ip, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "port"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*database_port != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*database_port);
|
||
}
|
||
*database_port = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*database_port, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "database"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*postgres_database != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*postgres_database);
|
||
}
|
||
*postgres_database = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*postgres_database, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "username"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
//***postgres_pwd = json_node->valuestring;
|
||
if (*postgres_username != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*postgres_username);
|
||
}
|
||
*postgres_username = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*postgres_username, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "password"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*postgres_password != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*postgres_password);
|
||
}
|
||
*postgres_password = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*postgres_password, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "schema"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*postgres_schema != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*postgres_schema);
|
||
}
|
||
*postgres_schema = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*postgres_schema, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "dnsname"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*postgres_dnsname != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*postgres_dnsname);
|
||
}
|
||
*postgres_dnsname = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*postgres_dnsname, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "tablePrefix"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*postgres_tableprefix != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*postgres_tableprefix);
|
||
}
|
||
*postgres_tableprefix = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*postgres_tableprefix, json_node->valuestring);
|
||
}
|
||
|
||
}
|
||
}
|
||
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);
|
||
//cout << ">>>GetDevice in reply" << (char*)ptr;
|
||
int flag = 1;
|
||
cJSON* json = cJSON_Parse(ptr); //json格式序列化
|
||
cJSON* json_node;
|
||
cJSON* json_param;
|
||
if (json == NULL) {
|
||
printf("nacos error %s\n", cJSON_GetErrorPtr());
|
||
}
|
||
else {
|
||
json_node = cJSON_GetObjectItem(json, "status"); //获取result
|
||
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
|
||
printf("status error :%s\n", cJSON_GetErrorPtr());
|
||
flag = 0;
|
||
}
|
||
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
|
||
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");
|
||
json_param = cJSON_GetObjectItem(json, "param"); //获取result
|
||
json_node = cJSON_GetObjectItem(json_param, "brokerList"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*broker_list != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*broker_list);
|
||
}
|
||
*broker_list = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*broker_list, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "hisTopic"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*topic_stat != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*topic_stat);
|
||
}
|
||
*topic_stat = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*topic_stat, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "pstTopic"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*topic_pst != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*topic_pst);
|
||
}
|
||
*topic_pst = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*topic_pst, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "pltTopic"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*topic_plt != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*topic_plt);
|
||
}
|
||
*topic_plt = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*topic_plt, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "eventTopic"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*topic_event != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*topic_event);
|
||
}
|
||
*topic_event = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*topic_event, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "almTopic"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*topic_alarm != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*topic_alarm);
|
||
}
|
||
*topic_alarm = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*topic_alarm, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "sngTopic"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*topic_sng != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*topic_sng);
|
||
}
|
||
*topic_sng = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*topic_sng, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "protocol"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*protocol != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*protocol);
|
||
}
|
||
*protocol = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*protocol, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "mechanisms"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*mechanisms != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*mechanisms);
|
||
}
|
||
*mechanisms = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*mechanisms, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "serviceName"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*service_name != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*service_name);
|
||
}
|
||
*service_name = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*service_name, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "principal"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*principal != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*principal);
|
||
}
|
||
*principal = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*principal, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "domainName"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*domain_name != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*domain_name);
|
||
}
|
||
*domain_name = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*domain_name, json_node->valuestring);
|
||
}
|
||
|
||
}
|
||
}
|
||
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);
|
||
//cout << ">>>GetDevice in reply" << (char*)ptr;
|
||
int flag = 1;
|
||
cJSON* json = cJSON_Parse(ptr); //json格式序列化
|
||
cJSON* json_node;
|
||
cJSON* json_param;
|
||
|
||
if (json == NULL) {
|
||
printf("nacos error %s\n", cJSON_GetErrorPtr());
|
||
}
|
||
else {
|
||
json_node = cJSON_GetObjectItem(json, "status"); //获取result
|
||
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
|
||
printf("status error :%s\n", cJSON_GetErrorPtr());
|
||
flag = 0;
|
||
}
|
||
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
|
||
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");
|
||
|
||
json_param = cJSON_GetObjectItem(json, "param"); //获取result
|
||
json_node = cJSON_GetObjectItem(json_param, "clientId"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*client_id != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*client_id);
|
||
}
|
||
*client_id = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*client_id, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "clientSecret"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*client_secret != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*client_secret);
|
||
}
|
||
*client_secret = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*client_secret, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "tokenUrl"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*token_url != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*token_url);
|
||
}
|
||
*token_url = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*token_url, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "deviceUrl"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
//***postgres_pwd = json_node->valuestring;
|
||
if (*device_url != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*device_url);
|
||
}
|
||
*device_url = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*device_url, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "grantType"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*grant_type != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*grant_type);
|
||
}
|
||
*grant_type = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*grant_type, json_node->valuestring);
|
||
}
|
||
|
||
|
||
}
|
||
}
|
||
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);
|
||
//cout << ">>>GetDevice in reply" << (char*)ptr;
|
||
int flag = 1;
|
||
cJSON* json = cJSON_Parse(ptr); //json格式序列化
|
||
cJSON* json_node;
|
||
cJSON* json_param;
|
||
|
||
if (json == NULL) {
|
||
printf("nacos error: %s\n", ptr);
|
||
}
|
||
else {
|
||
json_node = cJSON_GetObjectItem(json, "status"); //获取result
|
||
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
|
||
printf("status error :%s\n", cJSON_GetErrorPtr());
|
||
flag = 0;
|
||
}
|
||
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
|
||
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");
|
||
|
||
json_param = cJSON_GetObjectItem(json, "param"); //获取result
|
||
json_node = cJSON_GetObjectItem(json_param, "fileFlag"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
*file_flag= atoi(json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "sendFlag"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
*send_flag = atoi(json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "frontInst"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
*front_inst = atoi(json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "frontIP"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
//***postgres_pwd = json_node->valuestring;
|
||
if (*front_ip != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*front_ip);
|
||
}
|
||
*front_ip = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*front_ip, json_node->valuestring);
|
||
}
|
||
|
||
|
||
}
|
||
}
|
||
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);
|
||
//cout << ">>>GetDevice in reply" << (char*)ptr;
|
||
int flag = 1;
|
||
cJSON* json = cJSON_Parse(ptr); //json格式序列化
|
||
cJSON* json_node;
|
||
cJSON* json_param;
|
||
|
||
if (json == NULL) {
|
||
printf("nacos error %s\n", cJSON_GetErrorPtr());
|
||
}
|
||
else {
|
||
json_node = cJSON_GetObjectItem(json, "status"); //获取result
|
||
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
|
||
printf("status error :%s\n", cJSON_GetErrorPtr());
|
||
flag = 0;
|
||
}
|
||
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
|
||
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");
|
||
|
||
json_param = cJSON_GetObjectItem(json, "param"); //获取result
|
||
json_node = cJSON_GetObjectItem(json_param, "recall_lenth"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
*recall_len = atoi(json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "recall_start"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
*recall_sta = atoi(json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "recall_dailytime"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
*recall_daily = atoi(json_node->valuestring);
|
||
}
|
||
|
||
}
|
||
}
|
||
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);
|
||
//cout << ">>>GetDevice in reply" << (char*)ptr;
|
||
int flag = 1;
|
||
cJSON* json = cJSON_Parse(ptr); //json格式序列化
|
||
cJSON* json_node;
|
||
cJSON* json_param;
|
||
|
||
if (json == NULL) {
|
||
printf("nacos error %s\n", cJSON_GetErrorPtr());
|
||
}
|
||
else {
|
||
json_node = cJSON_GetObjectItem(json, "status"); //获取result
|
||
if (json_node == NULL || strcmp(json_node->valuestring, "000000") != 0) {
|
||
printf("status error :%s\n", cJSON_GetErrorPtr());
|
||
flag = 0;
|
||
}
|
||
json_node = cJSON_GetObjectItem(json, "errors"); //获取result
|
||
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");
|
||
|
||
json_param = cJSON_GetObjectItem(json, "param"); //获取result
|
||
json_node = cJSON_GetObjectItem(json_param, "udsUploadUrl"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*uds_upload_url != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*uds_upload_url);
|
||
}
|
||
*uds_upload_url = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*uds_upload_url, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "udsDownloadUrl"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*uds_download_url != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*uds_download_url);
|
||
}
|
||
*uds_download_url = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*uds_download_url, json_node->valuestring);
|
||
}
|
||
json_node = cJSON_GetObjectItem(json_param, "UdsDeleteUrl"); //获取result
|
||
if (json_node && json_node->type == cJSON_String) {
|
||
if (*uds_delete_url != NULL) {
|
||
// 如果已经分配了内存,则释放内存
|
||
free(*uds_delete_url);
|
||
}
|
||
*uds_delete_url = (char*)malloc(strlen(json_node->valuestring) + 1); // 分配内存
|
||
strcpy(*uds_delete_url, json_node->valuestring);
|
||
}
|
||
|
||
}
|
||
}
|
||
cJSON_Delete(json);
|
||
}
|
||
|
||
|
||
|
||
|
||
|
||
#ifdef __cplusplus
|
||
}
|
||
#endif
|