|
|
@@ -0,0 +1,256 @@
|
|
|
+package service
|
|
|
+
|
|
|
+import (
|
|
|
+ "context"
|
|
|
+ "encoding/json"
|
|
|
+
|
|
|
+ "github.com/mark3labs/mcp-go/mcp"
|
|
|
+ "github.com/mark3labs/mcp-go/server"
|
|
|
+
|
|
|
+ "fwh-toolbox-server/cmd/mcp/tools/helper"
|
|
|
+ "fwh-toolbox-server/domain"
|
|
|
+ "fwh-toolbox-server/services"
|
|
|
+)
|
|
|
+
|
|
|
+func registerAutoUpdateTools(s *server.MCPServer) {
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_get_options",
|
|
|
+ mcp.WithDescription("查询自动升级配置选项(升级包列表和 aGate 版本列表)"),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateGetOptions,
|
|
|
+ )
|
|
|
+
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_get_package",
|
|
|
+ mcp.WithDescription("查询指定升级包的详细信息"),
|
|
|
+ mcp.WithString("file_name", mcp.Required(), mcp.Description("升级包文件名")),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateGetPackage,
|
|
|
+ )
|
|
|
+
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_create_package",
|
|
|
+ mcp.WithDescription("创建升级包信息 [写操作]"),
|
|
|
+ mcp.WithString("package_type", mcp.Required(), mcp.Description("包类型:system, aGate, subDevice, others")),
|
|
|
+ mcp.WithString("info", mcp.Required(), mcp.Description("升级包信息 JSON,包含 id(文件名)、title、invVersion[]")),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateCreatePackage,
|
|
|
+ )
|
|
|
+
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_update_package",
|
|
|
+ mcp.WithDescription("更新升级包信息 [写操作]"),
|
|
|
+ mcp.WithString("file_name", mcp.Required(), mcp.Description("升级包文件名")),
|
|
|
+ mcp.WithString("info", mcp.Required(), mcp.Description("升级包信息 JSON,包含 id、title、invVersion[]")),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateUpdatePackage,
|
|
|
+ )
|
|
|
+
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_delete_package",
|
|
|
+ mcp.WithDescription("删除升级包信息 [写操作]"),
|
|
|
+ mcp.WithString("package_type", mcp.Required(), mcp.Description("包类型:system, aGate, subDevice, others")),
|
|
|
+ mcp.WithString("file_name", mcp.Required(), mcp.Description("升级包文件名")),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateDeletePackage,
|
|
|
+ )
|
|
|
+
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_get_apower_version",
|
|
|
+ mcp.WithDescription("查询 aPower 硬件版本列表"),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateGetAPowerVersion,
|
|
|
+ )
|
|
|
+
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_get_config",
|
|
|
+ mcp.WithDescription("查询指定 aGate 版本的自动升级配置"),
|
|
|
+ mcp.WithString("agate_version", mcp.Required(), mcp.Description("aGate 硬件版本号")),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateGetConfig,
|
|
|
+ )
|
|
|
+
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_update_config",
|
|
|
+ mcp.WithDescription("更新指定 aGate 版本的自动升级配置 [写操作]"),
|
|
|
+ mcp.WithString("agate_version", mcp.Required(), mcp.Description("aGate 硬件版本号")),
|
|
|
+ mcp.WithString("config", mcp.Required(), mcp.Description("配置 JSON,包含 config.defaultType 和 config.specialType")),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateUpdateConfig,
|
|
|
+ )
|
|
|
+
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_delete_config",
|
|
|
+ mcp.WithDescription("删除指定 aGate 版本的自动升级配置 [写操作]"),
|
|
|
+ mcp.WithString("agate_version", mcp.Required(), mcp.Description("aGate 硬件版本号")),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateDeleteConfig,
|
|
|
+ )
|
|
|
+
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_file_list",
|
|
|
+ mcp.WithDescription("查询自动升级配置文件列表(已生成的 Excel 文件)"),
|
|
|
+ mcp.WithNumber("page", mcp.Description("页码,从1开始,默认1")),
|
|
|
+ mcp.WithNumber("size", mcp.Description("每页数量,默认10")),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateFileList,
|
|
|
+ )
|
|
|
+
|
|
|
+ s.AddTool(
|
|
|
+ mcp.NewTool("service_auto_update_create_file",
|
|
|
+ mcp.WithDescription("生成新的自动升级配置 Excel 文件 [写操作]"),
|
|
|
+ ),
|
|
|
+ handleAutoUpdateCreateFile,
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateGetOptions(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ data, err := services.GetAutoUpdateConfigOption()
|
|
|
+ if err != nil {
|
|
|
+ return helper.ServiceError("查询升级配置选项", err), nil
|
|
|
+ }
|
|
|
+ return helper.JSONResult(data), nil
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateGetPackage(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ fileName, err := req.RequireString("file_name")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("file_name"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ data, err := services.GetAutoUpdatePackageInfo(fileName)
|
|
|
+ if err != nil {
|
|
|
+ return helper.ServiceError("查询升级包信息", err), nil
|
|
|
+ }
|
|
|
+ return helper.JSONResult(data), nil
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateCreatePackage(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ packageType, err := req.RequireString("package_type")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("package_type"), nil
|
|
|
+ }
|
|
|
+ infoStr, err := req.RequireString("info")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("info"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ var info domain.AutoUpdatePackages
|
|
|
+ if err := json.Unmarshal([]byte(infoStr), &info); err != nil {
|
|
|
+ return helper.ParamInvalidError("info", "需要有效的JSON格式"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := services.CreateAutoUpdatePackageInfo(packageType, info); err != nil {
|
|
|
+ return helper.ServiceError("创建升级包", err), nil
|
|
|
+ }
|
|
|
+ return helper.TextResult("升级包创建成功"), nil
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateUpdatePackage(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ fileName, err := req.RequireString("file_name")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("file_name"), nil
|
|
|
+ }
|
|
|
+ infoStr, err := req.RequireString("info")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("info"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ var info domain.AutoUpdatePackages
|
|
|
+ if err := json.Unmarshal([]byte(infoStr), &info); err != nil {
|
|
|
+ return helper.ParamInvalidError("info", "需要有效的JSON格式"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := services.UpdateAutoUpdatePackageInfo(fileName, info); err != nil {
|
|
|
+ return helper.ServiceError("更新升级包", err), nil
|
|
|
+ }
|
|
|
+ return helper.TextResult("升级包更新成功"), nil
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateDeletePackage(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ packageType, err := req.RequireString("package_type")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("package_type"), nil
|
|
|
+ }
|
|
|
+ fileName, err := req.RequireString("file_name")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("file_name"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := services.DeleteAutoUpdatePackageInfo(packageType, fileName); err != nil {
|
|
|
+ return helper.ServiceError("删除升级包", err), nil
|
|
|
+ }
|
|
|
+ return helper.TextResult("升级包删除成功"), nil
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateGetAPowerVersion(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ data, err := services.GetAutoUpdateAPowerVersion()
|
|
|
+ if err != nil {
|
|
|
+ return helper.ServiceError("查询aPower版本列表", err), nil
|
|
|
+ }
|
|
|
+ return helper.JSONResult(data), nil
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateGetConfig(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ agateVersion, err := req.RequireString("agate_version")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("agate_version"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ data, err := services.GetAutoUpdateConfig(agateVersion)
|
|
|
+ if err != nil {
|
|
|
+ return helper.ServiceError("查询升级配置", err), nil
|
|
|
+ }
|
|
|
+ return helper.JSONResult(data), nil
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateUpdateConfig(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ agateVersion, err := req.RequireString("agate_version")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("agate_version"), nil
|
|
|
+ }
|
|
|
+ configStr, err := req.RequireString("config")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("config"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ var config domain.AutoUpdateConfig
|
|
|
+ if err := json.Unmarshal([]byte(configStr), &config); err != nil {
|
|
|
+ return helper.ParamInvalidError("config", "需要有效的JSON格式"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := services.UpdateAutoUpdateConfig(agateVersion, config); err != nil {
|
|
|
+ return helper.ServiceError("更新升级配置", err), nil
|
|
|
+ }
|
|
|
+ return helper.TextResult("升级配置更新成功"), nil
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateDeleteConfig(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ agateVersion, err := req.RequireString("agate_version")
|
|
|
+ if err != nil {
|
|
|
+ return helper.ParamError("agate_version"), nil
|
|
|
+ }
|
|
|
+
|
|
|
+ if err := services.DeleteAutoUpdateConfig(agateVersion); err != nil {
|
|
|
+ return helper.ServiceError("删除升级配置", err), nil
|
|
|
+ }
|
|
|
+ return helper.TextResult("升级配置删除成功"), nil
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateFileList(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ page := uint(req.GetFloat("page", 1))
|
|
|
+ size := uint(req.GetFloat("size", 10))
|
|
|
+
|
|
|
+ data, err := services.GetAutoUpdateConfigFileInfo(page, size)
|
|
|
+ if err != nil {
|
|
|
+ return helper.ServiceError("查询配置文件列表", err), nil
|
|
|
+ }
|
|
|
+ return helper.JSONResult(data), nil
|
|
|
+}
|
|
|
+
|
|
|
+func handleAutoUpdateCreateFile(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
|
+ if err := services.CreateAutoUpdateConfigFile(); err != nil {
|
|
|
+ return helper.ServiceError("生成配置文件", err), nil
|
|
|
+ }
|
|
|
+ return helper.TextResult("配置文件生成成功"), nil
|
|
|
+}
|