Files
xiaomihome/xiaomi/devices/base.go
T
4566704 04333750ab 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 等迁移文档同步更新
2026-08-05 17:54:08 +08:00

207 lines
5.8 KiB
Go

// Package devices provides strongly-typed device control abstractions for
// Xiaomi smart home devices. Each device type defines an interface that hides
// siid/piid details behind semantic methods like SetTargetTemp() and SetMode().
//
// The package uses SPEC property resolution to automatically discover the
// correct siid/piid mappings for each device model, enabling cross-model
// compatibility without hardcoded property IDs.
package devices
import (
"context"
"strings"
"xiaomihome/miot"
"xiaomihome/xiaomi"
)
// Device is the common interface shared by all device control types.
// All device kinds (Switch, Light, AirConditioner, etc.) embed this interface.
type Device interface {
// DID returns the device's unique identifier.
DID() string
// Name returns the device's display name.
Name() string
// Model returns the device's model identifier.
Model() string
// Online returns whether the device is currently online.
Online() bool
// Info returns the full device information.
Info() *xiaomi.DeviceInfo
}
// BaseDevice provides common functionality shared by all device types.
// Each device type embeds BaseDevice and extends it with type-specific methods.
// BaseDevice implements the Device interface.
type BaseDevice struct {
client *xiaomi.Client
info *xiaomi.DeviceInfo
miotDev *miot.MIoTDevice
resolver *specResolver // cached SPEC resolver (created on demand)
}
// NewBaseDevice creates a new BaseDevice instance.
func NewBaseDevice(client *xiaomi.Client, info *xiaomi.DeviceInfo) BaseDevice {
return BaseDevice{
client: client,
info: info,
}
}
// DID returns the device's unique identifier.
func (d *BaseDevice) DID() string {
return d.info.DID
}
// 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
}
// GetProp reads a single property value from the device.
func (d *BaseDevice) GetProp(ctx context.Context, siid, piid int) (*xiaomi.PropertyValue, error) {
return d.client.GetProp(ctx, d.info.DID, siid, piid)
}
// SetProp writes a single property value to the device.
func (d *BaseDevice) SetProp(ctx context.Context, siid, piid int, value interface{}) error {
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)
}
// SubProp subscribes to property change notifications.
func (d *BaseDevice) SubProp(siid, piid int, handler xiaomi.PropHandler) (string, error) {
return d.client.SubProp(d.info.DID, siid, piid, handler)
}
// SubEvent subscribes to event notifications.
func (d *BaseDevice) SubEvent(siid, eiid int, handler xiaomi.EventHandler) (string, error) {
return d.client.SubEvent(d.info.DID, siid, eiid, handler)
}
// SubDeviceState subscribes to online/offline state changes.
func (d *BaseDevice) SubDeviceState(handler xiaomi.DeviceStateHandler) error {
return d.client.SubDeviceState(d.info.DID, handler)
}
// getMIoTDevice returns the underlying MIoTDevice (lazy-loaded).
func (d *BaseDevice) getMIoTDevice() *miot.MIoTDevice {
if d.miotDev == nil {
d.miotDev = d.client.GetMIoTDevice(d.info.DID)
}
return d.miotDev
}
// getSpecResolver returns a SPEC property resolver (lazy-loaded).
func (d *BaseDevice) getSpecResolver() *specResolver {
if d.resolver == nil {
dev := d.getMIoTDevice()
if dev != nil {
d.resolver = newSpecResolver(dev.SpecInstance())
}
}
return d.resolver
}
// specResolver is a thin wrapper to avoid importing specs package in every device file.
type specResolver struct {
byType map[string]*miot.MIoTSpecProperty
}
func newSpecResolver(spec *miot.MIoTSpecInstance) *specResolver {
if spec == nil {
return nil
}
r := &specResolver{
byType: make(map[string]*miot.MIoTSpecProperty),
}
for _, svc := range spec.Services {
for i := range svc.Properties {
prop := svc.Properties[i]
name := propertyShortName(prop.Type)
if name != "" {
if _, exists := r.byType[name]; !exists {
r.byType[name] = prop
}
}
}
}
return r
}
// propertyShortName extracts the short name from a SPEC property URN.
// E.g., "urn:miot-spec-v2:property:on:00000006:..." → "on".
func propertyShortName(urn string) string {
parts := strings.Split(urn, ":")
if len(parts) >= 4 && parts[2] == "property" {
return parts[3]
}
return urn
}
func (r *specResolver) find(propType string) (siid, piid int, found bool) {
if r == nil {
return 0, 0, false
}
if prop, ok := r.byType[propType]; ok {
return prop.SIID, prop.PIID, true
}
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 {
return ""
}
if prop, ok := r.byType[propType]; ok {
return prop.Format
}
return ""
}