|
4 | 4 |
|
5 | 5 | namespace thing {
|
6 | 6 |
|
7 |
| -void Config::SerializeToJson(Stream* output, std::function<void(int size)> handle_size) const { |
8 |
| - DynamicJsonBuffer jsonBuffer; |
9 |
| - JsonObject& root = jsonBuffer.createObject(); |
10 |
| - root["host"] = host.c_str(); |
11 |
| - root["auth"] = auth.c_str(); |
12 |
| - root["path"] = path.c_str(); |
13 |
| - root["wifi_ssid"] = wifi_ssid.c_str(); |
14 |
| - root["wifi_key"] = wifi_key.c_str(); |
15 |
| - root["analog_activation"] = analog_activation_threshold; |
16 |
| - root["wifi_connect_attempts"] = wifi_connect_attempts; |
| 7 | +ConfigJsonSerializer::ConfigJsonSerializer(const Config& config) { |
| 8 | + JsonObject& root = buffer_.createObject(); |
| 9 | + root_ = &root; |
| 10 | + root["host"] = config.host.c_str(); |
| 11 | + root["auth"] = config.auth.c_str(); |
| 12 | + root["path"] = config.path.c_str(); |
| 13 | + root["wifi_ssid"] = config.wifi_ssid.c_str(); |
| 14 | + root["wifi_key"] = config.wifi_key.c_str(); |
| 15 | + root["analog_activation"] = config.analog_activation_threshold; |
| 16 | + root["wifi_connect_attempts"] = config.wifi_connect_attempts; |
17 | 17 |
|
18 | 18 | JsonObject& pins_root = root.createNestedObject("pins");
|
19 |
| - pins_root["digital_in"] = pins.digital_in; |
20 |
| - pins_root["digital_out"] = pins.digital_out; |
21 |
| - pins_root["analog_in"] = pins.analog_in; |
22 |
| - pins_root["analog_out"] = pins.analog_out; |
23 |
| - pins_root["config_mode_button"] = pins.config_mode_button; |
24 |
| - |
25 |
| - handle_size(root.measureLength()); |
26 |
| - root.printTo(*output); |
| 19 | + pins_root["digital_in"] = config.pins.digital_in; |
| 20 | + pins_root["digital_out"] = config.pins.digital_out; |
| 21 | + pins_root["analog_in"] = config.pins.analog_in; |
| 22 | + pins_root["analog_out"] = config.pins.analog_out; |
| 23 | + pins_root["config_mode_button"] = config.pins.config_mode_button; |
27 | 24 | }
|
28 | 25 |
|
29 |
| -void Config::ReadFromJson(char* string) { |
30 |
| - DynamicJsonBuffer jsonBuffer; |
31 |
| - JsonObject& root = jsonBuffer.parseObject(string); |
32 |
| - host = root["host"].asString(); |
33 |
| - auth = root["auth"].asString(); |
34 |
| - path = root["path"].asString(); |
35 |
| - wifi_ssid = root["wifi_ssid"].asString(); |
36 |
| - wifi_key = root["wifi_key"].asString(); |
37 |
| - analog_activation_threshold = root["activation_threshold"]; |
38 |
| - wifi_connect_attempts = root["wifi_connect_attempts"]; |
39 |
| - |
40 |
| - pins.digital_in = root["pins"]["digital_in"]; |
41 |
| - pins.digital_out = root["pins"]["digital_out"]; |
42 |
| - pins.analog_in = root["pins"]["analog_in"]; |
43 |
| - pins.analog_out = root["pins"]["analog_out"]; |
44 |
| - pins.config_mode_button = root["pins"]["config_mode_button"]; |
| 26 | +ConfigJsonSerializer::ConfigJsonSerializer(char* serialized_config) { |
| 27 | + root_ = &(buffer_.parseObject(serialized_config)); |
| 28 | +} |
| 29 | + |
| 30 | +int ConfigJsonSerializer::content_length() const { |
| 31 | + return root_->measureLength(); |
| 32 | +} |
| 33 | + |
| 34 | +void ConfigJsonSerializer::SerializeTo(Stream* output) { |
| 35 | + // TODO: We "should" be able to have the root_ print directly to the stream |
| 36 | + // however it currently closes the connection half way through. Fixing this |
| 37 | + // would save ~250B of memory during serialization. |
| 38 | + String buffer; |
| 39 | + root_->printTo(buffer); |
| 40 | + output->print(buffer); |
| 41 | +} |
| 42 | + |
| 43 | +void ConfigJsonSerializer::DeserializeTo(Config* config) { |
| 44 | + config->host = root()["host"].asString(); |
| 45 | + config->auth = root()["auth"].asString(); |
| 46 | + config->path = root()["path"].asString(); |
| 47 | + config->wifi_ssid = root()["wifi_ssid"].asString(); |
| 48 | + config->wifi_key = root()["wifi_key"].asString(); |
| 49 | + config->analog_activation_threshold = root()["activation_threshold"]; |
| 50 | + config->wifi_connect_attempts = root()["wifi_connect_attempts"]; |
| 51 | + |
| 52 | + config->pins.digital_in = root()["pins"]["digital_in"]; |
| 53 | + config->pins.digital_out = root()["pins"]["digital_out"]; |
| 54 | + config->pins.analog_in = root()["pins"]["analog_in"]; |
| 55 | + config->pins.analog_out = root()["pins"]["analog_out"]; |
| 56 | + config->pins.config_mode_button = root()["pins"]["config_mode_button"]; |
45 | 57 | }
|
46 | 58 |
|
47 | 59 | };
|
|
0 commit comments