-
Notifications
You must be signed in to change notification settings - Fork 0
/
judi.hpp
96 lines (77 loc) · 1.99 KB
/
judi.hpp
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
#ifndef _JUDI_H_
#define _JUDI_H_
#include <ArduinoJson.h>
/* ************************************************************************** */
class JudiBuffer {
public:
char data[256] = {};
uint16_t length = 0;
uint16_t depth = 0;
void reset(void);
void insert_char(char currentChar);
bool completed(void);
void terminate(void);
};
void JudiBuffer::reset(void) {
memset(&data, 0, sizeof(data));
length = 0;
depth = 0;
}
void JudiBuffer::insert_char(char currentChar) {
if (currentChar == '{') {
depth++;
}
if (depth > 0) {
data[length++] = currentChar;
}
if (currentChar == '}' && depth > 0) {
depth--;
}
}
bool JudiBuffer::completed(void) {
if (depth == 0 && length > 0) {
return true;
}
return false;
}
void JudiBuffer::terminate(void) {
data[length] = 0; //
}
/* ************************************************************************** */
class JUDI {
public:
JudiBuffer buffer;
StaticJsonDocument<200> document;
StaticJsonDocument<50> device_info;
JsonObject message;
JUDI(String name, String serial_number);
bool update(char c);
bool contains(String string);
String operator[](String string);
};
JUDI::JUDI(String name, String serial_number) {
// initialize the device_info json object
device_info["update"]["device_info"]["product_name"] = name;
device_info["update"]["device_info"]["serial_number"] = serial_number;
}
bool JUDI::update(char c) {
buffer.insert_char(c);
if (buffer.completed()) {
buffer.terminate();
deserializeJson(document, buffer.data);
message = document.as<JsonObject>();
buffer.reset();
return true;
}
return false;
}
bool JUDI::contains(String string) {
return message.containsKey(string); //
}
String JUDI::operator[](String string) {
if (contains(string)) {
return String((const char *)message[string]);
}
return String("");
}
#endif // _JUDI_H_