Files
xiaomihome/bridge/subscribe.go
T
4566704 86c21ce91c feat: AC 三合一设备支持 + 新增 Plug/Gateway 驱动 + Cover 结构化重构
**AC 驱动增强**
- 支持 thermostat + air-conditioner + air-fresh 多服务三合一设备,属性跨服务合并解析
- 新增 specResolver.findValListInService/findInService/hasService/serviceShortName 方法
- 移除 KindThermostat,heater/electric-blanket/thermostat 重新归类为 KindAirConditioner

**Cover 窗帘重构**
- CoverProps 结构化:Position/Status/Fault 分离
- SetPosition 优先写 target-position,回退 current-position
- OnPropsChanged/FetchProps 支持多属性同步刷新

**新增设备类型**
- Plug 驱动:插座/插头,支持开合、在线状态、属性变更订阅
- Gateway 驱动:网关设备类型识别
- miot/miot_errors.go:新增 MIoT 云端错误码定义

**MIoT 客户端健壮性**
- SetProp 空结果视为失败;设备被移除或离线时自动刷新设备列表
- GetDevices 的 has_more 改用 parseBool 严格解析
- MIPS SubscribeMany 批量订阅减少往返
- matchURN 改为按段精确匹配,避免 tofan 误匹配 fan

**Bridge**
- 移除 Thermostat case,Cover 改用结构化 CoverProps
- 新增设备创建后自动订阅属性变更

**其他**
- 日志整体 Info → Debug 降噪
- .gitignore 忽略 /.codebuddy/memory
- 更新 devices_test 与 08_adv_filter_classify 示例
2026-08-23 19:42:55 +08:00

182 lines
5.6 KiB
Go

package bridge
import (
"context"
"fmt"
"xiaomihome/xiaomi"
"xiaomihome/xiaomi/devices"
)
// ── 回调 ──
// OnPropsChanged 注册全局属性变化回调。props 是 devices 包的类型化 Props 结构体,
// 调用方自行 switch 类型(ACProps / LightProps / FanProps ...)。
func (b *Bridge) OnPropsChanged(handler func(id int64, did, deviceType, changed string, props interface{})) {
b.mu.Lock()
b.onPropsChanged = handler
b.mu.Unlock()
}
// OnOnlineChanged 注册设备在线/离线变化回调。
func (b *Bridge) OnOnlineChanged(handler func(id int64, did string, online bool)) {
b.mu.Lock()
b.onOnlineChanged = 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 {
b.subscribeDevice(did, d)
}
}
// subscribeDevice 为单个设备注册 MQTT 属性和在线状态订阅。
func (b *Bridge) subscribeDevice(did string, d interface{}) {
devType := deviceTypeName(d)
switch ctrl := d.(type) {
case devices.AirConditioner:
ctrl.OnPropsChanged(func(props devices.ACProps, ch string) {
b.fire(did, devType, ch, props)
})
case devices.Light:
ctrl.OnPropsChanged(func(props devices.LightProps, ch string) {
b.fire(did, devType, ch, props)
})
case devices.Switch:
ctrl.OnPropsChanged(func(ch int, on bool) {
b.fire(did, devType, fmt.Sprintf("ch%d", ch), ctrl.GetProps())
})
case devices.Fan:
ctrl.OnPropsChanged(func(props devices.FanProps, ch string) {
b.fire(did, devType, ch, props)
})
case devices.Cover:
ctrl.OnPropsChanged(func(props devices.CoverProps) {
b.fire(did, devType, devices.PropPosition, props)
})
case devices.Humidifier:
ctrl.OnPropsChanged(func(props devices.HumidifierProps, ch string) {
b.fire(did, devType, ch, props)
})
case devices.Vacuum:
ctrl.OnPropsChanged(func(props devices.VacuumProps, ch string) {
b.fire(did, devType, ch, props)
})
case devices.WaterHeater:
ctrl.OnPropsChanged(func(props devices.HeaterProps, ch string) {
b.fire(did, devType, ch, props)
})
case devices.TempHumiditySensor:
ctrl.OnPropsChanged(func(temp, hum float64) {
b.fire(did, devType, "temperature_humidity", map[string]float64{"temperature": temp, "humidity": hum})
})
case devices.OccupancySensor:
ctrl.OnPropsChanged(func(occ bool, lux float64) {
b.fire(did, devType, "occupancy", map[string]interface{}{"occupied": occ, "illuminance": lux})
})
case devices.Speaker:
ctrl.OnPropsChanged(func(props devices.SpeakerProps, ch string) {
b.fire(did, devType, ch, props)
})
case devices.Plug:
ctrl.OnPropsChanged(func(on bool) {
b.fire(did, devType, devices.PropOn, on)
})
}
// 订阅设备在线/离线
if bd, ok := d.(interface{ SubDeviceState(xiaomi.DeviceStateHandler) error }); ok {
if err := bd.SubDeviceState(func(did string, state xiaomi.DeviceState) {
online := state == xiaomi.StateOnline
b.fireOnline(did, online)
if online {
go b.client.RefreshDeviceAllProps(did)
}
}); err != nil {
b.lgr.Warnf("[bridge] %d SubDeviceState %s: %v", b.id, did, err)
}
}
}
func (b *Bridge) fireOnline(did string, online bool) {
b.mu.RLock()
cb := b.onOnlineChanged
b.mu.RUnlock()
if cb != nil {
cb(b.id, did, online)
}
}
func (b *Bridge) fire(did, devType, changed string, props interface{}) {
b.mu.RLock()
cb := b.onPropsChanged
b.mu.RUnlock()
if cb != nil {
cb(b.id, did, devType, changed, props)
}
}
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.FetchProps(ctx); err == nil {
b.lgr.Debugf("[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.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Light state: on=%v brightness=%d", b.id, s.On, s.Brightness)
}
case devices.Switch:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Switch state: %v", b.id, s)
}
case devices.Fan:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Fan state: on=%v level=%d", b.id, s.On, s.FanLevel)
}
case devices.Humidifier:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Humidifier state: on=%v hum=%d", b.id, s.On, s.TargetHumidity)
}
case devices.Vacuum:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Vacuum state: on=%v mode=%d", b.id, s.On, s.Mode)
}
case devices.WaterHeater:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Heater: on=%v target=%.1f", b.id, s.On, s.TargetTemp)
}
case devices.Cover:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Cover: pos=%d status=%d fault=%d", b.id, s.Position, s.Status, s.Fault)
}
case devices.TempHumiditySensor:
if t, h, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Sensor: temp=%.1f hum=%.1f", b.id, t, h)
}
case devices.OccupancySensor:
if o, l, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Occupancy: occ=%v lux=%.0f", b.id, o, l)
}
case devices.Plug:
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[bridge] %d Plug state: on=%v", b.id, s.On)
}
}
}
}