diff --git a/DAQ/Makefile b/DAQ/Makefile index 0dba698..9450db1 100755 --- a/DAQ/Makefile +++ b/DAQ/Makefile @@ -11,7 +11,7 @@ CAENLIBS = -lCAENDigitizer ROOTLIBS = `root-config --cflags --glibs` -OBJS = startStopDialog.o programSetting.o triggerSummary.o registerSetting.o channelSettingPSD.o channelSettingPHA.o boardSetting.o ClassDigitizer.o FSUDAQ.o +OBJS = influxdb.o startStopDialog.o programSetting.o triggerSummary.o registerSetting.o channelSettingPSD.o channelSettingPHA.o boardSetting.o ClassDigitizer.o FSUDAQ.o ######################################################################### @@ -23,9 +23,9 @@ clean : ClassDigitizer.o : ClassDigitizer.cpp ClassDigitizer.h RegisterAddress.h macro.h ClassData.h $(CC) $(COPTS) -c ClassDigitizer.cpp -../test : test.cpp ClassDigitizer.o +../test : test.cpp ClassDigitizer.o influxdb.o @echo "--------- making test" - $(CC) $(COPTS) -o ../test test.cpp ClassDigitizer.o $(CAENLIBS) $(ROOTLIBS) + $(CC) $(COPTS) -o ../test test.cpp ClassDigitizer.o influxdb.o $(CAENLIBS) $(ROOTLIBS) -lcurl ../Analysis/EventBuilder : EventBuilder.cpp ClassData.h @echo "--------- making EventBuilder" @@ -37,7 +37,7 @@ ClassDigitizer.o : ClassDigitizer.cpp ClassDigitizer.h RegisterAddress.h macro.h ../FSUDAQ : FSUDAQDict.cxx $(OBJS) ClassData.h @echo "----------- creating FSUDAQ" - $(CC) $(COPTS) FSUDAQDict.cxx $(OBJS) -o ../FSUDAQ $(CAENLIBS) $(ROOTLIBS) + $(CC) $(COPTS) FSUDAQDict.cxx $(OBJS) -o ../FSUDAQ $(CAENLIBS) $(ROOTLIBS) -lcurl @ln -s -f DAQ/FSUDAQDict_rdict.pcm ../. FSUDAQDict.cxx : FSUDAQ.h FSUDAQLinkDef.h @@ -63,6 +63,10 @@ channelSettingPSD.o : channelSettingPSD.h channelSettingPSD.cpp registerSetting.o : registerSetting.h registerSetting.cpp @echo "----------- creating registerSetting.o" $(CC) $(COPTS) -c registerSetting.cpp $(ROOTLIBS) + +influxdb.o : influxdb.h influxdb.cpp + @echo "----------- creating influxdb.o" + $(CC) $(COPTS) -c influxdb.cpp -lcurl triggerSummary.o : triggerSummary.h triggerSummary.cpp @echo "----------- creating triggerSummary.o" diff --git a/DAQ/influxdb.cpp b/DAQ/influxdb.cpp new file mode 100644 index 0000000..1da58ae --- /dev/null +++ b/DAQ/influxdb.cpp @@ -0,0 +1,102 @@ +#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; +} diff --git a/DAQ/influxdb.h b/DAQ/influxdb.h new file mode 100644 index 0000000..0452742 --- /dev/null +++ b/DAQ/influxdb.h @@ -0,0 +1,44 @@ +#ifndef INFLUXDB_H +#define INFLUXDB_H + +#include +#include +#include +#include + +class InfluxDB{ + private: + + CURL * curl; + CURLcode respond; + long respondCode; + + std::string databaseIP; + std::string dataPoints; + + static size_t WriteCallBack(char *contents, size_t size, size_t nmemb, void *userp); + + void Execute(); + + public: + /// url = https://fsunuc.physics.fsu.edu/InfluxDB/ + InfluxDB(std::string url, bool verbose = false); + ~InfluxDB(); + + /// Query + std::string ShowDatabases(); + std::string Query(std::string databaseName, std::string query); + + void CreateDatabase(std::string databaseName); + + /// for single or batch write, + /// 1, addDataPoint first, you can add as many as you like + /// 2, writeData. + void AddDataPoint(std::string fullString); + void ClearDataPoints(); + void PrintDataPoints(); + void WriteData(std::string databaseName); + +}; + +#endif diff --git a/DAQ/test.cpp b/DAQ/test.cpp index ee4977a..607f831 100644 --- a/DAQ/test.cpp +++ b/DAQ/test.cpp @@ -2,6 +2,8 @@ #include "ClassData.h" #include "ClassDigitizer.h" +#include "influxdb.h" + #include "TROOT.h" #include "TSystem.h" #include "TApplication.h" @@ -72,7 +74,19 @@ int getch(void){ int main(int argc, char* argv[]){ + InfluxDB influx("https://fsunuc.physics.fsu.edu/influx/", false); + + //influx.ShowDatabases(); + + //influx.Query("testing", "Show measurements"); + + influx.AddDataPoint("Rate,Bd=0,Ch=0 value=10"); + influx.AddDataPoint("Rate,Bd=0,Ch=7 value=20"); + influx.AddDataPoint("Rate,Bd=0,Ch=15 value=30"); + influx.WriteData("testing"); /**////##################### Demo for loading and change setting without open a digitizer + + /** Digitizer * dig = new Digitizer(); dig->OpenDigitizer(0, 1, false, true); dig->LoadSettingBinaryToMemory("expDir/settings/setting_323.bin");