Skip to content

Commit

Permalink
Implement #125
Browse files Browse the repository at this point in the history
  • Loading branch information
marvinroger committed Aug 19, 2016
1 parent 65bbe76 commit c5c7f43
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 26 deletions.
36 changes: 27 additions & 9 deletions src/Homie.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ HomieClass::HomieClass()
: _setupCalled(false)
, __HOMIE_SIGNATURE("\x25\x48\x4f\x4d\x49\x45\x5f\x45\x53\x50\x38\x32\x36\x36\x5f\x46\x57\x25") {
strcpy(_interface.brand, DEFAULT_BRAND);
_interface.standalone = false;
strcpy(_interface.firmware.name, DEFAULT_FW_NAME);
strcpy(_interface.firmware.version, DEFAULT_FW_VERSION);
_interface.led.enabled = true;
Expand All @@ -21,7 +22,7 @@ HomieClass::HomieClass()
_interface.setupFunction = []() {};
_interface.loopFunction = []() {};
_interface.eventHandler = [](HomieEvent event) {};
_interface.readyToOperate = false;
_interface.connected = false;
_interface.logger = &_logger;
_interface.blinker = &_blinker;
_interface.config = &_config;
Expand All @@ -32,6 +33,7 @@ HomieClass::HomieClass()
_config.attachInterface(&_interface);
_blinker.attachInterface(&_interface);

_bootStandalone.attachInterface(&_interface);
_bootNormal.attachInterface(&_interface);
_bootConfig.attachInterface(&_interface);
}
Expand All @@ -53,9 +55,15 @@ void HomieClass::setup() {
_setupCalled = true;

if (!_config.load()) {
_boot = &_bootConfig;
_logger.logln(F("Triggering HOMIE_CONFIGURATION_MODE event..."));
_interface.eventHandler(HOMIE_CONFIGURATION_MODE);
if (_interface.standalone && !_config.canBypassStandalone()) {
_boot = &_bootStandalone;
_logger.logln(F("Triggering HOMIE_STANDALONE_MODE event..."));
_interface.eventHandler(HOMIE_STANDALONE_MODE);
} else {
_boot = &_bootConfig;
_logger.logln(F("Triggering HOMIE_CONFIGURATION_MODE event..."));
_interface.eventHandler(HOMIE_CONFIGURATION_MODE);
}
} else {
switch (_config.getBootMode()) {
case BOOT_NORMAL:
Expand Down Expand Up @@ -129,10 +137,6 @@ void HomieClass::__setBrand(const char* brand) {
_interface.brand[strlen(brand) - 10] = '\0';
}

bool HomieClass::isReadyToOperate() const {
return _interface.readyToOperate;
}

void HomieClass::setResettable(bool resettable) {
_interface.reset.able = resettable;
}
Expand Down Expand Up @@ -161,6 +165,20 @@ void HomieClass::setLoopFunction(OperationFunction function) {
_interface.loopFunction = function;
}

void HomieClass::setStandalone() {
_checkBeforeSetup(F("setStandalone"));

_interface.standalone = true;
}

bool HomieClass::isConfigured() const {
return _config.getBootMode() == BOOT_NORMAL;
}

bool HomieClass::isConnected() const {
return _interface.connected;
}

void HomieClass::onEvent(EventHandler handler) {
_checkBeforeSetup(F("onEvent"));

Expand All @@ -187,7 +205,7 @@ void HomieClass::eraseConfig() {
}

void HomieClass::setNodeProperty(const HomieNode& node, const char* property, const char* value, uint8_t qos, bool retained) {
if (!isReadyToOperate()) {
if (!isConnected()) {
_logger.logln(F("✖ setNodeProperty(): impossible now"));
return;
}
Expand Down
6 changes: 5 additions & 1 deletion src/Homie.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "Homie/Limits.hpp"
#include "Homie/Helpers.hpp"
#include "Homie/Boot/Boot.hpp"
#include "Homie/Boot/BootStandalone.hpp"
#include "Homie/Boot/BootNormal.hpp"
#include "Homie/Boot/BootConfig.hpp"

Expand Down Expand Up @@ -43,7 +44,9 @@ class HomieClass {
void eraseConfig();
void setSetupFunction(OperationFunction function);
void setLoopFunction(OperationFunction function);
bool isReadyToOperate() const;
void setStandalone();
bool isConfigured() const;
bool isConnected() const;
void setNodeProperty(const HomieNode& node, const String& property, const String& value, uint8_t qos = 1, bool retained = true) {
setNodeProperty(node, property.c_str(), value.c_str(), qos, retained);
}
Expand All @@ -54,6 +57,7 @@ class HomieClass {
private:
bool _setupCalled;
Boot* _boot;
BootStandalone _bootStandalone;
BootNormal _bootNormal;
BootConfig _bootConfig;
Interface _interface;
Expand Down
8 changes: 4 additions & 4 deletions src/Homie/Boot/BootNormal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ void BootNormal::_onWifiGotIp(const WiFiEventStationModeGotIP& event) {
}

void BootNormal::_onWifiDisconnected(const WiFiEventStationModeDisconnected& event) {
_interface->readyToOperate = false;
_interface->connected = false;
if (_interface->led.enabled) _interface->blinker->start(LED_WIFI_DELAY);
_uptimeTimer.reset();
_signalQualityTimer.reset();
Expand Down Expand Up @@ -145,7 +145,7 @@ void BootNormal::_onMqttConnected() {

_interface->mqttClient->publish(_prefixMqttTopic(PSTR("/$online")), 1, true, "true");

_interface->readyToOperate = true;
_interface->connected = true;
if (_interface->led.enabled) _interface->blinker->stop();

_interface->logger->logln(F("✔ MQTT ready"));
Expand All @@ -164,7 +164,7 @@ void BootNormal::_onMqttConnected() {
}

void BootNormal::_onMqttDisconnected(AsyncMqttClientDisconnectReason reason) {
_interface->readyToOperate = false;
_interface->connected = false;
if (!_mqttDisconnectNotified) {
_uptimeTimer.reset();
_signalQualityTimer.reset();
Expand Down Expand Up @@ -420,7 +420,7 @@ void BootNormal::loop() {
ESP.restart();
}

if (!_interface->readyToOperate) return;
if (!_interface->connected) return;

if (_signalQualityTimer.check()) {
uint8_t quality = Helpers::rssiToPercentage(WiFi.RSSI());
Expand Down
20 changes: 20 additions & 0 deletions src/Homie/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,26 @@ void Config::erase() {
if (!_spiffsBegin()) { return; }

SPIFFS.remove(CONFIG_FILE_PATH);
SPIFFS.remove(CONFIG_BYPASS_STANDALONE_FILE_PATH);
}

void Config::bypassStandalone() {
if (!_spiffsBegin()) { return; }

File bypassStandaloneFile = SPIFFS.open(CONFIG_BYPASS_STANDALONE_FILE_PATH, "w");
if (!bypassStandaloneFile) {
_interface->logger->logln(F("✖ Cannot open bypass standalone file"));
return;
}

bypassStandaloneFile.print("1");
bypassStandaloneFile.close();
}

bool Config::canBypassStandalone() {
if (!_spiffsBegin()) { return false; }

return SPIFFS.exists(CONFIG_BYPASS_STANDALONE_FILE_PATH);
}

void Config::write(const char* config) {
Expand Down
2 changes: 2 additions & 0 deletions src/Homie/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Config {
inline const ConfigStruct& get() const;
char* getSafeConfigFile() const;
void erase();
void bypassStandalone();
bool canBypassStandalone();
void write(const char* config);
bool patch(const char* patch);
BootMode getBootMode() const;
Expand Down
4 changes: 3 additions & 1 deletion src/Homie/Constants.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ namespace HomieInternals {
const float LED_MQTT_DELAY = 0.2;

const char CONFIG_UI_BUNDLE_PATH[] = "/homie/ui_bundle.gz";
const char CONFIG_BYPASS_STANDALONE_FILE_PATH[] = "/homie/BYPASS_STANDALONE";
const char CONFIG_FILE_PATH[] = "/homie/config.json";

enum BootMode : uint8_t {
BOOT_NORMAL = 1,
BOOT_STANDALONE = 1,
BOOT_NORMAL,
BOOT_CONFIG
};
} // namespace HomieInternals
4 changes: 3 additions & 1 deletion src/Homie/Datatypes/Interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ struct Interface {
/***** User configurable data *****/
char brand[MAX_BRAND_LENGTH];

bool standalone;

struct Firmware {
char name[MAX_FIRMWARE_NAME_LENGTH];
char version[MAX_FIRMWARE_VERSION_LENGTH];
Expand All @@ -40,7 +42,7 @@ struct Interface {
EventHandler eventHandler;

/***** Runtime data *****/
bool readyToOperate;
bool connected;
Logger* logger;
Blinker* blinker;
Config* config;
Expand Down
21 changes: 11 additions & 10 deletions src/HomieEvent.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
#pragma once

enum HomieEvent : uint8_t {
HOMIE_CONFIGURATION_MODE = 1,
HOMIE_NORMAL_MODE,
HOMIE_OTA_STARTED,
HOMIE_OTA_SUCCESSFUL,
HOMIE_OTA_FAILED,
HOMIE_ABOUT_TO_RESET,
HOMIE_WIFI_CONNECTED,
HOMIE_WIFI_DISCONNECTED,
HOMIE_MQTT_CONNECTED,
HOMIE_MQTT_DISCONNECTED
HOMIE_STANDALONE_MODE = 1,
HOMIE_CONFIGURATION_MODE,
HOMIE_NORMAL_MODE,
HOMIE_OTA_STARTED,
HOMIE_OTA_SUCCESSFUL,
HOMIE_OTA_FAILED,
HOMIE_ABOUT_TO_RESET,
HOMIE_WIFI_CONNECTED,
HOMIE_WIFI_DISCONNECTED,
HOMIE_MQTT_CONNECTED,
HOMIE_MQTT_DISCONNECTED
};

0 comments on commit c5c7f43

Please sign in to comment.