Skip to content

guowenhe/rtio-device-sdk-go

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

RTIO-DEVICE-SDK-GO

设备也是服务资源的提供者,RTIO采取REST-Like模型,使设备端开发具有和WEB服务开发相似的体验和高效。

RTIO-DEVICE-SDK-GO为Golang版SDK,用于连接RTIO服务,帮助设备端注册Handler(处理来自外部请求),并提供发送请求到RTIO代理的后端服务的方法。

开始使用

创建项目

mkdir ./hello-rtio
cd ./hello-rtio
go mod init hello-rtio

添加依赖

go get github.com/guowenhe/rtio-device-sdk-go/rtio

编写代码

添加文件 main.go,内容如下:

package main

import (
    "context"
    "log"
    "time"

    "github.com/guowenhe/rtio-device-sdk-go/rtio/devicesession"
)

func main() {
    serverAddr := "localhost:17017"
    deviceID := "cfa09baa-4913-4ad7-a936-3e26f9671b10"
    deviceSecret := "mb6bgso4EChvyzA05thF9+He"
    session, err := devicesession.Connect(context.Background(), deviceID, deviceSecret, serverAddr)
    if err != nil {
        log.Println(err)
        return
    }
    // URI: /greeter, CRC: 0xe5dcc140
    session.RegisterPostHandler(0xe5dcc140, func(req []byte) ([]byte, error) {
        log.Printf("received [%s] and reply [world]", string(req))
        return []byte("world"), nil

    })
    session.Serve(context.Background())

    // do other things
    time.Sleep(time.Minute * 30)
}

其中, 设备端URI /greeter对应的CRC为0xe5dcc140,可通过下面命令计算:

$ rtio-urihash -u "/greeter" -x
URI: /greeter, CRC: 0xe5dcc140

rtio-urihash命令安装:

go get github.com/guowenhe/rtio-urihash
go install github.com/guowenhe/rtio-urihash
$ rtio-urihash -h
Usage of rtio-urihash:
  -u string
        uri string, example: /uri/example
  -x    display digest with hex

运行

确定本地RTIO服务已经启动,可参考RTIO编译和运行。运行下面命令,以链接到RTIO服务:

go run main.go

请求设备

通过RTIO服务,请求设备端的/greeter接口,请求的data 为base64编码,可通过下面命令编码:

$ echo -n "hello" | base64
aGVsbG8=

通过curl发送Http请求:

$ curl -X POST http://127.0.0.1:17317/cfa09baa-4913-4ad7-a936-3e26f9671b10/post_handler -d '{"uri":"/greeter","id":12667,"data":"aGVsbG8="}'
{"id":12667, "code":"CODE_OK", "data":"d29ybGQ="}

其中响应的data 为base64编码,可通过下面命令解码:

$ echo -n "d29ybGQ=" | base64 -d
world

设备端输出:

$ go run main.go
2023/10/22 09:05:43 received [hello] and reply [world]

API列表

// 连接到RTIO服务 
func devicesession.Connect(ctx context.Context, deviceID, deviceSecret, serverAddr string) (*DeviceSession, error)

// 注册Get请求处理函数
(s *DeviceSession) RegisterGetHandler(uri uint32, handler func(req []byte) ([]byte, error)) error
// 注册Post请求处理函数 
func (s *DeviceSession) RegisterPostHandler(uri uint32, handler func(req []byte) ([]byte, error)) error
// 注册ObGet请求处理函数(客户端观察者模式 Observe-Get)
 (s *DeviceSession) RegisterObGetHandler(uri uint32, handler func(ctx context.Context, req []byte) (<-chan []byte, error)) error

// 启动服务
func (s *DeviceSession) Serve(ctx context.Context) error

// 发送Get请求到RTIO代理的设备服务(device-sevice)
func (s *DeviceSession) Get(uri uint32, Req []byte, timeout time.Duration) ([]byte, error) 
// 发送Get请求到RTIO代理的设备服务,带context
func (s *DeviceSession) GetWithContext(ctx context.Context, uri uint32, req []byte) ([]byte, error)
// 发送Post请求到RTIO代理的设备服务(device-sevice)
func (s *DeviceSession) Post(uri uint32, Req []byte, timeout time.Duration) ([]byte, error) 
// 发送Post请求到RTIO代理的设备服务,带context
func (s *DeviceSession) PostWithContext(ctx context.Context, uri uint32, req []byte) ([]byte, error)

 // 设置心跳间隔,默认300秒
func (s *DeviceSession) SetHeartbeatSeconds(n uint16) 

 // Trace级别设置,默认为1(0-不打印日志,1-关键信息,2-全部信息)
func rtio.SetTraceLevel(level uint32) 

About

device SDK for golang

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages