開源物聯網雲平臺 Thingsboard入門

開源物聯網雲平臺 Thingsboard入門

本文主要介紹一下Thingsboard 物聯網雲平臺的簡單使用。

一. Thingsboard 基礎知識和架構

ThingsBoard具有以下特點:
1.遙測數據收集功能: 可以可靠地收集和存儲遙測數據,以應對網絡和硬件故障。使用可自定義的Web儀表板或服務器端API訪問收集的數據。
2.開箱即用的多租戶支持多租戶安裝: 單個租戶可能具有多個租戶管理員以及數百萬個設備和客戶。
3.數據可視化: 提供了現成的30個可配置小部件,並能夠使用內置編輯器創建自己的小部件。內置線圖,數字和模擬儀表,地圖等等。
4. 水平可伸縮性: 隨着以集羣模式添加新的Thingsboard服務器,受支持的服務器端請求和設備的數量呈線性增加。無停機時間,服務器重啓或應用程序錯誤。
5. 物聯網規則引擎: 基於實體屬性或消息內容,使用靈活的規則鏈處理傳入的設備數據。使用自定義邏輯將數據轉發到外部系統或觸發警報。在警報上配置複雜的通知鏈。增強服務器端功能或使用高度可定製的規則來操作設備。使用拖放規則鏈設計器定義您的應用程序邏輯。
6.容錯: 所有物聯網服務器都是相同的。沒有主人工人或熱備用人員。自動檢測到節點故障。可以在不停機的情況下更換故障節點。使用可靠的NoSQL數據庫複製持久數據。
7.設備管理: 提供註冊和管理設備的功能。允許監視客戶端和供應服務器端設備屬性。爲服務器端應用程序提供API,以將RPC命令發送到設備,反之亦然。
8.安全性支持: MQTT和HTTP協議的傳輸加密。支持設備認證和設備憑證管理。
9.資產管理: 提供註冊和管理資產的功能。允許供應服務器端資產屬性並監視相關警報。使用關係建立實體層次結構的能力。
10. 自定義和集成: 使用可自定義的規則鏈,窗口小部件和傳輸實現來擴展默認平臺功能。除了MQTT,CoAP和HTTP支持之外,ThingsBoard用戶還可以使用自己的傳輸實現或自定義現有協議的行爲。
11.警報管理: 提供創建和管理與您的實體(設備,資產,客戶等)相關的警報的功能。允許實時警報監視以及將警報傳播到相關實體層次結構。在設備斷開連接或不活動事件時發出警報。
12.100%開源: ThingsBoard已獲得Apache許可2.0的許可,因此您可以在商業產品中免費使用任何它。您甚至可以將其託管爲SaaS或PaaS解決方案。
13. 微服務或單片支持爲入門或小型環境提供單片部署。提供升級到微服務的功能,以實現高可用性和水平可伸縮性。
14.SQL,NoSQL和混合數據庫支持: 各種數據庫選項,並能夠選擇在何處存儲主要實體以及在何處存儲遙測數據.

在這裏插入圖片描述

總體架構:
系統分層:
Device端:數據採集,需要支持MQTT或HTTP等協議
通訊通道:數據採集監聽
規則引擎:對數據進行過濾,處理
核心業務規則
服務側AIP和GW
用戶端:用戶和應用
在這裏插入圖片描述

測量數據處理:
在這裏插入圖片描述
RPC 過程:
在這裏插入圖片描述

規則引擎:
在這裏插入圖片描述
IoT網關:
在這裏插入圖片描述

規則引擎:
規則VS插件
生命週期
作用域
規則:過濾器,處理器,Action
插件:
處理消息
服務側API請求
WebSocket和應用
持久化和查詢事件
插件間RPC通信
在這裏插入圖片描述
在這裏插入圖片描述

遙測插件-系統插件,負責處理與設備屬性和遙測有關的各種請求。
RPC插件-允許使用REST API對設備執行RPC調用。 RPC調用將使用支持的網絡協議傳遞到設備。
設備消息傳遞插件-允許分配給相同客戶交換事件的設備。
發送郵件插件-允許發送電子郵件。您可以指定郵件服務器屬性。有關更多詳細信息,請參見插件文檔。
Kafka插件-允許將遙測消息推送到Apache Kafka。有關更多詳細信息,請參見插件文檔。
RabbitMQ插件-允許將遙測消息推送到RabbitMQ。有關更多詳細信息,請參見插件文檔。
REST API呼叫插件-允許使用REST API將遙測消息推送到外部服務器。有關更多詳細信息,請參見插件文檔。 時間RPC插件-允許從設備發送RPC請求以獲取當前服務器端時間戳。

