Initial commit: Cyrene Plugins SDK + Plugin Manager
Extracted from Cyrene main repo (backend/pkg/plugins + backend/plugin-manager). Contains SDK interfaces (Plugin/Tool/HostAPI), 13 built-in plugins, ToolRegistry with call log ring buffer, and Plugin Manager REST API service. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,112 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.yeij.top/AskaEth/Cyrene-Plugins/sdk"
|
||||
iotquery "git.yeij.top/AskaEth/Cyrene-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
|
||||
}
|
||||
Reference in New Issue
Block a user