自定义插件连接器 - 如何轻松集成您的 IoT 传感器

Highlighted
发布自 TeamViewer员工
TeamViewer员工

自定义插件连接器 - 如何轻松集成您的 IoT 传感器

This article applies to all TeamViewer IoT users who need to connect and monitor the IoT sensor network.

The solution described in this article requires TeamViewer IoT Agent version 1.1.102 (or later). To determine your agent version, run sudo teamviewer-iot-agent info  (If an error or no result indicates that the installed agent is an older version ).

The IoT environment is made up of different communities, some of whom like to write code, and some who don't like to write code. For those who don't like to write code, this guide makes it easy to integrate IoT sensors with The TeamViewer IoT agent without writing too much code.

This guide is for the following users:

  1. Install the TeamViewer IoT Agent (version 1.1.12 or higher)
  2. Want to monitor sensor metrics
  3. Want to interact directly with the TeamViewer IoT proxy API

Overview

This guide guides you through the integration of your own scripts (for retrieving sensor/metric values) with the TeamViewer IoT agent.  You can easily register sensor metrics with an agent and call your monitoring script at defined intervals to retrieve metric values.

Step 1 - Create a custom monitoring script

Before you integrate the sensor into the TeamViewer IoT agent, you need to define the metrics you want to monitor and be able to write shell scripts to retrieve from the sensor.

This guide does not describe how to write a shell script, but it does determine the script requirements to integrate the script into the TeamViewer IoT agent.

Your custom script:

  • The current value of the sensor metric should be retrieved
  • You want to monitor metrics should be printed to the console (JSON format) { Key: value }, where key is the index identifier, the value of the current value of the index.
    • For example, if you use a sensor that measures temperature and humidity, the script output will be {temperature: 23,humidity: 34} 
  • 可能存在输入参数
  • 应保存在/位于运行 TeamViewer IoT 代理的设备上
    • 例如 /usr/local/teamviewer-iot-agent/monitoring/temperature_humidity/your_monitoring_script.sh

下方通用示例显示的脚本可能与两个示例输入参数类似(传感器 ID 和您的 SDK 凭据)- 这两个参数将通过以下步骤执行:

./your_monitoring_script.sh sensor123 mypassword
your_monitoring_script.sh
--------------------------------------
var sensorId = args[0]; var credentialForSDK = args[1];var sensorSDK = new SensorSDK(credentialForSDK);var humidity = sensorSDK.getHumidity(sensorId);var temperature - sensorSDK.getTemperature(sesnorId);print{temperature:temperature_value, humidity:humidity_value}

在继续下一步之前,运行脚本并确保将有效的 JSON 打印到控制台。您可使用任何免费的在线 JSON 验证器确保输出是有效的 JSON。

 

第 2 步 - 将您的脚本与 TeamViewer IoT 代理集成

TeamViewer IoT 代理包括其自身的系统监视配置文件。 此配置文件将扩展为包含自定义脚本。 若您要手动编辑系统配置文件,请确保创建备份文件,以防文件因用户出错而受到破坏。

  1. 打开并编辑位于下列位置的 TeamViewer IoT 代理监控配置文件:
    • /var/lib/teamviewer-iot-agent/system_monitors.conf
    • 托管的传感器位于配置文件传感器节点下方
  2. 对于要集成的每个传感器(例如温度和湿度、亮度、压力等),必须将其注册信息附加到配置文件中
  3. 保存配置文件。
  4. 重新启动代理,重新加载配置文件。

    • teamviewer-iot-agent restart

  5. 登录 TeamViewer IoT 仪表板并验证添加到配置文件的传感器/指标当前是否出现在您的指标 选项卡中。

    • 将指标固定到您的仪表板中,并确保数据按照配置文件中指定的频率进行更新

 

JSON 配置文件示例:

{
sensors: [{ name: temperature_humidity_sensor1, monitoringService: /usr/local/teamvieweriot/sensors/temperature_humidity/monitor.sh, monitoringParams: sensor1 sdkCredential, frequency: 10, metrics: [{ name: Temperature, key: temperature, valueType: double, valueAnnotation: C }, { name: Humidity, key: humidity, valueType: double, valueAnnotation: }] }, ... ] }

 

JSON 模式

字段

类型

必填

说明

传感器:

JSON 对象

 

 

名称

字符串

显示传感器名称。

monitoringService

字符串

自定义监控脚本的完整路径。

频率

整数(秒)

若已设定频率,TeamViewer IoT 代理将按照指定间隔(例如每 10 秒)运行脚本。

若未设定频率,TeamViewer IoT 代理将在启动后运行脚本。 此处假定在自定义监控脚本中组织调度程序,并在指标的新值变化时进行打印。请注意,每条新 JSON 消息均应在新行中打印。

monitoringParams

字符串

发送到该脚本的其他参数。例如 url、host、sensorId 等。当您要使用相同的脚本监视同类型的多个传感器时,该字段非常有用。

 

 

 

 

指标

JSON 阵列

 

 

名称

字符串

 

显示指标名称

字符串

 

指标的短名称(标识符)。应作为监控脚本 JSON 输出中的键。

valueType

Enum

 

表示指标类型。可能的值是:

  • 整数
  • 双数
  • 字符串

valueAnnotation

字符串

 

在图表上显示指标值时,度量单位将显示为 Y 轴名称。

常用的注释是:%、C、Pa、req/sec、MB 等

 
监控配置文件
请注意,破坏监控配置文件可能导致提供的所有操作系统 (OS) 传感器/指标丢失。 为能够恢复损坏的文件,请确保在编辑之前备份配置文件。此外,请注意,在通过代理注册传感器/指标之后,代理将自动生成 sensorId 和 metricId 参数并将其作为属性添加到配置文件中。更改这些 ID 会导致历史数据丢失。 
 
监控频率
可通过代理配置文件或自定义脚本控制监控频率(频率参数) 。在 JSON 传感器示例中, 在配置属性中指定频率,从而使代理每隔 10 秒运行一次 脚本。在某些情况下,数据采集频率应通过脚本设定,而非在代理监视配置 文件中指定。 在这些情况下,配置 文件中的频率参数应留空。下方描述了两个用例:
  • You want to monitor discrete events (event-based metrics) for undefined frequencies;
    • For example, when monitoring is performed within a custom script when opening or closing a door
    • You can subscribe to the message queue, get the door move event and print to stdout.
    • Note: Each message/event should be printed to a new line
    • General example of use case 1:
      • Var queue = new Queue(); queue.openConnection(); queue.createSubscription(callback); function callback(value) { print {key:value}; }
  • You want to set the monitoring frequency to a high frequency (for example, 1 second). although it can be monitored by the agent 
The frequency parameter is configured in the configuration file for this purpose, but the best solution is to use a loop in the script to print a new indicator value every 1 second.
  • General example of use case 2:
    Var networkObject = new NetworkObject(); networkObject.openConnection(); try { while (true) { var value = networkObject.getValue(); print {key:value}; sleep(1000); } } Finally { connection.close( ) }