#include "influxdb.h" InfluxDB::InfluxDB(std::string url, bool verbose){ curl = curl_easy_init(); if( verbose) curl_easy_setopt(curl, CURLOPT_VERBOSE, 1); this->databaseIP = url; respondCode = 0; dataPoints = ""; } InfluxDB::~InfluxDB(){ curl_easy_cleanup(curl); } std::string InfluxDB::ShowDatabases(){ curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "/query").c_str()); std::string postFields="q=Show databases"; curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast(postFields.length())); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallBack); std::string readBuffer; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); Execute(); printf("|%s|\n", readBuffer.c_str()); return readBuffer; } std::string InfluxDB::Query(std::string databaseName, std::string query){ curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "/query?db=" + databaseName).c_str()); std::string postFields = "q=" + query; curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast(postFields.length())); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str()); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallBack); std::string readBuffer; curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); Execute(); printf("|%s|\n", readBuffer.c_str()); return readBuffer; } void InfluxDB::CreateDatabase(std::string databaseName){ curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "/query").c_str()); curl_easy_setopt(curl, CURLOPT_POST, 1); std::string postFields = "q=CREATE DATABASE " + databaseName; curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast(postFields.length())); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, postFields.c_str()); Execute(); } void InfluxDB::AddDataPoint(std::string fullString){ dataPoints += fullString + "\n"; } void InfluxDB::ClearDataPoints(){ dataPoints = ""; } void InfluxDB::PrintDataPoints(){ printf("%s\n", dataPoints.c_str()); } void InfluxDB::WriteData(std::string databaseName){ curl_easy_setopt(curl, CURLOPT_URL, (databaseIP + "write?db=" + databaseName).c_str()); curl_easy_setopt(curl, CURLOPT_POST, 1); curl_easy_setopt(curl, CURLOPT_POSTFIELDSIZE, static_cast(dataPoints.length())); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, dataPoints.c_str()); Execute(); } void InfluxDB::Execute(){ respond = curl_easy_perform(curl); curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &respondCode); printf("==== respond code %ld \n", respondCode); if( respond != CURLE_OK) printf("############# fail\n"); } size_t InfluxDB::WriteCallBack(char *contents, size_t size, size_t nmemb, void *userp){ ((std::string*)userp)->append((char*)contents, size * nmemb); return size * nmemb; }