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:
+17
-4
@@ -6,6 +6,8 @@ import (
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
|
||||
"xiaomihome/logger"
|
||||
)
|
||||
|
||||
// ============================================================================
|
||||
@@ -54,6 +56,9 @@ type MIoTDevice struct {
|
||||
valueSubList map[string]map[string]func(map[string]interface{}, interface{})
|
||||
subID int
|
||||
subIDMu sync.Mutex
|
||||
|
||||
// === Logger ===
|
||||
lgr logger.Logger
|
||||
}
|
||||
|
||||
// NewMIoTDevice creates a new MIoTDevice.
|
||||
@@ -68,6 +73,11 @@ func NewMIoTDevice(miotClient *MIoTClient, deviceInfo map[string]interface{}, sp
|
||||
deviceStateSubList: make(map[string]map[string]func(string, interface{})),
|
||||
valueSubList: make(map[string]map[string]func(map[string]interface{}, interface{})),
|
||||
subDevices: make(map[int]*MIoTDevice),
|
||||
lgr: logger.Default(),
|
||||
}
|
||||
|
||||
if specInstance != nil {
|
||||
dev.serviceList = specInstance.GetServices()
|
||||
}
|
||||
|
||||
// Parse device metadata from deviceInfo
|
||||
@@ -130,6 +140,9 @@ func NewMIoTDevice(miotClient *MIoTClient, deviceInfo map[string]interface{}, sp
|
||||
return dev
|
||||
}
|
||||
|
||||
// SetLogger sets a custom logger for MIoTDevice.
|
||||
func (d *MIoTDevice) SetLogger(l logger.Logger) { d.lgr = l }
|
||||
|
||||
// LoadFromCloud creates a MIoTDevice from cloud device data.
|
||||
// Aligns with Python: populating device_info from cloud response.
|
||||
func LoadFromCloud(miotClient *MIoTClient, cloudData map[string]interface{}) *MIoTDevice {
|
||||
@@ -193,7 +206,7 @@ func (d *MIoTDevice) SetSpecInstance(specInstance *MIoTSpecInstance) {
|
||||
// Returns: subscription ID for unsubscribing.
|
||||
func (d *MIoTDevice) SubProp(siid, piid int, handler func(map[string]interface{}, interface{})) string {
|
||||
if d.miotClient == nil {
|
||||
fmt.Printf("[MIoTDevice] Warning: miotClient is nil, cannot subscribe to property\n")
|
||||
d.lgr.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot subscribe to property\n")
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -216,7 +229,7 @@ func (d *MIoTDevice) SubProp(siid, piid int, handler func(map[string]interface{}
|
||||
// Aligns with Python MIoTDevice.unsub_prop.
|
||||
func (d *MIoTDevice) UnsubProp(subID string) {
|
||||
if d.miotClient == nil {
|
||||
fmt.Printf("[MIoTDevice] Warning: miotClient is nil, cannot unsubscribe from property\n")
|
||||
d.lgr.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot unsubscribe from property\n")
|
||||
return
|
||||
}
|
||||
|
||||
@@ -242,7 +255,7 @@ func (d *MIoTDevice) UnsubProp(subID string) {
|
||||
// Returns: subscription ID for unsubscribing.
|
||||
func (d *MIoTDevice) SubEvent(siid, eiid int, handler func(map[string]interface{}, interface{})) string {
|
||||
if d.miotClient == nil {
|
||||
fmt.Printf("[MIoTDevice] Warning: miotClient is nil, cannot subscribe to event\n")
|
||||
d.lgr.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot subscribe to event\n")
|
||||
return ""
|
||||
}
|
||||
|
||||
@@ -265,7 +278,7 @@ func (d *MIoTDevice) SubEvent(siid, eiid int, handler func(map[string]interface{
|
||||
// Aligns with Python MIoTDevice.unsub_event.
|
||||
func (d *MIoTDevice) UnsubEvent(subID string) {
|
||||
if d.miotClient == nil {
|
||||
fmt.Printf("[MIoTDevice] Warning: miotClient is nil, cannot unsubscribe from event\n")
|
||||
d.lgr.Warnf("[MIoTDevice] Warning: miotClient is nil, cannot unsubscribe from event\n")
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user