- 改动:go.mod module 声明与全部内部导入路径补全为完整域名路径,README 同步 - 原因:裸模块名无法被 go get 解析,发版前必须修正为 GOPRIVATE 可拉取路径
146 lines
3.7 KiB
Go
146 lines
3.7 KiB
Go
package xiaomi
|
|
|
|
import (
|
|
"context"
|
|
|
|
"git.zeroonesoft.cn/golib/xiaomihome/miot"
|
|
)
|
|
|
|
// DeviceFilter 设备过滤器函数类型
|
|
type DeviceFilter func(*DeviceInfo) bool
|
|
|
|
// FilterByHome 按家庭过滤
|
|
func FilterByHome(homeID string) DeviceFilter {
|
|
return func(d *DeviceInfo) bool { return d.HomeID == homeID }
|
|
}
|
|
|
|
// FilterByRoom 按房间过滤
|
|
func FilterByRoom(roomID string) DeviceFilter {
|
|
return func(d *DeviceInfo) bool { return d.RoomID == roomID }
|
|
}
|
|
|
|
// FilterByOnline 只返回在线设备
|
|
func FilterByOnline() DeviceFilter {
|
|
return func(d *DeviceInfo) bool { return d.Online }
|
|
}
|
|
|
|
// FilterByModel 按型号过滤
|
|
func FilterByModel(model string) DeviceFilter {
|
|
return func(d *DeviceInfo) bool { return d.Model == model }
|
|
}
|
|
|
|
// GetDevices 获取所有设备信息(强类型返回),可选过滤器
|
|
func (c *Client) GetDevices(ctx context.Context, opts ...DeviceFilter) ([]*DeviceInfo, error) {
|
|
rawList := c.inner.DeviceList()
|
|
if rawList == nil {
|
|
return nil, nil
|
|
}
|
|
|
|
devices := make([]*DeviceInfo, 0, len(rawList))
|
|
for did, raw := range rawList {
|
|
info := parseDeviceInfo(did, raw)
|
|
devices = append(devices, info)
|
|
}
|
|
|
|
// 应用过滤器
|
|
if len(opts) > 0 {
|
|
filtered := make([]*DeviceInfo, 0, len(devices))
|
|
for _, d := range devices {
|
|
ok := true
|
|
for _, f := range opts {
|
|
if !f(d) {
|
|
ok = false
|
|
break
|
|
}
|
|
}
|
|
if ok {
|
|
filtered = append(filtered, d)
|
|
}
|
|
}
|
|
return filtered, nil
|
|
}
|
|
|
|
return devices, nil
|
|
}
|
|
|
|
// GetDevice 获取单个设备信息
|
|
func (c *Client) GetDevice(ctx context.Context, did string) (*DeviceInfo, error) {
|
|
raw := c.inner.GetDeviceInfo(did)
|
|
if raw == nil {
|
|
// 尝试从 cached DeviceList 中获取
|
|
rawList := c.inner.DeviceList()
|
|
if rawList != nil {
|
|
if info, ok := rawList[did]; ok {
|
|
raw = info
|
|
}
|
|
}
|
|
}
|
|
|
|
if raw == nil || len(raw) == 0 {
|
|
return nil, ErrDeviceNotFound
|
|
}
|
|
|
|
return parseDeviceInfo(did, raw), nil
|
|
}
|
|
|
|
// RefreshDevices 刷新设备列表,homeIDs 为空则全量
|
|
func (c *Client) RefreshDevices(ctx context.Context, homeIDs ...string) error {
|
|
return c.inner.RefreshDevices(homeIDs)
|
|
}
|
|
|
|
// RefreshDeviceAllProps 触发设备全量属性刷新(设备上线后同步状态)。
|
|
func (c *Client) RefreshDeviceAllProps(did string) error {
|
|
return c.inner.RefreshDeviceAllProps(did)
|
|
}
|
|
|
|
// GetMIoTDevice 获取底层 MIoTDevice 实例(供设备控制抽象使用)
|
|
func (c *Client) GetMIoTDevice(did string) *miot.MIoTDevice {
|
|
// 先尝试缓存
|
|
if dev := c.getDevice(did); dev != nil {
|
|
return dev
|
|
}
|
|
|
|
// 从 DeviceList 获取 info 创建
|
|
raw := c.inner.GetDeviceInfo(did)
|
|
if raw == nil {
|
|
rawList := c.inner.DeviceList()
|
|
if rawList != nil {
|
|
raw = rawList[did]
|
|
}
|
|
}
|
|
|
|
if raw == nil {
|
|
return nil
|
|
}
|
|
|
|
return c.getOrCreateDevice(did, raw)
|
|
}
|
|
|
|
// parseDeviceInfo 从 map 解析 DeviceInfo
|
|
func parseDeviceInfo(did string, m map[string]interface{}) *DeviceInfo {
|
|
return &DeviceInfo{
|
|
DID: did,
|
|
Name: strVal(m, "name"),
|
|
Model: strVal(m, "model"),
|
|
Manufacturer: strVal(m, "manufacturer"),
|
|
FWVersion: strVal(m, "fw_version"),
|
|
Icon: strVal(m, "icon"),
|
|
URN: strVal(m, "urn"),
|
|
Token: strVal(m, "token"),
|
|
IP: strVal(m, "local_ip"),
|
|
RSSI: intVal(m, "rssi"),
|
|
SSID: strVal(m, "ssid"),
|
|
BSSID: strVal(m, "bssid"),
|
|
Online: boolVal(m, "online"),
|
|
HomeID: strVal(m, "home_id"),
|
|
HomeName: strVal(m, "home_name"),
|
|
RoomID: strVal(m, "room_id"),
|
|
RoomName: strVal(m, "room_name"),
|
|
OwnerID: strVal(m, "owner_id"),
|
|
ParentID: strVal(m, "parent_id"),
|
|
ParentModel: strVal(m, "parent_model"),
|
|
GroupID: strVal(m, "group_id"),
|
|
ConnectType: intVal(m, "connect_type"),
|
|
}
|
|
}
|