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
+22 -17
View File
@@ -38,7 +38,7 @@ func (c *MIoTClient) getOrCreateDevice(did string, info map[string]interface{})
spec, err := c.specParser.Parse(urn, model)
if err != nil {
fmt.Printf("[MIoTClient] getOrCreateDevice: SPEC parse failed for %s (%s/%s): %v\n", did, urn, model, err)
c.lgr.Errorf("[MIoTClient] getOrCreateDevice: SPEC parse failed for %s (%s/%s): %v\n", did, urn, model, err)
return nil
}
@@ -60,15 +60,15 @@ func (c *MIoTClient) getOrCreateDevice(did string, info map[string]interface{})
// Device management (aligned with Python miot_client.py)
// ============================================================================
// RefreshDevices refreshes device list from cloud.
// RefreshDevices refreshes device list from cloud for the given home IDs (nil=all).
// Aligns with Python: __update_devices_from_cloud_async (line 1384).
func (c *MIoTClient) RefreshDevices() error {
func (c *MIoTClient) RefreshDevices(homeIDs []string) error {
if c.http == nil {
return fmt.Errorf("HTTP client not available")
}
// 1. Get full device list from cloud
result, err := c.http.GetDevices(nil)
result, err := c.http.GetDevices(homeIDs)
if err != nil {
return fmt.Errorf("failed to get devices: %v", err)
}
@@ -143,7 +143,12 @@ func (c *MIoTClient) RefreshDevices() error {
}
}
// 3. Diff: compare cloudDevices with current deviceListCloud
// 3. Merge shared devices into cloud devices (python treats them the same)
for did, info := range sharedDevices {
cloudDevices[did] = info
}
// 4. Diff: compare cloudDevices with current deviceListCloud
changed := false
// Detect additions and updates
@@ -160,7 +165,7 @@ func (c *MIoTClient) RefreshDevices() error {
} else {
c.deviceListCache[did] = cloneMap(info)
}
fmt.Printf("[MIoTClient] Device added: %s (%v)\n", did, info["name"])
c.lgr.Infof("[MIoTClient] Device added: %s (%v)\n", did, info["name"])
changed = true
} else {
// Check if online status changed
@@ -172,7 +177,7 @@ func (c *MIoTClient) RefreshDevices() error {
if cacheInfo, ok := c.deviceListCache[did]; ok {
cacheInfo["online"] = newOnline
}
fmt.Printf("[MIoTClient] Device online changed: %s online=%v\n", did, newOnline)
c.lgr.Infof("[MIoTClient] Device online changed: %s online=%v\n", did, newOnline)
changed = true
}
}
@@ -186,7 +191,7 @@ func (c *MIoTClient) RefreshDevices() error {
cacheInfo["online"] = false
cacheInfo["push_available"] = false
}
fmt.Printf("[MIoTClient] Device removed from cloud: %s\n", did)
c.lgr.Infof("[MIoTClient] Device removed from cloud: %s\n", did)
changed = true
}
}
@@ -209,7 +214,7 @@ func (c *MIoTClient) RefreshDevices() error {
}
deviceKey := fmt.Sprintf("%s_%s", c.uid, c.cloudServer)
if err := c.storage.Save("miot_devices", deviceKey, deviceListForStorage); err != nil {
fmt.Printf("[MIoTClient] Warning: failed to save devices: %v\n", err)
c.lgr.Warnf("[MIoTClient] Warning: failed to save devices: %v\n", err)
}
}
@@ -218,7 +223,7 @@ func (c *MIoTClient) RefreshDevices() error {
c.ShowDevicesChangedNotify()
}
fmt.Printf("[MIoTClient] Devices refreshed: cloud=%d, cache=%d\n", len(cloudDevices), len(c.deviceListCache))
c.lgr.Infof("[MIoTClient] Devices refreshed: cloud=%d, cache=%d\n", len(cloudDevices), len(c.deviceListCache))
return nil
}
@@ -303,11 +308,11 @@ func (c *MIoTClient) aggregateDeviceState(did string) {
func (c *MIoTClient) LoadDevices() error {
// Try to load from cache first
if err := c.loadCacheDevice(); err != nil {
fmt.Printf("[MIoTClient] Failed to load from cache: %v\n", err)
c.lgr.Errorf("[MIoTClient] Failed to load from cache: %v\n", err)
}
// Then refresh from cloud
return c.RefreshDevices()
return c.RefreshDevices(c.homeIDs)
}
// GetDeviceInfo returns device metadata for a given DID.
@@ -351,15 +356,15 @@ func (c *MIoTClient) ScheduleRefreshDevices(delay time.Duration) {
}
c.refreshCloudDevicesTimer = time.AfterFunc(delay, func() {
fmt.Printf("[MIoTClient] Refreshing devices...\n")
if err := c.RefreshDevices(); err != nil {
fmt.Printf("[MIoTClient] Refresh failed, will retry: %v\n", err)
c.lgr.Infof("[MIoTClient] Refreshing devices...\n")
if err := c.RefreshDevices(c.homeIDs); err != nil {
c.lgr.Errorf("[MIoTClient] Refresh failed, will retry: %v\n", err)
// Retry after 60s
c.ScheduleRefreshDevices(RefreshCloudDevicesRetryDelay * time.Millisecond)
}
})
fmt.Printf("[MIoTClient] Device refresh scheduled in %v\n", delay)
c.lgr.Infof("[MIoTClient] Device refresh scheduled in %v\n", delay)
}
// Start starts the MIoTClient and schedules initial device load.
@@ -373,7 +378,7 @@ func (c *MIoTClient) Start() error {
// Schedule initial device load (delayed, aligns with REFRESH_CLOUD_DEVICES_DELAY=6 in Python)
c.ScheduleRefreshDevices(RefreshCloudDevicesDelay * time.Millisecond)
fmt.Printf("[MIoTClient] Started\n")
c.lgr.Infof("[MIoTClient] Started\n")
return nil
}