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,76 +10,64 @@ import (
|
||||
// TempHumiditySensor 温湿度传感器读取接口(只读)。
|
||||
type TempHumiditySensor interface {
|
||||
Device
|
||||
|
||||
GetTemperature(ctx context.Context) (float64, error)
|
||||
GetHumidity(ctx context.Context) (float64, error)
|
||||
}
|
||||
|
||||
// NewTempHumiditySensor 创建温湿度传感器。失败返回错误(含无 SPEC / 无相关属性)。
|
||||
func NewTempHumiditySensor(client *xiaomi.Client, info *xiaomi.DeviceInfo) (TempHumiditySensor, 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 := &tempHumidityDevice{
|
||||
BaseDevice: BaseDevice{client: client, info: info},
|
||||
}
|
||||
|
||||
for _, svc := range spec.Services {
|
||||
for _, prop := range svc.Properties {
|
||||
switch propertyShortName(prop.Type) {
|
||||
case "temperature":
|
||||
if !d.propTemp.valid {
|
||||
d.propTemp = siidPiid{svc.IID, prop.PIID, true}
|
||||
}
|
||||
case "relative-humidity":
|
||||
if !d.propHumidity.valid {
|
||||
d.propHumidity = siidPiid{svc.IID, prop.PIID, true}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if !d.propTemp.valid && !d.propHumidity.valid {
|
||||
return nil, fmt.Errorf("%w: no temperature or humidity property in SPEC", xiaomi.ErrNotSupported)
|
||||
}
|
||||
|
||||
return d, nil
|
||||
OnStateChanged(handler func(temp, humidity float64)) (string, error)
|
||||
}
|
||||
|
||||
type tempHumidityDevice struct {
|
||||
BaseDevice
|
||||
propTemp siidPiid
|
||||
propHumidity siidPiid
|
||||
stateCache [2]float64 // [0]=temperature, [1]=humidity
|
||||
}
|
||||
|
||||
func NewTempHumiditySensor(client *xiaomi.Client, info *xiaomi.DeviceInfo) (TempHumiditySensor, 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 := &tempHumidityDevice{BaseDevice: BaseDevice{client: client, info: info}}
|
||||
for _, svc := range spec.Services {
|
||||
for _, prop := range svc.Properties {
|
||||
switch propertyShortName(prop.Type) {
|
||||
case "temperature":
|
||||
if !d.propTemp.valid { d.propTemp = siidPiid{svc.IID, prop.PIID, true} }
|
||||
case "relative-humidity":
|
||||
if !d.propHumidity.valid { d.propHumidity = siidPiid{svc.IID, prop.PIID, true} }
|
||||
}
|
||||
}
|
||||
}
|
||||
if !d.propTemp.valid && !d.propHumidity.valid {
|
||||
return nil, fmt.Errorf("%w: no temperature or humidity property in SPEC", xiaomi.ErrNotSupported)
|
||||
}
|
||||
return d, nil
|
||||
}
|
||||
|
||||
func (d *tempHumidityDevice) GetTemperature(ctx context.Context) (float64, error) {
|
||||
if !d.propTemp.valid {
|
||||
return 0, xiaomi.ErrNotSupported
|
||||
}
|
||||
if !d.propTemp.valid { return 0, xiaomi.ErrNotSupported }
|
||||
prop, err := d.GetProp(ctx, d.propTemp.siid, d.propTemp.piid)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if err != nil { return 0, err }
|
||||
return toFloat(prop.Value), nil
|
||||
}
|
||||
func (d *tempHumidityDevice) GetHumidity(ctx context.Context) (float64, error) {
|
||||
if !d.propHumidity.valid { return 0, xiaomi.ErrNotSupported }
|
||||
prop, err := d.GetProp(ctx, d.propHumidity.siid, d.propHumidity.piid)
|
||||
if err != nil { return 0, err }
|
||||
return toFloat(prop.Value), nil
|
||||
}
|
||||
|
||||
func (d *tempHumidityDevice) GetHumidity(ctx context.Context) (float64, error) {
|
||||
if !d.propHumidity.valid {
|
||||
return 0, xiaomi.ErrNotSupported
|
||||
}
|
||||
prop, err := d.GetProp(ctx, d.propHumidity.siid, d.propHumidity.piid)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return toFloat(prop.Value), nil
|
||||
func (d *tempHumidityDevice) OnStateChanged(handler func(temp, humidity float64)) (string, error) {
|
||||
if !d.propTemp.valid && !d.propHumidity.valid { return "", xiaomi.ErrNotSupported }
|
||||
return d.SubProp(0, 0, func(did string, prop *xiaomi.PropertyValue) {
|
||||
switch {
|
||||
case d.propTemp.valid && prop.SIID == d.propTemp.siid && prop.PIID == d.propTemp.piid:
|
||||
d.stateCache[0] = toFloat(prop.Value)
|
||||
case d.propHumidity.valid && prop.SIID == d.propHumidity.siid && prop.PIID == d.propHumidity.piid:
|
||||
d.stateCache[1] = toFloat(prop.Value)
|
||||
}
|
||||
handler(d.stateCache[0], d.stateCache[1])
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user