From 41edfc09539bbd463bb3d68875ef6c3b4917df7f Mon Sep 17 00:00:00 2001 From: Piotr Dobrowolski Date: Sun, 4 Dec 2016 23:45:25 +0100 Subject: [PATCH] Last update before rewrite --- spejsiot/Endpoint.cpp | 82 ++++++++++++++++++++++++++++++++++------ spejsiot/Endpoint.h | 6 +++ spejsiot/SpejsNode.cpp | 86 ++++++------------------------------------ spejsiot/SpejsNode.h | 23 +++++++---- 4 files changed, 103 insertions(+), 94 deletions(-) diff --git a/spejsiot/Endpoint.cpp b/spejsiot/Endpoint.cpp index 110ccd9..35d9e74 100644 --- a/spejsiot/Endpoint.cpp +++ b/spejsiot/Endpoint.cpp @@ -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 void ValueEndpoint::updateValue(T newValue) { value = newValue; - - // TODO parent->notify(this, String(value)) ? - if(parent) - parent->notify(key, String(value)); + notify(String(value)); } template void ValueEndpoint::fillValue(JsonObject& obj) { @@ -24,17 +21,80 @@ template void ValueEndpoint::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; } diff --git a/spejsiot/Endpoint.h b/spejsiot/Endpoint.h index b61bce1..925bb3d 100644 --- a/spejsiot/Endpoint.h +++ b/spejsiot/Endpoint.h @@ -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 { diff --git a/spejsiot/SpejsNode.cpp b/spejsiot/SpejsNode.cpp index b1737b3..a2b5955 100644 --- a/spejsiot/SpejsNode.cpp +++ b/spejsiot/SpejsNode.cpp @@ -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"); - } -} diff --git a/spejsiot/SpejsNode.h b/spejsiot/SpejsNode.h index 4e5588f..7de896f 100644 --- a/spejsiot/SpejsNode.h +++ b/spejsiot/SpejsNode.h @@ -6,28 +6,36 @@ #include #include +#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 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);