-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue.h
More file actions
199 lines (165 loc) · 4.89 KB
/
Copy pathvalue.h
File metadata and controls
199 lines (165 loc) · 4.89 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*
================================================================================
DREAM core / Value container file
--------------------------------------------------------------------------------
Module of the Value class, which allows you to save data of various types,
saving the name of the sensor, units of measurement and the type of value.
--------------------------------------------------------------------------------
Copyright © 2019-2022 Dmytro Obukhov (DeLy Dreamer)
License: https://www.gnu.org/licenses/gpl-3.0.html
================================================================================
*/
#pragma once
#include <Arduino.h>
#include "null_defines.h"
namespace dream
{
enum class ValueDataType
{
Int,
Float,
String
};
class Value
{
private: //-------------------------------------------------------------
// Value
int as_int;
float as_float;
String as_string;
public: //--------------------------------------------------------------
String type;
String unit;
String sensor;
ValueDataType data_type;
int decimal_places = 0; // For float save
// Constructors
Value(const String &type, const String &unit, const String &sensor, String value) :
type(type), unit(unit), sensor(sensor)
{
setValue(value);
}
Value(const String &type, const String &unit, const String &sensor, float value, int digits) :
type(type), unit(unit), sensor(sensor)
{
setValue(value, digits);
}
Value(const String &type, const String &unit, const String &sensor, int value) :
type(type), unit(unit), sensor(sensor)
{
setValue(value);
}
//---------------------------------------------------------------------+
// Set value from value of specific type and convert it to string
//---------------------------------------------------------------------+
void setValue(const String &value)
{
if (value != NULL_STR && value.length() > 0)
{
this->data_type = ValueDataType::String;
this->as_string = value;
}
else
{
setNull();
}
}
void setValue(const float value, const int digits)
{
if (value != NULL_FLOAT && isnan(value) == false)
{
this->data_type = ValueDataType::Float;
this->as_float = value;
this->decimal_places = digits;
this->as_int = round(value);
this->as_string = String(value, digits);
}
else
{
setNull();
}
}
void setValue(const int value)
{
if (value != NULL_INT && isnan(value) == false)
{
this->data_type = ValueDataType::Int;
this->as_int = value;
this->as_float = value;
this->as_string = String(value);
}
else
{
setNull();
}
}
//---------------------------------------------------------------------+
// Convert value to specific type
// Returns 0.0 or 0 if value not valid
//---------------------------------------------------------------------+
float asFloat() const
{
return this->as_float;
}
int asInt() const
{
return this->as_int;
}
String asString() const
{
return this->as_string;
}
//---------------------------------------------------------------------+
// Set value as null
//---------------------------------------------------------------------+
void setNull()
{
this->as_int = NULL_INT;
this->as_float = NULL_FLOAT;
this->as_string = NULL_STR;
}
//---------------------------------------------------------------------+
// Is value NULL
//---------------------------------------------------------------------+
bool isNull() const
{
// The as_string always contains data.
// While other variables can be empty in case the data type is a string.
return (as_string == NULL_STR);
}
//---------------------------------------------------------------------+
// Is value type Integer or Float
//---------------------------------------------------------------------+
bool isNumeric() const
{
return (data_type == ValueDataType::Float || data_type == ValueDataType::Int);
}
//---------------------------------------------------------------------+
// Generate JSON of value
//---------------------------------------------------------------------+
String toJSON() const
{
String s = F("{\"type\":\"{t}\",\"unit\":\"{u}\",\"sensor\":\"{s}\",\"value\":\"{v}\"},");
s.replace("{t}", type);
s.replace("{u}", unit);
s.replace("{s}", sensor);
s.replace("{v}", as_string);
return s;
}
//---------------------------------------------------------------------+
// Generate string with all data
//---------------------------------------------------------------------+
String toString() const
{
String dt = "";
switch (data_type)
{
case ValueDataType::Int: dt = "i"; break;
case ValueDataType::Float: dt = "f"; break;
case ValueDataType::String: dt = "s"; break;
default: dt = "null"; break;
}
return "[" + sensor + "] " + type + " " + as_string + " " + unit + " (" + dt + ")";
}
};
}