主要变更: **类型系统重构** - HomeInfo.ID/RoomInfo.ID/HomeID 从 string 改为 int64,json tag 使用 ,string - GetHome/GetRooms/GetRoomDevices 接口同步修改 - 新增 int64FromStr 工具函数,更新 parseHomeInfo 解析逻辑 **新增 Speaker 设备** - 新增 KindSpeaker 类型 - classify 映射覆盖 speaker/bluetooth-speaker/alarm-clock/media-player/playback/music **设备驱动重构** - AC 驱动彻底重写:从硬编码常量改为基于 SPEC ValueList 的动态 valMapper - 所有设备(Fan/Humidifier/Light/OccupancySensor/Switch/TempHumiditySensor/Thermostat/Vacuum/WaterHeater/Cover)新增 GetState() 和 FetchState(ctx) 接口 - base.go 新增 specResolver.format() 方法 **MIoT 模块增强** - MIoTClient 新增 homeIDs 字段,支持按家庭过滤设备 - RefreshDevices 支持 homeIDs 参数 - SpecStdLib 引入 logger.Logger,log.Printf 全部迁移为结构化日志 **其他** - Config 新增 RedirectURL 和 HomeIDs 字段 - AuthInfo.ExpiresIn 类型从 int 改为 int32 - UUID 生成简化为 hex.EncodeToString - 新增 github.com/sirupsen/logrus 依赖 - .gitignore 新增 examples/*/data/ 忽略规则 - 删除已提交的 config.json 敏感配置
141 lines
3.4 KiB
Go
141 lines
3.4 KiB
Go
package xiaomi
|
|
|
|
import (
|
|
"context"
|
|
|
|
"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)
|
|
}
|
|
|
|
// 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"),
|
|
}
|
|
}
|