一、HTTP API 接口标准

接口鉴权

应用smartassistant接口,需将用户凭证smart-assistant-token,放在http申请的header中。格局如下:
"smart-assistant-token":"xxx"

  1. 返回规范数据结构

smartassistant接口均返回JSON格局数据,格局如下:

{"status":0,  // 状态码"reason":"", // 状态码形容"data":{}    // 所有业务数据返回都蕴含在data对象中}
  1. 错误码列表

二、设施类插件实现

开发前先浏览插件设计概要:插件零碎设计技术概要

应用 plugin-sdk(code:/pkg/plugin/sdk) 能够疏忽不重要的逻辑,疾速实现插件

插件实现

1) 获取sdk

    go get github.com/zhiting-tech/smartassistant

2) 定义设施

sdk中提供了预约义的设施模型,应用模型能够不便SA无效进行治理和管制

设施物模型设计如下:

light_bulb灯泡

switch开关

outlet插座

info设施详情

curtain窗帘

package pluginimport (  "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/instance"  "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/attribute")type Device struct {  Light instance.LightBulb  Info0 instance.Info  // 依据理论设施性能组合定义}func NewDevice() *Device {  // 定义属性  lightBulb := instance.LightBulb{    Power:      attribute.NewPower(),    ColorTemp:  instance.NewColorTemp(), // 可选字段须要初始化能力应用    Brightness: nil,                      // 可选字段不初始化则不从接口中显示  }  // 定义设施根底属性  info := instance.Info{    Name:         attribute.NewName(),    Identity:     attribute.NewIdentity(),    Model:        attribute.NewModel(),    Manufacturer: attribute.NewManufacturer(),    Version:      attribute.NewVersion(),  }  return &Device{    Light: lightBulb,    Info0: info,  }}

3) 实现设施接口 定义好设施之后,须要为设施实现如下几个办法:

package mainimport (  "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/server")type Device interface {  Identity() string             // 获取设施惟一值  Info() server.DeviceInfo      // 设施详情  Setup() error                 // 初始化设施属性  Update() error                // 更新设施所有属性值  Close() error                 // 回收所有资源  GetChannel() server.WatchChan // 返回告诉channel}

实现如下:

package pluginimport (  "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/instance"  "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/attribute"  "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(), // 可选字段须要初始化能力使    Brightness: nil,                      // 可选字段不初始化则不从接口中显示  }  info := instance.Info{    Name:         attribute.NewName(),    Identity:     attribute.NewIdentity(),    Model:        attribute.NewModel(),    Manufacturer: attribute.NewManufacturer(),    Version:      attribute.NewVersion(),  }  return &Device{    Light: lightBulb,    Info0: info,  }}func (d *Device) Info() server.DeviceInfo {  // 该办法返回设施的次要信息  panic("implement me")}func (d *Device) Setup() error {  // 设置设施的属性和相干配置(比方设施id、型号、厂商等,以及设施的属性更新触发函数)  panic("implement me")}func (d *Device) Update() error {  // 该办法在获取设施所有属性值时调用,通过调用attribute.SetBool()等办法更新  panic("implement me")}func (d *Device) Close() error {  // 自定义退出相干资源的回收  panic("implement me")}func (d *Device) GetChannel() server.WatchChan {  // 返回WatchChan频道,用于状态变更推送  panic("implement me")}

4) 初始化和运行

定义好设施和实现办法后,运行插件服务(包含grpc和http服务)

package mainimport (  "log"  "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/server"  sdk "github.com/zhiting-tech/smartassistant/pkg/server/sdk")func main() {  p := server.NewPluginServer() // 插件服务名  go func() {    // 发现设施,并将设施增加到manager中    d := NewDevice()    p.Manager.AddDevice(d)  }()  err := sdk.Run(p)  if err != nil {    log.Panicln(err)  }}

这样服务就会运行起来,并通过SA的etcd地址0.0.0.0:2379注册插件服务, SA会通过etcd发现插件服务并且建设通道开始通信并且转发申请和命令。

接着就能够开始了。