|
| 1 | +#ifndef DATA_MANAGER_HPP |
| 2 | +#define DATA_MANAGER_HPP |
| 3 | + |
| 4 | +#include <Arduino.h> |
| 5 | +#include <vector> |
| 6 | +#include "remote_protocol.hpp" |
| 7 | +#include "models.hpp" |
| 8 | +#include "Config.hpp" |
| 9 | +#include "../ui/assets/icons.h" |
| 10 | + |
| 11 | +class DataManager { |
| 12 | +public: |
| 13 | + static DataManager& getInstance() { |
| 14 | + static DataManager instance; |
| 15 | + return instance; |
| 16 | + } |
| 17 | + |
| 18 | + // --- Recipes --- |
| 19 | + bool isRecipesSynced() const { return recipesSynced; } |
| 20 | + bool isUsingMocks() const { return usingMocks; } |
| 21 | + const std::vector<ICocktail>& getRecipes() const { return recipes; } |
| 22 | + |
| 23 | + void clearRecipes() { |
| 24 | + recipes.clear(); |
| 25 | + recipesSynced = false; |
| 26 | + usingMocks = false; |
| 27 | + } |
| 28 | + |
| 29 | + void addRecipe(const RecipeSyncData& data) { |
| 30 | + // Clear if new sync session starts (Index 0) OR if we were using mocks |
| 31 | + if (data.index == 0 || usingMocks) { |
| 32 | + if (usingMocks) printf("[DataManager] Real data received. Clearing Mocks.\n"); |
| 33 | + else printf("[DataManager] New Sync Session (Index 0). Clearing old data.\n"); |
| 34 | + |
| 35 | + recipes.clear(); |
| 36 | + recipesSynced = false; |
| 37 | + usingMocks = false; |
| 38 | + } |
| 39 | + |
| 40 | + if (!recipesSynced) { |
| 41 | + recipesSynced = true; // Mark as syncing started |
| 42 | + } |
| 43 | + |
| 44 | + ICocktail c; |
| 45 | + c.name = String(data.name); |
| 46 | + mapMetadata(c); |
| 47 | + |
| 48 | + static const char* PUMP_NAMES[] = {"Cocacola", "Orange Juice", "Vodka", "Grenadine"}; |
| 49 | + for (int i=0; i<4; i++) { |
| 50 | + if (data.ingredientsMl[i] > 0) { |
| 51 | + c.ingredients.push_back({PUMP_NAMES[i], i+1, (int)data.ingredientsMl[i]}); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + recipes.push_back(c); |
| 56 | + lastUpdateTime = millis(); |
| 57 | + } |
| 58 | + |
| 59 | + void addRecipeFromConfig(const ICocktail& cocktail) { |
| 60 | + if (usingMocks) { |
| 61 | + recipes.clear(); |
| 62 | + usingMocks = false; |
| 63 | + } |
| 64 | + if (!recipesSynced) { |
| 65 | + recipes.clear(); |
| 66 | + recipesSynced = true; |
| 67 | + } |
| 68 | + ICocktail c = cocktail; |
| 69 | + mapMetadata(c); |
| 70 | + recipes.push_back(c); |
| 71 | + lastUpdateTime = millis(); |
| 72 | + } |
| 73 | + |
| 74 | + void updateRecipe(const ICocktail& updatedCocktail) { |
| 75 | + for (auto& c : recipes) { |
| 76 | + if (c.name == updatedCocktail.name) { |
| 77 | + c.ingredients = updatedCocktail.ingredients; |
| 78 | + lastUpdateTime = millis(); |
| 79 | + printf("[DataManager] Optimistic Update for: %s\n", c.name.c_str()); |
| 80 | + return; |
| 81 | + } |
| 82 | + } |
| 83 | + printf("[DataManager] Warning: Recipe not found for update: %s\n", updatedCocktail.name.c_str()); |
| 84 | + } |
| 85 | + |
| 86 | + void loadMocks() { |
| 87 | + if (recipesSynced && !recipes.empty() && !usingMocks) return; // Don't overwrite REAL live data |
| 88 | + |
| 89 | + printf("[DataManager] Loading Mocks.\n"); |
| 90 | + auto mocks = getDefaultMockCocktails(); |
| 91 | + recipes.clear(); |
| 92 | + for (const auto& m : mocks) { |
| 93 | + ICocktail c = m; |
| 94 | + mapMetadata(c); |
| 95 | + recipes.push_back(c); |
| 96 | + } |
| 97 | + recipesSynced = true; |
| 98 | + usingMocks = true; // Mark as Mocks |
| 99 | + lastUpdateTime = millis(); |
| 100 | + } |
| 101 | + |
| 102 | + |
| 103 | + // --- Pumps --- |
| 104 | + const IPumpSettings& getPumpSettings() const { return pumps; } |
| 105 | + |
| 106 | + void updatePumps(const PumpSyncData& data) { |
| 107 | + for(int i=0; i<4; i++) { |
| 108 | + pumps.pwm[i] = data.pwm[i]; |
| 109 | + pumps.timeMs[i] = (int)(data.calibration[i] * 1000.0f); |
| 110 | + } |
| 111 | + pumps.synced = true; |
| 112 | + lastUpdateTime = millis(); |
| 113 | + } |
| 114 | + |
| 115 | + unsigned long getLastUpdate() const { return lastUpdateTime; } |
| 116 | + |
| 117 | +private: |
| 118 | + DataManager() {} |
| 119 | + |
| 120 | + void mapMetadata(ICocktail& c) { |
| 121 | + // Centralized logic for icon/color assignment |
| 122 | + if (c.name.indexOf("Coca") >= 0) { c.icon = ICON_COCKTAIL_COCA_COLA; c.color = 0xFF0000; } |
| 123 | + else if (c.name.indexOf("Orange") >= 0) { c.icon = ICON_COCKTAIL_GIN_TONIC; c.color = 0xFFA500; } |
| 124 | + else if (c.name.indexOf("Vodka") >= 0) { c.icon = ICON_COCKTAIL_VODKA; c.color = 0x00FFFF; } |
| 125 | + else if (c.name.indexOf("Sex") >= 0) { c.icon = ICON_COCKTAIL_SEX_ON_BEACH; c.color = 0xFF1493; } |
| 126 | + else if (c.name.indexOf("Tequila") >= 0) { c.icon = ICON_COCKTAIL_PORN_STAR; c.color = 0xFF4500; } |
| 127 | + else if (c.name.indexOf("Gin") >= 0) { c.icon = ICON_COCKTAIL_GIN_TONIC; c.color = 0xADD8E6; } |
| 128 | + else { c.icon = ICON_COCKTAIL_VODKA; c.color = 0x888888; } |
| 129 | + } |
| 130 | + |
| 131 | + std::vector<ICocktail> recipes; |
| 132 | + bool recipesSynced = false; |
| 133 | + bool usingMocks = false; |
| 134 | + |
| 135 | + IPumpSettings pumps = getDefaultPumpSettings(); |
| 136 | + unsigned long lastUpdateTime = 0; |
| 137 | +}; |
| 138 | + |
| 139 | +#endif // DATA_MANAGER_HPP |
0 commit comments