源碼下載地址:https://github.com/thingsboard/thingsboard-gatewayhtml
國內大神源碼地址:https://github.com/guodaxia103/thingsboard-gatewaygit
目前在國內有文章寫出,Thingsboard Gateway是管理設備使用的,一個Thingsboard Gateway能夠管理1000個設備,請參考:https://blog.csdn.net/Zzhou1990/article/details/100106995?tdsourcetag=s_pcqq_aiomsggithub
前提是Thingsboard開發環境搭建,請參考《Thingsboard開發環境搭建及編譯項目》,由於Thingsboard Gateway也跟它相關web
爲了對Thingsboard有個概念,能夠參考官方文檔:https://thingsboard.io/docs/iot-gateway/what-is-iot-gateway/apache
下載源碼,git bash命令json
git clone https://github.com/guodaxia103/thingsboard-gateway
使用管理員身份打開命令行工具,並進入到項目目錄windows
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
錯誤截圖bash
緣由是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}" } ] } ] } } ]
進入到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配套使用,若是版本不一樣,則沒法進行處理
最終理想的狀態