Thingsboard Gateway开发环境

源码下载地址:https://github.com/thingsboard/thingsboard-gateway

国内大神源码地址:https://github.com/guodaxia103/thingsboard-gateway

 

目前在国内有文章写出,Thingsboard Gateway是管理设备使用的,一个Thingsboard Gateway可以管理1000个设备,请参考:https://blog.csdn.net/Zzhou1990/article/details/100106995?tdsourcetag=s_pcqq_aiomsg

Thingsboard Gateway开发环境_第1张图片

 

 

 

 

 

 

 

前提是Thingsboard开发环境搭建,请参考《Thingsboard开发环境搭建及编译项目》,因为Thingsboard Gateway也跟它相关

为了对Thingsboard有个概念,可以参考官方文档:https://thingsboard.io/docs/iot-gateway/what-is-iot-gateway/

Thingsboard Gateway开发环境_第2张图片

 

 

 

下载源码,git bash命令

git clone https://github.com/guodaxia103/thingsboard-gateway

 

Thingsboard Gateway开发环境_第3张图片

 

 

使用管理员身份打开命令行工具,并进入到项目目录

mvn clean install -Dmaven.test.skip=true

出现以下错误

E:\tb\thingsboard-gateway>mvn clean install -Dmaven.test.skip=true
[INFO] Scanning for projects...
[INFO]
[INFO] ----------------------< org.thingsboard:gateway >-----------------------
[INFO] Building Thingsboard Gateway 2.3.1-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[WARNING] The POM for org.thingsboard.common:data:jar:2.4.1-SNAPSHOT is missing, no dependency information available
[WARNING] The POM for org.thingsboard:netty-mqtt:jar:2.4.1-SNAPSHOT is missing, no dependency information available
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.575 s
[INFO] Finished at: 2019-09-26T13:35:35+08:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal on project gateway: Could not resolve dependencies for project org.thingsboard:gateway:jar:2.3.1-SNAPSHOT: The following artifacts could not be resolved: org.thingsboard.common:data:jar:2.4.1-SNAPSHOT, org.thingsboard:netty-mqtt:jar:2.4.1-SNAPSHOT: Could not find artifact org.thingsboard.common:data:jar:2.4.1-SNAPSHOT -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/DependencyResolutionException

错误截图

Thingsboard Gateway开发环境_第4张图片

 

原因是Thingsboard Gateway依赖的Thingsboard的版本不匹配

解决方案参考官方Issue:https://github.com/thingsboard/thingsboard-gateway/issues/26#issuecomment-381832965

首先,我们已经有了Thingsboard的开发环境,查看一下Thingsboard版本,这里是2.3.1

Thingsboard Gateway开发环境_第5张图片

 

修改Thingsboard Gateway源码中的pom.xml文件,记得也修改为2.3.1

Thingsboard Gateway开发环境_第6张图片

 

再次尝试编译

Thingsboard Gateway开发环境_第7张图片

等待几分钟最终编译成功

Thingsboard Gateway开发环境_第8张图片

 

编译成功后,在源码的目录下会生成target文件夹,里面的文件即是打包好的结果

其中tb-gateway-windows.zip是针对Windows平台的安装包

Thingsboard Gateway开发环境_第9张图片

 

目前的状态如下

Thingsboard Gateway开发环境_第10张图片

 

启动Thingsboard(在运行Thingsboard Gateway前,假使你已经知道了Thingsboard平台是如何运行的,也已经正常的编译成功源代码了)

Thingsboard Gateway开发环境_第11张图片

 

使用租户管理员,登陆到系统,并创建一个设备,并指定为网关

Thingsboard Gateway开发环境_第12张图片

 

进入到设备的属性中,将Access Token复制出来备用

Thingsboard Gateway开发环境_第13张图片

 

目前的状态如下

Thingsboard Gateway开发环境_第14张图片

 

Windows尝试安装Thingsboard Gateway,首先解压文件

Thingsboard Gateway开发环境_第15张图片

 

