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:
+4
-2
@@ -63,8 +63,9 @@ func (b *Bridge) RefreshDevices(ctx context.Context, homeIDs ...int64) error {
|
||||
}
|
||||
for _, info := range devs {
|
||||
if _, exists := b.dv[info.DID]; !exists {
|
||||
if d, createErr := devices.Create(b.client, info); createErr == nil {
|
||||
if d, createErr := devices.Create(b.client, info); createErr == nil && d != nil {
|
||||
b.dv[info.DID] = d
|
||||
b.subscribeDevice(info.DID, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -79,8 +80,9 @@ func (b *Bridge) RefreshDevices(ctx context.Context, homeIDs ...int64) error {
|
||||
}
|
||||
for _, info := range devList {
|
||||
if _, exists := b.dv[info.DID]; !exists {
|
||||
if d, createErr := devices.Create(b.client, info); createErr == nil {
|
||||
if d, createErr := devices.Create(b.client, info); createErr == nil && d != nil {
|
||||
b.dv[info.DID] = d
|
||||
b.subscribeDevice(info.DID, d)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+38
-20
@@ -9,7 +9,7 @@
|
||||
// b, _ := pool.Get(id)
|
||||
// ac, _ := b.AC("did")
|
||||
// ac.TurnOn(ctx)
|
||||
// state := ac.GetState()
|
||||
// state := ac.GetProps()
|
||||
package bridge
|
||||
|
||||
import (
|
||||
@@ -25,14 +25,22 @@ import (
|
||||
// ── 类型别名:业务层不需要 import devices ──
|
||||
|
||||
type (
|
||||
ACState = devices.ACState
|
||||
LightState = devices.LightState
|
||||
FanState = devices.FanState
|
||||
HumidifierState = devices.HumidifierState
|
||||
HeaterState = devices.HeaterState
|
||||
VacuumStatus = devices.VacuumStatus
|
||||
ACProps = devices.ACProps
|
||||
LightProps = devices.LightProps
|
||||
FanProps = devices.FanProps
|
||||
HumidifierProps = devices.HumidifierProps
|
||||
HeaterProps = devices.HeaterProps
|
||||
VacuumProps = devices.VacuumProps
|
||||
SwitchProps = devices.SwitchProps
|
||||
SpeakerProps = devices.SpeakerProps
|
||||
PlugProps = devices.PlugProps
|
||||
)
|
||||
|
||||
// OccupancyProps 人体传感器状态(有人/无人)。
|
||||
type OccupancyProps struct {
|
||||
Occupied bool `json:"occupied"`
|
||||
}
|
||||
|
||||
// ── Pool ──
|
||||
|
||||
// Pool 全局路由:id → Bridge。
|
||||
@@ -103,7 +111,8 @@ type Bridge struct {
|
||||
mu sync.RWMutex
|
||||
dv map[string]interface{} // DID → devices.Xxx interface
|
||||
|
||||
onStateChanged func(id int64, did, deviceType, changed string, state interface{})
|
||||
onPropsChanged func(id int64, did, deviceType, changed string, props interface{})
|
||||
onOnlineChanged func(id int64, did string, online bool)
|
||||
}
|
||||
|
||||
func newBridge(ctx context.Context, id int64, uuid string, auth xiaomi.AuthInfo, dataDir string, homeIDs []int64) (*Bridge, error) {
|
||||
@@ -162,18 +171,20 @@ func (b *Bridge) Devices() map[string]string {
|
||||
|
||||
func deviceTypeName(d interface{}) string {
|
||||
switch d.(type) {
|
||||
case devices.AirConditioner: return "air_conditioner"
|
||||
case devices.Light: return "light"
|
||||
case devices.Switch: return "switch"
|
||||
case devices.Fan: return "fan"
|
||||
case devices.Cover: return "cover"
|
||||
case devices.Humidifier: return "humidifier"
|
||||
case devices.Vacuum: return "vacuum"
|
||||
case devices.WaterHeater: return "water_heater"
|
||||
case devices.Thermostat: return "thermostat"
|
||||
case devices.TempHumiditySensor: return "temp_humidity_sensor"
|
||||
case devices.OccupancySensor: return "occupancy_sensor"
|
||||
case devices.Speaker: return "speaker"
|
||||
case devices.AirConditioner: return devices.TypeAirConditioner
|
||||
case devices.Light: return devices.TypeLight
|
||||
case devices.Switch: return devices.TypeSwitch
|
||||
case devices.Fan: return devices.TypeFan
|
||||
case devices.Cover: return devices.TypeCover
|
||||
case devices.Humidifier: return devices.TypeHumidifier
|
||||
case devices.Vacuum: return devices.TypeVacuum
|
||||
case devices.WaterHeater: return devices.TypeWaterHeater
|
||||
case devices.Thermostat: return devices.TypeThermostat
|
||||
case devices.TempHumiditySensor: return devices.TypeTempHumidSensor
|
||||
case devices.OccupancySensor: return devices.TypeOccupancySensor
|
||||
case devices.Speaker: return devices.TypeSpeaker
|
||||
case devices.Gateway: return devices.TypeGateway
|
||||
case devices.Plug: return devices.TypePlug
|
||||
default: return "unknown"
|
||||
}
|
||||
}
|
||||
@@ -257,6 +268,13 @@ func (b *Bridge) Occupancy(did string) (devices.OccupancySensor, error) {
|
||||
if !ok { return nil, fmt.Errorf("bridge: device %s is not an OccupancySensor", did) }
|
||||
return s, nil
|
||||
}
|
||||
func (b *Bridge) Plug(did string) (devices.Plug, error) {
|
||||
d, ok := b.dv[did]
|
||||
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
|
||||
pg, ok := d.(devices.Plug)
|
||||
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Plug", did) }
|
||||
return pg, nil
|
||||
}
|
||||
func (b *Bridge) Speaker(did string) (devices.Speaker, error) {
|
||||
d, ok := b.dv[did]
|
||||
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
|
||||
|
||||
+121
-79
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user