ThingsBoard
透過 HTTP Integration 與資料轉換器,在 ThingsBoard 接收裝置資料。
本文說明如何讓 ThingsBoard 接收 CeresGate 轉發的裝置資料。以下以 ThingsBoard PE(Professional Edition)的 HTTP Integration 為例;選單名稱可能因 ThingsBoard 版本略有不同。
運作方式
CeresGate → HTTP POST → ThingsBoard HTTP Integration → Uplink 資料轉換器 → 裝置的遙測資料
- CeresGate 每收到一筆裝置資料,就以 HTTP
POST把{"IMSI": …, "Payload": …}送到 Integration 的網址,並在標頭帶上 Adapter 設定的username與password(見轉發格式)。 - Integration 以這兩個標頭確認請求來自您的 CeresGate Adapter,再交給 Uplink 資料轉換器。
- 轉換器把內容轉成 ThingsBoard 的格式:以 IMSI 作為裝置名稱,解碼後的資料作為遙測資料(telemetry)。裝置第一次傳資料時,ThingsBoard 會自動建立該裝置。
- ThingsBoard 回應 HTTP 200,轉發紀錄即顯示「已轉發」。
1. 建立 Uplink 資料轉換器
- 在 ThingsBoard 左側選單選「Integrations center」→「Data converters」,新增一個轉換器。
- 類型選 Uplink,名稱自訂(例如
CeresGate uplink),程式語言選 JavaScript。 - 把下面的範例貼到 Decoder 函式中,儲存。
轉換器範例
// CeresGate -> ThingsBoard uplink data converter (JavaScript decoder).
// CeresGate POSTs: {"IMSI": "<15 digits>", "Payload": "<hex string>"}
// payload: the request body as an array of bytes; metadata: integration metadata.
function decodeToString(bytes) {
return String.fromCharCode.apply(String, bytes);
}
// Hex string → UTF-8 text. Returns null if the bytes are not valid UTF-8
// (for example binary data), so the caller can keep the hex as-is.
function hexToUtf8(hex) {
if (!hex) {
return "";
}
try {
return decodeURIComponent(hex.replace(/../g, "%$&"));
} catch (e) {
return null;
}
}
function parseJsonObject(text) {
try {
var value = JSON.parse(text);
return (value !== null && typeof value === "object" && !Array.isArray(value)) ? value : null;
} catch (e) {
return null;
}
}
var message = JSON.parse(decodeToString(payload));
var text = hexToUtf8(message.Payload);
var values = text === null ? null : parseJsonObject(text);
var telemetry;
if (values !== null) {
// The device sent a JSON object: each key becomes a telemetry key.
// If your device wraps its readings, e.g. {"D": [{...}]}, use:
// telemetry = values.D[0];
telemetry = values;
} else if (text !== null) {
telemetry = { raw: text }; // plain text
} else {
telemetry = { raw_hex: message.Payload }; // binary data
}
return {
deviceName: message.IMSI,
deviceType: "ceresgate-device",
attributes: {
IMSI: message.IMSI,
integrationName: metadata["integrationName"]
},
telemetry: telemetry
};
這個範例:
- 資料是 JSON 物件(例如
{"temp": 25.1, "battery": 87}):每個欄位成為一個遙測資料。 - 資料包在某個欄位裡(例如
{"D": [{"temp": 25.1}]}):把telemetry = values;改成telemetry = values.D[0];,依您的格式調整。 - 資料不是 JSON(例如純文字
hello udp :0):存成遙測資料raw,不會讓轉換失敗。 - 資料是二進位(不是有效的 UTF-8 文字):保留原本的 hex 字串,存成
raw_hex,請再依您裝置的格式解析。 - 以 UTF-8 解碼,中文等多位元組文字也能正確顯示。
- 裝置類型(
deviceType)設為ceresgate-device,可依需要修改;IMSI 與 Integration 名稱另存為裝置屬性(attributes)。
儲存前可以用轉換器編輯畫面的「Test decoder function」測試:輸入一筆 CeresGate 會送出的內容,例如
{"IMSI": "001013453258396", "Payload": "7b2274656d70223a32352e317d"}
輸出的 telemetry 應為 {"temp": 25.1}。
2. 建立 HTTP Integration
- 選「Integrations center」→「Integrations」,新增一個 Integration,類型選 HTTP。
- Uplink data converter 選第 1 步建立的轉換器。
- 開啟安全設定(Enable security),在標頭過濾(Headers filter)加入兩組自訂的鍵值:
username= 您自訂的帳號password= 您自訂的密碼
- 儲存後,複製 Integration 顯示的 HTTP endpoint URL,格式類似:
https://thingsboard.example.com/api/v1/integrations/http/3f2a9c1e-7b4d-4e8a-9c0f-1d2e3f4a5b6c
ThingsBoard 必須能從網際網路連線,CeresGate 才能送達。
3. 在 CeresGate 建立 Adapter
在入口網站左側選「Adapter 設定」,新增 ThingsBoard 類型的 Adapter:
| 欄位 | 填入 |
|---|---|
| 名稱 | 自訂,方便辨識即可 |
| httpurl | 第 2 步複製的 HTTP endpoint URL |
| username | 第 2 步標頭過濾中 username 的值 |
| password | 第 2 步標頭過濾中 password 的值 |
然後在「裝置」列表的 Adapter 欄位,為裝置選擇這個 Adapter。
4. 確認收到資料
- 讓裝置送出一筆資料。
- CeresGate 入口網站的「轉發紀錄」應顯示「已轉發」與 HTTP 200。
- 在 ThingsBoard 的「Devices」中會出現以 IMSI 命名的裝置,其「Latest telemetry」分頁顯示轉換後的資料。
沒有看到資料時,可以在 Integration 與轉換器的「Events」分頁查看收到的內容與錯誤;開啟除錯模式(Debug mode)可以看到每一筆的輸入與輸出,排查完畢後請關閉。
轉發失敗時
- HTTP 403:Adapter 的 username、password 與標頭過濾的值不一致。
- HTTP 404:httpurl 不正確,或 Integration 未啟用。
- HTTP 500:通常是轉換器的程式出錯。轉發紀錄只會顯示「ThingsBoard returned an internal error without a readable message」,詳細原因(例如出錯的行號)請查看轉換器的 Events。常見原因是裝置送出的內容不是轉換器預期的格式。
其他情況見轉發紀錄與排錯。