**命名统一(State → Props)** - 所有设备类型:XxxState → XxxProps,GetState() → GetProps(),FetchState() → FetchProps() - 回调接口:OnStateChanged → OnPropsChanged - Bridge 层类型别名、示例代码、文档同步更名 **新增设备类型** - KindPlug + Plug 驱动:智能插座/插头(TurnOn/TurnOff/IsOn/OnPropsChanged) - KindGateway + Gateway 驱动:网关设备类型识别 - KindOccupancySensor 从 KindSensor 独立,添加 OccupancyProps **设备分类系统改进** - classifyByModel 改为按 key 长度降序匹配(防止 sensor 先于 sensor-occupy) - 新增下划线变体识别(sensor_occupy、sensor_temp 等) - outlet 重新归类为 Plug(非 Switch) - 新增 Or() 组合过滤器 **连接桥增强** - 设备创建成功后自动订阅属性变更 - 新增 nil 防护检查 - 支持 Plug/Speaker/Gateway/Occupancy 类型的 FetchProps 刷新 - 日志级别 Info → Debug **AC 驱动增强** - 新增 HasPower()、GetElectricPower()、GetPowerConsumption() 功率接口 - ACProps 新增 ElectricPower、PowerConsumption 字段 - valMapper:Desc 为空时 fallback 用 Name,key 统一小写 **MIoT 核心** - 设备列表刷新改用 reflect.DeepEqual 全字段变更检测 - 移除 BLE/代理设备默认在线 hack - RefreshDeviceAllProps 增加离线检查 - 新增 miot_errors.go:MIoT 错误码定义 - 全局日志级别 Info→Debug/Warn(降低噪音) **示例与文档** - 所有示例代码同步 API 变更 - 传感器示例改用 Or 组合过滤器 - ARCH_PLAN 等迁移文档同步更新
274 lines
7.9 KiB
Go
274 lines
7.9 KiB
Go
package devices
|
|
|
|
import (
|
|
"context"
|
|
"strconv"
|
|
|
|
"xiaomihome/xiaomi"
|
|
)
|
|
|
|
// Light 灯光统一控制接口
|
|
type Light interface {
|
|
Device
|
|
TurnOn(ctx context.Context) error
|
|
TurnOff(ctx context.Context) error
|
|
Toggle(ctx context.Context) error
|
|
|
|
// 能力查询
|
|
HasBrightness() bool
|
|
HasColorTemp() bool
|
|
HasColor() bool
|
|
|
|
// 亮度 (1-100)
|
|
SetBrightness(ctx context.Context, pct int) error
|
|
GetBrightness(ctx context.Context) (int, error)
|
|
|
|
// 色温 (2700-6500K)
|
|
SetColorTemp(ctx context.Context, kelvin int) error
|
|
GetColorTemp(ctx context.Context) (int, error)
|
|
|
|
// 颜色 (0-255)
|
|
SetColor(ctx context.Context, r, g, b int) error
|
|
GetColor(ctx context.Context) (r, g, b int, err error)
|
|
|
|
OnPropsChanged(handler func(props LightProps, changed string)) (string, error)
|
|
GetProps() LightProps
|
|
FetchProps(ctx context.Context) (LightProps, error)
|
|
}
|
|
|
|
// LightProps 灯光完整状态(字段名对齐 SPEC type)
|
|
type LightProps struct {
|
|
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 {
|
|
BaseDevice
|
|
propOn siidPiid // "on"
|
|
propBrightness siidPiid // "brightness"
|
|
propColorTemp siidPiid // "color-temperature"
|
|
propColor siidPiid // "color"
|
|
|
|
propsCache LightProps // MQTT 推送维护的本地缓存
|
|
}
|
|
|
|
// NewLight creates a Light control for the given device.
|
|
func NewLight(client *xiaomi.Client, info *xiaomi.DeviceInfo) (Light, error) {
|
|
d := &lightDevice{
|
|
BaseDevice: NewBaseDevice(client, info),
|
|
}
|
|
|
|
resolver := d.getSpecResolver()
|
|
if resolver == nil {
|
|
return nil, xiaomi.ErrNoSPEC
|
|
}
|
|
|
|
// "on" is required
|
|
siid, piid, ok := resolver.find("on")
|
|
if !ok {
|
|
return nil, xiaomi.ErrNotSupported
|
|
}
|
|
d.propOn = siidPiid{siid, piid, true}
|
|
|
|
// Optional properties
|
|
if s, p, ok := resolver.find("brightness"); ok {
|
|
d.propBrightness = siidPiid{s, p, true}
|
|
}
|
|
if s, p, ok := resolver.find("color-temperature"); ok {
|
|
d.propColorTemp = siidPiid{s, p, true}
|
|
}
|
|
if s, p, ok := resolver.find("color"); ok {
|
|
d.propColor = siidPiid{s, p, true}
|
|
}
|
|
|
|
return d, nil
|
|
}
|
|
|
|
func (d *lightDevice) TurnOn(ctx context.Context) error {
|
|
if !d.propOn.valid {
|
|
return xiaomi.ErrNotSupported
|
|
}
|
|
return d.SetProp(ctx, d.propOn.siid, d.propOn.piid, true)
|
|
}
|
|
|
|
func (d *lightDevice) TurnOff(ctx context.Context) error {
|
|
if !d.propOn.valid {
|
|
return xiaomi.ErrNotSupported
|
|
}
|
|
return d.SetProp(ctx, d.propOn.siid, d.propOn.piid, false)
|
|
}
|
|
|
|
func (d *lightDevice) Toggle(ctx context.Context) error {
|
|
on, err := d.isOn(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if on {
|
|
return d.TurnOff(ctx)
|
|
}
|
|
return d.TurnOn(ctx)
|
|
}
|
|
|
|
func (d *lightDevice) isOn(ctx context.Context) (bool, error) {
|
|
prop, err := d.GetProp(ctx, d.propOn.siid, d.propOn.piid)
|
|
if err != nil {
|
|
return false, err
|
|
}
|
|
if b, ok := prop.Value.(bool); ok {
|
|
return b, nil
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
func (d *lightDevice) HasBrightness() bool { return d.propBrightness.valid }
|
|
func (d *lightDevice) HasColorTemp() bool { return d.propColorTemp.valid }
|
|
func (d *lightDevice) HasColor() bool { return d.propColor.valid }
|
|
|
|
func (d *lightDevice) SetBrightness(ctx context.Context, pct int) error {
|
|
if !d.propBrightness.valid {
|
|
return xiaomi.ErrNotSupported
|
|
}
|
|
return d.SetProp(ctx, d.propBrightness.siid, d.propBrightness.piid, pct)
|
|
}
|
|
|
|
func (d *lightDevice) GetBrightness(ctx context.Context) (int, error) {
|
|
if !d.propBrightness.valid {
|
|
return 0, xiaomi.ErrNotSupported
|
|
}
|
|
prop, err := d.GetProp(ctx, d.propBrightness.siid, d.propBrightness.piid)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return toInt(prop.Value), nil
|
|
}
|
|
|
|
func (d *lightDevice) SetColorTemp(ctx context.Context, kelvin int) error {
|
|
if !d.propColorTemp.valid {
|
|
return xiaomi.ErrNotSupported
|
|
}
|
|
return d.SetProp(ctx, d.propColorTemp.siid, d.propColorTemp.piid, kelvin)
|
|
}
|
|
|
|
func (d *lightDevice) GetColorTemp(ctx context.Context) (int, error) {
|
|
if !d.propColorTemp.valid {
|
|
return 0, xiaomi.ErrNotSupported
|
|
}
|
|
prop, err := d.GetProp(ctx, d.propColorTemp.siid, d.propColorTemp.piid)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return toInt(prop.Value), nil
|
|
}
|
|
|
|
func (d *lightDevice) SetColor(ctx context.Context, r, g, b int) error {
|
|
if !d.propColor.valid {
|
|
return xiaomi.ErrNotSupported
|
|
}
|
|
colorVal := (r << 16) | (g << 8) | b
|
|
return d.SetProp(ctx, d.propColor.siid, d.propColor.piid, colorVal)
|
|
}
|
|
|
|
func (d *lightDevice) GetColor(ctx context.Context) (r, g, b int, err error) {
|
|
if !d.propColor.valid {
|
|
return 0, 0, 0, xiaomi.ErrNotSupported
|
|
}
|
|
prop, err := d.GetProp(ctx, d.propColor.siid, d.propColor.piid)
|
|
if err != nil {
|
|
return 0, 0, 0, err
|
|
}
|
|
val := toInt(prop.Value)
|
|
r = (val >> 16) & 0xFF
|
|
g = (val >> 8) & 0xFF
|
|
b = val & 0xFF
|
|
return r, g, b, nil
|
|
}
|
|
|
|
func (d *lightDevice) OnPropsChanged(handler func(props LightProps, changed string)) (string, error) {
|
|
type field struct {
|
|
siid, piid int
|
|
name string
|
|
fill func(*LightProps, interface{})
|
|
}
|
|
var fields []field
|
|
if d.propOn.valid {
|
|
fields = append(fields, field{d.propOn.siid, d.propOn.piid, "on",
|
|
func(s *LightProps, v interface{}) { s.On, _ = v.(bool) }})
|
|
}
|
|
if d.propBrightness.valid {
|
|
fields = append(fields, field{d.propBrightness.siid, d.propBrightness.piid, "brightness",
|
|
func(s *LightProps, v interface{}) { s.Brightness = toInt(v) }})
|
|
}
|
|
if d.propColorTemp.valid {
|
|
fields = append(fields, field{d.propColorTemp.siid, d.propColorTemp.piid, "color-temperature",
|
|
func(s *LightProps, v interface{}) { s.ColorTemperature = toInt(v) }})
|
|
}
|
|
if d.propColor.valid {
|
|
fields = append(fields, field{d.propColor.siid, d.propColor.piid, "color",
|
|
func(s *LightProps, 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(0, 0, func(did string, prop *xiaomi.PropertyValue) {
|
|
for _, f := range fields {
|
|
if f.siid == prop.SIID && f.piid == prop.PIID {
|
|
f.fill(&d.propsCache, prop.Value)
|
|
handler(d.propsCache, f.name)
|
|
return
|
|
}
|
|
}
|
|
})
|
|
}
|
|
|
|
// toInt converts an interface{} value to int.
|
|
func toInt(v interface{}) int {
|
|
switch n := v.(type) {
|
|
case float64:
|
|
return int(n)
|
|
case int:
|
|
return n
|
|
case int64:
|
|
return int(n)
|
|
case uint8:
|
|
return int(n)
|
|
case uint16:
|
|
return int(n)
|
|
case uint32:
|
|
return int(n)
|
|
case string:
|
|
i, _ := strconv.Atoi(n)
|
|
return i
|
|
}
|
|
return 0
|
|
}
|
|
|
|
func (d *lightDevice) GetProps() LightProps { return d.propsCache }
|
|
func (d *lightDevice) FetchProps(ctx context.Context) (LightProps, error) {
|
|
keys := []xiaomi.PropKey{}
|
|
if d.propOn.valid { keys = append(keys, xiaomi.PropKey{DID: d.info.DID, SIID: d.propOn.siid, PIID: d.propOn.piid}) }
|
|
if d.propBrightness.valid { keys = append(keys, xiaomi.PropKey{DID: d.info.DID, SIID: d.propBrightness.siid, PIID: d.propBrightness.piid}) }
|
|
if d.propColorTemp.valid { keys = append(keys, xiaomi.PropKey{DID: d.info.DID, SIID: d.propColorTemp.siid, PIID: d.propColorTemp.piid}) }
|
|
if d.propColor.valid { keys = append(keys, xiaomi.PropKey{DID: d.info.DID, SIID: d.propColor.siid, PIID: d.propColor.piid}) }
|
|
props, err := d.client.GetProps(ctx, keys)
|
|
if err != nil { return d.propsCache, err }
|
|
result := LightProps{}
|
|
for _, p := range props {
|
|
switch {
|
|
case d.propOn.valid && p.SIID == d.propOn.siid && p.PIID == d.propOn.piid: result.On, _ = p.Value.(bool)
|
|
case d.propBrightness.valid && p.SIID == d.propBrightness.siid && p.PIID == d.propBrightness.piid: result.Brightness = toInt(p.Value)
|
|
case d.propColorTemp.valid && p.SIID == d.propColorTemp.siid && p.PIID == d.propColorTemp.piid: result.ColorTemperature = toInt(p.Value)
|
|
case d.propColor.valid && p.SIID == d.propColor.siid && p.PIID == d.propColor.piid:
|
|
if v, ok := p.Value.(float64); ok { c := int(v); result.ColorRGB = [3]int{(c>>16)&0xFF, (c>>8)&0xFF, c&0xFF} }
|
|
}
|
|
}
|
|
d.propsCache = result
|
|
return result, nil
|
|
}
|