state machine top level should work, light not added yet
This commit is contained in:
@@ -15,7 +15,8 @@ enum class StateId
|
||||
enum class EventId
|
||||
{
|
||||
PinInitialized,
|
||||
WifiConnected
|
||||
WifiConnected,
|
||||
WifiDisconnected
|
||||
};
|
||||
|
||||
class StartState : public State
|
||||
@@ -50,7 +51,12 @@ public:
|
||||
NetworkInitializeState(AppContext *appContext) : State("NetworkInitializeState", StateId::NetworkInitializeState), appContext(appContext) {}
|
||||
|
||||
void onEnter(StateMachineBase &stateMachine) override {
|
||||
if (appContext) {
|
||||
Serial.println("Entering NetworkInitializeState");
|
||||
if (appContext && appContext->network) {
|
||||
delete appContext->network;
|
||||
appContext->network = nullptr;
|
||||
}
|
||||
if (appContext && !appContext->network) {
|
||||
appContext->network = new Network(hostName, friendlyName);
|
||||
}
|
||||
}
|
||||
@@ -58,9 +64,11 @@ public:
|
||||
void onExit(StateMachineBase &stateMachine) override {
|
||||
if (appContext && appContext->network) {
|
||||
appContext->network->registerMDNS();
|
||||
appContext->otaHandler = new OTAHandler(hostName);
|
||||
if (!appContext->otaHandler) {
|
||||
appContext->otaHandler = new OTAHandler(hostName);
|
||||
}
|
||||
}
|
||||
Mqtt::connect(mqttBroker.data(), 1883, "smart_rgb_dev", "mqtt", "mqtt");
|
||||
Mqtt::connect(mqttBroker.data(), 1883, hostName.data(), "mqtt", "mqtt");
|
||||
}
|
||||
|
||||
void onUpdate(StateMachineBase &stateMachine) override {
|
||||
@@ -81,9 +89,11 @@ public:
|
||||
lastOtaPollMs = millis();
|
||||
lastMqttPollMs = millis();
|
||||
lastMqttCheckConnectionPollSecond = millis() / 1000;
|
||||
lastNetworkCheckPollMs = millis();
|
||||
}
|
||||
|
||||
void onExit(StateMachineBase &stateMachine) override {
|
||||
Serial.println("Exiting RunningState");
|
||||
}
|
||||
|
||||
void onUpdate(StateMachineBase &stateMachine) override {
|
||||
@@ -101,12 +111,20 @@ public:
|
||||
lastMqttCheckConnectionPollSecond = millis() / 1000;
|
||||
Mqtt::checkConnection();
|
||||
}
|
||||
if ((millis() - lastNetworkCheckPollMs) >= networkCheckPollInterval) {
|
||||
lastNetworkCheckPollMs = millis();
|
||||
if (appContext && appContext->network && !appContext->network->isConnected()) {
|
||||
stateMachine.postEvent(EventId::WifiDisconnected);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
AppContext *appContext = nullptr;
|
||||
uint32_t lastOtaPollMs = 0;
|
||||
static constexpr uint32_t otaPollInterval = 1000; // Poll every second
|
||||
uint32_t lastNetworkCheckPollMs = 0;
|
||||
static constexpr uint32_t networkCheckPollInterval = 5000; // Poll every 5 seconds
|
||||
uint32_t lastMqttPollMs = 0;
|
||||
static constexpr uint32_t mqttPollInterval = 100; // Poll every 100 milliseconds
|
||||
uint32_t lastMqttCheckConnectionPollSecond = 0;
|
||||
|
||||
Reference in New Issue
Block a user