centralized system restart
This commit is contained in:
parent
933ce71db4
commit
b9d96d3f02
@ -1,6 +1,7 @@
|
||||
#include <LittleFS.h>
|
||||
#include "Patrix.h"
|
||||
#include "http.h"
|
||||
#include "system.h"
|
||||
|
||||
void setup() {
|
||||
Log.info("Startup.");
|
||||
@ -13,6 +14,7 @@ void setup() {
|
||||
}
|
||||
|
||||
void loop() {
|
||||
systemLoop();
|
||||
wifiLoop();
|
||||
mqttLoop();
|
||||
if (isBootDelayComplete()) {
|
||||
@ -21,6 +23,5 @@ void loop() {
|
||||
httpSetup();
|
||||
}
|
||||
patrixLoop();
|
||||
httpLoop();
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,12 +1,10 @@
|
||||
#include "http.h"
|
||||
#include "mqtt.h"
|
||||
#include "system.h"
|
||||
|
||||
#include <LittleFS.h>
|
||||
|
||||
AsyncWebServer server(80);
|
||||
|
||||
bool httpDoRestart = false;
|
||||
|
||||
void httpNotFound(AsyncWebServerRequest *request) {
|
||||
if (request->method() == HTTP_OPTIONS) {
|
||||
request->send(200);
|
||||
@ -16,7 +14,7 @@ void httpNotFound(AsyncWebServerRequest *request) {
|
||||
}
|
||||
|
||||
void httpRestart([[maybe_unused]] AsyncWebServerRequest *request) {
|
||||
httpDoRestart = true;
|
||||
systemRequestRestart();
|
||||
request->send(200, "application/json", "true");
|
||||
}
|
||||
|
||||
@ -30,10 +28,6 @@ void httpSetup() {
|
||||
server.begin();
|
||||
}
|
||||
|
||||
void httpLoop() {
|
||||
if (httpDoRestart) {
|
||||
Log.info("Restarting...");
|
||||
delay(500);
|
||||
EspClass::restart();
|
||||
}
|
||||
void httpStop() {
|
||||
server.end();
|
||||
}
|
||||
@ -7,6 +7,6 @@ extern AsyncWebServer server;
|
||||
|
||||
void httpSetup();
|
||||
|
||||
void httpLoop();
|
||||
void httpStop();
|
||||
|
||||
#endif
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include "PubSubClient.h"
|
||||
#include "system.h"
|
||||
#include <WiFiClient.h>
|
||||
|
||||
WiFiClient client;
|
||||
@ -29,19 +30,15 @@ void mqttCallback(char *topic, const uint8_t *payload, unsigned int length) {
|
||||
Log.info("HELP");
|
||||
Log.info(" %s", "help");
|
||||
Log.info(" %s", "info");
|
||||
Log.info(" %s", "reboot");
|
||||
Log.info(" %s", "restart");
|
||||
} else if (strcmp(message, "info") == 0) {
|
||||
Log.info("INFO");
|
||||
Log.info(" %-10s %s", "SSID:", WiFi.SSID().c_str());
|
||||
Log.info(" %-10s %s", "IP:", WiFi.localIP().toString().c_str());
|
||||
Log.info(" %-10s %d", "RSSI:", WiFi.RSSI());
|
||||
Log.info(" %-10s %s", "uptime:", uptimeString().c_str());
|
||||
} else if (strcmp(message, "reboot") == 0) {
|
||||
Log.info("rebooting...");
|
||||
delay(500);
|
||||
mqtt.disconnect();
|
||||
delay(500);
|
||||
EspClass::restart();
|
||||
} else if (strcmp(message, "restart") == 0) {
|
||||
systemRequestRestart();
|
||||
}
|
||||
}
|
||||
|
||||
@ -62,6 +59,10 @@ void mqttLoop() {
|
||||
}
|
||||
}
|
||||
|
||||
void mqttDisconnect() {
|
||||
mqtt.disconnect();
|
||||
}
|
||||
|
||||
void mqttPublishValue(const String& name, const int32_t value, const char *unit) {
|
||||
mqttPublishValue(name, String(value), unit);
|
||||
}
|
||||
|
||||
@ -28,6 +28,8 @@ extern const String cmdTopic;
|
||||
|
||||
void mqttLoop();
|
||||
|
||||
void mqttDisconnect();
|
||||
|
||||
void mqttPublishValue(const String& name, int32_t value, const char *unit);
|
||||
|
||||
void mqttPublishValue(const String& name, int64_t value, const char *unit);
|
||||
|
||||
24
src/patrix/system.cpp
Normal file
24
src/patrix/system.cpp
Normal file
@ -0,0 +1,24 @@
|
||||
#include "system.h"
|
||||
|
||||
#include "mqtt.h"
|
||||
#include "http.h"
|
||||
|
||||
bool systemRestart = false;
|
||||
|
||||
void systemRequestRestart() {
|
||||
systemRestart = true;
|
||||
}
|
||||
|
||||
void systemLoop() {
|
||||
if (systemRestart) {
|
||||
Log.info("Restarting...");
|
||||
delay(250);
|
||||
|
||||
httpStop();
|
||||
mqttDisconnect();
|
||||
wifiDisconnect();
|
||||
|
||||
delay(250);
|
||||
EspClass::restart();
|
||||
}
|
||||
}
|
||||
8
src/patrix/system.h
Normal file
8
src/patrix/system.h
Normal file
@ -0,0 +1,8 @@
|
||||
#ifndef HELLIGKEIT_SYSTEM_H
|
||||
#define HELLIGKEIT_SYSTEM_H
|
||||
|
||||
void systemRequestRestart();
|
||||
|
||||
void systemLoop();
|
||||
|
||||
#endif
|
||||
@ -28,15 +28,20 @@ void wifiConnect() {
|
||||
ArduinoOTA.onError([](const ota_error_t error) {
|
||||
const char *name;
|
||||
switch (error) {
|
||||
case OTA_AUTH_ERROR: name = "AUTH";
|
||||
case OTA_AUTH_ERROR:
|
||||
name = "AUTH";
|
||||
break;
|
||||
case OTA_BEGIN_ERROR: name = "BEGIN";
|
||||
case OTA_BEGIN_ERROR:
|
||||
name = "BEGIN";
|
||||
break;
|
||||
case OTA_CONNECT_ERROR: name = "CONNECT";
|
||||
case OTA_CONNECT_ERROR:
|
||||
name = "CONNECT";
|
||||
break;
|
||||
case OTA_RECEIVE_ERROR: name = "RECEIVE";
|
||||
case OTA_RECEIVE_ERROR:
|
||||
name = "RECEIVE";
|
||||
break;
|
||||
case OTA_END_ERROR: name = "END";
|
||||
case OTA_END_ERROR:
|
||||
name = "END";
|
||||
break;
|
||||
default:
|
||||
name = "[???]";
|
||||
@ -133,6 +138,10 @@ void wifiLoop() {
|
||||
timeLoop();
|
||||
}
|
||||
|
||||
void wifiDisconnect() {
|
||||
WiFi.disconnect();
|
||||
}
|
||||
|
||||
String getTimeString() {
|
||||
time_t now;
|
||||
time(&now);
|
||||
|
||||
@ -7,6 +7,8 @@ void wifiConnect();
|
||||
|
||||
void wifiLoop();
|
||||
|
||||
void wifiDisconnect();
|
||||
|
||||
String getTimeString();
|
||||
|
||||
bool isWifiConnected();
|
||||
|
||||
Loading…
Reference in New Issue
Block a user