state machine top level should work, light not added yet
This commit is contained in:
@@ -17,6 +17,12 @@ Network::Network(std::string_view hostname, std::string_view apSsid) : hostname(
|
||||
webServer.begin();
|
||||
}
|
||||
|
||||
Network::~Network() {
|
||||
webServer.end();
|
||||
WifiManager.detachUI();
|
||||
WifiManager.detachWebServer();
|
||||
}
|
||||
|
||||
bool Network::isConnected() const {
|
||||
return WiFi.status() == WL_CONNECTED;
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
class Network {
|
||||
public:
|
||||
Network(std::string_view hostname, std::string_view apSsid = "Smart RGB");
|
||||
~Network();
|
||||
bool isConnected() const;
|
||||
std::string getHostname() const;
|
||||
bool setHostname(const std::string_view &hostname);
|
||||
|
||||
@@ -56,10 +56,8 @@ void setup() {
|
||||
stateMachine->addTransition(StateId::StartState, EventId::PinInitialized, StateId::NetworkInitializeState);
|
||||
stateMachine->addStateRaw(runningState);
|
||||
stateMachine->addTransition(StateId::NetworkInitializeState, EventId::WifiConnected, StateId::RunningState);
|
||||
stateMachine->addTransition(StateId::RunningState, EventId::WifiDisconnected, StateId::NetworkInitializeState);
|
||||
stateMachine->setInitialState(StateId::StartState);
|
||||
|
||||
// Mqtt::connect("10.238.75.81", 1883, "smart_rgb_client", "mqtt", "mqtt");
|
||||
// delay(1000); // Wait for MQTT connection to stabilize
|
||||
// light = new Light(pinR, pinG, pinB, pinCW, pinWW, mqttClient, "smart_rgb_light");
|
||||
}
|
||||
|
||||
|
||||
@@ -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