- 新增 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 示例(风扇/窗帘/传感器控制)
89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package xiaomi
|
|
|
|
import (
|
|
"context"
|
|
|
|
"xiaomihome/miot"
|
|
)
|
|
|
|
// GetProp 读取单个属性
|
|
func (c *Client) GetProp(ctx context.Context, did string, siid, piid int) (*PropertyValue, error) {
|
|
val, err := c.inner.GetProp(did, siid, piid)
|
|
if err != nil {
|
|
return nil, wrapErr("GetProp", did, err)
|
|
}
|
|
|
|
return &PropertyValue{
|
|
DID: did,
|
|
SIID: siid,
|
|
PIID: piid,
|
|
Value: val,
|
|
}, nil
|
|
}
|
|
|
|
// SetProp 写入单个属性
|
|
func (c *Client) SetProp(ctx context.Context, did string, siid, piid int, value interface{}) error {
|
|
if err := c.inner.SetProp(did, siid, piid, value); err != nil {
|
|
return wrapErr("SetProp", did, err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
// GetProps 批量读取属性
|
|
func (c *Client) GetProps(ctx context.Context, params []PropKey) ([]*PropertyValue, error) {
|
|
miotParams := make([]miot.PropParam, len(params))
|
|
for i, p := range params {
|
|
miotParams[i] = miot.PropParam{
|
|
DID: p.DID,
|
|
Siid: p.SIID,
|
|
Piid: p.PIID,
|
|
}
|
|
}
|
|
|
|
results, err := c.inner.GetProps(miotParams)
|
|
if err != nil {
|
|
return nil, wrapErr("GetProps", "", err)
|
|
}
|
|
|
|
props := make([]*PropertyValue, len(results))
|
|
for i, r := range results {
|
|
props[i] = &PropertyValue{
|
|
DID: r.DID,
|
|
SIID: r.Siid,
|
|
PIID: r.Piid,
|
|
Code: r.Code,
|
|
Value: r.Value,
|
|
}
|
|
}
|
|
|
|
return props, nil
|
|
}
|
|
|
|
// SetProps 批量写入属性
|
|
func (c *Client) SetProps(ctx context.Context, params []PropKeyValue) ([]*PropResult, error) {
|
|
miotParams := make([]miot.PropParam, len(params))
|
|
for i, p := range params {
|
|
miotParams[i] = miot.PropParam{
|
|
DID: p.DID,
|
|
Siid: p.SIID,
|
|
Piid: p.PIID,
|
|
Value: p.Value,
|
|
}
|
|
}
|
|
|
|
results, err := c.inner.SetProps(miotParams)
|
|
if err != nil {
|
|
return nil, wrapErr("SetProps", "", err)
|
|
}
|
|
|
|
propResults := make([]*PropResult, len(results))
|
|
for i, r := range results {
|
|
propResults[i] = &PropResult{
|
|
PropKey: PropKey{DID: r.DID, SIID: r.Siid, PIID: r.Piid},
|
|
Code: r.Code,
|
|
}
|
|
}
|
|
|
|
return propResults, nil
|
|
}
|