From bf18a04040851e443db8c65dd1b1a5c052cb7315 Mon Sep 17 00:00:00 2001 From: mei Date: Wed, 20 Aug 2025 10:07:10 +0800 Subject: [PATCH] =?UTF-8?q?feat(internal):=20=E6=B7=BB=E5=8A=A0=E7=A9=BA?= =?UTF-8?q?=E8=B0=83=E7=8A=B6=E6=80=81=E4=B8=8A=E6=8A=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 新增 Report 函数,实现空调状态的定期上报 - 添加 airConditionerBodyMessage 结构体用于构造上报数据 - 使用 http.Client 发送 POST 请求到上报 URL - 在 main.go 中调用 Report 函数,替换原有的空实现 --- configs/config.go | 8 +++++ internal/report.go | 85 ++++++++++++++++++++++++++++++++++++++++++++++ main.go | 6 ++-- 3 files changed, 95 insertions(+), 4 deletions(-) create mode 100644 configs/config.go diff --git a/configs/config.go b/configs/config.go new file mode 100644 index 0000000..5e68f53 --- /dev/null +++ b/configs/config.go @@ -0,0 +1,8 @@ +package configs + +const ( + AC_PORT = "/dev/ttyUSB0" + REPORT_URL = "https://pa-api.mmeiblog.cn" + X_API_KEY = "X-API-KEY" + AGENT_ID = 1 +) diff --git a/internal/report.go b/internal/report.go index 5bf0569..d0f89e3 100644 --- a/internal/report.go +++ b/internal/report.go @@ -1 +1,86 @@ package internal + +import ( + "bytes" + "encoding/json" + "io" + "log" + "net/http" + "time" + + "git.mmeiblog.cn/mei/FatAgent/configs" + "git.mmeiblog.cn/mei/FatAgent/pkg" +) + +const ( + reportPath = "/receive/airConditioner" + httpTimeout = 30 * time.Second +) + +type airConditionerBodyMessage struct { + AgentID int `json:"agentID"` + Timestamp int64 `json:"timestamp"` + Message pkg.ACStatus `json:"message"` +} + +var httpClient = &http.Client{ + Timeout: httpTimeout, +} + +func Report() { + now := time.Now().Unix() + + ac, err := pkg.NewACController(configs.AC_PORT) + if err != nil { + log.Printf("错误:创建控制器失败: %v", err) + return + } + + ACStatus, err := ac.GetAllStatus() + if err != nil { + log.Printf("错误:查询状态失败: %v", err) + return + } + + payload := airConditionerBodyMessage{ + AgentID: configs.AGENT_ID, + Timestamp: now, + Message: *ACStatus, + } + + bodyBytes, err := json.Marshal(payload) + if err != nil { + log.Printf("错误:JSON序列化失败: %v", err) + return + } + + url := configs.REPORT_URL + reportPath + req, err := http.NewRequest("POST", url, bytes.NewReader(bodyBytes)) + if err != nil { + log.Printf("错误:创建HTTP请求失败: %v", err) + return + } + + req.Header.Add("x-api-key", configs.X_API_KEY) + req.Header.Add("Content-Type", "application/json") + + res, err := httpClient.Do(req) + if err != nil { + log.Printf("错误:发送HTTP请求失败: %v", err) + return + } + defer res.Body.Close() + + if res.StatusCode != http.StatusOK { + log.Printf("警告:HTTP响应状态码非200,状态码: %d", res.StatusCode) + return + } + + body, err := io.ReadAll(res.Body) + if err != nil { + log.Printf("错误:读取HTTP响应失败: %v", err) + return + } + + log.Printf("上报成功,响应内容:%s", body) +} diff --git a/main.go b/main.go index cb9d3b5..7637778 100644 --- a/main.go +++ b/main.go @@ -6,16 +6,14 @@ import ( "os/signal" "syscall" - "git.mmeiblog.cn/mei/FatAgent/pkg" + "git.mmeiblog.cn/mei/FatAgent/internal" "github.com/robfig/cron/v3" ) func main() { c := cron.New() - c.AddFunc("@every 1m", func() { - pkg.NewACController("/dev/tty1") - }) + c.AddFunc("@every 1m", internal.Report) c.Start()