|
| 1 | +#include "schedule_manager.h" |
| 2 | +#include <esp_log.h> |
| 3 | +#include <ctime> |
| 4 | + |
| 5 | +#define TAG "ScheduleManager" |
| 6 | + |
| 7 | +void ScheduleManager::RegisterMcpTools() { |
| 8 | + auto& mcp_server = McpServer::GetInstance(); |
| 9 | + |
| 10 | + // 添加日程工具 |
| 11 | + mcp_server.AddTool("schedule.add", |
| 12 | + "Add a new schedule reminder", |
| 13 | + GetAddScheduleProperties(), |
| 14 | + AddScheduleTool); |
| 15 | + |
| 16 | + // 列出日程工具 |
| 17 | + mcp_server.AddTool("schedule.list", |
| 18 | + "List all schedule reminders", |
| 19 | + PropertyList(), |
| 20 | + ListSchedulesTool); |
| 21 | + |
| 22 | + // 删除日程工具 |
| 23 | + mcp_server.AddTool("schedule.remove", |
| 24 | + "Remove a schedule reminder", |
| 25 | + GetRemoveScheduleProperties(), |
| 26 | + RemoveScheduleTool); |
| 27 | + |
| 28 | + // 更新日程工具 |
| 29 | + mcp_server.AddTool("schedule.update", |
| 30 | + "Update an existing schedule reminder", |
| 31 | + GetUpdateScheduleProperties(), |
| 32 | + UpdateScheduleTool); |
| 33 | + |
| 34 | + ESP_LOGI(TAG, "Schedule MCP tools registered"); |
| 35 | +} |
| 36 | + |
| 37 | +PropertyList ScheduleManager::GetAddScheduleProperties() { |
| 38 | + PropertyList props; |
| 39 | + props.AddProperty("title", "Schedule title", PropertyType::kString, true); |
| 40 | + props.AddProperty("description", "Schedule description", PropertyType::kString, false); |
| 41 | + props.AddProperty("trigger_time", "Trigger time (Unix timestamp)", PropertyType::kNumber, true); |
| 42 | + props.AddProperty("recurring", "Whether this is a recurring schedule", PropertyType::kBoolean, false); |
| 43 | + props.AddProperty("repeat_interval", "Repeat interval in seconds", PropertyType::kNumber, false); |
| 44 | + return props; |
| 45 | +} |
| 46 | + |
| 47 | +PropertyList ScheduleManager::GetRemoveScheduleProperties() { |
| 48 | + PropertyList props; |
| 49 | + props.AddProperty("id", "Schedule ID to remove", PropertyType::kString, true); |
| 50 | + return props; |
| 51 | +} |
| 52 | + |
| 53 | +PropertyList ScheduleManager::GetUpdateScheduleProperties() { |
| 54 | + PropertyList props; |
| 55 | + props.AddProperty("id", "Schedule ID to update", PropertyType::kString, true); |
| 56 | + props.AddProperty("title", "New schedule title", PropertyType::kString, false); |
| 57 | + props.AddProperty("description", "New schedule description", PropertyType::kString, false); |
| 58 | + props.AddProperty("trigger_time", "New trigger time (Unix timestamp)", PropertyType::kNumber, false); |
| 59 | + props.AddProperty("enabled", "Whether the schedule is enabled", PropertyType::kBoolean, false); |
| 60 | + props.AddProperty("recurring", "Whether this is a recurring schedule", PropertyType::kBoolean, false); |
| 61 | + props.AddProperty("repeat_interval", "Repeat interval in seconds", PropertyType::kNumber, false); |
| 62 | + return props; |
| 63 | +} |
| 64 | + |
| 65 | +bool ScheduleManager::AddScheduleTool(const PropertyList& properties) { |
| 66 | + ScheduleItem item; |
| 67 | + item.id = std::to_string(time(nullptr)); // Use timestamp as ID |
| 68 | + item.title = properties.GetValue<std::string>("title"); |
| 69 | + item.description = properties.GetValue<std::string>("description", ""); |
| 70 | + item.trigger_time = properties.GetValue<int>("trigger_time"); |
| 71 | + item.recurring = properties.GetValue<bool>("recurring", false); |
| 72 | + item.repeat_interval = properties.GetValue<int>("repeat_interval", 0); |
| 73 | + item.created_at = std::to_string(time(nullptr)); |
| 74 | + |
| 75 | + ScheduleError result = ScheduleReminder::GetInstance().AddSchedule(item); |
| 76 | + |
| 77 | + switch (result) { |
| 78 | + case ScheduleError::kSuccess: |
| 79 | + ESP_LOGI(TAG, "Schedule added via MCP: %s", item.title.c_str()); |
| 80 | + return true; |
| 81 | + case ScheduleError::kMaxItemsReached: |
| 82 | + ESP_LOGE(TAG, "Failed to add schedule via MCP: maximum items reached"); |
| 83 | + return false; |
| 84 | + case ScheduleError::kDuplicateId: |
| 85 | + ESP_LOGE(TAG, "Failed to add schedule via MCP: duplicate ID"); |
| 86 | + return false; |
| 87 | + case ScheduleError::kInvalidTime: |
| 88 | + ESP_LOGE(TAG, "Failed to add schedule via MCP: invalid trigger time"); |
| 89 | + return false; |
| 90 | + case ScheduleError::kStorageError: |
| 91 | + ESP_LOGE(TAG, "Failed to add schedule via MCP: storage error"); |
| 92 | + return false; |
| 93 | + case ScheduleError::kNotInitialized: |
| 94 | + ESP_LOGE(TAG, "Failed to add schedule via MCP: not initialized"); |
| 95 | + return false; |
| 96 | + default: |
| 97 | + ESP_LOGE(TAG, "Failed to add schedule via MCP: unknown error"); |
| 98 | + return false; |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +bool ScheduleManager::ListSchedulesTool(const PropertyList& properties) { |
| 103 | + auto schedules = ScheduleReminder::GetInstance().GetSchedules(); |
| 104 | + |
| 105 | + // 这里可以返回日程列表给 MCP 客户端 |
| 106 | + // 实际实现中可能需要格式化输出 |
| 107 | + |
| 108 | + ESP_LOGI(TAG, "Listed %d schedules via MCP", schedules.size()); |
| 109 | + |
| 110 | + // 简单记录到日志 |
| 111 | + for (const auto& schedule : schedules) { |
| 112 | + ESP_LOGI(TAG, "Schedule: %s (ID: %s, Time: %ld)", |
| 113 | + schedule.title.c_str(), schedule.id.c_str(), schedule.trigger_time); |
| 114 | + } |
| 115 | + |
| 116 | + return true; |
| 117 | +} |
| 118 | + |
| 119 | +bool ScheduleManager::RemoveScheduleTool(const PropertyList& properties) { |
| 120 | + std::string id = properties.GetValue<std::string>("id"); |
| 121 | + ScheduleError result = ScheduleReminder::GetInstance().RemoveSchedule(id); |
| 122 | + |
| 123 | + switch (result) { |
| 124 | + case ScheduleError::kSuccess: |
| 125 | + ESP_LOGI(TAG, "Schedule removed via MCP: %s", id.c_str()); |
| 126 | + return true; |
| 127 | + case ScheduleError::kNotFound: |
| 128 | + ESP_LOGE(TAG, "Failed to remove schedule via MCP: schedule not found - %s", id.c_str()); |
| 129 | + return false; |
| 130 | + case ScheduleError::kStorageError: |
| 131 | + ESP_LOGE(TAG, "Failed to remove schedule via MCP: storage error - %s", id.c_str()); |
| 132 | + return false; |
| 133 | + case ScheduleError::kNotInitialized: |
| 134 | + ESP_LOGE(TAG, "Failed to remove schedule via MCP: not initialized - %s", id.c_str()); |
| 135 | + return false; |
| 136 | + default: |
| 137 | + ESP_LOGE(TAG, "Failed to remove schedule via MCP: unknown error - %s", id.c_str()); |
| 138 | + return false; |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +bool ScheduleManager::UpdateScheduleTool(const PropertyList& properties) { |
| 143 | + std::string id = properties.GetValue<std::string>("id"); |
| 144 | + |
| 145 | + // Get existing schedule |
| 146 | + ScheduleItem* existing_item = ScheduleReminder::GetInstance().GetSchedule(id); |
| 147 | + if (!existing_item) { |
| 148 | + ESP_LOGE(TAG, "Schedule not found for update: %s", id.c_str()); |
| 149 | + return false; |
| 150 | + } |
| 151 | + |
| 152 | + // Create updated schedule item |
| 153 | + ScheduleItem updated_item = *existing_item; |
| 154 | + |
| 155 | + // Update provided fields |
| 156 | + if (properties.HasValue("title")) { |
| 157 | + updated_item.title = properties.GetValue<std::string>("title"); |
| 158 | + } |
| 159 | + if (properties.HasValue("description")) { |
| 160 | + updated_item.description = properties.GetValue<std::string>("description"); |
| 161 | + } |
| 162 | + if (properties.HasValue("trigger_time")) { |
| 163 | + updated_item.trigger_time = properties.GetValue<int>("trigger_time"); |
| 164 | + } |
| 165 | + if (properties.HasValue("enabled")) { |
| 166 | + updated_item.enabled = properties.GetValue<bool>("enabled"); |
| 167 | + } |
| 168 | + if (properties.HasValue("recurring")) { |
| 169 | + updated_item.recurring = properties.GetValue<bool>("recurring"); |
| 170 | + } |
| 171 | + if (properties.HasValue("repeat_interval")) { |
| 172 | + updated_item.repeat_interval = properties.GetValue<int>("repeat_interval"); |
| 173 | + } |
| 174 | + |
| 175 | + ScheduleError result = ScheduleReminder::GetInstance().UpdateSchedule(id, updated_item); |
| 176 | + |
| 177 | + switch (result) { |
| 178 | + case ScheduleError::kSuccess: |
| 179 | + ESP_LOGI(TAG, "Schedule updated via MCP: %s", id.c_str()); |
| 180 | + return true; |
| 181 | + case ScheduleError::kNotFound: |
| 182 | + ESP_LOGE(TAG, "Failed to update schedule via MCP: schedule not found - %s", id.c_str()); |
| 183 | + return false; |
| 184 | + case ScheduleError::kStorageError: |
| 185 | + ESP_LOGE(TAG, "Failed to update schedule via MCP: storage error - %s", id.c_str()); |
| 186 | + return false; |
| 187 | + case ScheduleError::kNotInitialized: |
| 188 | + ESP_LOGE(TAG, "Failed to update schedule via MCP: not initialized - %s", id.c_str()); |
| 189 | + return false; |
| 190 | + default: |
| 191 | + ESP_LOGE(TAG, "Failed to update schedule via MCP: unknown error - %s", id.c_str()); |
| 192 | + return false; |
| 193 | + } |
| 194 | +} |
0 commit comments