主要变更: **类型系统重构** - 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 敏感配置
94 lines
2.7 KiB
Go
94 lines
2.7 KiB
Go
package devices
|
|
|
|
import (
|
|
"context"
|
|
"xiaomihome/xiaomi"
|
|
)
|
|
|
|
// Cover 窗帘/晾衣架统一控制接口
|
|
type Cover interface {
|
|
Device
|
|
Open(ctx context.Context) error
|
|
Close(ctx context.Context) error
|
|
Stop(ctx context.Context) error
|
|
|
|
SetPosition(ctx context.Context, pct int) error
|
|
GetPosition(ctx context.Context) (int, error)
|
|
|
|
OnStateChanged(handler func(position int)) (string, error)
|
|
GetState() int
|
|
FetchState(ctx context.Context) (int, error)
|
|
}
|
|
|
|
type coverDevice struct {
|
|
BaseDevice
|
|
propMotor siidPiid
|
|
propPosition siidPiid
|
|
|
|
stateCache int
|
|
}
|
|
|
|
const (
|
|
coverMotorOpen = 1
|
|
coverMotorClose = 0
|
|
coverMotorStop = 2
|
|
)
|
|
|
|
func NewCover(client *xiaomi.Client, info *xiaomi.DeviceInfo) (Cover, error) {
|
|
d := &coverDevice{BaseDevice: NewBaseDevice(client, info)}
|
|
resolver := d.getSpecResolver()
|
|
if resolver == nil {
|
|
return nil, xiaomi.ErrNoSPEC
|
|
}
|
|
siid, piid, ok := resolver.find("motor-control")
|
|
if !ok {
|
|
return nil, xiaomi.ErrNotSupported
|
|
}
|
|
d.propMotor = siidPiid{siid, piid, true}
|
|
if s, p, ok := resolver.find("current-position"); ok {
|
|
d.propPosition = siidPiid{s, p, true}
|
|
}
|
|
return d, nil
|
|
}
|
|
|
|
func (d *coverDevice) Open(ctx context.Context) error {
|
|
if !d.propMotor.valid { return xiaomi.ErrNotSupported }
|
|
return d.SetProp(ctx, d.propMotor.siid, d.propMotor.piid, coverMotorOpen)
|
|
}
|
|
func (d *coverDevice) Close(ctx context.Context) error {
|
|
if !d.propMotor.valid { return xiaomi.ErrNotSupported }
|
|
return d.SetProp(ctx, d.propMotor.siid, d.propMotor.piid, coverMotorClose)
|
|
}
|
|
func (d *coverDevice) Stop(ctx context.Context) error {
|
|
if !d.propMotor.valid { return xiaomi.ErrNotSupported }
|
|
return d.SetProp(ctx, d.propMotor.siid, d.propMotor.piid, coverMotorStop)
|
|
}
|
|
func (d *coverDevice) SetPosition(ctx context.Context, pct int) error {
|
|
if !d.propPosition.valid { return xiaomi.ErrNotSupported }
|
|
return d.SetProp(ctx, d.propPosition.siid, d.propPosition.piid, pct)
|
|
}
|
|
func (d *coverDevice) GetPosition(ctx context.Context) (int, error) {
|
|
if !d.propPosition.valid { return 0, xiaomi.ErrNotSupported }
|
|
prop, err := d.GetProp(ctx, d.propPosition.siid, d.propPosition.piid)
|
|
if err != nil { return 0, err }
|
|
return toInt(prop.Value), nil
|
|
}
|
|
|
|
func (d *coverDevice) OnStateChanged(handler func(position int)) (string, error) {
|
|
if !d.propPosition.valid { return "", xiaomi.ErrNotSupported }
|
|
return d.SubProp(0, 0, func(did string, prop *xiaomi.PropertyValue) {
|
|
if prop.SIID == d.propPosition.siid && prop.PIID == d.propPosition.piid {
|
|
d.stateCache = toInt(prop.Value)
|
|
handler(d.stateCache)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (d *coverDevice) GetState() int { return d.stateCache }
|
|
func (d *coverDevice) FetchState(ctx context.Context) (int, error) {
|
|
pos, err := d.GetPosition(ctx)
|
|
if err != nil { return d.stateCache, err }
|
|
d.stateCache = pos
|
|
return pos, nil
|
|
}
|