-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsen2LP.cpp
127 lines (114 loc) · 3.75 KB
/
sen2LP.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
sen2lp.h - Library for converting sensor readings into Line Protocol that can be sent to InfluxDB
Created by Juuso Solonen, November 16, 2024.
Released into the public domain.
*/
#include "Arduino.h"
#include "sen2lp.h"
Field::Field(const String& fkey, const String& fvalue, FieldType fieldType)
: key(fkey), value(fvalue), type(fieldType) {}
void Field::formatValue(String &result) {
switch (this->type) {
case INT: {
float floatVal = atof(this->value.c_str());
int intVal = (int)floatVal;
String intStr = String(intVal);
result += intStr + "i"; // Concatenate integer as string
break;
}
case STR: {
result += "\"";
result += this->value;
result += "\"";
break;
}
case BOOL: {
// Simplify the boolean comparison by using String comparison
if (this->value == "true" || this->value == "false" || this->value == "T" || this->value == "F"
|| this->value == "True" || this->value == "False" || this->value == "t" || this->value == "f"
|| this->value == "TRUE" || this->value == "FALSE") {
result += this->value;
} else {
int val = atoi(this->value.c_str());
result += (val > 0 ? "true" : "false");
}
break;
}
default: {
float fVal = atof(this->value.c_str());
String fStr = String(fVal, 2); // 2 decimal places
result += fStr;
break;
}
}
}
Sensor::Sensor(uint8_t vCount, const String& measurement, const String& tags, unsigned long time)
: measurement(measurement), tags(tags), timestamp(time), size(vCount) {
data = new Field*[size]; // Allocate space for the sensor readings
for (uint8_t i = 0; i < size; ++i) {
data[i] = nullptr; // Initialize each element to nullptr
}
}
Sensor::~Sensor() {
for (uint8_t i = 0; i < size; ++i) {
delete data[i];
}
delete[] data;
}
void Sensor::setTimestamp(unsigned long newTimestamp) {
this->timestamp = newTimestamp;
}
Field* Sensor::allocateData(uint8_t index, const String key, const String value, FieldType type) {
if (index < size) {
if (data[index] == nullptr) {
data[index] = new Field(key, value, type);
return data[index];
} else {
Serial.println("Data already allocated at index");
}
} else {
Serial.println("Index out of bounds.");
}
return nullptr;
}
void Sensor::getFormattedFields(String &result) {
result = ""; // Reset the result string
for (uint8_t i = 0; i < size; ++i) {
if (data[i] != nullptr) {
result += data[i]->key;
result += "=";
result += data[i]->value;
if (i < size - 1) {
result += ",";
}
}
}
}
void Sensor::getLineProtocol(String &result) {
result = ""; // Reset the result string
result += measurement;
result += ",";
result += tags;
for (uint8_t i = 0; i < size; ++i) {
if (data[i] != nullptr) {
if (i == 0) {
result += " ";
}
result += data[i]->key;
result += "=";
if (data[i]->type != FieldType::FLT) {
data[i]->formatValue(result); // Append formatted value
} else { // Hack-ish fix for FLT type formatting
result += data[i]->value;
}
if (i < size - 1) {
result += ",";
}
}
}
if (this->timestamp > 0) {
String timestampStr = String(this->timestamp);
result += " ";
result += timestampStr;
}
}