API-REST API
http://YOUR_HOST:PORT/swagger-ui.html 比如:http://localhost:9090/swagger-ui.html#/
獲取tokens:
curl -X POST --header ‘Content-Type: application/json’ --header ‘Accept: application/json’ -d ‘{“username”:“[email protected]”, “password”:“tenant”}’ 'http://THINGSBOARD_URL/api/auth/login‘
使用tokens:X-Authorization

在這裏插入圖片描述

基本消息參見MQTT消息
設備連接:
Topic: v1/gateway/connect Message: {“device”:“Device A”}
Topic: v1/gateway/disconnect Message: {“device”:“Device A”}
屬性API:
Topic: v1/gateway/attributes Message: {“Device A”:{“attribute1”:“value1”, “attribute2”: 42}, “Device B”:{“attribute1”:“value1”, “attribute2”: 42}}
Topic: v1/gateway/attributes/request Message: {“id”: $request_id, “device”: “Device A”, “client”: true, “key”: “attribute1”}

二. 從源代碼編譯的方法和注意事項

mvn clean install -DskipTests
在這裏插入圖片描述
我第一次編譯時遇到了錯誤,折騰了好幾次,
靠譜的方法如下: 選擇一個你要編譯的release 分支
$ git branch -a
master

  • release-2.4
    remotes/origin/HEAD -> origin/master
    remotes/origin/develop/2.4
    remotes/origin/develop/3.0
    remotes/origin/feature/backpressure
    remotes/origin/feature/claiming
    remotes/origin/feature/edge
    remotes/origin/feature/geofencing
    remotes/origin/feature/missing-translate-ua-ru
    remotes/origin/feature/security-updates
    remotes/origin/feature/spring-boot-2
    remotes/origin/feature/sql-rate-limits
    remotes/origin/feature/swagger-config-to-file
    remotes/origin/master
    remotes/origin/openjdk8
    remotes/origin/pgrisu-features/add_optional_widget_title_tooltip
    remotes/origin/pgrisu-features/widget_entity_datakeys_mixed_creation_order
    remotes/origin/release-1.0
    remotes/origin/release-1.1
    remotes/origin/release-1.2
    remotes/origin/release-1.3
    remotes/origin/release-1.4
    remotes/origin/release-2.0
    remotes/origin/release-2.2
    remotes/origin/release-2.3
    remotes/origin/release-2.4
    remotes/origin/revert-1637-release-2.3-entity-view-cn
    remotes/origin/revert-1642-mqtt-keep-alive-fix
    remotes/origin/revert-1692-1686-No-response-to-client-side-RPC-via-CoAP

git checkout release-2.4
#然後執行:
mvn clean install -DskipTests
中間遇到多次 node.js 編譯的問題, 基本都是權限問題引起的,
注意:1.要確保你的當前用戶可以修改和當前用戶目錄下的.npm/目錄
2.確保源代碼目錄下的文件夾你都有訪問權限。
之前因爲遇到了權限問題,使用了sudo 導致編譯老是出問題。
正常情況下用當前用戶編譯即可。

三. 安裝部署

docker 單實例安裝: start-thingsboard-docker.sh
把下面的代碼拷貝到文件start-thingsboard-docker.sh,然後添加執行權限 chmod +x start-thingsboard-docker.sh ,然後執行即可.
首次啓動需要幾分鐘時間。

#!/usr/bin/env bash

echo "https://hub.docker.com/r/thingsboard/tb-cassandra"
#docker pull thingsboard/tb-cassandra

docker stop mytb
docker rm   mytb

docker run -it \
-p 9090:9090 \
-p 1883:1883 \
-p 5683:5683/udp \
-v `pwd`/mytb-data:/data \
-v `pwd`/mytb-logs:/var/log/thingsboard \
--name mytb \
--restart always \
thingsboard/tb-cassandra

echo "http://localhost:9090"
echo "ThingsBoard login page. Use the following default credentials:"
echo "系統管理員: [email protected] / sysadmin"
echo "租戶管理員: [email protected] / tenant"
echo "用戶賬號: [email protected] / customer"

echo "-p 9090:9090 - connect local port 9090 to exposed internal HTTP port 9090"
echo "-p 1883:1883 - connect local port 1883 to exposed internal MQTT port 1883"
echo "-p 5683:5683 - connect local port 5683 to exposed internal COAP port 5683"

用戶類型有3種,密碼賬號如上所示.
系統管理員: [email protected] 密碼: sysadmin
租戶管理員: [email protected] 密碼: tenant
用戶賬號: [email protected] 密碼: customer

四. 測試驗證

使用系統管理員登陸:
在這裏插入圖片描述
在這裏插入圖片描述

租戶管理員登陸:
在這裏插入圖片描述

在這裏插入圖片描述

測試: 添加一個IoT設備,然後持續持續的發送數據, 然後讀取和展示

