This repository has been archived on 2026-08-12. You can view files and clone it. You cannot open issues or pull requests or push a commit.
Files
Cyrene/backend/plugin-manager/cmd/iot_adapter.go
T
AskaEth 673ff752c5 feat: 插件-工具合并 — 创建 pkg/plugins 共享模块并移除 tool-engine
- 新增 backend/pkg/plugins/ 共享模块:SDK 接口、PluginManager、ToolRegistry(含环形缓冲区调用日志)
- 13 个通用插件从 plugin-manager 迁移至共享模块(import 路径统一)
- ai-core 切换至共享 ToolRegistry,进程内执行(零网络开销),包装 6 个专属工具
- plugin-manager 迁移至共享模块,保留管理 REST API
- 新增 DevTools 插件管理面板(侧边栏 → 🔌 插件管理)
- 移除 tool-engine 服务(从 go.work、DevTools 配置、编译系统)
- 工具调用记录 API 从 Tool-Engine 迁至 AI-Core(/api/v1/tools/calls)
- ai-core ContextStore 启动时从 PostgreSQL 恢复会话历史
- 清理所有过时引用和备份文件

Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
2026-05-25 20:52:39 +08:00

113 lines
3.0 KiB
Go

package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"strings"
"time"
"github.com/yourname/cyrene-ai/pkg/plugins/sdk"
iotquery "github.com/yourname/cyrene-ai/pkg/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
}