sdk

package
v1.9.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2022 License: Apache-2.0 Imports: 16 Imported by: 0

README

plugin-sdk

proto gen
go install google.golang.org/protobuf/cmd/protoc-gen-go

go get -u github.com/golang/protobuf/{proto,protoc-gen-go}

protoc --proto_path=:./proto ./proto/*.proto  --go_out=plugins=grpc:./proto
插件实现
  1. 获取sdk
go get github.com/zhiting-tech/smartassistant
  1. 定义设备
package plugin

import "github.com/zhiting-tech/smartassistant/pkg/plugin/sdk/instance"

type Device struct {
	Light instance.LightBulb
	Info0 instance.Info
	// 根据实际设备功能组合定义
}

func NewDevice() *Device {
	return &Device{
		Light: instance.NewLightBulb(),
		Info0: instance.NewInfo(),
	}
}
  1. 实现设备接口

package plugin

import (
	"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 {
	return &Device{
		Light: instance.NewLightBulb(),
		Info0: instance.NewInfo(),
	}
}

func (d *Device) Info() plugin.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() plugin.WatchChan {
	// 返回WatchChan频道,用于状态变更推送
	panic("implement me")
}

  1. 初始化和运行
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 := plugin.NewPluginServer("demo")
	go func() {
		// 发现设备
		d := NewDevice()
		p.Manager.AddDevice(d)
	}()
	err := sdk.Run(p)
	if err != nil {
		log.Panicln(err)
	}
}
镜像编译和部署

暂时仅支持以镜像方式安装插件,调试正常后,编译成镜像提供给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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Run

func Run(p *server.Server) error

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL