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
+30 -30
View File
@@ -49,13 +49,13 @@ type Speaker interface {
ExecuteTextDirective(ctx context.Context, text string, silent bool) error
PlayMusic(ctx context.Context) error
OnStateChanged(handler func(state SpeakerState, changed string)) (string, error)
GetState() SpeakerState
FetchState(ctx context.Context) (SpeakerState, error)
OnPropsChanged(handler func(props SpeakerProps, changed string)) (string, error)
GetProps() SpeakerProps
FetchProps(ctx context.Context) (SpeakerProps, error)
}
// SpeakerState 音箱完整状态(字段名对齐 SPEC type)
type SpeakerState struct {
// SpeakerProps 音箱完整状态(字段名对齐 SPEC type)
type SpeakerProps struct {
On bool `json:"on"` // SPEC: "on"
Volume int `json:"volume"` // SPEC: "volume"
Mute bool `json:"mute"` // SPEC: "mute"
@@ -86,7 +86,7 @@ type speakerDevice struct {
actExecuteTextDirective isAction
actPlayMusic isAction
stateCache SpeakerState
propsCache SpeakerProps
}
// NewSpeaker creates a Speaker control for the given device.
@@ -183,7 +183,7 @@ func (d *speakerDevice) TurnOff(ctx context.Context) error {
}
func (d *speakerDevice) Toggle(ctx context.Context) error {
s := d.GetState()
s := d.GetProps()
if s.On {
return d.TurnOff(ctx)
}
@@ -339,24 +339,24 @@ func (d *speakerDevice) PlayMusic(ctx context.Context) error {
// ── 订阅 / 状态 ──
func (d *speakerDevice) OnStateChanged(handler func(state SpeakerState, changed string)) (string, error) {
func (d *speakerDevice) OnPropsChanged(handler func(props SpeakerProps, changed string)) (string, error) {
type field struct {
siid, piid int
name string
fill func(*SpeakerState, interface{})
fill func(*SpeakerProps, interface{})
}
var fields []field
add := func(sp siidPiid, name string, fill func(*SpeakerState, interface{})) {
add := func(sp siidPiid, name string, fill func(*SpeakerProps, interface{})) {
if sp.valid {
fields = append(fields, field{sp.siid, sp.piid, name, fill})
}
}
add(d.propOn, "on", func(s *SpeakerState, v interface{}) { s.On, _ = v.(bool) })
add(d.propVolume, "volume", func(s *SpeakerState, v interface{}) { s.Volume = toInt(v) })
add(d.propMute, "mute", func(s *SpeakerState, v interface{}) { s.Mute, _ = v.(bool) })
add(d.propPlayState, "play-state", func(s *SpeakerState, v interface{}) { s.PlayState = toInt(v) })
add(d.propSleepMode, "sleep-mode", func(s *SpeakerState, v interface{}) { s.SleepMode, _ = v.(bool) })
add(d.propAudioID, "audio-id", func(s *SpeakerState, v interface{}) {
add(d.propOn, "on", func(s *SpeakerProps, v interface{}) { s.On, _ = v.(bool) })
add(d.propVolume, "volume", func(s *SpeakerProps, v interface{}) { s.Volume = toInt(v) })
add(d.propMute, "mute", func(s *SpeakerProps, v interface{}) { s.Mute, _ = v.(bool) })
add(d.propPlayState, "play-state", func(s *SpeakerProps, v interface{}) { s.PlayState = toInt(v) })
add(d.propSleepMode, "sleep-mode", func(s *SpeakerProps, v interface{}) { s.SleepMode, _ = v.(bool) })
add(d.propAudioID, "audio-id", func(s *SpeakerProps, v interface{}) {
if str, ok := v.(string); ok {
s.AudioID = str
}
@@ -368,17 +368,17 @@ func (d *speakerDevice) OnStateChanged(handler func(state SpeakerState, changed
return d.SubProp(0, 0, func(did string, prop *xiaomi.PropertyValue) {
for _, f := range fields {
if f.siid == prop.SIID && f.piid == prop.PIID {
f.fill(&d.stateCache, prop.Value)
handler(d.stateCache, f.name)
f.fill(&d.propsCache, prop.Value)
handler(d.propsCache, f.name)
return
}
}
})
}
func (d *speakerDevice) GetState() SpeakerState { return d.stateCache }
func (d *speakerDevice) GetProps() SpeakerProps { return d.propsCache }
func (d *speakerDevice) FetchState(ctx context.Context) (SpeakerState, error) {
func (d *speakerDevice) FetchProps(ctx context.Context) (SpeakerProps, error) {
keys := []xiaomi.PropKey{}
add := func(sp siidPiid) {
if sp.valid {
@@ -394,29 +394,29 @@ func (d *speakerDevice) FetchState(ctx context.Context) (SpeakerState, error) {
props, err := d.client.GetProps(ctx, keys)
if err != nil {
return d.stateCache, err
return d.propsCache, err
}
state := SpeakerState{}
result := SpeakerProps{}
for _, p := range props {
switch {
case match(d.propOn, p):
state.On, _ = p.Value.(bool)
result.On, _ = p.Value.(bool)
case match(d.propVolume, p):
state.Volume = toInt(p.Value)
result.Volume = toInt(p.Value)
case match(d.propMute, p):
state.Mute, _ = p.Value.(bool)
result.Mute, _ = p.Value.(bool)
case match(d.propPlayState, p):
state.PlayState = toInt(p.Value)
result.PlayState = toInt(p.Value)
case match(d.propSleepMode, p):
state.SleepMode, _ = p.Value.(bool)
result.SleepMode, _ = p.Value.(bool)
case match(d.propAudioID, p):
if s, ok := p.Value.(string); ok {
state.AudioID = s
result.AudioID = s
}
}
}
d.stateCache = state
return state, nil
d.propsCache = result
return result, nil
}
func match(sp siidPiid, p *xiaomi.PropertyValue) bool {