- 新增 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 示例(风扇/窗帘/传感器控制)
119 lines
3.0 KiB
Go
119 lines
3.0 KiB
Go
package devices
|
|
|
|
import (
|
|
"context"
|
|
"xiaomihome/xiaomi"
|
|
)
|
|
|
|
// Humidifier 加湿器/除湿机统一控制接口
|
|
type Humidifier interface {
|
|
Device
|
|
TurnOn(ctx context.Context) error
|
|
TurnOff(ctx context.Context) error
|
|
IsOn(ctx context.Context) (bool, error)
|
|
|
|
SetTargetHumidity(ctx context.Context, pct int) error
|
|
GetTargetHumidity(ctx context.Context) (int, error)
|
|
|
|
SetFanLevel(ctx context.Context, level int) error
|
|
GetFanLevel(ctx context.Context) (int, error)
|
|
}
|
|
|
|
type humidifierDevice struct {
|
|
BaseDevice
|
|
propOn siidPiid // "on"
|
|
propTargetHumidity siidPiid // "target-humidity"
|
|
propFanLevel siidPiid // "fan-level"
|
|
}
|
|
|
|
// NewHumidifier creates a Humidifier control for the given device.
|
|
func NewHumidifier(client *xiaomi.Client, info *xiaomi.DeviceInfo) (Humidifier, error) {
|
|
d := &humidifierDevice{
|
|
BaseDevice: NewBaseDevice(client, info),
|
|
}
|
|
|
|
resolver := d.getSpecResolver()
|
|
if resolver == nil {
|
|
return nil, xiaomi.ErrNoSPEC
|
|
}
|
|
|
|
siid, piid, ok := resolver.find("on")
|
|
if !ok {
|
|
return nil, xiaomi.ErrNotSupported
|
|
}
|
|
d.propOn = siidPiid{siid, piid, true}
|
|
|
|
if s, p, ok := resolver.find("target-humidity"); ok {
|
|
d.propTargetHumidity = siidPiid{s, p, true}
|
|
}
|
|
if s, p, ok := resolver.find("fan-level"); ok {
|
|
d.propFanLevel = siidPiid{s, p, true}
|
|
}
|
|
|
|
return d, nil
|
|
}
|
|
|
|
func (d *humidifierDevice) TurnOn(ctx context.Context) error {
|
|
if !d.propOn.valid {
|
|
return xiaomi.ErrNotSupported
|
|
}
|
|
return d.SetProp(ctx, d.propOn.siid, d.propOn.piid, true)
|
|
}
|
|
|
|
func (d *humidifierDevice) TurnOff(ctx context.Context) error {
|
|
if !d.propOn.valid {
|
|
return xiaomi.ErrNotSupported
|
|
}
|
|
return d.SetProp(ctx, d.propOn.siid, d.propOn.piid, false)
|
|
}
|
|
|
|
func (d *humidifierDevice) IsOn(ctx context.Context) (bool, error) {
|
|
if !d.propOn.valid {
|
|
return false, xiaomi.ErrNotSupported
|
|
}
|
|
prop, err := d.GetProp(ctx, d.propOn.siid, d.propOn.piid)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if b, ok := prop.Value.(bool); ok {
|
|
return b, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func (d *humidifierDevice) SetTargetHumidity(ctx context.Context, pct int) error {
|
|
if !d.propTargetHumidity.valid {
|
|
return xiaomi.ErrNotSupported
|
|
}
|
|
return d.SetProp(ctx, d.propTargetHumidity.siid, d.propTargetHumidity.piid, pct)
|
|
}
|
|
|
|
func (d *humidifierDevice) GetTargetHumidity(ctx context.Context) (int, error) {
|
|
if !d.propTargetHumidity.valid {
|
|
return 0, xiaomi.ErrNotSupported
|
|
}
|
|
prop, err := d.GetProp(ctx, d.propTargetHumidity.siid, d.propTargetHumidity.piid)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return toInt(prop.Value), nil
|
|
}
|
|
|
|
func (d *humidifierDevice) SetFanLevel(ctx context.Context, level int) error {
|
|
if !d.propFanLevel.valid {
|
|
return xiaomi.ErrNotSupported
|
|
}
|
|
return d.SetProp(ctx, d.propFanLevel.siid, d.propFanLevel.piid, level)
|
|
}
|
|
|
|
func (d *humidifierDevice) GetFanLevel(ctx context.Context) (int, error) {
|
|
if !d.propFanLevel.valid {
|
|
return 0, xiaomi.ErrNotSupported
|
|
}
|
|
prop, err := d.GetProp(ctx, d.propFanLevel.siid, d.propFanLevel.piid)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return toInt(prop.Value), nil
|
|
}
|