Settings.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <Settings.h>
  2. #include <ArduinoJson.h>
  3. #include <FS.h>
  4. #include <IntParsing.h>
  5. #include <algorithm>
  6. #define PORT_POSITION(s) ( s.indexOf(':') )
  7. bool Settings::hasAuthSettings() {
  8. return adminUsername.length() > 0 && adminPassword.length() > 0;
  9. }
  10. bool Settings::isAutoRestartEnabled() {
  11. return _autoRestartPeriod > 0;
  12. }
  13. size_t Settings::getAutoRestartPeriod() {
  14. if (_autoRestartPeriod == 0) {
  15. return 0;
  16. }
  17. return std::max(_autoRestartPeriod, static_cast<size_t>(MINIMUM_RESTART_PERIOD));
  18. }
  19. void Settings::deserialize(Settings& settings, String json) {
  20. DynamicJsonBuffer jsonBuffer;
  21. JsonObject& parsedSettings = jsonBuffer.parseObject(json);
  22. settings.patch(parsedSettings);
  23. }
  24. void Settings::updateDeviceIds(JsonArray& arr) {
  25. if (arr.success()) {
  26. if (this->deviceIds) {
  27. delete this->deviceIds;
  28. }
  29. this->deviceIds = new uint16_t[arr.size()];
  30. this->numDeviceIds = arr.size();
  31. arr.copyTo(this->deviceIds, arr.size());
  32. }
  33. }
  34. void Settings::updateGatewayConfigs(JsonArray& arr) {
  35. if (arr.success()) {
  36. if (this->gatewayConfigs) {
  37. delete[] this->gatewayConfigs;
  38. }
  39. this->gatewayConfigs = new GatewayConfig*[arr.size()];
  40. this->numGatewayConfigs = arr.size();
  41. for (size_t i = 0; i < arr.size(); i++) {
  42. JsonArray& params = arr[i];
  43. if (params.success() && params.size() == 3) {
  44. this->gatewayConfigs[i] = new GatewayConfig(parseInt<uint16_t>(params[0]), params[1], params[2]);
  45. } else {
  46. Serial.print(F("Settings - skipped parsing gateway ports settings for element #"));
  47. Serial.println(i);
  48. }
  49. }
  50. }
  51. }
  52. void Settings::patch(JsonObject& parsedSettings) {
  53. if (parsedSettings.success()) {
  54. this->setIfPresent<String>(parsedSettings, "admin_username", adminUsername);
  55. this->setIfPresent(parsedSettings, "admin_password", adminPassword);
  56. this->setIfPresent(parsedSettings, "ce_pin", cePin);
  57. this->setIfPresent(parsedSettings, "csn_pin", csnPin);
  58. this->setIfPresent(parsedSettings, "reset_pin", resetPin);
  59. this->setIfPresent(parsedSettings, "packet_repeats", packetRepeats);
  60. this->setIfPresent(parsedSettings, "http_repeat_factor", httpRepeatFactor);
  61. this->setIfPresent(parsedSettings, "auto_restart_period", _autoRestartPeriod);
  62. this->setIfPresent(parsedSettings, "mqtt_server", _mqttServer);
  63. this->setIfPresent(parsedSettings, "mqtt_username", mqttUsername);
  64. this->setIfPresent(parsedSettings, "mqtt_password", mqttPassword);
  65. this->setIfPresent(parsedSettings, "mqtt_topic_pattern", mqttTopicPattern);
  66. if (parsedSettings.containsKey("radio_interface_type")) {
  67. this->radioInterfaceType = Settings::typeFromString(parsedSettings["radio_interface_type"]);
  68. }
  69. if (parsedSettings.containsKey("device_ids")) {
  70. JsonArray& arr = parsedSettings["device_ids"];
  71. updateDeviceIds(arr);
  72. }
  73. if (parsedSettings.containsKey("gateway_configs")) {
  74. JsonArray& arr = parsedSettings["gateway_configs"];
  75. updateGatewayConfigs(arr);
  76. }
  77. }
  78. }
  79. void Settings::load(Settings& settings) {
  80. if (SPIFFS.exists(SETTINGS_FILE)) {
  81. File f = SPIFFS.open(SETTINGS_FILE, "r");
  82. String settingsContents = f.readStringUntil(SETTINGS_TERMINATOR);
  83. f.close();
  84. deserialize(settings, settingsContents);
  85. } else {
  86. settings.save();
  87. }
  88. }
  89. String Settings::toJson(const bool prettyPrint) {
  90. String buffer = "";
  91. StringStream s(buffer);
  92. serialize(s, prettyPrint);
  93. return buffer;
  94. }
  95. void Settings::save() {
  96. File f = SPIFFS.open(SETTINGS_FILE, "w");
  97. if (!f) {
  98. Serial.println(F("Opening settings file failed"));
  99. } else {
  100. serialize(f);
  101. f.close();
  102. }
  103. }
  104. void Settings::serialize(Stream& stream, const bool prettyPrint) {
  105. DynamicJsonBuffer jsonBuffer;
  106. JsonObject& root = jsonBuffer.createObject();
  107. root["admin_username"] = this->adminUsername;
  108. root["admin_password"] = this->adminPassword;
  109. root["ce_pin"] = this->cePin;
  110. root["csn_pin"] = this->csnPin;
  111. root["reset_pin"] = this->resetPin;
  112. root["radio_interface_type"] = typeToString(this->radioInterfaceType);
  113. root["packet_repeats"] = this->packetRepeats;
  114. root["http_repeat_factor"] = this->httpRepeatFactor;
  115. root["auto_restart_period"] = this->_autoRestartPeriod;
  116. root["mqtt_server"] = this->_mqttServer;
  117. root["mqtt_username"] = this->mqttUsername;
  118. root["mqtt_password"] = this->mqttPassword;
  119. root["mqtt_topic_pattern"] = this->mqttTopicPattern;
  120. if (this->deviceIds) {
  121. JsonArray& arr = jsonBuffer.createArray();
  122. arr.copyFrom(this->deviceIds, this->numDeviceIds);
  123. root["device_ids"] = arr;
  124. }
  125. if (this->gatewayConfigs) {
  126. JsonArray& arr = jsonBuffer.createArray();
  127. for (size_t i = 0; i < this->numGatewayConfigs; i++) {
  128. JsonArray& elmt = jsonBuffer.createArray();
  129. elmt.add(this->gatewayConfigs[i]->deviceId);
  130. elmt.add(this->gatewayConfigs[i]->port);
  131. elmt.add(this->gatewayConfigs[i]->protocolVersion);
  132. arr.add(elmt);
  133. }
  134. root["gateway_configs"] = arr;
  135. }
  136. if (prettyPrint) {
  137. root.prettyPrintTo(stream);
  138. } else {
  139. root.printTo(stream);
  140. }
  141. }
  142. String Settings::mqttServer() {
  143. int pos = PORT_POSITION(_mqttServer);
  144. if (pos == -1) {
  145. return _mqttServer;
  146. } else {
  147. return _mqttServer.substring(0, pos);
  148. }
  149. }
  150. uint16_t Settings::mqttPort() {
  151. int pos = PORT_POSITION(_mqttServer);
  152. if (pos == -1) {
  153. return DEFAULT_MQTT_PORT;
  154. } else {
  155. return atoi(_mqttServer.c_str() + pos + 1);
  156. }
  157. }
  158. RadioInterfaceType Settings::typeFromString(const String& s) {
  159. if (s.equalsIgnoreCase("lt8900")) {
  160. return LT8900;
  161. } else {
  162. return nRF24;
  163. }
  164. }
  165. String Settings::typeToString(RadioInterfaceType type) {
  166. switch (type) {
  167. case LT8900:
  168. return "LT8900";
  169. case nRF24:
  170. default:
  171. return "nRF24";
  172. }
  173. }