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:
+27
-29
@@ -23,16 +23,22 @@ type Switch interface {
|
||||
ToggleChannel(ctx context.Context, ch int) error
|
||||
IsChannelOn(ctx context.Context, ch int) (bool, error)
|
||||
|
||||
OnStateChanged(handler func(ch int, on bool)) (string, error)
|
||||
GetState() []bool
|
||||
FetchState(ctx context.Context) ([]bool, error)
|
||||
OnPropsChanged(handler func(ch int, on bool)) (string, error)
|
||||
GetProps() SwitchProps
|
||||
FetchProps(ctx context.Context) (SwitchProps, error)
|
||||
}
|
||||
|
||||
// SwitchProps 多键开关完整状态。
|
||||
// JSON: {"on":[true,false,true]},索引 0 = 通道 1
|
||||
type SwitchProps struct {
|
||||
On []bool `json:"on"`
|
||||
}
|
||||
|
||||
// switchDevice is the concrete Switch implementation.
|
||||
type switchDevice struct {
|
||||
BaseDevice
|
||||
channels []siidPiid // "on" property per channel (index 0 = channel 1)
|
||||
stateCache []bool // MQTT 推送维护的本地缓存,索引 0 = 通道 1
|
||||
propsCache SwitchProps
|
||||
}
|
||||
|
||||
type siidPiid struct {
|
||||
@@ -46,7 +52,6 @@ func NewSwitch(client *xiaomi.Client, info *xiaomi.DeviceInfo) (Switch, error) {
|
||||
BaseDevice: NewBaseDevice(client, info),
|
||||
}
|
||||
|
||||
// Use the full SPEC to find all "on" properties (supports multi-channel)
|
||||
miotDev := d.getMIoTDevice()
|
||||
if miotDev == nil || miotDev.SpecInstance() == nil {
|
||||
return nil, xiaomi.ErrNoSPEC
|
||||
@@ -66,7 +71,7 @@ func NewSwitch(client *xiaomi.Client, info *xiaomi.DeviceInfo) (Switch, error) {
|
||||
return nil, xiaomi.ErrNotSupported
|
||||
}
|
||||
d.channels = channels
|
||||
d.stateCache = make([]bool, len(channels))
|
||||
d.propsCache.On = make([]bool, len(channels))
|
||||
|
||||
return d, nil
|
||||
}
|
||||
@@ -75,18 +80,9 @@ func (d *switchDevice) ChannelCount() int {
|
||||
return len(d.channels)
|
||||
}
|
||||
|
||||
func (d *switchDevice) TurnOn(ctx context.Context) error {
|
||||
return d.TurnOnChannel(ctx, 1)
|
||||
}
|
||||
|
||||
func (d *switchDevice) TurnOff(ctx context.Context) error {
|
||||
return d.TurnOffChannel(ctx, 1)
|
||||
}
|
||||
|
||||
func (d *switchDevice) Toggle(ctx context.Context) error {
|
||||
return d.ToggleChannel(ctx, 1)
|
||||
}
|
||||
|
||||
func (d *switchDevice) TurnOn(ctx context.Context) error { return d.TurnOnChannel(ctx, 1) }
|
||||
func (d *switchDevice) TurnOff(ctx context.Context) error { return d.TurnOffChannel(ctx, 1) }
|
||||
func (d *switchDevice) Toggle(ctx context.Context) error { return d.ToggleChannel(ctx, 1) }
|
||||
func (d *switchDevice) IsOn(ctx context.Context) (bool, error) {
|
||||
return d.IsChannelOn(ctx, 1)
|
||||
}
|
||||
@@ -133,13 +129,12 @@ func (d *switchDevice) IsChannelOn(ctx context.Context, ch int) (bool, error) {
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (d *switchDevice) OnStateChanged(handler func(ch int, on bool)) (string, error) {
|
||||
func (d *switchDevice) OnPropsChanged(handler func(ch int, on bool)) (string, error) {
|
||||
if len(d.channels) == 0 {
|
||||
return "", xiaomi.ErrNotSupported
|
||||
}
|
||||
// 构建 siid/piid → channel index 的快速查找表
|
||||
type chField struct{ siid, piid int }
|
||||
lookup := make(map[chField]int) // key → channel index (0-based)
|
||||
lookup := make(map[chField]int)
|
||||
for i, c := range d.channels {
|
||||
lookup[chField{c.siid, c.piid}] = i
|
||||
}
|
||||
@@ -147,8 +142,8 @@ func (d *switchDevice) OnStateChanged(handler func(ch int, on bool)) (string, er
|
||||
for k, idx := range lookup {
|
||||
if k.siid == prop.SIID && k.piid == prop.PIID {
|
||||
if b, ok := prop.Value.(bool); ok {
|
||||
d.stateCache[idx] = b
|
||||
handler(idx+1, b) // ch = idx+1
|
||||
d.propsCache.On[idx] = b
|
||||
handler(idx+1, b)
|
||||
}
|
||||
return
|
||||
}
|
||||
@@ -157,21 +152,24 @@ func (d *switchDevice) OnStateChanged(handler func(ch int, on bool)) (string, er
|
||||
}
|
||||
|
||||
func (d *switchDevice) channel(ch int) (siidPiid, bool) {
|
||||
i := ch - 1 // channel 1 → index 0
|
||||
i := ch - 1
|
||||
if i < 0 || i >= len(d.channels) {
|
||||
return siidPiid{}, false
|
||||
}
|
||||
return d.channels[i], true
|
||||
}
|
||||
|
||||
func (d *switchDevice) GetState() []bool { return d.stateCache }
|
||||
func (d *switchDevice) FetchState(ctx context.Context) ([]bool, error) {
|
||||
func (d *switchDevice) GetProps() SwitchProps { return d.propsCache }
|
||||
|
||||
func (d *switchDevice) FetchProps(ctx context.Context) (SwitchProps, error) {
|
||||
keys := make([]xiaomi.PropKey, len(d.channels))
|
||||
for i, c := range d.channels {
|
||||
keys[i] = xiaomi.PropKey{DID: d.info.DID, SIID: c.siid, PIID: c.piid}
|
||||
}
|
||||
props, err := d.client.GetProps(ctx, keys)
|
||||
if err != nil { return d.stateCache, err }
|
||||
if err != nil {
|
||||
return d.propsCache, err
|
||||
}
|
||||
states := make([]bool, len(d.channels))
|
||||
for i, c := range d.channels {
|
||||
for _, p := range props {
|
||||
@@ -181,6 +179,6 @@ func (d *switchDevice) FetchState(ctx context.Context) ([]bool, error) {
|
||||
}
|
||||
}
|
||||
}
|
||||
d.stateCache = states
|
||||
return states, nil
|
||||
d.propsCache.On = states
|
||||
return d.propsCache, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user