mirror of
https://github.com/mattintech/pyBTMCP.git
synced 2026-07-11 13:01:53 +00:00
Firmware: - Fixed WiFi reconnection bug after power cycle by adding WiFi.persistent(false) and disabling auto-connect/auto-reconnect - Fixed AP mode not broadcasting by properly handling WIFI_AP_STA mode transitions - Refactored to service-oriented architecture: - device_state: Central state management with event callbacks - config_service: NVS persistence (renamed from config_manager) - wifi_service: WiFi STA/AP management (extracted from main.cpp) - mqtt_service: MQTT client and message routing (extracted from main.cpp) - ble_service: BLE GATT services (renamed from ble_services) - web_service: HTTP configuration portal (renamed from web_portal) - main.cpp reduced from ~470 lines to ~60 lines (thin orchestrator) - Each service is self-contained with setup()/loop() pattern Backend: - Added heart rate variation endpoint - UI improvements Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
86 lines
2.3 KiB
C++
86 lines
2.3 KiB
C++
#include "services/config_service.h"
|
|
#include "config.h"
|
|
|
|
ConfigService& ConfigService::getInstance() {
|
|
static ConfigService instance;
|
|
return instance;
|
|
}
|
|
|
|
bool ConfigService::load() {
|
|
preferences.begin(NVS_NAMESPACE, true); // Read-only
|
|
|
|
configured = preferences.getBool("configured", false);
|
|
wifiSsid = preferences.getString("wifi_ssid", "");
|
|
wifiPassword = preferences.getString("wifi_pass", "");
|
|
mqttHost = preferences.getString("mqtt_host", "");
|
|
mqttPort = preferences.getUShort("mqtt_port", DEFAULT_MQTT_PORT);
|
|
deviceId = preferences.getString("device_id", getDefaultDeviceId());
|
|
|
|
preferences.end();
|
|
|
|
return configured;
|
|
}
|
|
|
|
void ConfigService::save() {
|
|
preferences.begin(NVS_NAMESPACE, false); // Read-write
|
|
|
|
preferences.putBool("configured", configured);
|
|
preferences.putString("wifi_ssid", wifiSsid);
|
|
preferences.putString("wifi_pass", wifiPassword);
|
|
preferences.putString("mqtt_host", mqttHost);
|
|
preferences.putUShort("mqtt_port", mqttPort);
|
|
preferences.putString("device_id", deviceId);
|
|
|
|
preferences.end();
|
|
|
|
Serial.println("Configuration saved to NVS");
|
|
}
|
|
|
|
void ConfigService::clear() {
|
|
preferences.begin(NVS_NAMESPACE, false);
|
|
preferences.clear();
|
|
preferences.end();
|
|
|
|
configured = false;
|
|
wifiSsid = "";
|
|
wifiPassword = "";
|
|
mqttHost = "";
|
|
mqttPort = DEFAULT_MQTT_PORT;
|
|
deviceId = "";
|
|
|
|
Serial.println("Configuration cleared");
|
|
}
|
|
|
|
void ConfigService::setWifiCredentials(const String& ssid, const String& password) {
|
|
wifiSsid = ssid;
|
|
wifiPassword = password;
|
|
if (ssid.length() > 0) {
|
|
configured = true;
|
|
}
|
|
}
|
|
|
|
void ConfigService::setMqttConfig(const String& host, uint16_t port) {
|
|
mqttHost = host;
|
|
mqttPort = port;
|
|
}
|
|
|
|
void ConfigService::setDeviceId(const String& id) {
|
|
deviceId = id.length() > 0 ? id : getDefaultDeviceId();
|
|
}
|
|
|
|
String ConfigService::getAPName() const {
|
|
uint32_t chipId = 0;
|
|
for (int i = 0; i < 17; i += 8) {
|
|
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
|
|
}
|
|
return String(AP_SSID_PREFIX) + String(chipId, HEX);
|
|
}
|
|
|
|
String ConfigService::getDefaultDeviceId() const {
|
|
uint32_t chipId = 0;
|
|
for (int i = 0; i < 17; i += 8) {
|
|
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
|
|
}
|
|
return String(DEFAULT_DEVICE_ID_PREFIX) + String(chipId, HEX);
|
|
}
|