Files
xiaomihome/bridge/bridge.go
T
4566704 b808e51b3e refactor: 重大架构升级 — HomeID/RoomID int64化 + 设备驱动重构 + 结构化日志
主要变更:

**类型系统重构**
- 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 敏感配置
2026-07-13 10:40:20 +08:00

267 lines
7.6 KiB
Go

// Package bridge 提供多用户小米设备管理门面。
//
// 外部只需 import 这一个包,无需直接依赖 xiaomi/devices/miot。
//
// 用法:
//
// pool := bridge.NewPool()
// pool.Add(ctx, id, uuid, auth, dataDir)
// b, _ := pool.Get(id)
// ac, _ := b.AC("did")
// ac.TurnOn(ctx)
// state := ac.GetState()
package bridge
import (
"context"
"fmt"
"sync"
"xiaomihome/logger"
"xiaomihome/xiaomi"
"xiaomihome/xiaomi/devices"
)
// ── 类型别名:业务层不需要 import devices ──
type (
ACState = devices.ACState
LightState = devices.LightState
FanState = devices.FanState
HumidifierState = devices.HumidifierState
HeaterState = devices.HeaterState
VacuumStatus = devices.VacuumStatus
)
// ── Pool ──
// Pool 全局路由:id → Bridge。
type Pool struct {
mu sync.RWMutex
items map[int64]*Bridge
}
// NewPool 创建 Pool。
func NewPool() *Pool {
return &Pool{items: make(map[int64]*Bridge)}
}
// Get 按 id 查找。
func (p *Pool) Get(id int64) (*Bridge, bool) {
p.mu.RLock()
defer p.mu.RUnlock()
b, ok := p.items[id]
return b, ok
}
// Add 新建并注册一个 Bridge。
func (p *Pool) Add(ctx context.Context, id int64, uuid string, auth xiaomi.AuthInfo, dataDir string, homeIDs []int64) (*Bridge, error) {
p.mu.Lock()
defer p.mu.Unlock()
if _, exists := p.items[id]; exists {
return nil, fmt.Errorf("bridge: %d already exists", id)
}
b, err := newBridge(ctx, id, uuid, auth, dataDir, homeIDs)
if err != nil {
return nil, err
}
p.items[id] = b
return b, nil
}
// Remove 移除并关闭。
func (p *Pool) Remove(id int64) error {
p.mu.Lock()
b, ok := p.items[id]
if ok {
delete(p.items, id)
}
p.mu.Unlock()
if !ok {
return fmt.Errorf("bridge: %d not found", id)
}
return b.Close()
}
// Close 关闭所有。
func (p *Pool) Close() {
p.mu.Lock()
defer p.mu.Unlock()
for id, b := range p.items {
b.Close()
delete(p.items, id)
}
}
// ── Bridge ──
// Bridge 封装单个用户的 Client + 类型化设备控制器 + 状态缓存。
type Bridge struct {
id int64
client *xiaomi.Client
lgr logger.Logger
mu sync.RWMutex
dv map[string]interface{} // DID → devices.Xxx interface
onStateChanged func(id int64, did, deviceType, changed string, state interface{})
}
func newBridge(ctx context.Context, id int64, uuid string, auth xiaomi.AuthInfo, dataDir string, homeIDs []int64) (*Bridge, error) {
idStr := fmt.Sprintf("%d", id)
client, err := xiaomi.NewClient(ctx, xiaomi.Config{
AuthInfo: auth,
DataDir: dataDir,
UUID: uuid,
UID: idStr,
HomeIDs: homeIDs,
})
if err != nil {
return nil, fmt.Errorf("bridge: %w", err)
}
b := &Bridge{
id: id,
client: client,
lgr: logger.Default(),
dv: make(map[string]interface{}),
}
devList, err := client.GetDevices(ctx)
if err != nil {
client.Close()
return nil, fmt.Errorf("bridge: GetDevices: %w", err)
}
for _, info := range devList {
d, createErr := devices.Create(client, info)
if createErr != nil {
continue
}
b.dv[info.DID] = d
}
b.lgr.Infof("[bridge] %d: %d devices", id, len(b.dv))
return b, nil
}
// SetLogger 设置日志记录器。
func (b *Bridge) SetLogger(l logger.Logger) { b.lgr = l }
func (b *Bridge) Close() error { return b.client.Close() }
// Devices 返回设备列表(DID → 类型名)。
func (b *Bridge) Devices() map[string]string {
b.mu.RLock()
defer b.mu.RUnlock()
out := make(map[string]string, len(b.dv))
for did, d := range b.dv {
out[did] = deviceTypeName(d)
}
return out
}
func deviceTypeName(d interface{}) string {
switch d.(type) {
case devices.AirConditioner: return "air_conditioner"
case devices.Light: return "light"
case devices.Switch: return "switch"
case devices.Fan: return "fan"
case devices.Cover: return "cover"
case devices.Humidifier: return "humidifier"
case devices.Vacuum: return "vacuum"
case devices.WaterHeater: return "water_heater"
case devices.Thermostat: return "thermostat"
case devices.TempHumiditySensor: return "temp_humidity_sensor"
case devices.OccupancySensor: return "occupancy_sensor"
case devices.Speaker: return "speaker"
default: return "unknown"
}
}
// ── 类型化设备访问器 ──
func (b *Bridge) AC(did string) (devices.AirConditioner, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
ac, ok := d.(devices.AirConditioner)
if !ok { return nil, fmt.Errorf("bridge: device %s is not an AirConditioner", did) }
return ac, nil
}
func (b *Bridge) Light(did string) (devices.Light, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
lt, ok := d.(devices.Light)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Light", did) }
return lt, nil
}
func (b *Bridge) Switch(did string) (devices.Switch, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
sw, ok := d.(devices.Switch)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Switch", did) }
return sw, nil
}
func (b *Bridge) Fan(did string) (devices.Fan, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
f, ok := d.(devices.Fan)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Fan", did) }
return f, nil
}
func (b *Bridge) Cover(did string) (devices.Cover, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
cv, ok := d.(devices.Cover)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Cover", did) }
return cv, nil
}
func (b *Bridge) Humidifier(did string) (devices.Humidifier, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
h, ok := d.(devices.Humidifier)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Humidifier", did) }
return h, nil
}
func (b *Bridge) Vacuum(did string) (devices.Vacuum, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
v, ok := d.(devices.Vacuum)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Vacuum", did) }
return v, nil
}
func (b *Bridge) WaterHeater(did string) (devices.WaterHeater, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
wh, ok := d.(devices.WaterHeater)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a WaterHeater", did) }
return wh, nil
}
func (b *Bridge) Thermostat(did string) (devices.Thermostat, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
t, ok := d.(devices.Thermostat)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Thermostat", did) }
return t, nil
}
func (b *Bridge) TempHumidity(did string) (devices.TempHumiditySensor, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
s, ok := d.(devices.TempHumiditySensor)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a TempHumiditySensor", did) }
return s, nil
}
func (b *Bridge) Occupancy(did string) (devices.OccupancySensor, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
s, ok := d.(devices.OccupancySensor)
if !ok { return nil, fmt.Errorf("bridge: device %s is not an OccupancySensor", did) }
return s, nil
}
func (b *Bridge) Speaker(did string) (devices.Speaker, error) {
d, ok := b.dv[did]
if !ok { return nil, fmt.Errorf("bridge: device %s not found", did) }
sp, ok := d.(devices.Speaker)
if !ok { return nil, fmt.Errorf("bridge: device %s is not a Speaker", did) }
return sp, nil
}