-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathData.cpp
More file actions
222 lines (175 loc) · 5.13 KB
/
Copy pathData.cpp
File metadata and controls
222 lines (175 loc) · 5.13 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <Arduino.h>
#include "Data.h"
#include <FS.h>
#include <ArduinoJson.h>
Data::Data()
{
}
void Data::printconfig() {
if (SPIFFS.exists("/bulbdata.json")) {
File configFile = SPIFFS.open("/bulbdata.json", "r");
while (configFile.available()) {
//Lets read line by line from the file
String line = configFile.readString();
Serial.println(line);
}
configFile.close();
} else {
Serial.println("printconfig: failed to open bulbdata.json");
}
//end read
}
void Data::mountbulbdata() {
//clean FS, for testing
//SPIFFS.format();
//read configuration from FS json
//Serial.println("mounting Buµlb FS...");
//Serial.println(" . starting");
if (SPIFFS.exists("/bulbdata.json")) {
File configFile = SPIFFS.open("/bulbdata.json", "r");
printconfig();
if (configFile) {
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
if (json.success()) {
//Serial.println("\nbulbdata parsed json");
} else {
Serial.println("failed to load bulb data file");
}
}
}
//end read
}
String Data::get_config() {
if (SPIFFS.exists("/bulbdata.json")) {
File configFile = SPIFFS.open("/bulbdata.json", "r");
if (configFile) {
size_t size = configFile.size();
// Allocate a buffer to store contents of the file.
std::unique_ptr<char[]> buf(new char[size]);
configFile.readBytes(buf.get(), size);
DynamicJsonBuffer jsonBuffer;
JsonObject& json = jsonBuffer.parseObject(buf.get());
//json.printTo(Serial);
if (json.success()) {
//Serial.println("\nbulbdata parsed json");
String tmpjson;
json.printTo(tmpjson);
configFile.close();
return tmpjson;
} else {
Serial.println("Data::get_config(): failed to parse bulb data file");
return "";
}
}
} else {
Serial.println("Data::get_config(): File does not exist");
return "";
}
}
void Data::save_config(JsonObject& root)
{
File configFile = SPIFFS.open("/bulbdata.json", "w");
if (!configFile) {
Serial.println("failed to open config file for writing");
}
root.printTo(configFile);
configFile.close();
}
void Data::create_component(JsonObject& root, String component)
{
//genenate control ID
int randOne = 0xB0;
int randTwo = 0x27; randTwo = random(0x27, 0x88);
int randThree = 0x59; randThree = random(0x10, 0x6D);
JsonObject& bulb = root.createNestedObject(component);
bulb["ComponentID"] = component;
bulb["type"] = 1; // 1 for color, 2 for white, ...
bulb["ControlIDone"] = randOne;
bulb["ControlIDTwo"] = randTwo;
bulb["ControlIDThree"] = randThree;
JsonObject& state = bulb.createNestedObject("state");
state["status"] = false;
state["brightness"] = 254;
state["colorr"] = 0;
state["colorg"] = 0;
state["colorb"] = 0;
// save to file
save_config(root);
}
// Updates a specific state for a component
bool Data::update_bulb_state(String component, String properties, int value){
String config = get_config();
DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject(config);
JsonObject& bulb = obj[component];
bulb["ComponentID"] = bulb["ComponentID"];
bulb["type"] = bulb["type"];
bulb["ControlIDone"] = bulb["ControlIDone"];
bulb["ControlIDTwo"] = bulb["ControlIDTwo"];
bulb["ControlIDThree"] = bulb["ControlIDThree"];
// keeping previous states
int status = bulb["state"]["status"];
int brightness = bulb["state"]["brightness"];
int colorr = bulb["state"]["colorr"];
int colorg = bulb["state"]["colorg"];
int colorb = bulb["state"]["colorb"];
JsonObject& state = bulb.createNestedObject("state");
// update the color Status
if (properties == "status"){
state["status"] = value;
}else{
state["status"] = status;
}
// update the color Brightness
if (properties == "brightness"){
state["brightness"] = value;
}else{
state["brightness"] = brightness;
}
// update the color R
if (properties == "colorr"){
state["colorr"] = value;
}else{
state["colorr"] = colorr;
}
// update the color G
if (properties == "colorg"){
state["colorg"] = value;
}else{
state["colorg"] = colorg;
}
// set the color B
if (properties == "colorb"){
state["colorb"] = value;
}else{
state["colorr"] = colorb;
}
// save to file
save_config(obj);
}
// returns 0/false if doesn't exist; 1/true if exists
bool Data::search_bulb(String component){
String config = get_config();
DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject(config);
bool exists = false;
if (obj.containsKey(component))
{
exists = true;
}else{
exists = false;
}
return exists;
}
void Data::add_bulb(String component)
{
String config = get_config();
DynamicJsonBuffer jsonBuffer;
JsonObject& obj = jsonBuffer.parseObject(config);
create_component(obj, component);
}