增加待机报文缓存,无网状态也能执行待机任务;投影仪指令结果以设备状态为准

This commit is contained in:
2025-04-29 13:48:35 +08:00
parent 40293e5e9b
commit e1384504f1
13 changed files with 266 additions and 32 deletions

63
config/cache_publish.go Normal file
View File

@@ -0,0 +1,63 @@
package config
import (
"encoding/json"
"fmt"
"github.com/eclipse/paho.golang/paho"
"os"
)
// Cache MQTT消息缓存
type Cache string
// Get 读取缓存数据
func (s Cache) Get() (*paho.Publish, error) {
// 判断文件是否存在
if _, err := os.Stat(string(s)); os.IsNotExist(err) {
return nil, fmt.Errorf("文件不存在: %s", err)
}
// 读取文件内容
file, err := os.ReadFile(string(s))
if err != nil {
return nil, fmt.Errorf("读取文件失败: %w", err)
}
// 解析数据
data := &paho.Publish{}
err = json.Unmarshal(file, data)
if err != nil {
return nil, fmt.Errorf("解析数据失败: %w", err)
}
return data, nil
}
// Set 设置缓存数据
func (s Cache) Set(data *paho.Publish) error {
if s == "" {
return fmt.Errorf("缓存路径不能为空")
}
if data == nil {
return nil
}
// 创建文件
file, err := os.Create(string(s))
if err != nil {
return fmt.Errorf("创建文件失败: %w", err)
}
defer file.Close()
// 序列化数据
dataBytes, err := json.Marshal(data)
if err != nil {
return fmt.Errorf("序列化数据失败: %w", err)
}
// 写入数据
_, err = file.Write(dataBytes)
if err != nil {
return fmt.Errorf("写入数据失败: %w", err)
}
return nil
}