Last update before rewrite

master
informatic 2016-12-04 23:45:25 +01:00
parent 81306827ad
commit 41edfc0953
4 changed files with 103 additions and 94 deletions

View File

@ -7,16 +7,13 @@ void Endpoint::bind(String _key, SpejsNode* _parent) {
}
void Endpoint::notify(String value) {
/*if(parent)
parent->notify(this, value);*/
if(parent)
parent->notify(key, value);
}
template <class T> void ValueEndpoint<T>::updateValue(T newValue) {
value = newValue;
// TODO parent->notify(this, String(value)) ?
if(parent)
parent->notify(key, String(value));
notify(String(value));
}
template <class T> void ValueEndpoint<T>::fillValue(JsonObject& obj) {
@ -24,17 +21,80 @@ template <class T> void ValueEndpoint<T>::fillValue(JsonObject& obj) {
}
EndpointResult ControlEndpoint::onValue(String key, String value) {
// TODO
if (value == "ota") {
return 200;
return startOTA();
} else if(value == "restart") {
System.restart();
return 200;
} else {
return 400;
}
}
EndpointResult ControlEndpoint::startOTA() {
uint8_t slot;
rboot_config bootconf;
String romURL = OTA_URL + parent->deviceID + "/rom0.bin";
String spiffsURL = OTA_URL + parent->deviceID + "/spiff_rom.bin";
Serial.println("Updating...");
// need a clean object, otherwise if run before and failed will not run again
if (otaUpdater) delete otaUpdater;
otaUpdater = new rBootHttpUpdate();
bootconf = rboot_get_config();
if (parent->currentSlot == 0)
slot = 1;
else
slot = 0;
Serial.printf("Updating to rom %d.\r\n", slot);
// flash rom to position indicated in the rBoot config rom table
otaUpdater->addItem(bootconf.roms[slot], romURL);
#ifndef DISABLE_SPIFFS
// use user supplied values (defaults for 4mb flash in makefile)
if (slot == 0) {
otaUpdater->addItem(RBOOT_SPIFFS_0, spiffsURL);
} else {
otaUpdater->addItem(RBOOT_SPIFFS_1, spiffsURL);
}
#endif
otaUpdater->setCallback(otaUpdateDelegate(&ControlEndpoint::otaUpdateCallback, this));
otaUpdater->start();
//notify("ota", "started");
return 200;
}
void ControlEndpoint::otaUpdateCallback(bool result) {
if(result == true) {
// success
//notify("ota", "finished");
Serial.println("ota finished");
uint8 slot;
if (parent->currentSlot == 0)
slot = 1;
else
slot = 0;
// set to boot new rom and then reboot
Serial.printf("Firmware updated, rebooting to rom %d...\r\n", slot);
rboot_set_temp_rom(slot);
System.restart();
} else {
Serial.println("ota failed");
//notify("ota", "failed");
}
}
void OutputEndpoint::fillValue(JsonObject& obj) {
obj["value"] = currentValue;
}
@ -44,8 +104,8 @@ EndpointResult OutputEndpoint::onValue(String key, String value) {
currentValue = 1;
} else if (value == "0" or value == "off") {
currentValue = 0;
} else if (value == "toggle") {
currentValue = !currentValue;
//} else if (value == "toggle") {
// currentValue = !currentValue;
} else {
return 400;
}

View File

@ -39,6 +39,12 @@ class ControlEndpoint : public Endpoint {
public:
ControlEndpoint() : Endpoint("control") {}
EndpointResult onValue(String key, String value);
void otaUpdateCallback(bool result);
protected:
rBootHttpUpdate* otaUpdater = 0;
EndpointResult startOTA();
};
class OutputEndpoint : public Endpoint {

View File

@ -29,10 +29,17 @@ void SpejsNode::init() {
}
void SpejsNode::keepAliveHandler() {
static int failureCounter = 0;
if(mqtt.getConnectionState() != eTCS_Connected) {
Serial.println("Reconnecting");
onConnected();
if(failureCounter++ < 5)
onConnected();
else
System.restart();
} else {
failureCounter = 0;
uint8_t mode;
if(rboot_get_last_boot_mode(&mode)) {
if(mode == MODE_TEMP_ROM) {
@ -171,8 +178,10 @@ void SpejsNode::onConnected() {
bool SpejsNode::notify(String key, String value) {
if(mqtt.getConnectionState() == eTCS_Connected) {
mqtt.publish(TOPIC_PREFIX + deviceID + "/" + key, value, true);
mqtt.publish(TOPIC_PREFIX + deviceID + "/" + key + "/state", value, true);
return true;
} else {
Serial.println("MQTT Not Connected!!!");
}
return false;
@ -199,76 +208,3 @@ void SpejsNode::mqttCallback(String origtopic, String value) {
Serial.println("unknown topic?");
}
}
void SpejsNode::controlHandler(String key, String value) {
Serial.println("Control command: " + value);
if(value == "ota") {
startOTA();
} else if(value == "restart") {
System.restart();
} else {
Serial.println("Invalid command");
}
}
void SpejsNode::startOTA() {
uint8_t slot;
rboot_config bootconf;
String romURL = OTA_URL + deviceID + "/rom0.bin";
String spiffsURL = OTA_URL + deviceID + "/spiff_rom.bin";
Serial.println("Updating...");
// need a clean object, otherwise if run before and failed will not run again
if (otaUpdater) delete otaUpdater;
otaUpdater = new rBootHttpUpdate();
bootconf = rboot_get_config();
if (currentSlot == 0)
slot = 1;
else
slot = 0;
Serial.printf("Updating to rom %d.\r\n", slot);
// flash rom to position indicated in the rBoot config rom table
otaUpdater->addItem(bootconf.roms[slot], romURL);
#ifndef DISABLE_SPIFFS
// use user supplied values (defaults for 4mb flash in makefile)
if (slot == 0) {
otaUpdater->addItem(RBOOT_SPIFFS_0, spiffsURL);
} else {
otaUpdater->addItem(RBOOT_SPIFFS_1, spiffsURL);
}
#endif
otaUpdater->setCallback(otaUpdateDelegate(&SpejsNode::otaUpdateCallback, this));
otaUpdater->start();
notify("ota", "started");
}
void SpejsNode::otaUpdateCallback(bool result) {
if(result == true) {
// success
notify("ota", "finished");
uint8 slot;
if (currentSlot == 0)
slot = 1;
else
slot = 0;
// set to boot new rom and then reboot
Serial.printf("Firmware updated, rebooting to rom %d...\r\n", slot);
rboot_set_temp_rom(slot);
System.restart();
} else {
notify("ota", "failed");
}
}

View File

@ -6,28 +6,36 @@
#include <SmingCore/SmingCore.h>
#include <Endpoint.h>
#define D0 16
#define D1 5
#define D2 4
#define D3 0
#define D4 2
#define D5 14
#define D6 12
#define D7 13
#define D8 15
class SpejsNode {
protected:
String deviceID;
String deviceType;
MqttClient mqtt;
HttpServer http;
Timer keepaliveTimer;
rBootHttpUpdate* otaUpdater = 0;
HashMap<String, Endpoint*> endpoints;
void onConnected();
void startOTA();
void keepAliveHandler();
uint8_t currentSlot;
void buildMetadata(JsonObjectStream* stream);
public:
String deviceID;
String deviceType;
uint8_t currentSlot;
SpejsNode(String _deviceType) :
mqtt(MQTT_BROKER, MQTT_PORT, MqttStringSubscriptionCallback(&SpejsNode::mqttCallback, this)),
deviceType(_deviceType) {};
@ -37,7 +45,6 @@ public:
bool notify(String key, String value);
void registerEndpoint(String key, Endpoint* cb);
void mqttCallback(String, String);
void controlHandler(String, String);
void otaUpdateCallback(bool result);
void httpFile(HttpRequest &request, HttpResponse &response);
void httpIndex(HttpRequest &request, HttpResponse &response);