打开Thingsboard Gateway目录下conf配置文件

Thingsboard Gateway开发环境_第16张图片

修改配置文件内容

 Thingsboard Gateway开发环境_第17张图片

 

 就使用刚才编译代码时的管理员身份打开的命令行窗口

#进入到指定的目录
cd target\tb-gateway-windows\tb-gateway

#安装网关服务 
install.bat

#开始网关服务
net start tb-gateway

运行截图

Thingsboard Gateway开发环境_第18张图片

 

目前的状态如下

Thingsboard Gateway开发环境_第19张图片

 

 

进入到Thingsboard,查看网关的最后遥测数据,只要出现以下参数变为0,即Thingsboard Gateway与Thingsboard平台已经连接上了

#All values should be set to “0”(以下三个属性应该都是0)
devicesOnline:0
attributesUploaded:0
telemetryUploaded:0

浏览器界面

Thingsboard Gateway开发环境_第20张图片

 

目前的状态如下

Thingsboard Gateway开发环境_第21张图片

 

尝试一下Thingsboard Gateway的功能先,首先使用最常用的MQTT协议

为了测试,我在本机也搭建了一个mosquitto环境,另外,也修改了mosquitto的端口为1884(找到文件C:\Program Files\mosquitto\mosquitto.conf,找到节点port 1883,将1883修改成1884,并将前面的#删除,重启mosquitto服务让配置生效)

官方参考配置文件地址:https://thingsboard.io/docs/iot-gateway/resources/mqtt-gui-extension-configuration.json

文件内容:

[
  {
    "id": "mqtt",
    "type": "MQTT",
    "configuration": {
      "brokers": [
        {
          "host": "127.0.0.1",
          "port": 1884,
          "ssl": false,
          "retryInterval": 3000,
          "credentials": {
            "type": "anonymous"
          },
          "mapping": [
            {
              "topicFilter": "sensors",
              "converter": {
                "type": "json",
                "filterExpression": "$",
                "deviceNameJsonExpression": "${$.serialNumber}",
                "deviceTypeJsonExpression": "${$.model}",
                "attributes": [
                  {
                    "type": "string",
                    "key": "model",
                    "value": "${$.model}"
                  }
                ],
                "timeseries": [
                  {
                    "type": "double",
                    "key": "temperature",
                    "value": "${$.temperature}"
                  }
                ]
              }
            },
            {
              "topicFilter": "sensor/+/temperature",
              "converter": {
                "type": "json",
                "filterExpression": "$",
                "deviceNameTopicExpression": "(?<=sensor/)(.*?)(?=/temperature)",
                "timeout": 60000,
                "timeseries": [
                  {
                    "type": "double",
                    "key": "temperature",
                    "value": "${$.value}"
                  }
                ]
              }
            }
          ],
          "connectRequests": [
            {
              "topicFilter": "sensors/connect",
              "deviceNameJsonExpression": "${$.serialNumber}"
            },
            {
              "topicFilter": "sensor/+/connect",
              "deviceNameTopicExpression": "(?<=sensor/)(.*?)(?=/connect)"
            }
          ],
          "disconnectRequests": [
            {
              "topicFilter": "sensors/disconnect",
              "deviceNameJsonExpression": "${$.serialNumber}"
            },
            {
              "topicFilter": "sensor/+/disconnect",
              "deviceNameTopicExpression": "(?<=sensor/)(.*?)(?=/disconnect)"
            }
          ],
          "attributeRequests": [
            {
              "topicFilter": "sensors/attributes",
              "deviceNameJsonExpression": "${$.serialNumber}",
              "attributeKeyJsonExpression": "${$.key}",
              "requestIdJsonExpression": "${$.requestId}",
              "clientScope": false,
              "responseTopicExpression": "sensors/${deviceName}/attributes/${responseId}",
              "valueExpression": "{\"${attributeKey}\":\"${attributeValue}\"}"
            },
            {
              "topicFilter": "sensors/+/attributes/+/request/+",
              "deviceNameTopicExpression": "(?<=sensors/)(.*?)(?=/attributes)",
              "attributeKeyTopicExpression": "(?<=attributes/)(.*?)(?=/request)",
              "requestIdTopicExpression": "(?<=request/)(.*?)($)",
              "clientScope": false,
              "responseTopicExpression": "sensors/${deviceName}/attributes/${attributeKey}/response/${requestId}",
              "valueExpression": "${attributeValue}"
            }
          ],
          "attributeUpdates": [
            {
              "deviceNameFilter": ".*",
              "attributeFilter": ".*",
              "topicExpression": "sensor/${deviceName}/${attributeKey}",
              "valueExpression": "{\"${attributeKey}\":\"${attributeValue}\"}"
            }
          ],
          "serverSideRpc": [
            {
              "deviceNameFilter": ".*",
              "methodFilter": "echo",
              "requestTopicExpression": "sensor/${deviceName}/request/${methodName}/${requestId}",
              "responseTopicExpression": "sensor/${deviceName}/response/${methodName}/${requestId}",
              "responseTimeout": 10000,
              "valueExpression": "${params}"
            },
            {
              "deviceNameFilter": ".*",
              "methodFilter": "no-reply",
              "requestTopicExpression": "sensor/${deviceName}/request/${methodName}/${requestId}",
              "valueExpression": "${params}"
            }
          ]
        }
      ]
    }
  }
]
View Code

 

进入到Thingsboard的网关中,查看配置,点击上传

Thingsboard Gateway开发环境_第22张图片

 

将下载的json配置文件拖动到上传框,并点击上传

Thingsboard Gateway开发环境_第23张图片

 

可以看到网关的状态为同步状态

Thingsboard Gateway开发环境_第24张图片

查看其中的配置内容,请留意,现在的MQTT服务器是127.0.0.1,端口是1884,使用的就是本机安装mosquitto服务器

Thingsboard Gateway开发环境_第25张图片

 

MQTTBox(下载地址:http://workswithweb.com/mqttbox.html)

Thingsboard Gateway开发环境_第26张图片

 

创建一个连接,直接连接到本机的mosquitto上

Thingsboard Gateway开发环境_第27张图片

 

然后在MQTTBox上发布一条消息,点击发送

#主题
sensors
#消息体
{"serialNumber":"SN-001", "model":"T1000", "temperature":36.6}

Thingsboard Gateway开发环境_第28张图片

 

刷新Thingsboard设备的列表,可以看到多了一个SN-001的设备

Thingsboard Gateway开发环境_第29张图片

并且新加入的设置也是有遥测值的

Thingsboard Gateway开发环境_第30张图片

 

新设备与网关进行了关联

Thingsboard Gateway开发环境_第31张图片

如果需要设置设备在线或离线状态,需要发送主题sensor/设备编号/connect(离线,需要发送主题sensor/设备编号/disconnect

Thingsboard Gateway开发环境_第32张图片

 

 

 

 

 

 

 

目前的状态如下

Thingsboard Gateway开发环境_第33张图片

 

 

即然本地MQTT Broker是可以的,那么远程的MQTT Broker呢?是否可用呢

在Thingsboard中修改网关的MQTT设置,并保存

Thingsboard Gateway开发环境_第34张图片

 

再次使用MQTTBox测试

Thingsboard Gateway开发环境_第35张图片

 

 

在Thingsboard设备列表中又增加了SN-002的设备,且已经有了遥测值

Thingsboard Gateway开发环境_第36张图片

 

 

 

 

 

 

 

提醒,这里提醒一下,Thingsboard Gateway需要与Thingsboard配套使用,如果版本不同,则无法进行处理 

 

 

 

 

 

 

 

 

 

 

 

 

 

最终理想的状态

Thingsboard Gateway开发环境_第37张图片

 

你可能感兴趣的:(Thingsboard Gateway开发环境)