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:
+44
-13
@@ -29,15 +29,15 @@ type Light interface {
|
||||
SetColor(ctx context.Context, r, g, b int) error
|
||||
GetColor(ctx context.Context) (r, g, b int, err error)
|
||||
|
||||
OnStateChanged(handler func(state LightState)) (string, error)
|
||||
OnStateChanged(handler func(state LightState, changed string)) (string, error)
|
||||
}
|
||||
|
||||
// LightState 灯光完整状态
|
||||
// LightState 灯光完整状态(字段名对齐 SPEC type)
|
||||
type LightState struct {
|
||||
Power bool `json:"power"`
|
||||
Brightness int `json:"brightness"`
|
||||
ColorTemp int `json:"color_temp"`
|
||||
ColorRGB [3]int `json:"color_rgb"`
|
||||
On bool `json:"on"` // SPEC: "on"
|
||||
Brightness int `json:"brightness"` // SPEC: "brightness"
|
||||
ColorTemperature int `json:"colorTemperature"` // SPEC: "color-temperature"
|
||||
ColorRGB [3]int `json:"colorRgb"` // SPEC: "color"
|
||||
}
|
||||
|
||||
type lightDevice struct {
|
||||
@@ -46,6 +46,8 @@ type lightDevice struct {
|
||||
propBrightness siidPiid // "brightness"
|
||||
propColorTemp siidPiid // "color-temperature"
|
||||
propColor siidPiid // "color"
|
||||
|
||||
stateCache LightState // MQTT 推送维护的本地缓存
|
||||
}
|
||||
|
||||
// NewLight creates a Light control for the given device.
|
||||
@@ -179,16 +181,45 @@ func (d *lightDevice) GetColor(ctx context.Context) (r, g, b int, err error) {
|
||||
return r, g, b, nil
|
||||
}
|
||||
|
||||
func (d *lightDevice) OnStateChanged(handler func(state LightState)) (string, error) {
|
||||
if !d.propOn.valid {
|
||||
func (d *lightDevice) OnStateChanged(handler func(state LightState, changed string)) (string, error) {
|
||||
type field struct {
|
||||
siid, piid int
|
||||
name string
|
||||
fill func(*LightState, interface{})
|
||||
}
|
||||
var fields []field
|
||||
if d.propOn.valid {
|
||||
fields = append(fields, field{d.propOn.siid, d.propOn.piid, "on",
|
||||
func(s *LightState, v interface{}) { s.On, _ = v.(bool) }})
|
||||
}
|
||||
if d.propBrightness.valid {
|
||||
fields = append(fields, field{d.propBrightness.siid, d.propBrightness.piid, "brightness",
|
||||
func(s *LightState, v interface{}) { s.Brightness = toInt(v) }})
|
||||
}
|
||||
if d.propColorTemp.valid {
|
||||
fields = append(fields, field{d.propColorTemp.siid, d.propColorTemp.piid, "color-temperature",
|
||||
func(s *LightState, v interface{}) { s.ColorTemperature = toInt(v) }})
|
||||
}
|
||||
if d.propColor.valid {
|
||||
fields = append(fields, field{d.propColor.siid, d.propColor.piid, "color",
|
||||
func(s *LightState, v interface{}) {
|
||||
if v, ok := v.(float64); ok {
|
||||
c := int(v)
|
||||
s.ColorRGB = [3]int{(c >> 16) & 0xFF, (c >> 8) & 0xFF, c & 0xFF}
|
||||
}
|
||||
}})
|
||||
}
|
||||
if len(fields) == 0 {
|
||||
return "", xiaomi.ErrNotSupported
|
||||
}
|
||||
return d.SubProp(d.propOn.siid, d.propOn.piid, func(did string, prop *xiaomi.PropertyValue) {
|
||||
state := LightState{}
|
||||
if b, ok := prop.Value.(bool); ok {
|
||||
state.Power = b
|
||||
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
|
||||
}
|
||||
}
|
||||
handler(state)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user