Skip to main content

Dummy 适配器

概述​

Dummy 适配器是 Octopus 一种用于测试和 Demo 的模拟适配器。

注册信息​

版本注册名称端点 Socket是否可用
v1alpha1adaptors.edge.cattle.io/dummydummy.sock*

支持模型​

类型设备组版本是否可用
DummySpecialDevicedevices.edge.cattle.iov1alpha1*
DummyProtocolDevicedevices.edge.cattle.iov1alpha1*

支持平台​

操作系统架构
linuxamd64
linuxarm
linuxarm64

使用方式​

kubectl apply -f https://raw.githubusercontent.com/cnrancher/octopus/master/adaptors/dummy/deploy/e2e/all_in_one.yaml

权限​

对 Octopus 授予权限,如下所示:

  Resources                                           Non-Resource URLs  Resource Names  Verbs
--------- ----------------- -------------- -----
dummyprotocoldevices.devices.edge.cattle.io [] [] [create delete get list patch update watch]
dummyspecialdevices.devices.edge.cattle.io [] [] [create delete get list patch update watch]
dummyprotocoldevices.devices.edge.cattle.io/status [] [] [get patch update]
dummyspecialdevices.devices.edge.cattle.io/status [] [] [get patch update]

YAML 示例​

指定DummySpecialDevice设备,以下示例假定客厅中有一个名为living-room-fan的风扇待连接。

```YAML
apiVersion: edge.cattle.io/v1alpha1
kind: DeviceLink
metadata:
name: living-room-fan
spec:
adaptor:
node: edge-worker
name: adaptors.edge.cattle.io/dummy
model:
apiVersion: "devices.edge.cattle.io/v1alpha1"
kind: "DummySpecialDevice"
# uses Secret resources
references:
- name: "ca"
secret:
name: "living-room-fan-mqtt-ca"
- name: "tls"
secret:
name: "living-room-fan-mqtt-tls"
template:
metadata:
labels:
device: living-room-fan
spec:
# integrates with MQTT
extension:
mqtt:
client:
server: tcps://test.mosquitto.org:8884
tlsConfig:
caFilePEMRef:
name: ca
item: ca.crt
certFilePEMRef:
name: tls
item: tls.crt
keyFilePEMRef:
name: tls
item: tls.key
serverName: test.mosquitto.org
insecureSkipVerify: true
message:
# uses dynamic topic with namespaced name
topic: "cattle.io/octopus/:namespace/:name"
protocol:
location: "living_room"
gear: slow
"on": true
```

指定一个 "DummyProtocolDevice "设备链接来连接 localhost 的 chaos robot。

```YAML
apiVersion: edge.cattle.io/v1alpha1
kind: DeviceLink
metadata:
name: localhost-robot
spec:
adaptor:
node: edge-worker
name: adaptors.edge.cattle.io/dummy
model:
apiVersion: "devices.edge.cattle.io/v1alpha1"
kind: "DummyProtocolDevice"
template:
metadata:
labels:
device: localhost-robot
spec:
protocol:
ip: "127.0.0.1"
properties:
name:
type: string
description: "The name (unique identifier) of the robot."
readOnly: true
gender:
type: object
description: "The gender of the robot."
objectProperties:
name:
type: string
description: "The name of the gender."
code:
type: int
description: "The code of the gender."
friends:
type: array
description: "The name list of the robot's friends."
arrayProperties:
type: string
description: "The name of the friend."
power:
type: float
description: "The power of the robot."
```

更多的 "DummyDevice"设备链接实例,请参考deploy/e2e目录。

DummySpecialDevice​

DummySpecialDevice可被视为模拟风扇。

参数描述类型是否必填
metadata元数据详情请参考metav1.ObjectMeta否
spec设备的期望状态DummySpecialDeviceSpec是
status设备的实际状态DummySpecialDeviceStatus否

DummySpecialDeviceSpec​

参数描述类型是否必填
extension设备是否有与 MQTT 插件基础DeviceExtensionSpec否
protocol访问设备时使用的传输协议DummySpecialDeviceProtocol是
on设备是否已经启动bool是
gear如果设备已启动,上报设备运转的频率DummySpecialDeviceGear否

DummySpecialDeviceStatus​

参数描述类型是否必填
extension集群使用的 MQTT 插件的配置DeviceExtensionStatus否
gear如果设备已启动,上报设备运转的频率DummySpecialDeviceGear否
rotatingSpeed设备的转速int32是

DummySpecialDeviceProtocol​

参数描述类型是否必填
location设备所处的位置string是

DummySpecialDeviceGear​

DummySpecialDeviceGear 定义了设备运行的速度。

参数描述类型是否必填
slow从 0 开始,每 3 秒增加一次,直至达到 100string否
middle从 100 开始,每 2 秒增加一次,直至达到 200string否
fast从 200 开始,每 1 秒增加一次,直至达到 300string否

DummyProtocolDevice​

您可以将DummyProtocolDevice 看成一个 chaos protocol robot,它的值每两秒会变化一次。

参数描述类型是否必填
metadata元数据metav1.ObjectMeta否
spec设备的期望状态DummyProtocolDeviceSpec是
status设备的实际状态DummyProtocolDeviceStatus否

DummyProtocolDeviceSpec​

参数描述类型是否必填
extension集群使用的 MQTT 插件的配置DeviceExtensionSpec否
protocol访问设备时使用的传输协议DummyProtocolDeviceProtocol是
props设备属性的期望值DummyProtocolDeviceSpecProps否

DummyProtocolDeviceStatus​

