717ad65b05
- Plugin SDK: Plugin/Tool/ComplexTool/HostAPI 标准化接口 - Plugin Manager: 插件生命周期管理 (Install/Enable/Disable/Uninstall/Reload) - Tool Registry: 聚合工具注册表 (Register/Execute/Dispatch) - 13 个内置插件: 将原有硬编码工具迁移为标准插件格式 - REST API: 11 个端点 (net/http, 零外部依赖) - ai-core 集成: PluginManagerClient 替代本地工具调用 - plugin.json 元数据: 每个插件含完整 author/version/category/permissions Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
113 lines
3.0 KiB
Go
113 lines
3.0 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/yourname/cyrene-ai/plugin-manager/internal/sdk"
|
|
iotquery "github.com/yourname/cyrene-ai/plugin-manager/plugins/iot_query"
|
|
)
|
|
|
|
type iotClient struct {
|
|
baseURL string
|
|
httpClient *http.Client
|
|
}
|
|
|
|
func newIoTClient(baseURL string) *iotClient {
|
|
return &iotClient{
|
|
baseURL: baseURL,
|
|
httpClient: &http.Client{Timeout: 5 * time.Second},
|
|
}
|
|
}
|
|
|
|
func (c *iotClient) GetAllDevices(ctx context.Context) ([]sdk.IoTDeviceState, error) {
|
|
url := c.baseURL + "/api/v1/devices"
|
|
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var result struct {
|
|
Devices []sdk.IoTDeviceState `json:"devices"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&result); err != nil {
|
|
return nil, err
|
|
}
|
|
return result.Devices, nil
|
|
}
|
|
|
|
func (c *iotClient) GetDevice(ctx context.Context, deviceID string) (*sdk.IoTDeviceState, error) {
|
|
url := fmt.Sprintf("%s/api/v1/devices/%s", c.baseURL, deviceID)
|
|
req, _ := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
resp, err := c.httpClient.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var dev sdk.IoTDeviceState
|
|
if err := json.NewDecoder(resp.Body).Decode(&dev); err != nil {
|
|
return nil, err
|
|
}
|
|
return &dev, nil
|
|
}
|
|
|
|
// iotControllerAdapter adapts IoTClient to iotcontrol.IoTController.
|
|
type iotControllerAdapter struct {
|
|
query iotquery.IoTClient
|
|
client *http.Client
|
|
baseURL string
|
|
}
|
|
|
|
func newIoTControllerAdapter(query iotquery.IoTClient, baseURL string) *iotControllerAdapter {
|
|
return &iotControllerAdapter{
|
|
query: query,
|
|
client: &http.Client{Timeout: 5 * time.Second},
|
|
baseURL: baseURL,
|
|
}
|
|
}
|
|
|
|
func (a *iotControllerAdapter) GetDevice(ctx context.Context, deviceID string) (*sdk.IoTDeviceState, error) {
|
|
return a.query.GetDevice(ctx, deviceID)
|
|
}
|
|
|
|
func (a *iotControllerAdapter) SetDeviceProperty(ctx context.Context, deviceID, property string, value interface{}) error {
|
|
url := fmt.Sprintf("%s/api/v1/devices/%s/property", a.baseURL, deviceID)
|
|
body, _ := json.Marshal(map[string]interface{}{"property": property, "value": value})
|
|
req, _ := http.NewRequestWithContext(ctx, "POST", url, strings.NewReader(string(body)))
|
|
req.Header.Set("Content-Type", "application/json")
|
|
resp, err := a.client.Do(req)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer resp.Body.Close()
|
|
if resp.StatusCode >= 400 {
|
|
msg, _ := io.ReadAll(resp.Body)
|
|
return fmt.Errorf("set property failed: HTTP %d - %s", resp.StatusCode, string(msg))
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (a *iotControllerAdapter) ToggleDevice(ctx context.Context, deviceID string) (*sdk.IoTDeviceState, error) {
|
|
url := fmt.Sprintf("%s/api/v1/devices/%s/toggle", a.baseURL, deviceID)
|
|
req, _ := http.NewRequestWithContext(ctx, "POST", url, nil)
|
|
resp, err := a.client.Do(req)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer resp.Body.Close()
|
|
|
|
var dev sdk.IoTDeviceState
|
|
if err := json.NewDecoder(resp.Body).Decode(&dev); err != nil {
|
|
return nil, err
|
|
}
|
|
return &dev, nil
|
|
}
|