refactor: 重大架构升级 — HomeID/RoomID int64化 + 设备驱动重构 + 结构化日志
主要变更: **类型系统重构** - HomeInfo.ID/RoomInfo.ID/HomeID 从 string 改为 int64,json tag 使用 ,string - GetHome/GetRooms/GetRoomDevices 接口同步修改 - 新增 int64FromStr 工具函数,更新 parseHomeInfo 解析逻辑 **新增 Speaker 设备** - 新增 KindSpeaker 类型 - classify 映射覆盖 speaker/bluetooth-speaker/alarm-clock/media-player/playback/music **设备驱动重构** - AC 驱动彻底重写:从硬编码常量改为基于 SPEC ValueList 的动态 valMapper - 所有设备(Fan/Humidifier/Light/OccupancySensor/Switch/TempHumiditySensor/Thermostat/Vacuum/WaterHeater/Cover)新增 GetState() 和 FetchState(ctx) 接口 - base.go 新增 specResolver.format() 方法 **MIoT 模块增强** - MIoTClient 新增 homeIDs 字段,支持按家庭过滤设备 - RefreshDevices 支持 homeIDs 参数 - SpecStdLib 引入 logger.Logger,log.Printf 全部迁移为结构化日志 **其他** - Config 新增 RedirectURL 和 HomeIDs 字段 - AuthInfo.ExpiresIn 类型从 int 改为 int32 - UUID 生成简化为 hex.EncodeToString - 新增 github.com/sirupsen/logrus 依赖 - .gitignore 新增 examples/*/data/ 忽略规则 - 删除已提交的 config.json 敏感配置
This commit is contained in:
+23
-10
@@ -7,7 +7,6 @@ import (
|
||||
"fmt"
|
||||
"hash/crc32"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
@@ -17,6 +16,8 @@ import (
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"xiaomihome/logger"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
@@ -121,6 +122,7 @@ type MIoTSpecParser struct {
|
||||
storage *MIoTStorage
|
||||
specDir string
|
||||
stdLib *SpecStdLib
|
||||
lgr logger.Logger
|
||||
initDone bool
|
||||
initMu sync.Mutex
|
||||
}
|
||||
@@ -130,14 +132,19 @@ func NewMIoTSpecParser(lang string, storage *MIoTStorage, specDir string) *MIoTS
|
||||
if lang == "" {
|
||||
lang = DEFAULT_INTEGRATION_LANGUAGE
|
||||
}
|
||||
return &MIoTSpecParser{
|
||||
p := &MIoTSpecParser{
|
||||
lang: lang,
|
||||
storage: storage,
|
||||
specDir: specDir,
|
||||
stdLib: NewSpecStdLib(lang),
|
||||
lgr: logger.Default(),
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
// SetLogger sets a custom logger for MIoTSpecParser.
|
||||
func (p *MIoTSpecParser) SetLogger(l logger.Logger) { p.lgr = l }
|
||||
|
||||
// Init initializes the parser, loading or refreshing the standard library.
|
||||
func (p *MIoTSpecParser) Init() error {
|
||||
p.initMu.Lock()
|
||||
@@ -161,7 +168,7 @@ func (p *MIoTSpecParser) Init() error {
|
||||
ts, _ = v.Int64()
|
||||
}
|
||||
if ts > 0 && time.Now().Unix()-ts < SPEC_STD_LIB_EFFECTIVE_TIME {
|
||||
log.Printf("[spec] using cached spec std lib, ts=%d", ts)
|
||||
p.lgr.Debugf("[spec] using cached spec std lib, ts=%d", ts)
|
||||
p.stdLib.Load(dataMap)
|
||||
p.initDone = true
|
||||
return nil
|
||||
@@ -173,12 +180,12 @@ func (p *MIoTSpecParser) Init() error {
|
||||
|
||||
// Refresh from cloud
|
||||
if err := p.stdLib.Refresh(); err != nil {
|
||||
log.Printf("[spec] refresh std lib failed: %v", err)
|
||||
p.lgr.Debugf("[spec] refresh std lib failed: %v", err)
|
||||
// Try stale cache as fallback
|
||||
if cacheMap, ok := stdLibCache.(map[string]interface{}); ok {
|
||||
if dataField, ok := cacheMap["data"]; ok {
|
||||
if dataMap, ok := dataField.(map[string]interface{}); ok {
|
||||
log.Printf("[spec] using stale cached spec std lib")
|
||||
p.lgr.Debugf("[spec] using stale cached spec std lib")
|
||||
p.stdLib.Load(dataMap)
|
||||
p.initDone = true
|
||||
return nil
|
||||
@@ -275,7 +282,7 @@ func (p *MIoTSpecParser) downloadSpecFile(urn, filePath string) error {
|
||||
return fmt.Errorf("write %s: %w", filePath, err)
|
||||
}
|
||||
|
||||
log.Printf("[spec] downloaded spec: %s -> %s", urn, filePath)
|
||||
p.lgr.Debugf("[spec] downloaded spec: %s -> %s", urn, filePath)
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -567,6 +574,7 @@ func (p *MIoTSpecParser) skipProprietary(item map[string]interface{}) bool {
|
||||
// SpecStdLib is the MIoT-Spec-V2 standard library for multi-language descriptions.
|
||||
type SpecStdLib struct {
|
||||
lang string
|
||||
lgr logger.Logger
|
||||
devices map[string]map[string]string
|
||||
services map[string]map[string]string
|
||||
properties map[string]map[string]string
|
||||
@@ -580,8 +588,9 @@ func NewSpecStdLib(lang string) *SpecStdLib {
|
||||
if lang == "" {
|
||||
lang = DEFAULT_INTEGRATION_LANGUAGE
|
||||
}
|
||||
return &SpecStdLib{
|
||||
s := &SpecStdLib{
|
||||
lang: lang,
|
||||
lgr: logger.Default(),
|
||||
devices: make(map[string]map[string]string),
|
||||
services: make(map[string]map[string]string),
|
||||
properties: make(map[string]map[string]string),
|
||||
@@ -589,8 +598,12 @@ func NewSpecStdLib(lang string) *SpecStdLib {
|
||||
actions: make(map[string]map[string]string),
|
||||
values: make(map[string]map[string]string),
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
// SetLogger sets a custom logger for SpecStdLib.
|
||||
func (s *SpecStdLib) SetLogger(l logger.Logger) { s.lgr = l }
|
||||
|
||||
// Load loads standard library data from a parsed map.
|
||||
func (s *SpecStdLib) Load(data map[string]interface{}) {
|
||||
if data == nil {
|
||||
@@ -690,7 +703,7 @@ func (s *SpecStdLib) Refresh() error {
|
||||
var lastErr error
|
||||
for attempt := 0; attempt < 3; attempt++ {
|
||||
if attempt > 0 {
|
||||
log.Printf("[spec] retry std lib refresh, attempt %d", attempt)
|
||||
s.lgr.Debugf("[spec] retry std lib refresh, attempt %d", attempt)
|
||||
time.Sleep(time.Duration(attempt) * time.Second)
|
||||
}
|
||||
|
||||
@@ -723,7 +736,7 @@ func (s *SpecStdLib) Refresh() error {
|
||||
url := fmt.Sprintf("https://cdn.cnbj1.fds.api.mi-img.com/res-conf/xiaomi-home/std_ex_%s.json", name)
|
||||
extData, err := s.fetchJSONMapMap(httpClient, url)
|
||||
if err != nil {
|
||||
log.Printf("[spec] get external std lib failed, %s: %v", name, err)
|
||||
s.lgr.Debugf("[spec] get external std lib failed, %s: %v", name, err)
|
||||
continue
|
||||
}
|
||||
if extData != nil {
|
||||
@@ -763,7 +776,7 @@ func (s *SpecStdLib) Refresh() error {
|
||||
|
||||
if len(errs) > 0 {
|
||||
lastErr = fmt.Errorf("std lib refresh errors: %v", errs)
|
||||
log.Printf("[spec] std lib refresh error: %v", lastErr)
|
||||
s.lgr.Debugf("[spec] std lib refresh error: %v", lastErr)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user