init: tianyan-edge edge agent with Ascend NPU inference backend

- Go main process: stream ingestor, infer client, event uploader,
  heartbeat, config agent, OTA agent
- Python inference server rewritten to use CANN ACL (acl module)
  replacing ultralytics/PyTorch; supports YOLOv8 raw and YOLOv10
  NMS-free output formats via OUTPUT_FORMAT env var
- systemd services for edge-agent and edge-infer
- build/install/package scripts targeting arm64

Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-05-06 21:49:04 +08:00
commit f6a85d7dfc
22 changed files with 1219 additions and 0 deletions

View File

@@ -0,0 +1,59 @@
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
}
}

View File

@@ -0,0 +1,54 @@
package control
import (
"bytes"
"context"
"encoding/json"
"fmt"
"log"
"net/http"
"time"
"tianyan-edge/internal/config"
)
type Heartbeat struct {
cfg *config.Config
}
func NewHeartbeat(cfg *config.Config) *Heartbeat {
return &Heartbeat{cfg: cfg}
}
func (h *Heartbeat) Run(ctx context.Context) {
client := &http.Client{Timeout: 5 * time.Second}
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
h.send(client)
}
}
}
func (h *Heartbeat) send(client *http.Client) {
payload := map[string]any{
"edge_id": h.cfg.EdgeID,
"version": h.cfg.Version,
"ts": float64(time.Now().UnixMilli()) / 1000.0,
}
body, _ := json.Marshal(payload)
url := fmt.Sprintf("%s/api/v1/edge/heartbeat", h.cfg.CloudURL)
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+h.cfg.EdgeToken)
resp, err := client.Do(req)
if err != nil {
log.Printf("heartbeat: %v", err)
return
}
resp.Body.Close()
}

View File

@@ -0,0 +1,115 @@
package control
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"encoding/json"
"fmt"
"io"
"log"
"net/http"
"os"
"path/filepath"
"time"
"tianyan-edge/internal/config"
)
type OTAAgent struct {
cfg *config.Config
}
func NewOTAAgent(cfg *config.Config) *OTAAgent {
return &OTAAgent{cfg: cfg}
}
func (o *OTAAgent) Run(ctx context.Context) {
client := &http.Client{Timeout: 30 * time.Second}
ticker := time.NewTicker(10 * time.Minute)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return
case <-ticker.C:
if inMaintenanceWindow() {
o.check(client)
}
}
}
}
func inMaintenanceWindow() bool {
h := time.Now().Hour()
return h >= 22 || h < 6
}
func (o *OTAAgent) check(client *http.Client) {
url := fmt.Sprintf("%s/api/v1/edge/update/manifest?edge_id=%s&current_version=%s",
o.cfg.CloudURL, o.cfg.EdgeID, o.cfg.Version)
req, _ := http.NewRequest(http.MethodGet, url, nil)
req.Header.Set("Authorization", "Bearer "+o.cfg.EdgeToken)
resp, err := client.Do(req)
if err != nil {
log.Printf("ota manifest: %v", err)
return
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return
}
var mf map[string]string
if err := json.NewDecoder(resp.Body).Decode(&mf); err != nil {
return
}
target := mf["target_version"]
if target == "" || target == o.cfg.Version {
return
}
if err := o.downloadAndVerify(client, mf); err != nil {
o.report(client, target, "failed")
return
}
o.report(client, target, "verified")
}
func (o *OTAAgent) downloadAndVerify(client *http.Client, mf map[string]string) error {
url, target := mf["package_url"], mf["target_version"]
expected := mf["sha256"]
if url == "" || expected == "" {
return fmt.Errorf("invalid manifest")
}
resp, err := client.Get(url)
if err != nil {
return err
}
defer resp.Body.Close()
data, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
sum := sha256.Sum256(data)
actual := hex.EncodeToString(sum[:])
if actual != expected {
return fmt.Errorf("checksum mismatch")
}
_ = os.MkdirAll("/opt/tianyan-edge/staging", 0o755)
path := filepath.Join("/opt/tianyan-edge/staging", target+".tar.gz")
return os.WriteFile(path, data, 0o644)
}
func (o *OTAAgent) report(client *http.Client, version, status string) {
payload := map[string]any{"edge_id": o.cfg.EdgeID, "version": version,
"status": status, "ts": float64(time.Now().UnixMilli()) / 1000.0}
body, _ := json.Marshal(payload)
url := fmt.Sprintf("%s/api/v1/edge/update/report", o.cfg.CloudURL)
req, _ := http.NewRequest(http.MethodPost, url, bytes.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Bearer "+o.cfg.EdgeToken)
resp, err := client.Do(req)
if err == nil {
resp.Body.Close()
}
}