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:
@@ -10,79 +10,64 @@ import (
|
||||
// OccupancySensor 人在传感器读取接口(只读)。
|
||||
type OccupancySensor interface {
|
||||
Device
|
||||
|
||||
// IsOccupied 是否有人。
|
||||
IsOccupied(ctx context.Context) (bool, error)
|
||||
|
||||
// GetIlluminance 环境光照度(lux),部分传感器支持。
|
||||
GetIlluminance(ctx context.Context) (float64, error)
|
||||
}
|
||||
|
||||
// NewOccupancySensor 创建人在传感器。失败返回错误(含无 SPEC / 无相关属性)。
|
||||
func NewOccupancySensor(client *xiaomi.Client, info *xiaomi.DeviceInfo) (OccupancySensor, error) {
|
||||
if info == nil {
|
||||
return nil, xiaomi.ErrDeviceNotFound
|
||||
}
|
||||
|
||||
miotDev := client.GetMIoTDevice(info.DID)
|
||||
if miotDev == nil {
|
||||
return nil, fmt.Errorf("%w: device not initialized", xiaomi.ErrDeviceNotFound)
|
||||
}
|
||||
spec := miotDev.SpecInstance()
|
||||
if spec == nil {
|
||||
return nil, fmt.Errorf("%w: device has no SPEC information", xiaomi.ErrNotSupported)
|
||||
}
|
||||
|
||||
d := &occupancyDevice{
|
||||
BaseDevice: BaseDevice{client: client, info: info},
|
||||
}
|
||||
|
||||
for _, svc := range spec.Services {
|
||||
for _, prop := range svc.Properties {
|
||||
switch propertyShortName(prop.Type) {
|
||||
case "occupancy":
|
||||
if !d.propOccupancy.valid {
|
||||
d.propOccupancy = siidPiid{svc.IID, prop.PIID, true}
|
||||
}
|
||||
case "illumination":
|
||||
if !d.propIlluminance.valid {
|
||||
d.propIlluminance = siidPiid{svc.IID, prop.PIID, true}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !d.propOccupancy.valid {
|
||||
return nil, fmt.Errorf("%w: no occupancy property in SPEC", xiaomi.ErrNotSupported)
|
||||
}
|
||||
|
||||
return d, nil
|
||||
OnStateChanged(handler func(occupied bool, illuminance float64)) (string, error)
|
||||
}
|
||||
|
||||
type occupancyDevice struct {
|
||||
BaseDevice
|
||||
propOccupancy siidPiid
|
||||
propIlluminance siidPiid
|
||||
stateCache [2]interface{} // [0]=bool occupied, [1]=float64 lux
|
||||
}
|
||||
|
||||
func NewOccupancySensor(client *xiaomi.Client, info *xiaomi.DeviceInfo) (OccupancySensor, error) {
|
||||
if info == nil { return nil, xiaomi.ErrDeviceNotFound }
|
||||
miotDev := client.GetMIoTDevice(info.DID)
|
||||
if miotDev == nil { return nil, fmt.Errorf("%w: device not initialized", xiaomi.ErrDeviceNotFound) }
|
||||
spec := miotDev.SpecInstance()
|
||||
if spec == nil { return nil, fmt.Errorf("%w: device has no SPEC information", xiaomi.ErrNotSupported) }
|
||||
d := &occupancyDevice{BaseDevice: BaseDevice{client: client, info: info}}
|
||||
for _, svc := range spec.Services {
|
||||
for _, prop := range svc.Properties {
|
||||
switch propertyShortName(prop.Type) {
|
||||
case "occupancy":
|
||||
if !d.propOccupancy.valid { d.propOccupancy = siidPiid{svc.IID, prop.PIID, true} }
|
||||
case "illumination":
|
||||
if !d.propIlluminance.valid { d.propIlluminance = siidPiid{svc.IID, prop.PIID, true} }
|
||||
}
|
||||
}
|
||||
}
|
||||
if !d.propOccupancy.valid { return nil, fmt.Errorf("%w: no occupancy property in SPEC", xiaomi.ErrNotSupported) }
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *occupancyDevice) IsOccupied(ctx context.Context) (bool, error) {
|
||||
prop, err := d.GetProp(ctx, d.propOccupancy.siid, d.propOccupancy.piid)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
if b, ok := prop.Value.(bool); ok {
|
||||
return b, nil
|
||||
}
|
||||
if err != nil { return false, err }
|
||||
if b, ok := prop.Value.(bool); ok { return b, nil }
|
||||
return false, nil
|
||||
}
|
||||
|
||||
func (d *occupancyDevice) GetIlluminance(ctx context.Context) (float64, error) {
|
||||
if !d.propIlluminance.valid {
|
||||
return 0, xiaomi.ErrNotSupported
|
||||
}
|
||||
if !d.propIlluminance.valid { return 0, xiaomi.ErrNotSupported }
|
||||
prop, err := d.GetProp(ctx, d.propIlluminance.siid, d.propIlluminance.piid)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err != nil { return 0, err }
|
||||
return toFloat(prop.Value), nil
|
||||
}
|
||||
|
||||
func (d *occupancyDevice) OnStateChanged(handler func(occupied bool, illuminance float64)) (string, error) {
|
||||
if !d.propOccupancy.valid && !d.propIlluminance.valid { return "", xiaomi.ErrNotSupported }
|
||||
return d.SubProp(0, 0, func(did string, prop *xiaomi.PropertyValue) {
|
||||
switch {
|
||||
case d.propOccupancy.valid && prop.SIID == d.propOccupancy.siid && prop.PIID == d.propOccupancy.piid:
|
||||
if b, ok := prop.Value.(bool); ok { d.stateCache[0] = b }
|
||||
case d.propIlluminance.valid && prop.SIID == d.propIlluminance.siid && prop.PIID == d.propIlluminance.piid:
|
||||
d.stateCache[1] = toFloat(prop.Value)
|
||||
}
|
||||
occ, _ := d.stateCache[0].(bool)
|
||||
lux, _ := d.stateCache[1].(float64)
|
||||
handler(occ, lux)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user