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:
@@ -0,0 +1,147 @@
|
||||
package bridge
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"xiaomihome/xiaomi/devices"
|
||||
)
|
||||
|
||||
// ── 回调 ──
|
||||
|
||||
// OnStateChanged 注册全局设备状态变化回调。state 是 devices 包的类型化 State 结构体,
|
||||
// 调用方自行 switch 类型(ACState / LightState / FanState ...)。
|
||||
func (b *Bridge) OnStateChanged(handler func(id int64, did, deviceType, changed string, state interface{})) {
|
||||
b.mu.Lock()
|
||||
b.onStateChanged = handler
|
||||
b.mu.Unlock()
|
||||
}
|
||||
|
||||
// StartReceiving 为所有设备注册 MQTT 消息接收回调(MQTT 订阅已在 miot 层完成)。
|
||||
func (b *Bridge) StartReceiving() {
|
||||
b.mu.RLock()
|
||||
defer b.mu.RUnlock()
|
||||
for did, d := range b.dv {
|
||||
devType := deviceTypeName(d)
|
||||
switch ctrl := d.(type) {
|
||||
case devices.AirConditioner:
|
||||
ctrl.OnStateChanged(func(s devices.ACState, ch string) {
|
||||
b.fire(did, devType, ch, s)
|
||||
})
|
||||
case devices.Light:
|
||||
ctrl.OnStateChanged(func(s devices.LightState, ch string) {
|
||||
b.fire(did, devType, ch, s)
|
||||
})
|
||||
case devices.Switch:
|
||||
ctrl.OnStateChanged(func(ch int, on bool) {
|
||||
b.fire(did, devType, fmt.Sprintf("ch%d", ch), map[string]interface{}{"channel": ch, "on": on})
|
||||
})
|
||||
case devices.Fan:
|
||||
ctrl.OnStateChanged(func(s devices.FanState, ch string) {
|
||||
b.fire(did, devType, ch, s)
|
||||
})
|
||||
case devices.Cover:
|
||||
ctrl.OnStateChanged(func(pos int) {
|
||||
b.fire(did, devType, devices.PropPosition, pos)
|
||||
})
|
||||
case devices.Humidifier:
|
||||
ctrl.OnStateChanged(func(s devices.HumidifierState, ch string) {
|
||||
b.fire(did, devType, ch, s)
|
||||
})
|
||||
case devices.Vacuum:
|
||||
ctrl.OnStateChanged(func(s devices.VacuumStatus, ch string) {
|
||||
b.fire(did, devType, ch, s)
|
||||
})
|
||||
case devices.WaterHeater:
|
||||
ctrl.OnStateChanged(func(s devices.HeaterState, ch string) {
|
||||
b.fire(did, devType, ch, s)
|
||||
})
|
||||
case devices.Thermostat:
|
||||
ctrl.OnStateChanged(func(s devices.HeaterState, ch string) {
|
||||
b.fire(did, devType, ch, s)
|
||||
})
|
||||
case devices.TempHumiditySensor:
|
||||
ctrl.OnStateChanged(func(temp, hum float64) {
|
||||
b.fire(did, devType, "temperature_humidity", map[string]float64{"temperature": temp, "humidity": hum})
|
||||
})
|
||||
case devices.OccupancySensor:
|
||||
ctrl.OnStateChanged(func(occ bool, lux float64) {
|
||||
b.fire(did, devType, "occupancy", map[string]interface{}{"occupied": occ, "illuminance": lux})
|
||||
})
|
||||
case devices.Speaker:
|
||||
ctrl.OnStateChanged(func(s devices.SpeakerState, ch string) {
|
||||
b.fire(did, devType, ch, s)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bridge) fire(did, devType, changed string, state interface{}) {
|
||||
b.mu.RLock()
|
||||
cb := b.onStateChanged
|
||||
b.mu.RUnlock()
|
||||
if cb != nil {
|
||||
cb(b.id, did, devType, changed, state)
|
||||
}
|
||||
}
|
||||
|
||||
func deviceTypeNameRaw(d interface{}) string {
|
||||
if d == nil { return "unknown" }
|
||||
return deviceTypeName(d)
|
||||
}
|
||||
|
||||
// ── 状态 ──
|
||||
|
||||
// FetchAllStates 批量云端拉取所有设备状态,更新缓存。
|
||||
func (b *Bridge) FetchAllStates(ctx context.Context) {
|
||||
b.mu.RLock()
|
||||
defer b.mu.RUnlock()
|
||||
for _, d := range b.dv {
|
||||
switch ctrl := d.(type) {
|
||||
case devices.AirConditioner:
|
||||
if s, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d AC state: on=%v mode=%s target=%.1f", b.id, s.On, s.Mode, s.TargetTemperature)
|
||||
}
|
||||
case devices.Light:
|
||||
if s, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d Light state: on=%v brightness=%d", b.id, s.On, s.Brightness)
|
||||
}
|
||||
case devices.Switch:
|
||||
if s, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d Switch state: %v", b.id, s)
|
||||
}
|
||||
case devices.Fan:
|
||||
if s, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d Fan state: on=%v level=%d", b.id, s.On, s.FanLevel)
|
||||
}
|
||||
case devices.Humidifier:
|
||||
if s, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d Humidifier state: on=%v hum=%d", b.id, s.On, s.TargetHumidity)
|
||||
}
|
||||
case devices.Vacuum:
|
||||
if s, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d Vacuum state: on=%v mode=%d", b.id, s.On, s.Mode)
|
||||
}
|
||||
case devices.WaterHeater:
|
||||
if s, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d Heater: on=%v target=%.1f", b.id, s.On, s.TargetTemp)
|
||||
}
|
||||
case devices.Thermostat:
|
||||
if s, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d Thermostat: target=%.1f current=%.1f", b.id, s.TargetTemp, s.CurrentTemp)
|
||||
}
|
||||
case devices.Cover:
|
||||
if pos, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d Cover: pos=%d", b.id, pos)
|
||||
}
|
||||
case devices.TempHumiditySensor:
|
||||
if t, h, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d Sensor: temp=%.1f hum=%.1f", b.id, t, h)
|
||||
}
|
||||
case devices.OccupancySensor:
|
||||
if o, l, err := ctrl.FetchState(ctx); err == nil {
|
||||
b.lgr.Infof("[bridge] %d Occupancy: occ=%v lux=%.0f", b.id, o, l)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user