参数描述类型是否必填
extension集群使用的 MQTT 插件的配置DeviceExtensionStatus否
props设备属性的实际值map[string][dummyprotocoldevicestatusprops](#dummyprotocoldevicestatusprops)否

DummyProtocolDeviceProtocol​

参数描述类型是否必填
ip连接设备时用到的 ip 地址string是

DummyProtocolDeviceSpecProps​

说明:

  • DummyProtocolDeviceSpecObjectOrArrayProps和DummyProtocolDeviceSpecProps相同
  • 使用DummyProtocolDeviceSpecObjectOrArrayProps 的目的是避免对象循环引用
参数描述类型是否必填
type设备属性的类型,可选值包括:string、int、float、boolean、object 和 arrayDummyProtocolDevicePropertyType是
description属性描述string否
readOnly是否只读bool否
arrayProps数组类型的属性DummyProtocolDeviceSpecObjectOrArrayProps否
objectProps对象类型的属性[string][dummyprotocoldevicespecobjectorarrayprops](#dummyprotocoldevicespecprops)否

DummyProtocolDeviceStatusProperty​

说明:

  • DummyProtocolDeviceStatusObjectOrArrayProperty 和DummyProtocolDeviceStatusProperty相同
  • 使用DummyProtocolDeviceStatusObjectOrArrayProperty的目的是避免对象循环引用
参数描述类型是否必填
type设备属性的类型DummyProtocolDevicePropertyType是
intValue如果设备属性的类型是 int,上报 int 的值 Rint否
stringValue如果设备属性的类型是 string,上报 string 的值string否
floatValue如果设备属性的类型是 float,上报 float 的值resource.Quantity kubernetes-sigs/controller-tools/issues#245否
booleanValue如果设备属性的类型是 boolean,上报 boolean 的值boolean否
arrayValue如果设备属性的类型是 boolean,上报 array 的值DummyProtocolDeviceStatusObjectOrArrayProps否
objectValue如果设备属性的类型是 object,上报 object 的值DummyProtocolDeviceStatusObjectOrArrayProps否

DummyProtocolDevicePropertyType​

DummyProtocolDevicePropertyType 描述了设备属性的类型。

参数描述类型是否必填
stringint 类型属性的值string否
intint 类型属性的值string否
floatfloat 类型属性的值string否
booleanboolean 类型属性的值string否
arrayarray 类型属性的值string否
objectobject 类型属性的值string否

DummyDeviceExtension​

参数描述类型是否必填
mqtt说明 MQTT 插件的配置*v1alpha1.MQTTOptionsSpec是

Demo 演示​

  1. 创建一个DeviceLink以连接 DummySpecialDevice,该设备模拟客厅的风扇。

    kubectl apply -f https://raw.githubusercontent.com/cnrancher/octopus/master/adaptors/dummy/deploy/e2e/dl_specialdevice.yaml

    将上面创建的风扇状态同步到远程 MQTT 代理服务器。

    # create a Generic Secret to store the CA for connecting test.mosquitto.org.
    kubectl create secret generic living-room-fan-mqtt-ca --from-file=ca.crt=./test/integration/physical/testdata/mosquitto.org.crt

    # create a TLS Secret to store the TLS/SSL keypair for connecting test.mosquitto.org.
    kubectl create secret tls living-room-fan-mqtt-tls --key ./test/integration/physical/testdata/client-key.pem --cert ./test/integration/physical/testdata/client.crt

    # publish status to test.mosquitto.org
    kubectl apply -f https://raw.githubusercontent.com/cnrancher/octopus/master/adaptors/dummy/deploy/e2e/dl_specialdevice_with_mqtt.yaml

    使用mosquitto_sub工具观看同步状态。

    # get mqtt broker server
    kubectl get dummyspecialdevices.devices.edge.cattle.io living-room-fan -o jsonpath="{.status.extension.mqtt.client.server}"

    # get topic name
    kubectl get dummyspecialdevices.devices.edge.cattle.io living-room-fan -o jsonpath="{.status.extension.mqtt.message.topicName}"
    # use mosquitto_sub

    mosquitto_sub -h {the host of mqtt broker server} -p {the port of mqtt broker server} -t {the topic name}
    # mosquitto_sub -h test.mosquitto.org -p 1883 -t cattle.io/octopus/default/living-room-fan
  2. 创建一个DeviceLink以连接 DummyProtocolDevice,该设备模拟一个智能机器人,它可以在 2 秒内随机填充所需的属性。

    kubectl apply -f https://raw.githubusercontent.com/cnrancher/octopus/master/adaptors/dummy/deploy/e2e/dl_protocoldevice.yaml

    将以上创建的机械的答案同步到远程 MQTT 代理服务器。

    # publish status to test.mosquitto.org
    kubectl apply -f https://raw.githubusercontent.com/cnrancher/octopus/master/adaptors/dummy/deploy/e2e/dl_protocoldevice_with_mqtt.yaml

    使用mosquitto_sub工具观看同步的结果。

    # get mqtt broker server
    kubectl get dummyprotocoldevices.devices.edge.cattle.io localhost-robot -o jsonpath="{.status.extension.mqtt.client.server}"

    # get topic name
    kubectl get dummyprotocoldevices.devices.edge.cattle.io localhost-robot -o jsonpath="{.status.extension.mqtt.message.topicName}"

    # use mosquitto_sub
    mosquitto_sub -h {the host of mqtt broker server} -p {the port of mqtt broker server} -t {the topic name}
    # mosquitto_sub -h test.mosquitto.org -p 1883 -t cattle.io/octopus/835aea2e-5f80-4d14-88f5-40c4bda41aa3