forked from abegyoung/gusto-corr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinflux.c
104 lines (74 loc) · 2.35 KB
/
influx.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <curl/curl.h>
#include "corrspec.h"
#include "influx.h"
//the returned data
//double influx_return;
struct influxStruct influxReturn;
// Callback function to handle the response from the InfluxDB server
struct influxStruct write_callback(void *contents, size_t size, size_t nmemb, void *userp) {
char *token;
int position = 0;
char time[64]="";
char ID[12]="";
int scanID;
float value;
size_t realsize = size * nmemb;
token = strtok(contents, ",");
// Parse the InfluxDB return string
while (token != NULL ) {
token = strtok(NULL, ",");
if (position == 4) //get time string from influxdb return
strncpy(time, token+12, 23);
if (position == 5){ //get scanID from influxdb return
strncpy(ID, token+1, strlen(token)-1);
scanID = atoi(ID);
}
if (position == 6) //get value string from influxdb return
value = atof(token);
position++;
}
//printf("value = %.3f\n", value);
//printf("scanID = %d\n", scanID);
//printf("time = %s\n", time);
influxReturn.value = value;
influxReturn.scanID = scanID;
//return realsize;
//return influx_return;
return influxReturn;
}
// worker for the Influx DB query
// Takes: curl handle
// Operates: makes the function callback( )
struct influxStruct influxWorker(CURL *curl, char *query)
{
CURLcode res;
if (curl) {
// Set the InfluxDB URL
curl_easy_setopt(curl, CURLOPT_URL, INFLUXDB_URL);
// Set the POST data (query)
curl_easy_setopt(curl, CURLOPT_POSTFIELDS, query);
// Set the callback function to handle the response
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback);
// Make the HTTP POST request
res = curl_easy_perform(curl);
}
// Cleanup Influx DB
curl_easy_cleanup(curl);
curl_global_cleanup();
//return influx_return; //return the value
return influxReturn; //return the struct
}
// Initialization function to connect to the Influx DB.
// Returns: The curl handle to pass to the handler function
CURL *init_influx()
{
CURL *curl;
// Initialize libcurl
curl_global_init(CURL_GLOBAL_DEFAULT);
// Create a CURL handle
curl = curl_easy_init();
return curl;
}