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
+26
View File
@@ -55,19 +55,31 @@ func (d *BaseDevice) DID() string {
// Name returns the device's display name.
func (d *BaseDevice) Name() string {
d.refreshInfo()
return d.info.Name
}
// Model returns the device's model identifier.
func (d *BaseDevice) Model() string {
d.refreshInfo()
return d.info.Model
}
// Online returns whether the device is currently online.
func (d *BaseDevice) Online() bool {
d.refreshInfo()
return d.info.Online
}
// refreshInfo 从客户端缓存拉取最新设备信息,有变化时原地更新。
func (d *BaseDevice) refreshInfo() {
info, err := d.client.GetDevice(context.Background(), d.info.DID)
if err != nil || info == nil {
return
}
d.info = info
}
// Info returns the full device information.
func (d *BaseDevice) Info() *xiaomi.DeviceInfo {
return d.info
@@ -83,6 +95,12 @@ func (d *BaseDevice) SetProp(ctx context.Context, siid, piid int, value interfac
return d.client.SetProp(ctx, d.info.DID, siid, piid, value)
}
// Ping 通过实际读属性验证设备是否在线,不走缓存。
func (d *BaseDevice) Ping(ctx context.Context, siid, piid int) bool {
_, err := d.GetProp(ctx, siid, piid)
return err == nil
}
// Action invokes a device action.
func (d *BaseDevice) Action(ctx context.Context, siid, aiid int, params []interface{}) (*xiaomi.ActionResult, error) {
return d.client.Action(ctx, d.info.DID, siid, aiid, params)
@@ -168,6 +186,14 @@ func (r *specResolver) find(propType string) (siid, piid int, found bool) {
return 0, 0, false
}
// prop returns the full SPEC property for the given type name.
func (r *specResolver) prop(propType string) *miot.MIoTSpecProperty {
if r == nil {
return nil
}
return r.byType[propType]
}
// format returns the SPEC Format for a property type ("bool", "uint8", "float", "string").
func (r *specResolver) format(propType string) string {
if r == nil {