chore: 提交高层 xiaomi 封装库及示例
- 新增 xiaomi/examples/ 下 8 个示例程序(用户/家庭查询、设备列表、开关/灯光/空调控制、属性订阅、高级过滤分类、SPEC 解析) - miot_client_sub.go: 重构 SubProp/SubEvent,使用 buildPropTopic/buildEventTopic 支持通配符订阅(siid/piid=0 → +),并修复锁顺序问题(将 RequestRefreshProp 移到 Lock 外) - spec_parser.go: 新增 downloadSpecFile 方法,本地 SPEC 文件缺失时自动从 miot-spec.org 下载 - ARCH_PLAN.md: 架构设计从 Proposed 更新为 Accepted(v1.0→v1.1),补充设备分类/工厂/SPEC 映射等模块设计 - xiaomi/: 新增 miot 上层强类型封装模块,包含 Client 主入口、用户/家庭/设备 API、属性读写、动作调用、订阅通知,以及 devices/ 设备控制抽象(Switch/Light/AirConditioner/Fan/Cover/Humidifier/Vacuum/WaterHeater/Thermostat)和 specs/ SPEC 查询辅助 - 更多 xiaomi 示例(风扇/窗帘/传感器控制)
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
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 // 0=关, 100=全开
|
||||
GetPosition(ctx context.Context) (int, error)
|
||||
}
|
||||
|
||||
type coverDevice struct {
|
||||
BaseDevice
|
||||
propMotor siidPiid // "motor-control" — open/close/stop
|
||||
propPosition siidPiid // "current-position"
|
||||
}
|
||||
|
||||
const (
|
||||
coverMotorOpen = 1
|
||||
coverMotorClose = 0
|
||||
coverMotorStop = 2
|
||||
)
|
||||
|
||||
// NewCover creates a Cover control for the given device.
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user