快速开始

目标

通过几行关键代码,即可将“设备能力”以URI形式映射到云端或边缘端,可直接使用HTTP对设备访问、控制。

原理

                                                                 
   Device:PRINTER-001           Native │ Could or Edge                 
  ┌───────────────────────┐            │             ┌────────────┐ 
  │                       │            │             │            │ 
  │ /printer/action       │            │             │            │ 
  │ /printer/status       │  tcp/tls   │             │            │ 
  │                       │ ───────────┼───────────► │            │ 
  │                       │            │             │            │ 
  └───────────────────────┘            │             │            │ 
                                       │             │            │ 
   HTTPClient                          │             │            │ 
  ┌───────────────────────┐            │             │            │ 
  │ post $RTIO/PRINTER-001│            │             │            │ 
  │ req:                  │            │             │    rtio    │ 
  │ {                     │            │             │            │ 
  │   uri: /printer/action│            │             │            │ 
  │   data: cmd=start     │            │             │            │ 
  │ }                     │  http/https│             │            │ 
  │                       │ ───────────┼───────────► │            │ 
  │                       │ ◄─ ─ ─ ─ ─ ┼ ─ ─ ─ ─ ─ ─ │            │ 
  │ resp:                 │            │             │            │ 
  │ {                     │            │             │            │ 
  │   code: ok            │            │             │            │ 
  │   data: starting      │            │             │            │ 
  │ }                     │            │             │            │ 
  └───────────────────────┘            │             └────────────┘ 
   Phone/WEB/PC...                    

以上为示意图。

  • 物联网设备PRINTER-001与RTIO服务端建立起长连接。
  • HTTPClient向设备PRINTER-001的URI/printer/action发出请求。
  • RTIO将该请求转发到设备上并将设备处理结果通过HTTP响应传回给HTTPClient。

从调用端看,“设备能力”通过RTIO服务被映射到了云端,而不必关心其实现、通信等细节。

编译和运行

环境需要安装golang(1.21版本以上)和make工具。

git clone https://github.com/mkrainbow/rtio.git
cd rtio
make

成功编译后,out目录下会生成执行文件,主要文件如下。

$ tree out/
out/
├── examples
│   ├── certificates
│   ├── simple_device
│   └── simple_device_tls
├── rtio

运行RTIO服务端,可通过./out/rtio -h查看帮助。

$ ./out/rtio -disable.deviceverify -disable.hubconfiger -log.level=info
INF cmd/rtio/rtio.go:83 > rtio starting ...

另一终端模拟设备。显示如下日志为成功连接到RTIO服务。

$ ./out/examples/simple_device
INF internal/devicehub/client/devicesession/devicesession.go:660 > serving device_ip=127.0.0.1:47032 deviceid=cfa09baa-4913-4ad7-a936-3e26f9671b09
INF internal/devicehub/client/devicesession/devicesession.go:601 > verify pass

再打开一个终端,通过curl访问RTIO服务,请求到设备的URI/rainbow,将"hello"字符发给设备,设备响应"world"。

$ curl http://localhost:17917/cfa09baa-4913-4ad7-a936-3e26f9671b09 -d '{"method":"copost", "uri":"/rainbow","id":12667,"data":"aGVsbG8="}'
{"id":12667,"fid":0,"code":"OK","data":"d29ybGQ="}

其中,“aGVsbG8=“为"hello"的base64编码,“d29ybGQ=“为"world"的base64编码。可通过以下命令在终端里编解码。


$ echo -n "hello" | base64       # Encode
aGVsbG8=
$ echo -n "d29ybGQ=" | base64 -d # Decode
world

设备端代码

下面是Golang设备端实现。

import (
    "github.com/mkrainbow/rtio-device-sdk-go/rtio"
)

func main() {

    // Connect to rtio service.
    session, err := rtio.Connect(context.Background(), *deviceID, *deviceSecret, *serverAddr)

    // ...
    
    // Register handler for URI.
    session.RegisterCoPostHandler("/rainbow", func(req []byte) ([]byte, error) {
        log.Printf("received [%s] and reply [world]", string(req))
        return []byte("world"), nil
    })

    // Session serve in the background.
    session.Serve(context.Background())

    // Do other things.
    time.Sleep(time.Hour * 8760)
}

物联网设备端SDK

Golang

RTIO设备端SDK,Golang版:

通常适合运行完整Linux的设备,比如ARM32位及以上单板。

C

RTIO设备端SDK,C语言版:

适合资源受限设备,比如运行在FreeRTOS等实时操作系统上。