使用租戶管理員登陸: 租戶管理員: [email protected] 密碼: tenant

  1. (添加設備)點擊右下角的中間橙色圓圈

在這裏插入圖片描述
在這裏插入圖片描述

添加成功後,點擊左上角的 設備–》(你的設備名稱)

在這裏插入圖片描述
在這裏插入圖片描述
複製令牌和複製設備ID 待會會用到.

測試往Thingsboard 發送數據.
把下面的python 腳本保存爲simple-mqtt-python.py


import paho.mqtt.client as mqtt
from time import sleep
import random

# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, rc, *extra_params):
   print("連接成功")
   print('連接結果代碼 code= '+str(rc))
   #print('***' + str(r))
   # Subscribing in on_connect() means that if we lose the connection and
   # reconnect then subscriptions will be renewed.
   # client.subscribe('v1/devices/me/attributes')
   # client.subscribe('v1/devices/me/attributes/response/+')
   # client.subscribe('v1/devices/me/rpc/request/+')



# The callback for when a PUBLISH message is received from the server.
def on_message(client, userdata, msg):
   print('主題: ' + msg.topic + '\n消息: ' + str(msg.payload))
   # if msg.topic.startswith( 'v1/devices/me/rpc/request/'):
   #     requestId = msg.topic[len('v1/devices/me/rpc/request/'):len(msg.topic)]
   #     print('This is a RPC call. RequestID: ' + requestId + '. Going to reply now!')
   #     client.publish('v1/devices/me/rpc/response/' + requestId, "{\"value1\":\"A\", \"value2\":\"B\"}", 1)
   # ATTRIBUTES = '{\"firmware_version\":\"1.0.2\", \"serial_number\":\"SN-001\"}'
   # TELEMETRY = '{\"temperature\":28, \"humidity\":139.0, \"active\": false}'
   #
   # client.publish('v1/devices/me/attributes', ATTRIBUTES);
   # print("ok1 " + ATTRIBUTES)
   #
   # client.publish('v1/devices/me/telemetry', TELEMETRY);
   # print("ok2 " + TELEMETRY)

#用戶需要根據實際情況填寫下面的內容.

THINGSBOARD_HOST="localhost"
MQTT_PORT=1883
ACCESS_TOKEN="xWVlx8J6tkdxyrf8yvC7" #使用TOKEN進行相關驗證 , 請更換爲你的設備訪問令牌。

client = mqtt.Client()
client.on_connect = on_connect
client.on_message = on_message
#client.publish('v1/devices/me/attributes/request/1', "{\"clientKeys\":\"model\"}", 1)

client.username_pw_set(ACCESS_TOKEN)

#connect(self, host, port=1883, keepalive=60, bind_address=""
client.connect(THINGSBOARD_HOST, MQTT_PORT, 1)

#client.loop_forever()

client.loop_start();
sleep(1)


# ATTRIBUTES = '{\"firmware_version\":\"1.0.2\", \"serial_number\":\"SN-001\"}'
# TELEMETRY = '{\"temperature\":28, \"humidity\":149.0, \"active\": false}'

for i in range(500000):
    temperature=10 + random.randint(-15, 30)
    humidity=60+random.randint(0, 40);
    ATTRIBUTES = '{\"firmware_version\":\"1.0.2\", \"serial_number\":\"SN-001\"}'
    TELEMETRY = '{\"temperature\":'+str(temperature)+', \"humidity\":'+str(humidity)+', \"active\": false}'
    client.publish('v1/devices/me/attributes', ATTRIBUTES);
    #print("ok1 " + ATTRIBUTES)

    client.publish('v1/devices/me/telemetry', TELEMETRY);
    print("ok2 " + TELEMETRY)
    sleep(1)

client.loop_stop();
client.disconnect()


#client.disconnect();



# Blocking call that processes network traffic, dispatches callbacks and
# handles reconnecting.
# Other loop*() functions are available that give a threaded interface and a
# manual interface.


#執行環境是python3
執行python simple-mqtt-python.py

在這裏插入圖片描述

這時打開網頁 設備–>happy-device 就可以看到 上傳到Thingsboard 的實時數據
在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述

在這裏插入圖片描述
然後點擊右下角的 紅色打勾, 應用更改。即可生效。

訪問實時數據:http://localhost:9090/api/v1/xWVlx8J6tkdxyrf8yvC7/attributes
在這裏插入圖片描述

export JWT_TOKEN=“xWVlx8J6tkdxyrf8yvC7”
curl -v -X GET http://localhost:9090/api/plugins/telemetry/DEVICE/2b58cc70-e9ab-11e9-9d84-7f35a90422a9/keys/timeseries
–header “Content-Type:application/json”
–header “X-Authorization: $JWT_TOKEN”

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章