**命名统一(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 等迁移文档同步更新
122 lines
4.1 KiB
Go
122 lines
4.1 KiB
Go
package devices
|
|
|
|
import (
|
|
"xiaomihome/xiaomi"
|
|
)
|
|
|
|
// Create creates the appropriate device control interface for the given device.
|
|
// It uses the device's URN (SPEC type) to determine which control interface to return.
|
|
//
|
|
// Returns:
|
|
// - Switch, Light, AirConditioner, Fan, Cover, Humidifier, Vacuum,
|
|
// WaterHeater, or Thermostat depending on the device type
|
|
// - nil, nil if the device type is not recognized (no error, just unsupported)
|
|
// - an error if there was a problem creating the control
|
|
//
|
|
// Usage:
|
|
//
|
|
// info, _ := client.GetDevice(ctx, "some-did")
|
|
// ctrl, err := devices.Create(client, info)
|
|
// switch d := ctrl.(type) {
|
|
// case devices.Switch:
|
|
// d.TurnOn(ctx)
|
|
// case devices.Light:
|
|
// d.SetBrightness(ctx, 80)
|
|
// }
|
|
func Create(client *xiaomi.Client, info *xiaomi.DeviceInfo) (interface{}, error) {
|
|
return createImpl(client, info)
|
|
}
|
|
|
|
// NewDevice creates the appropriate device control interface, returning the
|
|
// common Device interface. Use type switch to access type-specific methods:
|
|
//
|
|
// dev, err := devices.NewDevice(client, info)
|
|
// switch d := dev.(type) {
|
|
// case devices.Switch:
|
|
// d.TurnOn(ctx)
|
|
// case devices.Light:
|
|
// d.SetBrightness(ctx, 80)
|
|
// }
|
|
func NewDevice(client *xiaomi.Client, info *xiaomi.DeviceInfo) (Device, error) {
|
|
ctrl, err := createImpl(client, info)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if ctrl == nil {
|
|
return nil, nil
|
|
}
|
|
return ctrl.(Device), nil
|
|
}
|
|
|
|
// createImpl is the internal implementation shared by Create and NewDevice.
|
|
func createImpl(client *xiaomi.Client, info *xiaomi.DeviceInfo) (interface{}, error) {
|
|
if info == nil {
|
|
return nil, xiaomi.ErrDeviceNotFound
|
|
}
|
|
|
|
urn := info.URN
|
|
|
|
// Determine device type from URN and try constructors in order
|
|
var ctrl interface{}
|
|
var err error
|
|
|
|
// URN-based routing
|
|
// miot-spec-v2: air-conditioner → urn:miot-spec-v2:device:air-conditioner:...
|
|
// miot-spec-v2: light → urn:miot-spec-v2:device:light:...
|
|
// etc.
|
|
switch {
|
|
case matchURN(urn, "air-conditioner", "airer", "air-condition-outlet", "air-condition-eco", "ac-partner"):
|
|
ctrl, err = NewAirConditioner(client, info)
|
|
case matchURN(urn, "light", "lightbulb", "night-light", "ceiling-light"):
|
|
ctrl, err = NewLight(client, info)
|
|
case matchURN(urn, "switch", "powerstrip", "toggle-switch", "wireless-switch", "curtain-switch"):
|
|
ctrl, err = NewSwitch(client, info)
|
|
case matchURN(urn, "plug", "outlet"):
|
|
ctrl, err = NewPlug(client, info)
|
|
case matchURN(urn, "fan", "ceiling-fan", "ventilator", "air-fresh", "air-purifier"):
|
|
ctrl, err = NewFan(client, info)
|
|
case matchURN(urn, "curtain", "airer-rack", "airer"):
|
|
ctrl, err = NewCover(client, info)
|
|
case matchURN(urn, "humidifier", "dehumidifier", "humidifier-dehumidifier"):
|
|
ctrl, err = NewHumidifier(client, info)
|
|
case matchURN(urn, "vacuum", "sweeper", "robot-cleaner"):
|
|
ctrl, err = NewVacuum(client, info)
|
|
case matchURN(urn, "water-heater", "water-cooler", "kettle"):
|
|
ctrl, err = NewWaterHeater(client, info)
|
|
case matchURN(urn, "heater", "electric-blanket", "thermostat"):
|
|
ctrl, err = NewThermostat(client, info)
|
|
case matchURN(urn, "sensor-occupy", "sensor_occupy", "occupancy-sensor", "occupancy_sensor", "motion-sensor", "motion_sensor", "magnet"):
|
|
ctrl, err = NewOccupancySensor(client, info)
|
|
case matchURN(urn, "sensor-temp", "sensor_temp", "sensor-humidity", "sensor_humidity", "sensor-ht", "sensor_ht", "sensor-smoke", "sensor-gasm", "illumination"):
|
|
ctrl, err = NewTempHumiditySensor(client, info)
|
|
case matchURN(urn, "speaker", "bluetooth-speaker", "alarm-clock", "media-player", "playback", "music"):
|
|
ctrl, err = NewSpeaker(client, info)
|
|
case matchURN(urn, "gateway"):
|
|
ctrl, err = NewGateway(client, info)
|
|
default:
|
|
// Not a supported device type — not an error, just returns nil
|
|
return nil, nil
|
|
}
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return ctrl, nil
|
|
}
|
|
|
|
// matchURN checks if the device URN contains any of the given type strings.
|
|
func matchURN(urn string, types ...string) bool {
|
|
for _, t := range types {
|
|
if len(urn) >= len(t) {
|
|
// Simple substring match — URN format: urn:miot-spec-v2:device:TYPE:...
|
|
for i := 0; i <= len(urn)-len(t); i++ {
|
|
if urn[i:i+len(t)] == t {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return false
|
|
}
|