refactor: 重构 token 管理,新增 NATS adapter 和多示例
按照 TOKEN_REFACTOR.md / FIX_CONFIG.md 方案重构核心层: - miot: Init() 优先读 storage token,新增 RefreshAuthInfo() 和刷新回调 - xiaomi: Config 改用 AuthInfo 结构体(含 UUID/UID/DataDir/OnTokenRefreshed) - xiaomi: NewClient(ctx, Config) 新签名,新增 RefreshToken(),后台自动刷新 - xiaomi/devices: 设备控制接口重构 - 新增示例 12_ac_subscribe,其余示例适配新 Config
This commit is contained in:
+72
-90
@@ -11,131 +11,113 @@ type Vacuum interface {
|
||||
Start(ctx context.Context) error
|
||||
Stop(ctx context.Context) error
|
||||
ReturnToDock(ctx context.Context) error
|
||||
|
||||
SetFanLevel(ctx context.Context, level int) error
|
||||
GetFanLevel(ctx context.Context) (int, error)
|
||||
|
||||
GetStatus(ctx context.Context) (VacuumStatus, error)
|
||||
|
||||
OnStateChanged(handler func(state VacuumStatus, changed string)) (string, error)
|
||||
}
|
||||
|
||||
// VacuumStatus 扫地机运行状态
|
||||
// VacuumStatus 扫地机运行状态(字段名对齐 SPEC type)
|
||||
type VacuumStatus struct {
|
||||
Power bool `json:"power"`
|
||||
Mode int `json:"mode"` // 0=idle, 1=sweep, 2=mop, 3=sweep+mop, 4=charging
|
||||
FanLevel int `json:"fan_level"`
|
||||
Battery int `json:"battery"` // 0-100
|
||||
On bool `json:"on"`
|
||||
Mode int `json:"mode"`
|
||||
FanLevel int `json:"fanLevel"`
|
||||
Battery int `json:"battery"`
|
||||
}
|
||||
|
||||
type vacuumDevice struct {
|
||||
BaseDevice
|
||||
propOn siidPiid // "on" — start/stop control
|
||||
propMode siidPiid // "mode" — 0=charge(dock), 1=sweep, etc.
|
||||
propFanLevel siidPiid // "fan-level" (suction power)
|
||||
propBattery siidPiid // "battery-level"
|
||||
propOn siidPiid
|
||||
propMode siidPiid
|
||||
propFanLevel siidPiid
|
||||
propBattery siidPiid
|
||||
stateCache VacuumStatus
|
||||
}
|
||||
|
||||
// NewVacuum creates a Vacuum control for the given device.
|
||||
func NewVacuum(client *xiaomi.Client, info *xiaomi.DeviceInfo) (Vacuum, error) {
|
||||
d := &vacuumDevice{
|
||||
BaseDevice: NewBaseDevice(client, info),
|
||||
}
|
||||
|
||||
d := &vacuumDevice{BaseDevice: NewBaseDevice(client, info)}
|
||||
resolver := d.getSpecResolver()
|
||||
if resolver == nil {
|
||||
return nil, xiaomi.ErrNoSPEC
|
||||
}
|
||||
|
||||
if resolver == nil { return nil, xiaomi.ErrNoSPEC }
|
||||
siid, piid, ok := resolver.find("on")
|
||||
if !ok {
|
||||
return nil, xiaomi.ErrNotSupported
|
||||
}
|
||||
if !ok { return nil, xiaomi.ErrNotSupported }
|
||||
d.propOn = siidPiid{siid, piid, true}
|
||||
|
||||
if s, p, ok := resolver.find("mode"); ok {
|
||||
d.propMode = siidPiid{s, p, true}
|
||||
}
|
||||
if s, p, ok := resolver.find("fan-level"); ok {
|
||||
d.propFanLevel = siidPiid{s, p, true}
|
||||
}
|
||||
if s, p, ok := resolver.find("battery-level"); ok {
|
||||
d.propBattery = siidPiid{s, p, true}
|
||||
}
|
||||
|
||||
if s, p, ok := resolver.find("mode"); ok { d.propMode = siidPiid{s, p, true} }
|
||||
if s, p, ok := resolver.find("fan-level"); ok { d.propFanLevel = siidPiid{s, p, true} }
|
||||
if s, p, ok := resolver.find("battery-level"); ok { d.propBattery = siidPiid{s, p, true} }
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *vacuumDevice) Start(ctx context.Context) error {
|
||||
if !d.propOn.valid {
|
||||
return xiaomi.ErrNotSupported
|
||||
}
|
||||
return d.SetProp(ctx, d.propOn.siid, d.propOn.piid, true)
|
||||
_, err := d.Action(ctx, d.propOn.siid, 1, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *vacuumDevice) Stop(ctx context.Context) error {
|
||||
if !d.propOn.valid {
|
||||
return xiaomi.ErrNotSupported
|
||||
}
|
||||
return d.SetProp(ctx, d.propOn.siid, d.propOn.piid, false)
|
||||
_, err := d.Action(ctx, d.propOn.siid, 0, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *vacuumDevice) ReturnToDock(ctx context.Context) error {
|
||||
if !d.propMode.valid {
|
||||
// Try using on=false as fallback
|
||||
return d.Stop(ctx)
|
||||
}
|
||||
return d.SetProp(ctx, d.propMode.siid, d.propMode.piid, 0)
|
||||
_, err := d.Action(ctx, d.propOn.siid, 2, nil)
|
||||
return err
|
||||
}
|
||||
|
||||
func (d *vacuumDevice) SetFanLevel(ctx context.Context, level int) error {
|
||||
if !d.propFanLevel.valid {
|
||||
return xiaomi.ErrNotSupported
|
||||
}
|
||||
if !d.propFanLevel.valid { return xiaomi.ErrNotSupported }
|
||||
return d.SetProp(ctx, d.propFanLevel.siid, d.propFanLevel.piid, level)
|
||||
}
|
||||
|
||||
func (d *vacuumDevice) GetFanLevel(ctx context.Context) (int, error) {
|
||||
if !d.propFanLevel.valid {
|
||||
return 0, xiaomi.ErrNotSupported
|
||||
}
|
||||
if !d.propFanLevel.valid { return 0, xiaomi.ErrNotSupported }
|
||||
prop, err := d.GetProp(ctx, d.propFanLevel.siid, d.propFanLevel.piid)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err != nil { return 0, err }
|
||||
return toInt(prop.Value), nil
|
||||
}
|
||||
|
||||
func (d *vacuumDevice) GetStatus(ctx context.Context) (VacuumStatus, error) {
|
||||
// 云端实时查询
|
||||
keys := []xiaomi.PropKey{}
|
||||
add := func(sp siidPiid) {
|
||||
if sp.valid { keys = append(keys, xiaomi.PropKey{DID: d.info.DID, SIID: sp.siid, PIID: sp.piid}) }
|
||||
}
|
||||
add(d.propOn); add(d.propMode); add(d.propFanLevel); add(d.propBattery)
|
||||
props, err := d.client.GetProps(ctx, keys)
|
||||
if err != nil { return VacuumStatus{}, err }
|
||||
status := VacuumStatus{}
|
||||
|
||||
if d.propOn.valid {
|
||||
prop, err := d.GetProp(ctx, d.propOn.siid, d.propOn.piid)
|
||||
if err == nil {
|
||||
if b, ok := prop.Value.(bool); ok {
|
||||
status.Power = b
|
||||
}
|
||||
for _, p := range props {
|
||||
switch {
|
||||
case d.propOn.valid && p.SIID == d.propOn.siid && p.PIID == d.propOn.piid:
|
||||
status.On, _ = p.Value.(bool)
|
||||
case d.propMode.valid && p.SIID == d.propMode.siid && p.PIID == d.propMode.piid:
|
||||
status.Mode = toInt(p.Value)
|
||||
case d.propFanLevel.valid && p.SIID == d.propFanLevel.siid && p.PIID == d.propFanLevel.piid:
|
||||
status.FanLevel = toInt(p.Value)
|
||||
case d.propBattery.valid && p.SIID == d.propBattery.siid && p.PIID == d.propBattery.piid:
|
||||
status.Battery = toInt(p.Value)
|
||||
}
|
||||
}
|
||||
|
||||
if d.propMode.valid {
|
||||
prop, err := d.GetProp(ctx, d.propMode.siid, d.propMode.piid)
|
||||
if err == nil {
|
||||
status.Mode = toInt(prop.Value)
|
||||
}
|
||||
}
|
||||
|
||||
if d.propFanLevel.valid {
|
||||
prop, err := d.GetProp(ctx, d.propFanLevel.siid, d.propFanLevel.piid)
|
||||
if err == nil {
|
||||
status.FanLevel = toInt(prop.Value)
|
||||
}
|
||||
}
|
||||
|
||||
if d.propBattery.valid {
|
||||
prop, err := d.GetProp(ctx, d.propBattery.siid, d.propBattery.piid)
|
||||
if err == nil {
|
||||
status.Battery = toInt(prop.Value)
|
||||
}
|
||||
}
|
||||
|
||||
return status, nil
|
||||
}
|
||||
|
||||
func (d *vacuumDevice) OnStateChanged(handler func(state VacuumStatus, changed string)) (string, error) {
|
||||
type field struct{ siid, piid int; name string; fill func(*VacuumStatus, interface{}) }
|
||||
var fields []field
|
||||
if d.propOn.valid {
|
||||
fields = append(fields, field{d.propOn.siid, d.propOn.piid, "on", func(s *VacuumStatus, v interface{}) { s.On, _ = v.(bool) }})
|
||||
}
|
||||
if d.propMode.valid {
|
||||
fields = append(fields, field{d.propMode.siid, d.propMode.piid, "mode", func(s *VacuumStatus, v interface{}) { s.Mode = toInt(v) }})
|
||||
}
|
||||
if d.propFanLevel.valid {
|
||||
fields = append(fields, field{d.propFanLevel.siid, d.propFanLevel.piid, "fan-level", func(s *VacuumStatus, v interface{}) { s.FanLevel = toInt(v) }})
|
||||
}
|
||||
if d.propBattery.valid {
|
||||
fields = append(fields, field{d.propBattery.siid, d.propBattery.piid, "battery", func(s *VacuumStatus, v interface{}) { s.Battery = toInt(v) }})
|
||||
}
|
||||
if len(fields) == 0 { return "", xiaomi.ErrNotSupported }
|
||||
return d.SubProp(0, 0, func(did string, prop *xiaomi.PropertyValue) {
|
||||
for _, f := range fields {
|
||||
if f.siid == prop.SIID && f.piid == prop.PIID {
|
||||
f.fill(&d.stateCache, prop.Value)
|
||||
handler(d.stateCache, f.name)
|
||||
return
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user