Fork of the espurna firmware for `mhsw` switches
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

145 lines
3.2 KiB

  1. #include "AutoOTA.h"
  2. #include <functional>
  3. #include <ArduinoJson.h>
  4. #include <ESP8266httpUpdate.h>
  5. void AutoOTAClass::setServer(String server) {
  6. _server = server;
  7. }
  8. void AutoOTAClass::setModel(String model) {
  9. _model = model;
  10. }
  11. void AutoOTAClass::setVersion(String version) {
  12. _version = version;
  13. }
  14. void AutoOTAClass::onMessage(TMessageFunction fn) {
  15. _callback = fn;
  16. }
  17. String AutoOTAClass::getNewVersion() {
  18. return _newVersion;
  19. }
  20. String AutoOTAClass::getNewFirmware() {
  21. return _newFirmware;
  22. }
  23. String AutoOTAClass::getNewFileSystem() {
  24. return _newFileSystem;
  25. }
  26. int AutoOTAClass::getErrorNumber() {
  27. return _errorNumber;
  28. }
  29. String AutoOTAClass::getErrorString() {
  30. return _errorString;
  31. }
  32. String AutoOTAClass::_getPayload() {
  33. HTTPClient http;
  34. char url[100];
  35. String payload = "";
  36. _callback(AUTO_OTA_START);
  37. sprintf(url, "%s/%s/%s", _server.c_str(), _model.c_str(), _version.c_str());
  38. http.begin(url);
  39. int httpCode = http.GET();
  40. if (httpCode > 0) payload = http.getString();
  41. http.end();
  42. return payload;
  43. }
  44. bool AutoOTAClass::_checkUpdates() {
  45. String payload = _getPayload();
  46. if (payload.length() == 0) {
  47. _callback(AUTO_OTA_NO_RESPONSE_ERROR);
  48. return false;
  49. }
  50. StaticJsonBuffer<500> jsonBuffer;
  51. JsonObject& response = jsonBuffer.parseObject(payload);
  52. if (!response.success()) {
  53. _callback(AUTO_OTA_PARSE_ERROR);
  54. return false;
  55. }
  56. if (response.size() == 0) {
  57. _callback(AUTO_OTA_UPTODATE);
  58. return false;
  59. }
  60. _newVersion = response.get<String>("version");
  61. _newFileSystem = response.get<String>("spiffs");
  62. _newFirmware = response.get<String>("firmware");
  63. _callback(AUTO_OTA_UPDATING);
  64. return true;
  65. }
  66. void AutoOTAClass::_doUpdate() {
  67. char url[100];
  68. bool error = false;
  69. uint8_t updates = 0;
  70. if (_newFileSystem.length() > 0) {
  71. // Update SPIFFS
  72. sprintf(url, "%s/%s", _server.c_str(), _newFileSystem.c_str());
  73. t_httpUpdate_return ret = ESPhttpUpdate.updateSpiffs(url);
  74. if (ret == HTTP_UPDATE_FAILED) {
  75. error = true;
  76. _errorNumber = ESPhttpUpdate.getLastError();
  77. _errorString = ESPhttpUpdate.getLastErrorString();
  78. _callback(AUTO_OTA_FILESYSTEM_UPDATE_ERROR);
  79. } else if (ret == HTTP_UPDATE_OK) {
  80. updates++;
  81. _callback(AUTO_OTA_FILESYSTEM_UPDATED);
  82. }
  83. }
  84. if (!error && (_newFirmware.length() > 0)) {
  85. // Update binary
  86. sprintf(url, "%s%s", _server.c_str(), _newFirmware.c_str());
  87. t_httpUpdate_return ret = ESPhttpUpdate.update(url);
  88. if (ret == HTTP_UPDATE_FAILED) {
  89. error = true;
  90. _errorNumber = ESPhttpUpdate.getLastError();
  91. _errorString = ESPhttpUpdate.getLastErrorString();
  92. _callback(AUTO_OTA_FIRMWARE_UPDATE_ERROR);
  93. } else if (ret == HTTP_UPDATE_OK) {
  94. updates++;
  95. _callback(AUTO_OTA_FIRMWARE_UPDATED);
  96. }
  97. }
  98. if (!error && (updates > 0)) {
  99. _callback(AUTO_OTA_RESET);
  100. ESP.restart();
  101. }
  102. }
  103. void AutoOTAClass::handle() {
  104. _callback(AUTO_OTA_START);
  105. if (_checkUpdates()) _doUpdate();
  106. _callback(AUTO_OTA_END);
  107. }
  108. AutoOTAClass AutoOTA;