源码下载地址: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开发环境搭建,请参考《Thingsboard开发环境搭建及编译项目》,因为Thingsboard Gateway也跟它相关
为了对Thingsboard有个概念,可以参考官方文档:https://thingsboard.io/docs/iot-gateway/what-is-iot-gateway/
下载源码,git bash命令
git clone https://github.com/guodaxia103/thingsboard-gateway
使用管理员身份打开命令行工具,并进入到项目目录
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依赖的Thingsboard的版本不匹配
解决方案参考官方Issue:https://github.com/thingsboard/thingsboard-gateway/issues/26#issuecomment-381832965
首先,我们已经有了Thingsboard的开发环境,查看一下Thingsboard版本,这里是2.3.1
修改Thingsboard Gateway源码中的pom.xml文件,记得也修改为2.3.1
再次尝试编译
等待几分钟最终编译成功
编译成功后,在源码的目录下会生成target文件夹,里面的文件即是打包好的结果
其中tb-gateway-windows.zip是针对Windows平台的安装包
目前的状态如下
启动Thingsboard(在运行Thingsboard Gateway前,假使你已经知道了Thingsboard平台是如何运行的,也已经正常的编译成功源代码了)
使用租户管理员,登陆到系统,并创建一个设备,并指定为网关
进入到设备的属性中,将Access Token复制出来备用
目前的状态如下
Windows尝试安装Thingsboard Gateway,首先解压文件
打开Thingsboard Gateway目录下conf配置文件
修改配置文件内容
就使用刚才编译代码时的管理员身份打开的命令行窗口
#进入到指定的目录
cd target\tb-gateway-windows\tb-gateway
#安装网关服务
install.bat
#开始网关服务
net start tb-gateway
运行截图
目前的状态如下
进入到Thingsboard,查看网关的最后遥测数据,只要出现以下参数变为0,即Thingsboard Gateway与Thingsboard平台已经连接上了
#All values should be set to “0”(以下三个属性应该都是0)
devicesOnline:0
attributesUploaded:0
telemetryUploaded:0
浏览器界面
目前的状态如下
尝试一下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的网关中,查看配置,点击上传
将下载的json配置文件拖动到上传框,并点击上传
可以看到网关的状态为同步状态
查看其中的配置内容,请留意,现在的MQTT服务器是127.0.0.1,端口是1884,使用的就是本机安装mosquitto服务器
MQTTBox(下载地址:http://workswithweb.com/mqttbox.html)
创建一个连接,直接连接到本机的mosquitto上
然后在MQTTBox上发布一条消息,点击发送
#主题
sensors
#消息体
{"serialNumber":"SN-001", "model":"T1000", "temperature":36.6}
刷新Thingsboard设备的列表,可以看到多了一个SN-001的设备
并且新加入的设置也是有遥测值的
新设备与网关进行了关联
如果需要设置设备在线或离线状态,需要发送主题sensor/设备编号/connect(离线,需要发送主题sensor/设备编号/disconnect)
目前的状态如下
即然本地MQTT Broker是可以的,那么远程的MQTT Broker呢?是否可用呢
在Thingsboard中修改网关的MQTT设置,并保存
再次使用MQTTBox测试
在Thingsboard设备列表中又增加了SN-002的设备,且已经有了遥测值
提醒,这里提醒一下,Thingsboard Gateway需要与Thingsboard配套使用,如果版本不同,则无法进行处理
最终理想的状态