关于智能家居:智能家居开源平台智汀家庭云开发插件

5次阅读

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


此文档形容如何开发一个简略插件,面向插件开发者。
1. 插件实现
1.1 获取 sdk

    go get github.com/zhiting-tech/smartassistant

1.2 定义设施

package plugin

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

import (
  "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 初始化和运行

package main

import (
  "log"

  "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/server"
  "github.com/zhiting-tech/smartassistant/pkg/server/sdk"
)

func main() {p := server.NewPluginServer("demo")
  go func() {
    // 发现设施
    d := NewDevice("abcdefg")
    p.Manager.AddDevice(d)
  }()
  err := sdk.Run(p)
  if err != nil {log.Panicln(err)
  }
}

2. 开发范例
demo-plugin : 通过上文的插件实现教程实现的示例插件;这是一个模仿设施写的一个简略插件服务,不依赖硬件,实现了外围插件的性能

3. 镜像编译和部署
临时仅反对以镜像形式装置插件,调试失常后,编译成镜像提供给 SA
·Dockerfile 示例参考

FROM golang:1.16-alpine as builder
RUN apk add build-base
COPY . /app
WORKDIR /app
RUN go env -w GOPROXY="goproxy.cn,direct"
RUN go build -ldflags="-w -s" -o demo-plugin

FROM alpine
WORKDIR /app
COPY --from=builder /app/demo-plugin /app/demo-plugin

# static file
COPY ./html ./html
ENTRYPOINT ["/app/demo-plugin"]

·编译镜像

docker build -f your_plugin_Dockerfile -t your_plugin_name

·运行插件

docker run -net=host your_plugin_name

// 留神:-net=host 参数只有 linux 环境才有用。
正文完
 0