60 lines
1.2 KiB
Go
60 lines
1.2 KiB
Go
|
|
package control
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"encoding/json"
|
||
|
|
"fmt"
|
||
|
|
"log"
|
||
|
|
"net/http"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"tianyan-edge/internal/config"
|
||
|
|
)
|
||
|
|
|
||
|
|
type ConfigAgent struct {
|
||
|
|
cfg *config.Config
|
||
|
|
}
|
||
|
|
|
||
|
|
func NewConfigAgent(cfg *config.Config) *ConfigAgent {
|
||
|
|
return &ConfigAgent{cfg: cfg}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *ConfigAgent) Run(ctx context.Context) {
|
||
|
|
client := &http.Client{Timeout: 5 * time.Second}
|
||
|
|
ticker := time.NewTicker(60 * time.Second)
|
||
|
|
defer ticker.Stop()
|
||
|
|
for {
|
||
|
|
select {
|
||
|
|
case <-ctx.Done():
|
||
|
|
return
|
||
|
|
case <-ticker.C:
|
||
|
|
a.pull(client)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func (a *ConfigAgent) pull(client *http.Client) {
|
||
|
|
url := fmt.Sprintf("%s/api/v1/edge/config?edge_id=%s", a.cfg.CloudURL, a.cfg.EdgeID)
|
||
|
|
req, _ := http.NewRequest(http.MethodGet, url, nil)
|
||
|
|
req.Header.Set("Authorization", "Bearer "+a.cfg.EdgeToken)
|
||
|
|
resp, err := client.Do(req)
|
||
|
|
if err != nil {
|
||
|
|
log.Printf("config pull: %v", err)
|
||
|
|
return
|
||
|
|
}
|
||
|
|
defer resp.Body.Close()
|
||
|
|
if resp.StatusCode != 200 {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
var data map[string]any
|
||
|
|
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
|
||
|
|
return
|
||
|
|
}
|
||
|
|
if v, ok := data["infer_fps"].(float64); ok && int(v) > 0 {
|
||
|
|
a.cfg.InferFPS = int(v)
|
||
|
|
}
|
||
|
|
if v, ok := data["conf_threshold"].(float64); ok {
|
||
|
|
a.cfg.ConfThreshold = v
|
||
|
|
}
|
||
|
|
}
|