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
+19 -15
View File
@@ -25,12 +25,16 @@ func (c *MIoTClient) SubProp(did string, handler func(map[string]interface{}, in
subID := fmt.Sprintf("%p", handler)
c.subTree.Sub(topic, handler, nil)
fmt.Printf("[MIoTClient] Subscribed to property: %s (topic=%s, subID=%s)\n", did, topic, subID)
c.lgr.Infof("[MIoTClient] Subscribed to property: %s (topic=%s, subID=%s)\n", did, topic, subID)
c.mu.Unlock()
// RequestRefreshProp must be outside the lock (it also acquires c.mu)
c.RequestRefreshProp(did, siid, piid)
// siid=0 && piid=0 为通配符订阅,展开为设备所有可读属性逐条刷新
if siid == 0 && piid == 0 {
c.RefreshDeviceAllProps(did)
} else {
c.RequestRefreshProp(did, siid, piid)
}
return subID
}
@@ -48,7 +52,7 @@ func (c *MIoTClient) UnsubProp(did string, subID string) {
// TODO: Implement proper tracking of per-device subscription count
// For now, just log
fmt.Printf("[MIoTClient] Unsubscribed from property: %s (subID=%s)\n", did, subID)
c.lgr.Infof("[MIoTClient] Unsubscribed from property: %s (subID=%s)\n", did, subID)
}
// SubEvent subscribes to device events.
@@ -65,7 +69,7 @@ func (c *MIoTClient) SubEvent(did string, handler func(map[string]interface{}, i
subID := fmt.Sprintf("%p", handler)
c.subTree.Sub(topic, handler, nil)
fmt.Printf("[MIoTClient] Subscribed to event: %s (topic=%s, subID=%s)\n", did, topic, subID)
c.lgr.Infof("[MIoTClient] Subscribed to event: %s (topic=%s, subID=%s)\n", did, topic, subID)
return subID
}
@@ -79,7 +83,7 @@ func (c *MIoTClient) UnsubEvent(did string, subID string) {
// Remove from subTree by subID
c.subTree.Unsub(subID)
fmt.Printf("[MIoTClient] Unsubscribed from event: %s (subID=%s)\n", did, subID)
c.lgr.Infof("[MIoTClient] Unsubscribed from event: %s (subID=%s)\n", did, subID)
}
// updateDeviceMsgSub updates device message subscription.
@@ -151,7 +155,7 @@ func (c *MIoTClient) updateDeviceMsgSub(did string) {
// Update subscription source
c.subSourceList[did] = newSource
fmt.Printf("[MIoTClient] updateDeviceMsgSub: %s source %s → %s (gw=%v/%v, lan=%v/%v, cloud=%v)\n",
c.lgr.Infof("[MIoTClient] updateDeviceMsgSub: %s source %s → %s (gw=%v/%v, lan=%v/%v, cloud=%v)\n",
did, oldSource, newSource, gwOnline, gwPushAvailable, lanOnline, lanPushAvailable, cloudOnline)
}
@@ -256,13 +260,13 @@ func (c *MIoTClient) handleMIPSMessage(did string, payload []byte) {
// Fallback: binary MIPS protocol (used by gateway mipsLocal)
msg := UnpackMipsMessage(payload)
if msg == nil || msg.Payload == "" {
fmt.Printf("[MIoTClient] handleMIPSMessage: empty or invalid payload for %s (%d bytes)\n", did, len(payload))
c.lgr.Infof("[MIoTClient] handleMIPSMessage: empty or invalid payload for %s (%d bytes)\n", did, len(payload))
return
}
// Parse JSON from unpacked payload
if err := json.Unmarshal([]byte(msg.Payload), &params); err != nil {
fmt.Printf("[MIoTClient] handleMIPSMessage: JSON parse error for %s: %v\n", did, err)
c.lgr.Errorf("[MIoTClient] handleMIPSMessage: JSON parse error for %s: %v\n", did, err)
return
}
@@ -285,7 +289,7 @@ func (c *MIoTClient) dispatchMIPSMessage(did string, params map[string]interface
} else if _, ok := params["eiid"]; ok {
c.OnEventMsg(params, nil)
} else {
fmt.Printf("[MIoTClient] handleMIPSMessage: unknown message format for %s: %v\n", did, params)
c.lgr.Infof("[MIoTClient] handleMIPSMessage: unknown message format for %s: %v\n", did, params)
}
}
@@ -297,11 +301,11 @@ func (c *MIoTClient) handleMIPSStateMessage(did string, payload []byte) {
// Fallback: binary MIPS protocol
msg := UnpackMipsMessage(payload)
if msg == nil || msg.Payload == "" {
fmt.Printf("[MIoTClient] handleMIPSStateMessage: empty payload for %s (%d bytes)\n", did, len(payload))
c.lgr.Infof("[MIoTClient] handleMIPSStateMessage: empty payload for %s (%d bytes)\n", did, len(payload))
return
}
if err := json.Unmarshal([]byte(msg.Payload), &params); err != nil {
fmt.Printf("[MIoTClient] handleMIPSStateMessage: JSON parse error for %s: %v\n", did, err)
c.lgr.Errorf("[MIoTClient] handleMIPSStateMessage: JSON parse error for %s: %v\n", did, err)
return
}
}
@@ -322,7 +326,7 @@ func (c *MIoTClient) handleMIPSStateMessage(did string, payload []byte) {
}
if len(state) == 0 {
fmt.Printf("[MIoTClient] handleMIPSStateMessage: no state info in payload for %s: %v\n", did, params)
c.lgr.Infof("[MIoTClient] handleMIPSStateMessage: no state info in payload for %s: %v\n", did, params)
return
}
@@ -368,7 +372,7 @@ func (c *MIoTClient) OnPropMsg(params map[string]interface{}, ctx interface{}) {
}
if did == "" {
fmt.Printf("[MIoTClient] OnPropMsg: missing did in params=%v\n", params)
c.lgr.Infof("[MIoTClient] OnPropMsg: missing did in params=%v\n", params)
return
}
@@ -420,7 +424,7 @@ func (c *MIoTClient) OnEventMsg(params map[string]interface{}, ctx interface{})
}
if did == "" {
fmt.Printf("[MIoTClient] OnEventMsg: missing did in params=%v\n", params)
c.lgr.Infof("[MIoTClient] OnEventMsg: missing did in params=%v\n", params)
return
}