- 改动:go.mod module 声明与全部内部导入路径补全为完整域名路径,README 同步 - 原因:裸模块名无法被 go get 解析,发版前必须修正为 GOPRIVATE 可拉取路径
89 lines
1.9 KiB
Go
89 lines
1.9 KiB
Go
package xiaomi
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.zeroonesoft.cn/golib/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
|
|
}
|