工場データを扱う
対応エディションEdgeCloudEnterprise
MQTT と API を通じて UNS データを操作できます。
Tier0 で生成した認証情報を使って MQTT クライアントを UNS broker に接続できます。
MQTT 認証情報を生成する
Section titled “MQTT 認証情報を生成する”Connect > Source > MQTT Device で MQTT 認証情報を生成し、記録します。
MQTT クライアントを接続する
Section titled “MQTT クライアントを接続する”- MQTTX をインストールして開きます。
- UNS broker の情報と生成した認証情報で接続を追加します。
Terminal window {"Host": "mqtt.tier0.dev","Port": 1883,"Client ID": "<generated client ID>","Username": "<generated username>","Password": "<generated password>"}
- MQTT Explorer をインストールして開きます。
- UNS broker の情報と生成した認証情報で新しい接続を追加します。
Terminal window {"Protocol": "mqtt://","Host": "mqtt.tier0.dev","Port": 1883,"Username": "<generated username>","Password": "<generated password>"} - 接続前に ADVANCED で Client ID を認証情報の ID に変更します。
- Event Flow を使用:Tier0 の Node-RED ベースのデータ処理モジュール。
- Flows で Event Flow を作成し、
mqtt inノードから始めます。 - ノードの Server に UNS broker(Flow と同じ名前)を選び、Topic に UNS モデルを指定します。
- 独立した Node-RED インスタンスを使用
- Node-RED をインストールして開きます。
mqtt inノードに次の情報でサーバーを追加します。Terminal window {{/* under Connection */}"Server": "mqtt://mqtt.tier0.dev","Port": 1883,"Client ID": "<generated clientId>",{/* under Security */}"Username": "<generated username>","Password": "<generated password>"}
接続をテストする
Section titled “接続をテストする”- クライアントの状態が正常であることを確認します。
- UNS モデルの Topic をコピーし、クライアントからメッセージを配信します。
- Base URL:
https://<your-tier0-host>/openapi/v2 - Authentication: Tier0 で取得した API キーを
x-api-keyヘッダーに指定します。
リクエスト例
Section titled “リクエスト例”-
POST /uns/metadata:find— Collections を一覧表示して Collection ID を取得Terminal window const base = "https://your-tier0-host/openapi/v2";const response = await fetch(`${base}/uns/metadata:find`, {method: "POST",headers: { "content-type": "application/json", "x-api-key": "your-api-key" },body: JSON.stringify({"kind": "collection", "filter": {}, "page": {"mode": "offset", "page": 1, "size": 20}}),});const result = await response.json();if (!response.ok || result.code !== 0) throw new Error(result.error?.code || result.msg);console.log(result.data.items);Terminal window import requestsresponse = requests.post("https://your-tier0-host/openapi/v2/uns/metadata:find",headers={"x-api-key": "your-api-key"},json={"kind": "collection", "filter": {}, "page": {"mode": "offset", "page": 1, "size": 20}},timeout=30,)response.raise_for_status()result = response.json()if result["code"] != 0:raise RuntimeError(result.get("error", result.get("msg")))print(result["data"]["items"])Terminal window curl --fail-with-body -X POST "https://your-tier0-host/openapi/v2/uns/metadata:find" \-H "content-type: application/json" \-H "x-api-key: your-api-key" \-d '{"kind": "collection", "filter": {}, "page": {"mode": "offset", "page": 1, "size": 20}}' -
POST /uns/collections/{collectionId}/records:find— Records を一覧表示して Record ID を取得Terminal window const base = "https://your-tier0-host/openapi/v2";const collectionId = "your-collection-id";const response = await fetch(`${base}/uns/collections/${encodeURIComponent(collectionId)}/records:find`, {method: "POST",headers: { "content-type": "application/json", "x-api-key": "your-api-key" },body: JSON.stringify({"limit": 20}),});const result = await response.json();if (!response.ok || result.code !== 0) throw new Error(result.error?.code || result.msg);console.log(result.data);Terminal window import requestsresponse = requests.post("https://your-tier0-host/openapi/v2/uns/collections/your-collection-id/records:find",headers={"x-api-key": "your-api-key"},json={"limit": 20},timeout=30,)response.raise_for_status()result = response.json()if result["code"] != 0:raise RuntimeError(result.get("error", result.get("msg")))print(result["data"])Terminal window curl --fail-with-body -X POST "https://your-tier0-host/openapi/v2/uns/collections/your-collection-id/records:find" \-H "content-type: application/json" \-H "x-api-key: your-api-key" \-d '{"limit": 20}' -
POST /uns/collections/{collectionId}/samples:latest— Record の最新 Sample を取得Terminal window const base = "https://your-tier0-host/openapi/v2";const collectionId = "your-collection-id";const response = await fetch(`${base}/uns/collections/${encodeURIComponent(collectionId)}/samples:latest`, {method: "POST",headers: { "content-type": "application/json", "x-api-key": "your-api-key" },body: JSON.stringify({"recordIds": ["record_123"]}),});const result = await response.json();if (!response.ok || result.code !== 0) throw new Error(result.error?.code || result.msg);console.log(result.data);Terminal window import requestsresponse = requests.post("https://your-tier0-host/openapi/v2/uns/collections/your-collection-id/samples:latest",headers={"x-api-key": "your-api-key"},json={"recordIds": ["record_123"]},timeout=30,)response.raise_for_status()result = response.json()if result["code"] != 0:raise RuntimeError(result.get("error", result.get("msg")))print(result["data"])Terminal window curl --fail-with-body -X POST "https://your-tier0-host/openapi/v2/uns/collections/your-collection-id/samples:latest" \-H "content-type: application/json" \-H "x-api-key: your-api-key" \-d '{"recordIds": ["record_123"]}' -
POST /uns/collections/{collectionId}/samples:find— 指定期間の Samples を取得Terminal window const base = "https://your-tier0-host/openapi/v2";const collectionId = "your-collection-id";const response = await fetch(`${base}/uns/collections/${encodeURIComponent(collectionId)}/samples:find`, {method: "POST",headers: { "content-type": "application/json", "x-api-key": "your-api-key" },body: JSON.stringify({"recordIds": ["record_123"], "from": 1792454400000, "to": 1792540800000, "filter": {"temperature": {"$gt": 20}}, "limit": 20}),});const result = await response.json();if (!response.ok || result.code !== 0) throw new Error(result.error?.code || result.msg);console.log(result.data);Terminal window import requestsresponse = requests.post("https://your-tier0-host/openapi/v2/uns/collections/your-collection-id/samples:find",headers={"x-api-key": "your-api-key"},json={"recordIds": ["record_123"], "from": 1792454400000, "to": 1792540800000, "filter": {"temperature": {"$gt": 20}}, "limit": 20},timeout=30,)response.raise_for_status()result = response.json()if result["code"] != 0:raise RuntimeError(result.get("error", result.get("msg")))print(result["data"])Terminal window curl --fail-with-body -X POST "https://your-tier0-host/openapi/v2/uns/collections/your-collection-id/samples:find" \-H "content-type: application/json" \-H "x-api-key: your-api-key" \-d '{"recordIds": ["record_123"], "from": 1792454400000, "to": 1792540800000, "filter": {"temperature": {"$gt": 20}}, "limit": 20}'