v1.2.0: Firmware refactor to service-oriented architecture + WiFi fixes

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>
This commit is contained in:
2026-01-19 16:49:54 -05:00
parent 9b740ebdd0
commit c95cd33343
22 changed files with 1402 additions and 956 deletions

View File

@@ -0,0 +1,58 @@
#ifndef BLE_SERVICE_H
#define BLE_SERVICE_H
#include <Arduino.h>
// ============================================
// BLE Service UUIDs (Bluetooth SIG standard)
// ============================================
#define HEART_RATE_SERVICE_UUID "180D"
#define HEART_RATE_MEASUREMENT_UUID "2A37"
#define BODY_SENSOR_LOCATION_UUID "2A38"
#define BATTERY_SERVICE_UUID "180F"
#define BATTERY_LEVEL_UUID "2A19"
#define FITNESS_MACHINE_SERVICE_UUID "1826"
#define TREADMILL_DATA_UUID "2ACD"
#define FITNESS_MACHINE_FEATURE_UUID "2ACC"
// ============================================
// BLE Service
// Manages BLE advertising and GATT services
// ============================================
class BleService {
public:
static BleService& getInstance();
// Prevent copying
BleService(const BleService&) = delete;
BleService& operator=(const BleService&) = delete;
// Lifecycle
void setup();
void loop();
// Configuration
void setupHeartRate();
void setupTreadmill();
void stop();
// Notifications
void notifyHeartRate(uint8_t bpm);
void notifyTreadmill(uint16_t speed, int16_t incline, uint32_t distance);
void updateBattery(uint8_t level);
// State
bool isClientConnected() const { return deviceConnected; }
private:
BleService() = default;
bool deviceConnected = false;
unsigned long lastNotify = 0;
void initBLE();
};
#define bleService BleService::getInstance()
#endif // BLE_SERVICE_H

View File

@@ -0,0 +1,64 @@
#ifndef CONFIG_SERVICE_H
#define CONFIG_SERVICE_H
#include <Arduino.h>
#include <Preferences.h>
// ============================================
// Config Service
// Manages persistent configuration in NVS
// ============================================
class ConfigService {
public:
static ConfigService& getInstance();
// Prevent copying
ConfigService(const ConfigService&) = delete;
ConfigService& operator=(const ConfigService&) = delete;
// Load config from NVS
bool load();
// Save config to NVS
void save();
// Clear all config
void clear();
// Check if configured
bool isConfigured() const { return configured; }
// Getters
const String& getWifiSsid() const { return wifiSsid; }
const String& getWifiPassword() const { return wifiPassword; }
const String& getMqttHost() const { return mqttHost; }
uint16_t getMqttPort() const { return mqttPort; }
const String& getDeviceId() const { return deviceId; }
// Setters
void setWifiCredentials(const String& ssid, const String& password);
void setMqttConfig(const String& host, uint16_t port);
void setDeviceId(const String& id);
// Get unique AP name based on chip ID
String getAPName() const;
// Get unique default device ID based on chip ID
String getDefaultDeviceId() const;
private:
ConfigService() = default;
bool configured = false;
String wifiSsid = "";
String wifiPassword = "";
String mqttHost = "";
uint16_t mqttPort = 1883;
String deviceId = "";
Preferences preferences;
};
#define configService ConfigService::getInstance()
#endif // CONFIG_SERVICE_H

View File

@@ -0,0 +1,45 @@
#ifndef MQTT_SERVICE_H
#define MQTT_SERVICE_H
#include <Arduino.h>
// ============================================
// MQTT Service
// Manages MQTT connection and message routing
// ============================================
class MqttService {
public:
static MqttService& getInstance();
// Prevent copying
MqttService(const MqttService&) = delete;
MqttService& operator=(const MqttService&) = delete;
// Lifecycle
void setup();
void loop();
// State
bool isConnected() const { return mqttConnected; }
// Publishing
void publishStatus();
void publishValues();
private:
MqttService() = default;
bool mqttConnected = false;
unsigned long lastMqttAttempt = 0;
unsigned long lastStatusReport = 0;
void connectToMQTT();
void handleMessage(char* topic, byte* payload, unsigned int length);
// Static callback wrapper for PubSubClient
static void messageCallback(char* topic, byte* payload, unsigned int length);
};
#define mqttService MqttService::getInstance()
#endif // MQTT_SERVICE_H

View File

@@ -0,0 +1,35 @@
#ifndef WEB_SERVICE_H
#define WEB_SERVICE_H
#include <Arduino.h>
// ============================================
// Web Service
// Manages HTTP server for configuration portal
// ============================================
class WebService {
public:
static WebService& getInstance();
// Prevent copying
WebService(const WebService&) = delete;
WebService& operator=(const WebService&) = delete;
// Lifecycle
void setup();
void loop();
private:
WebService() = default;
void handleRoot();
void handleGetStatus();
void handlePostConfig();
void handleReset();
void handleResetDistance();
void handleSetBattery();
};
#define webService WebService::getInstance()
#endif // WEB_SERVICE_H

View File

@@ -0,0 +1,49 @@
#ifndef WIFI_SERVICE_H
#define WIFI_SERVICE_H
#include <Arduino.h>
// ============================================
// WiFi Service
// Manages WiFi STA and AP modes
// ============================================
class WiFiService {
public:
static WiFiService& getInstance();
// Prevent copying
WiFiService(const WiFiService&) = delete;
WiFiService& operator=(const WiFiService&) = delete;
// Lifecycle
void setup();
void loop();
// State queries
bool isConnected() const { return wifiConnected; }
bool isApActive() const { return apModeActive; }
String getIP() const;
// Actions
void startAP();
void stopAP();
void reconnect(); // Trigger reconnection attempt
private:
WiFiService() = default;
bool wifiConnected = false;
bool apModeActive = false;
unsigned long lastWifiAttempt = 0;
int wifiRetryCount = 0;
static const int MAX_WIFI_RETRIES = 5;
void connectToWiFi();
void startAPMode();
void stopAPMode();
};
#define wifiService WiFiService::getInstance()
#endif // WIFI_SERVICE_H