style: fix indentation and formatting in doorbell code
This commit is contained in:
@@ -20,7 +20,7 @@ void setup() {
|
|||||||
logic.finishBoot();
|
logic.finishBoot();
|
||||||
}
|
}
|
||||||
|
|
||||||
// void loop() {
|
void loop() {
|
||||||
TouchEvent evt = display.readTouch();
|
TouchEvent evt = display.readTouch();
|
||||||
|
|
||||||
logic.update();
|
logic.update();
|
||||||
@@ -55,13 +55,6 @@ if(st.deviceState == DeviceState::ALERTING) {
|
|||||||
// Or: logic.dismissAlert();
|
// Or: logic.dismissAlert();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Serial console (unchanged)
|
|
||||||
if(Serial.available()) {
|
|
||||||
// ...
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// ── Serial console ──
|
|
||||||
if(Serial.available()) {
|
if(Serial.available()) {
|
||||||
String cmd = Serial.readStringUntil('\n');
|
String cmd = Serial.readStringUntil('\n');
|
||||||
cmd.trim();
|
cmd.trim();
|
||||||
|
|||||||
@@ -53,8 +53,8 @@ void DoorbellLogic::begin(const char* version, const char* boardName,
|
|||||||
Serial.printf("[CONFIG] ADMIN_URL: %s\n", _adminUrl.c_str());
|
Serial.printf("[CONFIG] ADMIN_URL: %s\n", _adminUrl.c_str());
|
||||||
|
|
||||||
// Boot status
|
// Boot status
|
||||||
flushStatus(String("BOOTED — ") + _net.getSSID() + " "
|
flushStatus(String("BOOTED — ") + _net.getSSID() + " " + _net.getIP()
|
||||||
+ _net.getIP() + " RSSI:" + String(_net.getRSSI()));
|
+ " RSSI:" + String(_net.getRSSI()));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DoorbellLogic::finishBoot() {
|
void DoorbellLogic::finishBoot() {
|
||||||
@@ -78,7 +78,8 @@ void DoorbellLogic::update() {
|
|||||||
_state.wifiSsid = _net.getSSID();
|
_state.wifiSsid = _net.getSSID();
|
||||||
_state.ipAddr = _net.getIP();
|
_state.ipAddr = _net.getIP();
|
||||||
}
|
}
|
||||||
if (!_net.checkConnection()) return;
|
if(!_net.checkConnection())
|
||||||
|
return;
|
||||||
|
|
||||||
// Poll
|
// Poll
|
||||||
if(now - _lastPollMs >= POLL_INTERVAL_MS) {
|
if(now - _lastPollMs >= POLL_INTERVAL_MS) {
|
||||||
@@ -112,7 +113,8 @@ void DoorbellLogic::update() {
|
|||||||
_state.backlightOn = false;
|
_state.backlightOn = false;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
default: break;
|
default:
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,32 +130,39 @@ void DoorbellLogic::pollTopics() {
|
|||||||
void DoorbellLogic::pollTopic(const String& url, const char* label) {
|
void DoorbellLogic::pollTopic(const String& url, const char* label) {
|
||||||
String body;
|
String body;
|
||||||
int code = _net.httpGet(url.c_str(), body);
|
int code = _net.httpGet(url.c_str(), body);
|
||||||
if (code != 200 || body.length() == 0) return;
|
if(code != 200 || body.length() == 0)
|
||||||
|
return;
|
||||||
|
|
||||||
// ntfy returns newline-delimited JSON
|
// ntfy returns newline-delimited JSON
|
||||||
int pos = 0;
|
int pos = 0;
|
||||||
while(pos < (int)body.length()) {
|
while(pos < (int)body.length()) {
|
||||||
int nl = body.indexOf('\n', pos);
|
int nl = body.indexOf('\n', pos);
|
||||||
if (nl < 0) nl = body.length();
|
if(nl < 0)
|
||||||
|
nl = body.length();
|
||||||
String line = body.substring(pos, nl);
|
String line = body.substring(pos, nl);
|
||||||
line.trim();
|
line.trim();
|
||||||
pos = nl + 1;
|
pos = nl + 1;
|
||||||
if (line.length() == 0) continue;
|
if(line.length() == 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
JsonDocument doc;
|
JsonDocument doc;
|
||||||
if (deserializeJson(doc, line)) continue;
|
if(deserializeJson(doc, line))
|
||||||
|
continue;
|
||||||
|
|
||||||
const char* evt = doc["event"] | "";
|
const char* evt = doc["event"] | "";
|
||||||
if (strcmp(evt, "message") != 0) continue;
|
if(strcmp(evt, "message") != 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
const char* title = doc["title"] | "";
|
const char* title = doc["title"] | "";
|
||||||
const char* message = doc["message"] | "";
|
const char* message = doc["message"] | "";
|
||||||
Serial.printf("[%s] title=\"%s\" message=\"%s\"\n",
|
Serial.printf("[%s] title=\"%s\" message=\"%s\"\n", label, title, message);
|
||||||
label, title, message);
|
|
||||||
|
|
||||||
if (strcmp(label, "ALERT") == 0) onAlert(String(title), String(message));
|
if(strcmp(label, "ALERT") == 0)
|
||||||
else if (strcmp(label, "SILENCE") == 0) onSilence();
|
onAlert(String(title), String(message));
|
||||||
else if (strcmp(label, "ADMIN") == 0) onAdmin(String(message));
|
else if(strcmp(label, "SILENCE") == 0)
|
||||||
|
onSilence();
|
||||||
|
else if(strcmp(label, "ADMIN") == 0)
|
||||||
|
onAdmin(String(message));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -176,7 +185,8 @@ void DoorbellLogic::onAlert(const String& title, const String& body) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void DoorbellLogic::onSilence() {
|
void DoorbellLogic::onSilence() {
|
||||||
if (_state.deviceState != DeviceState::ALERTING) return;
|
if(_state.deviceState != DeviceState::ALERTING)
|
||||||
|
return;
|
||||||
Serial.println("[SILENCE] Alert silenced");
|
Serial.println("[SILENCE] Alert silenced");
|
||||||
_state.silenceStartMs = millis();
|
_state.silenceStartMs = millis();
|
||||||
transition(DeviceState::SILENCED);
|
transition(DeviceState::SILENCED);
|
||||||
@@ -219,8 +229,7 @@ void DoorbellLogic::flushStatus(const String& message) {
|
|||||||
|
|
||||||
void DoorbellLogic::heartbeat() {
|
void DoorbellLogic::heartbeat() {
|
||||||
String m = String("HEARTBEAT ") + deviceStateStr(_state.deviceState)
|
String m = String("HEARTBEAT ") + deviceStateStr(_state.deviceState)
|
||||||
+ " up:" + String(millis() / 1000) + "s"
|
+ " up:" + String(millis() / 1000) + "s" + " RSSI:" + String(_net.getRSSI())
|
||||||
+ " RSSI:" + String(_net.getRSSI())
|
|
||||||
+ " heap:" + String(ESP.getFreeHeap());
|
+ " heap:" + String(ESP.getFreeHeap());
|
||||||
#ifdef BOARD_HAS_PSRAM
|
#ifdef BOARD_HAS_PSRAM
|
||||||
m += " psram:" + String(ESP.getFreePsram());
|
m += " psram:" + String(ESP.getFreePsram());
|
||||||
@@ -237,26 +246,28 @@ void DoorbellLogic::transition(DeviceState s) {
|
|||||||
|
|
||||||
void DoorbellLogic::onSerialCommand(const String& cmd) {
|
void DoorbellLogic::onSerialCommand(const String& cmd) {
|
||||||
Serial.printf("[CMD] %s\n", cmd.c_str());
|
Serial.printf("[CMD] %s\n", cmd.c_str());
|
||||||
if (cmd == "alert") onAlert("Test Alert", "Serial test");
|
if(cmd == "alert")
|
||||||
else if (cmd == "silence") onSilence();
|
onAlert("Test Alert", "Serial test");
|
||||||
else if (cmd == "reboot") ESP.restart();
|
else if(cmd == "silence")
|
||||||
else if (cmd == "dashboard") onAdmin("dashboard");
|
onSilence();
|
||||||
else if (cmd == "off") onAdmin("off");
|
else if(cmd == "reboot")
|
||||||
|
ESP.restart();
|
||||||
|
else if(cmd == "dashboard")
|
||||||
|
onAdmin("dashboard");
|
||||||
|
else if(cmd == "off")
|
||||||
|
onAdmin("off");
|
||||||
else if(cmd == "status") {
|
else if(cmd == "status") {
|
||||||
Serial.printf("[STATE] %s screen:%s bl:%s\n",
|
Serial.printf("[STATE] %s screen:%s bl:%s\n", deviceStateStr(_state.deviceState),
|
||||||
deviceStateStr(_state.deviceState),
|
screenIdStr(_state.screen), _state.backlightOn ? "ON" : "OFF");
|
||||||
screenIdStr(_state.screen),
|
|
||||||
_state.backlightOn ? "ON" : "OFF");
|
|
||||||
Serial.printf("[MEM] heap:%d", ESP.getFreeHeap());
|
Serial.printf("[MEM] heap:%d", ESP.getFreeHeap());
|
||||||
#ifdef BOARD_HAS_PSRAM
|
#ifdef BOARD_HAS_PSRAM
|
||||||
Serial.printf(" psram:%d", ESP.getFreePsram());
|
Serial.printf(" psram:%d", ESP.getFreePsram());
|
||||||
#endif
|
#endif
|
||||||
Serial.println();
|
Serial.println();
|
||||||
Serial.printf("[NET] %s RSSI:%d IP:%s\n",
|
Serial.printf("[NET] %s RSSI:%d IP:%s\n", _state.wifiSsid.c_str(), _state.wifiRssi,
|
||||||
_state.wifiSsid.c_str(), _state.wifiRssi,
|
|
||||||
_state.ipAddr.c_str());
|
_state.ipAddr.c_str());
|
||||||
}
|
} else
|
||||||
else Serial.println(F("[CMD] alert|silence|reboot|dashboard|off|status"));
|
Serial.println(F("[CMD] alert|silence|reboot|dashboard|off|status"));
|
||||||
}
|
}
|
||||||
|
|
||||||
void DoorbellLogic::setScreen(ScreenID s) {
|
void DoorbellLogic::setScreen(ScreenID s) {
|
||||||
|
|||||||
Reference in New Issue
Block a user