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:
2026-07-13 10:40:20 +08:00
parent 8ffc9e4e44
commit b808e51b3e
68 changed files with 2506 additions and 688 deletions
+14 -7
View File
@@ -7,12 +7,13 @@ import (
"encoding/binary"
"encoding/hex"
"fmt"
"log"
"strings"
"sync"
"time"
"github.com/hashicorp/mdns"
"xiaomihome/logger"
)
// ============================================================================
@@ -165,6 +166,7 @@ type MipsService struct {
mu sync.Mutex
services map[string]*MipsServiceData // keyed by groupID
subs map[subKey]MipsServiceChangeHandler
lgr logger.Logger
stopCh chan struct{}
}
@@ -177,12 +179,17 @@ type subKey struct {
// NewMipsService creates a new MipsService.
func NewMipsService() *MipsService {
return &MipsService{
s := &MipsService{
services: make(map[string]*MipsServiceData),
subs: make(map[subKey]MipsServiceChangeHandler),
lgr: logger.Default(),
}
return s
}
// SetLogger sets a custom logger for MipsService.
func (s *MipsService) SetLogger(l logger.Logger) { s.lgr = l }
// Init starts the mDNS browser in a background goroutine.
// Safe to call multiple times; subsequent calls after the first are no-ops
// unless preceded by Deinit().
@@ -196,7 +203,7 @@ func (s *MipsService) Init() {
s.mu.Unlock()
go s.runBrowser()
log.Println("[mdns] mips service browser started")
s.lgr.Infof("[mdns] mips service browser started")
}
// Deinit stops the browser goroutine and clears all cached services and
@@ -218,7 +225,7 @@ func (s *MipsService) Deinit() {
s.subs = make(map[subKey]MipsServiceChangeHandler)
s.mu.Unlock()
log.Println("[mdns] mips service deinitialized")
s.lgr.Infof("[mdns] mips service deinitialized")
}
// GetServices returns cached services. When groupID is non-empty, only that
@@ -282,7 +289,7 @@ func (s *MipsService) runBrowser() {
// Run the blocking Query in a goroutine so we can detect stop.
go func() {
if err := mdns.Query(params); err != nil {
log.Printf("[mdns] query error: %v", err)
s.lgr.Infof("[mdns] query error: %v", err)
}
close(entriesCh)
}()
@@ -304,7 +311,7 @@ func (s *MipsService) runBrowser() {
func (s *MipsService) handleEntry(entry *mdns.ServiceEntry) {
svc, err := entryToServiceData(entry)
if err != nil {
log.Printf("[mdns] skip entry: %v", err)
s.lgr.Infof("[mdns] skip entry: %v", err)
return
}
if !svc.validService() {
@@ -332,7 +339,7 @@ func (s *MipsService) handleEntry(entry *mdns.ServiceEntry) {
// callSubsLocked notifies all subscribers whose groupID matches.
// Caller MUST hold s.mu.
func (s *MipsService) callSubsLocked(groupID string, state MipsServiceState, data map[string]interface{}) {
log.Printf("[mdns] service change: groupID=%s, state=%s", groupID, state)
s.lgr.Infof("[mdns] service change: groupID=%s, state=%s", groupID, state)
for sk, handler := range s.subs {
if sk.groupID == groupID || sk.groupID == "*" {
go handler(groupID, state, data)