此文档形容如何开发一个简略插件,面向插件开发者。1.插件实现1.1获取sdk
go get github.com/zhiting-tech/smartassistant1.2定义设施
package pluginimport ( "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/attribute" "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/instance" "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/server")type Device struct { Light instance.LightBulb Info0 instance.Info // 依据理论设施性能组合定义}func NewDevice() *Device { // 定义属性 lightBulb := instance.LightBulb{ Power: attribute.NewPower(), ColorTemp: attribute.NewColorTemp(), // 依据须要初始化可选字段 } info := instance.Info{ Identity: attribute.NewIdentity(), Model: attribute.NewModel(), Manufacturer: attribute.NewManufacturer(), Version: attribute.NewVersion(), } return &Device{ Light: lightBulb, Info0: info, }}1.3实现设施接口
package pluginimport ( "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/attribute" "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/instance" "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/server")type Device struct { LightBulb instance.LightBulb Info0 instance.Info // 依据理论设施性能组合定义 identity string ch server.WatchChan}func NewDevice(identity string) *Device { // 定义设施属性 lightBulb := instance.LightBulb{ Power: attribute.NewPower(), ColorTemp: instance.NewColorTemp(), Brightness: instance.NewBrightness(), } // 定义设施根底属性 info := instance.Info{ Name: attribute.NewName(), Identity: attribute.NewIdentity(), Model: attribute.NewModel(), Manufacturer: attribute.NewManufacturer(), Version: attribute.NewVersion(), } return &Device{ LightBulb: lightBulb, Info0: info, identity: identity, ch: make(chan server.Notification, 5), }}func (d *Device) Info() server.DeviceInfo { // 该办法返回设施的次要信息 return d.identity}func (d *Device) update(attr string) attribute.UpdateFunc { return func(val interface{}) error { switch attr { case "power": d.LightBulb.Power.SetString(val.(string)) case "brightness": d.LightBulb.Brightness.SetInt(val.(int)) case "color_temp": d.LightBulb.ColorTemp.SetInt(val.(int)) } n := server.Notification{ Identity: d.identity, InstanceID: 1, Attr: attr, Val: val, } select { case d.ch <- n: default: } return nil }}func (d *Device) Setup() error { // 设置设施的属性和相干配置(比方设施id、型号、厂商等,以及设施的属性更新触发函数) d.Info0.Identity.SetString("123456") d.Info0.Model.SetString("model") d.Info0.Manufacturer.SetString("manufacturer") d.LightBulb.Brightness.SetRange(1, 100) d.LightBulb.ColorTemp.SetRange(1000, 5000) // 给属性设置更新函数,在执行命名时,该函数会被执行 d.LightBulb.Power.SetUpdateFunc(d.update("power")) d.LightBulb.Brightness.SetUpdateFunc(d.update("brightness")) d.LightBulb.ColorTemp.SetUpdateFunc(d.update("color_temp")) return nil}func (d *Device) Update() error { // 该办法在获取设施所有属性值时调用,通过调用attribute.SetBool()等办法更新 // d.LightBulb.Power.SetString("on") // d.LightBulb.Brightness.SetInt(100) // d.LightBulb.ColorTemp.SetInt(2000) return nil}func (d *Device) Close() error { // 自定义退出相干资源的回收 close(d.ch) return nil}func (d *Device) GetChannel() server.WatchChan { // 返回WatchChan频道,用于状态变更推送 return d.ch}1.4初始化和运行
...