Kubeedge - 4: 玩转 Device twin

什么是 Device twin

Device twin

  • 设备的动态属性,在 KubeEdge 中的设备孪生 包含了desired value(期望值)reported value(真实值)
  • 在 KubeEdge 利用 Device Model 和 Device Instance 的机制,通过一套 Device Model 生成大量 Device Instance 的机制实现关联管理

Device Commpents in KubeEdge


Membership Module

  • 该模块主要负责绑定新加入的设备与指定的边缘节点(其实就是NodeSelector的体现)。比如温度传感器关联在边缘节点node-A上,蓝牙音箱关联在了节点node-B上,如果云端要控制蓝牙音箱,那就要把数据准确的推到node-B上

Twin Module

  • 该模块主要负责所有设备孪生相关的操作。比如,设备孪生更新(device twin update)、设备孪生获取(device twin get)和设备孪生同步至云端(device twin sync-to-cloud)

Communication Module

  • 该模块主要负责各个子模块之间的通信

Device Module

  • 该模块主要负责执行设备相关的操作,比如处理设备状态(device status)更新和设备属性(device attribute)更新。
    (PS. 如果不搞清楚“设备属性”、“设备状态”和“设备孪生”的概念,那么初学者很容易混淆Twin Module和Device Module这两个子模块的功能职责。事实上,我不太明白如此设计的原因是什么,总觉得这部分的设计稍有欠妥)
  • 此外,DeviceTwin模块会在本地数据库(SQLite)中创建3张表,即Device Table、Device Attribute Table和Device Twin Table,分别记录设备的基本信息、设备的属性信息和设备孪生信息。表的具体字段设计详见这里。

MoreInfo Device Model

  • 如何定义Device Model
    一个 Device Model 的资源文件大概是:
    apiVersion: devices.kubeedge.io/v1alpha2
    kind: DeviceModel
    metadata:
    name: sensor-tag-model
    namespace: default
    spec:
    properties:
    - name: temperature
    description: temperature in degree celsius
    type:
    int:
    accessMode: ReadWrite
    maximum: 100
    unit: degree celsius
    - name: temperature-enable
    description: enable data collection of temperature sensor
    type:
    string:
    accessMode: ReadWrite
    defaultValue: 'OFF'

MoreInfo Device Instance

一个 Device Instance 大概是:

apiVersion: devices.kubeedge.io/v1alpha2
kind: Device
metadata:
name: sensor-tag-instance-01
labels:
description: TISimplelinkSensorTag
manufacturer: TexasInstruments
model: CC2650
spec:
deviceModelRef:
name: sensor-tag-model
protocol:
modbus:
slaveID: 1
common:
com:
serialPort: '1'
baudRate: 115200
dataBits: 8
parity: even
stopBits: 1
nodeSelector:
nodeSelectorTerms:
- matchExpressions:
- key: ''
operator: In
values:
- node1
propertyVisitors:
- propertyName: temperature
modbus:
register: CoilRegister
offset: 2
limit: 1
scale: 1
isSwap: true
isRegisterSwap: true
- propertyName: temperature-enable
modbus:
register: DiscreteInputRegister
offset: 3
limit: 1
scale: 1.0
isSwap: true
isRegisterSwap: true
status:
twins:
- propertyName: temperature
reported:
metadata:
timestamp: '1550049403598'
type: int
value: '10'
desired:
metadata:
timestamp: '1550049403598'
type: int

Data Topic

  • 在KubeEdge v1.4之后,也添加了可以在边缘端的Data topic模块
    apiVersion: devices.kubeedge.io/v1alpha1
    kind: Device
    metadata:
    ...
    spec:
    deviceModelRef:
    ...
    protocol:
    ...
    nodeSelector:
    ...
    propertyVisitors:
    ...
    data:
    dataTopic: "$ke/events/device/+/data/update"
    dataProperties:
    - propertyName: pressure
    metadata:
    type: int
    - propertyName: temperature
    metadata:
    type: int

Topic 汇总

topic 发布者(publish) 订阅者(subscribe) 用途简介 mapper是否必须实现
$hw/events/node/+/membership/updated edgecore mapper 订阅设备列表的变化 建议实现
$hw/events/node/+/membership/get mapper edgecore 查询设备列表 建议实现
$hw/events/node/+/membership/get/result edgecore mapper 获取查询设备列表的结果 建议实现
$hw/events/device/+/updated edgecore mapper 订阅设备属性描述的变化
$hw/events/device/+/twin/update/result edgecore mapper 获取设备属性更新是否成功 建议实现
$hw/events/device/+/twin/update/delta edgecore mapper 获取设备属性更新的值
$hw/events/device/+/twin/update/document edgecore mapper 获取设备属性更新的操作记录
$hw/events/device/+/twin/get/result edgecore mapper、apps 返回获取设备属性的值
$hw/events/device/+/twin/update mapper edgecore 通知设备属性的值更新
$hw/events/device/+/twin/get mapper,apps edgecore 获取设备属性的值
$hw/events/device/+/state/update mapper edgecore 通知设备状态更新 建议实现
$hw/events/device/+/state/update/result edgecore mapper 获取设备状态更新结果
$ke/events/device/+/data/update mapper apps 获取设备属性的时序数据
$hw/events/upload/# 暂无 edgecore 转发云端
SYS/dis/upload_records 暂无 edgecore 转发云端

致谢/参考