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開發環境搭建,請參考《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配套使用,如果版本不同,則無法進行處理 

 

 

 

 

 

 

 

 

 

 

 

 

 

最終理想的狀態

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM