|
| 1 | +/** |
| 2 | + * mcpd — Tool Groups |
| 3 | + * |
| 4 | + * Organize tools into named groups for bulk enable/disable and logical grouping. |
| 5 | + * Useful for MCU projects with many tools (e.g., "sensors", "actuators", "network"). |
| 6 | + */ |
| 7 | + |
| 8 | +#ifndef MCPD_TOOL_GROUP_H |
| 9 | +#define MCPD_TOOL_GROUP_H |
| 10 | + |
| 11 | +#include <Arduino.h> |
| 12 | +#include <functional> |
| 13 | +#include <map> |
| 14 | +#include <set> |
| 15 | +#include <vector> |
| 16 | + |
| 17 | +namespace mcpd { |
| 18 | + |
| 19 | +/** |
| 20 | + * Represents a named group of tools. |
| 21 | + */ |
| 22 | +struct ToolGroup { |
| 23 | + String name; // Group identifier (e.g., "sensors") |
| 24 | + String description; // Human-readable description |
| 25 | + bool enabled = true; // Group-level enable/disable |
| 26 | + std::set<String> tools; // Tool names in this group |
| 27 | + |
| 28 | + ToolGroup() = default; |
| 29 | + ToolGroup(const char* name, const char* desc = "") |
| 30 | + : name(name), description(desc) {} |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * Manages tool groups — logical groupings for bulk operations. |
| 35 | + * |
| 36 | + * Tools can belong to multiple groups. Disabling a group disables |
| 37 | + * all its tools (unless they belong to another enabled group). |
| 38 | + * |
| 39 | + * Usage: |
| 40 | + * manager.createGroup("sensors", "All sensor tools"); |
| 41 | + * manager.addToolToGroup("temperature_read", "sensors"); |
| 42 | + * manager.addToolToGroup("humidity_read", "sensors"); |
| 43 | + * manager.disableGroup("sensors"); // disables both tools |
| 44 | + */ |
| 45 | +class ToolGroupManager { |
| 46 | +public: |
| 47 | + /** |
| 48 | + * Create a new tool group. |
| 49 | + * @return true if created, false if already exists |
| 50 | + */ |
| 51 | + bool createGroup(const char* name, const char* description = "") { |
| 52 | + String key(name); |
| 53 | + if (_groups.count(key)) return false; |
| 54 | + _groups[key] = ToolGroup(name, description); |
| 55 | + return true; |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Remove a tool group. |
| 60 | + * @return true if removed, false if not found |
| 61 | + */ |
| 62 | + bool removeGroup(const char* name) { |
| 63 | + String key(name); |
| 64 | + auto it = _groups.find(key); |
| 65 | + if (it == _groups.end()) return false; |
| 66 | + // Remove reverse mappings |
| 67 | + for (auto& tool : it->second.tools) { |
| 68 | + auto tit = _toolToGroups.find(tool); |
| 69 | + if (tit != _toolToGroups.end()) { |
| 70 | + tit->second.erase(key); |
| 71 | + if (tit->second.empty()) _toolToGroups.erase(tit); |
| 72 | + } |
| 73 | + } |
| 74 | + _groups.erase(it); |
| 75 | + return true; |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Add a tool to a group. |
| 80 | + * Creates the group if it doesn't exist. |
| 81 | + * @return true if the tool was added (not already in group) |
| 82 | + */ |
| 83 | + bool addToolToGroup(const char* toolName, const char* groupName) { |
| 84 | + String gKey(groupName); |
| 85 | + String tKey(toolName); |
| 86 | + if (!_groups.count(gKey)) { |
| 87 | + createGroup(groupName); |
| 88 | + } |
| 89 | + bool inserted = _groups[gKey].tools.insert(tKey).second; |
| 90 | + if (inserted) { |
| 91 | + _toolToGroups[tKey].insert(gKey); |
| 92 | + } |
| 93 | + return inserted; |
| 94 | + } |
| 95 | + |
| 96 | + /** |
| 97 | + * Remove a tool from a group. |
| 98 | + * @return true if removed |
| 99 | + */ |
| 100 | + bool removeToolFromGroup(const char* toolName, const char* groupName) { |
| 101 | + String gKey(groupName); |
| 102 | + String tKey(toolName); |
| 103 | + auto it = _groups.find(gKey); |
| 104 | + if (it == _groups.end()) return false; |
| 105 | + bool erased = it->second.tools.erase(tKey) > 0; |
| 106 | + if (erased) { |
| 107 | + auto tit = _toolToGroups.find(tKey); |
| 108 | + if (tit != _toolToGroups.end()) { |
| 109 | + tit->second.erase(gKey); |
| 110 | + if (tit->second.empty()) _toolToGroups.erase(tit); |
| 111 | + } |
| 112 | + } |
| 113 | + return erased; |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Enable or disable a group. |
| 118 | + * @return true if group exists |
| 119 | + */ |
| 120 | + bool enableGroup(const char* name, bool enabled = true) { |
| 121 | + String key(name); |
| 122 | + auto it = _groups.find(key); |
| 123 | + if (it == _groups.end()) return false; |
| 124 | + it->second.enabled = enabled; |
| 125 | + return true; |
| 126 | + } |
| 127 | + |
| 128 | + bool disableGroup(const char* name) { return enableGroup(name, false); } |
| 129 | + |
| 130 | + /** |
| 131 | + * Check if a group is enabled. |
| 132 | + */ |
| 133 | + bool isGroupEnabled(const char* name) const { |
| 134 | + String key(name); |
| 135 | + auto it = _groups.find(key); |
| 136 | + if (it == _groups.end()) return false; |
| 137 | + return it->second.enabled; |
| 138 | + } |
| 139 | + |
| 140 | + /** |
| 141 | + * Check if a group exists. |
| 142 | + */ |
| 143 | + bool hasGroup(const char* name) const { |
| 144 | + return _groups.count(String(name)) > 0; |
| 145 | + } |
| 146 | + |
| 147 | + /** |
| 148 | + * Check if a tool is disabled by group membership. |
| 149 | + * A tool is group-disabled if ALL its groups are disabled. |
| 150 | + * Tools with no group are never group-disabled. |
| 151 | + */ |
| 152 | + bool isToolGroupDisabled(const char* toolName) const { |
| 153 | + String tKey(toolName); |
| 154 | + auto it = _toolToGroups.find(tKey); |
| 155 | + if (it == _toolToGroups.end()) return false; // no group = not group-disabled |
| 156 | + // If tool is in at least one enabled group, it's not disabled |
| 157 | + for (auto& gName : it->second) { |
| 158 | + auto git = _groups.find(gName); |
| 159 | + if (git != _groups.end() && git->second.enabled) return false; |
| 160 | + } |
| 161 | + return true; // all groups disabled |
| 162 | + } |
| 163 | + |
| 164 | + /** |
| 165 | + * Get all tools in a group. |
| 166 | + */ |
| 167 | + std::set<String> getToolsInGroup(const char* name) const { |
| 168 | + String key(name); |
| 169 | + auto it = _groups.find(key); |
| 170 | + if (it == _groups.end()) return {}; |
| 171 | + return it->second.tools; |
| 172 | + } |
| 173 | + |
| 174 | + /** |
| 175 | + * Get all groups a tool belongs to. |
| 176 | + */ |
| 177 | + std::set<String> getGroupsForTool(const char* toolName) const { |
| 178 | + String tKey(toolName); |
| 179 | + auto it = _toolToGroups.find(tKey); |
| 180 | + if (it == _toolToGroups.end()) return {}; |
| 181 | + return it->second; |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Get all group names. |
| 186 | + */ |
| 187 | + std::vector<String> getGroupNames() const { |
| 188 | + std::vector<String> names; |
| 189 | + names.reserve(_groups.size()); |
| 190 | + for (auto& pair : _groups) { |
| 191 | + names.push_back(pair.first); |
| 192 | + } |
| 193 | + return names; |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * Get a group by name (const). |
| 198 | + * @return pointer to group, or nullptr if not found |
| 199 | + */ |
| 200 | + const ToolGroup* getGroup(const char* name) const { |
| 201 | + String key(name); |
| 202 | + auto it = _groups.find(key); |
| 203 | + if (it == _groups.end()) return nullptr; |
| 204 | + return &it->second; |
| 205 | + } |
| 206 | + |
| 207 | + /** |
| 208 | + * Get total number of groups. |
| 209 | + */ |
| 210 | + size_t groupCount() const { return _groups.size(); } |
| 211 | + |
| 212 | + /** |
| 213 | + * Serialize all groups to JSON. |
| 214 | + * Returns: [{"name":"sensors","description":"...","enabled":true,"tools":["a","b"]}, ...] |
| 215 | + */ |
| 216 | + String toJson() const { |
| 217 | + String json = "["; |
| 218 | + bool first = true; |
| 219 | + for (auto& pair : _groups) { |
| 220 | + if (!first) json += ","; |
| 221 | + first = false; |
| 222 | + json += "{\"name\":\"" + pair.second.name + "\""; |
| 223 | + if (pair.second.description.length() > 0) { |
| 224 | + json += ",\"description\":\"" + pair.second.description + "\""; |
| 225 | + } |
| 226 | + json += ",\"enabled\":"; |
| 227 | + json += pair.second.enabled ? "true" : "false"; |
| 228 | + json += ",\"tools\":["; |
| 229 | + bool tfirst = true; |
| 230 | + for (auto& t : pair.second.tools) { |
| 231 | + if (!tfirst) json += ","; |
| 232 | + tfirst = false; |
| 233 | + json += "\"" + t + "\""; |
| 234 | + } |
| 235 | + json += "]}"; |
| 236 | + } |
| 237 | + json += "]"; |
| 238 | + return json; |
| 239 | + } |
| 240 | + |
| 241 | +private: |
| 242 | + std::map<String, ToolGroup> _groups; |
| 243 | + std::map<String, std::set<String>> _toolToGroups; // reverse index: tool → groups |
| 244 | +}; |
| 245 | + |
| 246 | +} // namespace mcpd |
| 247 | + |
| 248 | +#endif // MCPD_TOOL_GROUP_H |
0 commit comments