feat: State→Props 命名统一 + 新增 Plug/Gateway 设备 + 分类系统重构

**命名统一(State → Props)**
- 所有设备类型:XxxState → XxxProps,GetState() → GetProps(),FetchState() → FetchProps()
- 回调接口:OnStateChanged → OnPropsChanged
- Bridge 层类型别名、示例代码、文档同步更名

**新增设备类型**
- KindPlug + Plug 驱动:智能插座/插头(TurnOn/TurnOff/IsOn/OnPropsChanged)
- KindGateway + Gateway 驱动:网关设备类型识别
- KindOccupancySensor 从 KindSensor 独立,添加 OccupancyProps

**设备分类系统改进**
- classifyByModel 改为按 key 长度降序匹配(防止 sensor 先于 sensor-occupy)
- 新增下划线变体识别(sensor_occupy、sensor_temp 等)
- outlet 重新归类为 Plug(非 Switch)
- 新增 Or() 组合过滤器

**连接桥增强**
- 设备创建成功后自动订阅属性变更
- 新增 nil 防护检查
- 支持 Plug/Speaker/Gateway/Occupancy 类型的 FetchProps 刷新
- 日志级别 Info → Debug

**AC 驱动增强**
- 新增 HasPower()、GetElectricPower()、GetPowerConsumption() 功率接口
- ACProps 新增 ElectricPower、PowerConsumption 字段
- valMapper:Desc 为空时 fallback 用 Name,key 统一小写

**MIoT 核心**
- 设备列表刷新改用 reflect.DeepEqual 全字段变更检测
- 移除 BLE/代理设备默认在线 hack
- RefreshDeviceAllProps 增加离线检查
- 新增 miot_errors.go:MIoT 错误码定义
- 全局日志级别 Info→Debug/Warn(降低噪音)

**示例与文档**
- 所有示例代码同步 API 变更
- 传感器示例改用 Or 组合过滤器
- ARCH_PLAN 等迁移文档同步更新
This commit is contained in:
2026-08-05 17:54:08 +08:00
parent b808e51b3e
commit 04333750ab
37 changed files with 916 additions and 536 deletions
+121 -79
View File
@@ -4,16 +4,24 @@ import (
"context"
"fmt"
"xiaomihome/xiaomi"
"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{})) {
// 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.onStateChanged = handler
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()
}
@@ -22,66 +30,96 @@ 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)
})
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(pos int) {
b.fire(did, devType, devices.PropPosition, pos)
})
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.Thermostat:
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) fire(did, devType, changed string, state interface{}) {
func (b *Bridge) fireOnline(did string, online bool) {
b.mu.RLock()
cb := b.onStateChanged
cb := b.onOnlineChanged
b.mu.RUnlock()
if cb != nil {
cb(b.id, did, devType, changed, state)
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)
}
}
@@ -99,48 +137,52 @@ func (b *Bridge) FetchAllStates(ctx context.Context) {
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)
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.FetchState(ctx); err == nil {
b.lgr.Infof("[bridge] %d Light state: on=%v brightness=%d", b.id, s.On, s.Brightness)
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.FetchState(ctx); err == nil {
b.lgr.Infof("[bridge] %d Switch state: %v", b.id, s)
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.FetchState(ctx); err == nil {
b.lgr.Infof("[bridge] %d Fan state: on=%v level=%d", b.id, s.On, s.FanLevel)
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.FetchState(ctx); err == nil {
b.lgr.Infof("[bridge] %d Humidifier state: on=%v hum=%d", b.id, s.On, s.TargetHumidity)
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.FetchState(ctx); err == nil {
b.lgr.Infof("[bridge] %d Vacuum state: on=%v mode=%d", b.id, s.On, s.Mode)
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.FetchState(ctx); err == nil {
b.lgr.Infof("[bridge] %d Heater: on=%v target=%.1f", b.id, s.On, s.TargetTemp)
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.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)
if s, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[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)
if pos, err := ctrl.FetchProps(ctx); err == nil {
b.lgr.Debugf("[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)
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.FetchState(ctx); err == nil {
b.lgr.Infof("[bridge] %d Occupancy: occ=%v lux=%.0f", b.id, o, l)
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)
}
}
}