@ -0,0 +1,145 @@ | |||
#include "AutoOTA.h" | |||
#include <functional> | |||
#include <ArduinoJson.h> | |||
#include <ESP8266httpUpdate.h> | |||
void AutoOTAClass::setServer(String server) { | |||
_server = server; | |||
} | |||
void AutoOTAClass::setModel(String model) { | |||
_model = model; | |||
} | |||
void AutoOTAClass::setVersion(String version) { | |||
_version = version; | |||
} | |||
void AutoOTAClass::onMessage(TMessageFunction fn) { | |||
_callback = fn; | |||
} | |||
String AutoOTAClass::getNewVersion() { | |||
return _newVersion; | |||
} | |||
String AutoOTAClass::getNewFirmware() { | |||
return _newFirmware; | |||
} | |||
String AutoOTAClass::getNewFileSystem() { | |||
return _newFileSystem; | |||
} | |||
int AutoOTAClass::getErrorNumber() { | |||
return _errorNumber; | |||
} | |||
String AutoOTAClass::getErrorString() { | |||
return _errorString; | |||
} | |||
String AutoOTAClass::_getPayload() { | |||
HTTPClient http; | |||
char url[100]; | |||
String payload = ""; | |||
_callback(AUTO_OTA_START); | |||
sprintf(url, "%s/%s/%s", _server.c_str(), _model.c_str(), _version.c_str()); | |||
http.begin(url); | |||
int httpCode = http.GET(); | |||
if (httpCode > 0) payload = http.getString(); | |||
http.end(); | |||
return payload; | |||
} | |||
bool AutoOTAClass::_checkUpdates() { | |||
String payload = _getPayload(); | |||
if (payload.length() == 0) { | |||
_callback(AUTO_OTA_NO_RESPONSE_ERROR); | |||
return false; | |||
} | |||
StaticJsonBuffer<500> jsonBuffer; | |||
JsonObject& response = jsonBuffer.parseObject(payload); | |||
if (!response.success()) { | |||
_callback(AUTO_OTA_PARSE_ERROR); | |||
return false; | |||
} | |||
if (response.size() == 0) { | |||
_callback(AUTO_OTA_UPTODATE); | |||
return false; | |||
} | |||
_newVersion = response.get<String>("version"); | |||
_newFileSystem = response.get<String>("spiffs"); | |||
_newFirmware = response.get<String>("firmware"); | |||
_callback(AUTO_OTA_UPDATING); | |||
return true; | |||
} | |||
void AutoOTAClass::_doUpdate() { | |||
char url[100]; | |||
bool error = false; | |||
uint8_t updates = 0; | |||
if (_newFileSystem.length() > 0) { | |||
// Update SPIFFS | |||
sprintf(url, "%s/%s", _server.c_str(), _newFileSystem.c_str()); | |||
t_httpUpdate_return ret = ESPhttpUpdate.updateSpiffs(url); | |||
if (ret == HTTP_UPDATE_FAILED) { | |||
error = true; | |||
_errorNumber = ESPhttpUpdate.getLastError(); | |||
_errorString = ESPhttpUpdate.getLastErrorString(); | |||
_callback(AUTO_OTA_FILESYSTEM_UPDATE_ERROR); | |||
} else if (ret == HTTP_UPDATE_OK) { | |||
updates++; | |||
_callback(AUTO_OTA_FILESYSTEM_UPDATED); | |||
} | |||
} | |||
if (!error && (_newFirmware.length() > 0)) { | |||
// Update binary | |||
sprintf(url, "%s%s", _server.c_str(), _newFirmware.c_str()); | |||
t_httpUpdate_return ret = ESPhttpUpdate.update(url); | |||
if (ret == HTTP_UPDATE_FAILED) { | |||
error = true; | |||
_errorNumber = ESPhttpUpdate.getLastError(); | |||
_errorString = ESPhttpUpdate.getLastErrorString(); | |||
_callback(AUTO_OTA_FIRMWARE_UPDATE_ERROR); | |||
} else if (ret == HTTP_UPDATE_OK) { | |||
updates++; | |||
_callback(AUTO_OTA_FIRMWARE_UPDATED); | |||
} | |||
} | |||
if (!error && (updates > 0)) { | |||
_callback(AUTO_OTA_RESET); | |||
ESP.restart(); | |||
} | |||
} | |||
void AutoOTAClass::handle() { | |||
_callback(AUTO_OTA_START); | |||
if (_checkUpdates()) _doUpdate(); | |||
_callback(AUTO_OTA_END); | |||
} | |||
AutoOTAClass AutoOTA; |
@ -0,0 +1,65 @@ | |||
#ifndef _AUTO_OTA_h | |||
#define _AUTO_OTA_h | |||
#include <functional> | |||
#include <ArduinoJson.h> | |||
#include <ESP8266httpUpdate.h> | |||
typedef enum { | |||
AUTO_OTA_START, | |||
AUTO_OTA_UPTODATE, | |||
AUTO_OTA_UPDATING, | |||
AUTO_OTA_FILESYSTEM_UPDATED, | |||
AUTO_OTA_FIRMWARE_UPDATED, | |||
AUTO_OTA_RESET, | |||
AUTO_OTA_END, | |||
AUTO_OTA_NO_RESPONSE_ERROR, | |||
AUTO_OTA_PARSE_ERROR, | |||
AUTO_OTA_FILESYSTEM_UPDATE_ERROR, | |||
AUTO_OTA_FIRMWARE_UPDATE_ERROR | |||
} auto_ota_t; | |||
class AutoOTAClass { | |||
public: | |||
typedef std::function<void(auto_ota_t)> TMessageFunction; | |||
void setServer(String server); | |||
void setModel(String model); | |||
void setVersion(String version); | |||
String getNewVersion(); | |||
String getNewFirmware(); | |||
String getNewFileSystem(); | |||
int getErrorNumber(); | |||
String getErrorString(); | |||
void onMessage(TMessageFunction fn); | |||
void handle(); | |||
private: | |||
String _server; | |||
String _model; | |||
String _version; | |||
String _newVersion; | |||
String _newFirmware; | |||
String _newFileSystem; | |||
int _errorNumber; | |||
String _errorString; | |||
TMessageFunction _callback; | |||
String _getPayload(); | |||
bool _checkUpdates(); | |||
void _doUpdate(); | |||
}; | |||
extern AutoOTAClass AutoOTA; | |||
#endif /* _AUTO_OTA_h */ |
@ -1,14 +1,14 @@ | |||
[ | |||
{ | |||
"model": "SONOFF", | |||
"firmware": { | |||
"origin": { | |||
"model": "SONOFF", | |||
"min": "*", | |||
"max": "0.9.2" | |||
}, | |||
"target": { | |||
"version": "0.9.2", | |||
"firmware": "/firmware/espurna-0.9.2.bin", | |||
"spiffs": "/firmware/espurna-0.9.1-spiffs.bin" | |||
"version": "0.9.3", | |||
"firmware": "/firmware/espurna-0.9.3.bin", | |||
"spiffs": "/firmware/espurna-0.9.3-spiffs.bin" | |||
} | |||
} | |||
] |
@ -1,4 +1,10 @@ | |||
<?php | |||
// Application middleware | |||
// e.g: $app->add(new \Slim\Csrf\Guard); | |||
$container = $app->getContainer(); | |||
$settings = $container->get('settings'); | |||
$app->add(new RKA\Middleware\IpAddress( | |||
$settings['rka']['check_proxy_headers'], | |||
$settings['rka']['trusted_proxies'] | |||
)); |
@ -1,57 +0,0 @@ | |||
<!DOCTYPE html> | |||
<html> | |||
<head> | |||
<meta charset="utf-8"/> | |||
<title>ESPurna Update Server</title> | |||
<link href='//fonts.googleapis.com/css?family=Lato:300' rel='stylesheet' type='text/css'> | |||
<style> | |||
body { | |||
margin: 50px 0 0 0; | |||
padding: 0; | |||
width: 100%; | |||
font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; | |||
text-align: center; | |||
color: #aaa; | |||
font-size: 18px; | |||
} | |||
h1 { | |||
color: #719e40; | |||
letter-spacing: -3px; | |||
font-family: 'Lato', sans-serif; | |||
font-size: 100px; | |||
font-weight: 200; | |||
margin-bottom: 0; | |||
} | |||
</style> | |||
</head> | |||
<body> | |||
<h1>ESPurna Update Server</h1> | |||
<table> | |||
<thead> | |||
<tr> | |||
<th>Device</th> | |||
<th>Minimum Hardware Version</th> | |||
<th>Maximum Hardware Version</th> | |||
<th>Minimum Firmware Version</th> | |||
<th>Maximum Firmware Version</th> | |||
<th>Latest Firmware Version</th> | |||
</tr> | |||
</thead> | |||
<tbody> | |||
{% for version in versions %} | |||
<tr> | |||
<td>{{ version.model }}</td> | |||
<td>{{ version.hardware_min }}</td> | |||
<td>{{ version.hardware_max }}</td> | |||
<td>{{ version.firmware_min }}</td> | |||
<td>{{ version.firmware_max }}</td> | |||
<td>{{ version.firmware_version }}</td> | |||
</tr> | |||
{% endfor %} | |||
</tbody> | |||
</table> | |||
</body> | |||
</html> |