主要变更: **类型系统重构** - HomeInfo.ID/RoomInfo.ID/HomeID 从 string 改为 int64,json tag 使用 ,string - GetHome/GetRooms/GetRoomDevices 接口同步修改 - 新增 int64FromStr 工具函数,更新 parseHomeInfo 解析逻辑 **新增 Speaker 设备** - 新增 KindSpeaker 类型 - classify 映射覆盖 speaker/bluetooth-speaker/alarm-clock/media-player/playback/music **设备驱动重构** - AC 驱动彻底重写:从硬编码常量改为基于 SPEC ValueList 的动态 valMapper - 所有设备(Fan/Humidifier/Light/OccupancySensor/Switch/TempHumiditySensor/Thermostat/Vacuum/WaterHeater/Cover)新增 GetState() 和 FetchState(ctx) 接口 - base.go 新增 specResolver.format() 方法 **MIoT 模块增强** - MIoTClient 新增 homeIDs 字段,支持按家庭过滤设备 - RefreshDevices 支持 homeIDs 参数 - SpecStdLib 引入 logger.Logger,log.Printf 全部迁移为结构化日志 **其他** - Config 新增 RedirectURL 和 HomeIDs 字段 - AuthInfo.ExpiresIn 类型从 int 改为 int32 - UUID 生成简化为 hex.EncodeToString - 新增 github.com/sirupsen/logrus 依赖 - .gitignore 新增 examples/*/data/ 忽略规则 - 删除已提交的 config.json 敏感配置
118 lines
3.9 KiB
Go
118 lines
3.9 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", "outlet", "powerstrip", "toggle-switch", "wireless-switch", "curtain-switch"):
|
|
ctrl, err = NewSwitch(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", "motion-sensor", "magnet"):
|
|
ctrl, err = NewOccupancySensor(client, info)
|
|
case matchURN(urn, "sensor-temp", "sensor-humidity", "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)
|
|
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
|
|
}
|