关于智能家居:智能家居开源生态正确的HTTP-API-接口规范以及设备类插件实现

6次阅读

共计 2896 个字符,预计需要花费 8 分钟才能阅读完成。

一、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 plugin

import (
  "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 main

import ("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 plugin

import (
  "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 main

import (
  "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 发现插件服务并且建设通道开始通信并且转发申请和命令。

接着就能够开始了。

正文完
 0