style: fix indentation and formatting in doorbell code

This commit is contained in:
2026-02-17 03:19:12 -08:00
parent d61c9c60bf
commit 23712a152b
2 changed files with 123 additions and 119 deletions

View File

@@ -20,7 +20,7 @@ void setup() {
logic.finishBoot();
}
// void loop() {
void loop() {
TouchEvent evt = display.readTouch();
logic.update();
@@ -55,13 +55,6 @@ if(st.deviceState == DeviceState::ALERTING) {
// Or: logic.dismissAlert();
}
}
// Serial console (unchanged)
if(Serial.available()) {
// ...
}
}
// ── Serial console ──
if(Serial.available()) {
String cmd = Serial.readStringUntil('\n');
cmd.trim();

View File

@@ -53,8 +53,8 @@ void DoorbellLogic::begin(const char* version, const char* boardName,
Serial.printf("[CONFIG] ADMIN_URL: %s\n", _adminUrl.c_str());
// Boot status
flushStatus(String("BOOTED — ") + _net.getSSID() + " "
+ _net.getIP() + " RSSI:" + String(_net.getRSSI()));
flushStatus(String("BOOTED — ") + _net.getSSID() + " " + _net.getIP()
+ " RSSI:" + String(_net.getRSSI()));
}
void DoorbellLogic::finishBoot() {
@@ -78,7 +78,8 @@ void DoorbellLogic::update() {
_state.wifiSsid = _net.getSSID();
_state.ipAddr = _net.getIP();
}
if (!_net.checkConnection()) return;
if(!_net.checkConnection())
return;
// Poll
if(now - _lastPollMs >= POLL_INTERVAL_MS) {
@@ -112,7 +113,8 @@ void DoorbellLogic::update() {
_state.backlightOn = false;
}
break;
default: break;
default:
break;
}
}
@@ -128,32 +130,39 @@ void DoorbellLogic::pollTopics() {
void DoorbellLogic::pollTopic(const String& url, const char* label) {
String 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
int pos = 0;
while(pos < (int)body.length()) {
int nl = body.indexOf('\n', pos);
if (nl < 0) nl = body.length();
if(nl < 0)
nl = body.length();
String line = body.substring(pos, nl);
line.trim();
pos = nl + 1;
if (line.length() == 0) continue;
if(line.length() == 0)
continue;
JsonDocument doc;
if (deserializeJson(doc, line)) continue;
if(deserializeJson(doc, line))
continue;
const char* evt = doc["event"] | "";
if (strcmp(evt, "message") != 0) continue;
if(strcmp(evt, "message") != 0)
continue;
const char* title = doc["title"] | "";
const char* message = doc["message"] | "";
Serial.printf("[%s] title=\"%s\" message=\"%s\"\n",
label, title, message);
Serial.printf("[%s] title=\"%s\" message=\"%s\"\n", label, title, message);
if (strcmp(label, "ALERT") == 0) onAlert(String(title), String(message));
else if (strcmp(label, "SILENCE") == 0) onSilence();
else if (strcmp(label, "ADMIN") == 0) onAdmin(String(message));
if(strcmp(label, "ALERT") == 0)
onAlert(String(title), 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() {
if (_state.deviceState != DeviceState::ALERTING) return;
if(_state.deviceState != DeviceState::ALERTING)
return;
Serial.println("[SILENCE] Alert silenced");
_state.silenceStartMs = millis();
transition(DeviceState::SILENCED);
@@ -219,8 +229,7 @@ void DoorbellLogic::flushStatus(const String& message) {
void DoorbellLogic::heartbeat() {
String m = String("HEARTBEAT ") + deviceStateStr(_state.deviceState)
+ " up:" + String(millis() / 1000) + "s"
+ " RSSI:" + String(_net.getRSSI())
+ " up:" + String(millis() / 1000) + "s" + " RSSI:" + String(_net.getRSSI())
+ " heap:" + String(ESP.getFreeHeap());
#ifdef BOARD_HAS_PSRAM
m += " psram:" + String(ESP.getFreePsram());
@@ -237,26 +246,28 @@ void DoorbellLogic::transition(DeviceState s) {
void DoorbellLogic::onSerialCommand(const String& cmd) {
Serial.printf("[CMD] %s\n", cmd.c_str());
if (cmd == "alert") onAlert("Test Alert", "Serial test");
else if (cmd == "silence") onSilence();
else if (cmd == "reboot") ESP.restart();
else if (cmd == "dashboard") onAdmin("dashboard");
else if (cmd == "off") onAdmin("off");
if(cmd == "alert")
onAlert("Test Alert", "Serial test");
else if(cmd == "silence")
onSilence();
else if(cmd == "reboot")
ESP.restart();
else if(cmd == "dashboard")
onAdmin("dashboard");
else if(cmd == "off")
onAdmin("off");
else if(cmd == "status") {
Serial.printf("[STATE] %s screen:%s bl:%s\n",
deviceStateStr(_state.deviceState),
screenIdStr(_state.screen),
_state.backlightOn ? "ON" : "OFF");
Serial.printf("[STATE] %s screen:%s bl:%s\n", deviceStateStr(_state.deviceState),
screenIdStr(_state.screen), _state.backlightOn ? "ON" : "OFF");
Serial.printf("[MEM] heap:%d", ESP.getFreeHeap());
#ifdef BOARD_HAS_PSRAM
Serial.printf(" psram:%d", ESP.getFreePsram());
#endif
Serial.println();
Serial.printf("[NET] %s RSSI:%d IP:%s\n",
_state.wifiSsid.c_str(), _state.wifiRssi,
Serial.printf("[NET] %s RSSI:%d IP:%s\n", _state.wifiSsid.c_str(), _state.wifiRssi,
_state.ipAddr.c_str());
}
else Serial.println(F("[CMD] alert|silence|reboot|dashboard|off|status"));
} else
Serial.println(F("[CMD] alert|silence|reboot|dashboard|off|status"));
}
void DoorbellLogic::setScreen(ScreenID s) {