特定のIoTシナリオでは、リソースが限られ、仕様が低いデバイス、または高いネットワークスループットを必要とするデバイスは、JSON形式のThing Specification Language (TSL) モデルを使用してIoT Platformと通信できません。 この問題を解決するために、デバイスからIoT Platformに生データを渡すことができます。 IoT Platformは、送信したスクリプトに基づいて、デバイス固有の形式とJSON形式の間でデータを変換するデータ解析機能を提供します。 データを処理する前にこの機能を使用できます。
背景情報
次の図は、デバイスとサーバー間のデータの流れを示しています。
この例では、環境データを収集するデバイスが使用されます。
デバイスから IoT Platform への接続
IoT PlatformコンソールIoT Platformコンソールにログインします。
[概要] ページで、[すべての環境] をクリックします。 [すべての環境] タブで、管理するインスタンスを見つけ、インスタンスIDまたはインスタンス名をクリックします。
左側のナビゲーションウィンドウで、EnvSensorという名前のプロダクトが作成されています。 を選択します。 [プロダクト] ページで、[プロダクトの作成] をクリックしてプロダクトを作成します。 この例では、
[データ型] パラメーターを [カスタム] に設定します。 他のパラメーターにはデフォルト値を使用します。 詳細については、「プロダクトの作成」をご参照ください。
プロダクトの作成後、[TSLの定義] をクリックしてTSL機能を追加します。 次に、TSLモデルを公開します。
この例では、サンプルのTSLモデルが提供されています。 TSLモデルを使用して、一度に複数のTSL機能を追加できます。 詳細については、「TSL機能の一括追加」をご参照ください。
左側のナビゲーションウィンドウで、[デバイス] > [デバイス] を選択します。 [デバイス] ページで、[デバイスリスト] をクリックします。 [デバイスリスト] タブで、[デバイスの追加] をクリックして、EnvSensor製品のデバイスを作成します。 この例では、Esensorという名前のデバイスが作成されます。
デバイスの作成後、デバイス証明書に関する情報を取得します。 情報には、ProductKey、DeviceName、およびDeviceSecretが含まれます。
デバイスを構成し、テストを実行します。
Link SDKを使用してデバイスをシミュレートし、IoT Platformにメッセージを送信します。 この例では、Node.js用のLink SDKが使用されています。 デバイスを設定する方法の詳細については、「デバイスをIoT Platformに接続してデータを送信する」をご参照ください。
Link SDKの使用方法の詳細については、「Link SDKを使用したデバイス接続」をご参照ください。
サンプルコード:
const mqtt = require('aliyun-iot-mqtt'); // デバイス証明書に関する情報を指定します。 var options = { productKey: "g18lk ****" 、 deviceName: "Esensor" 、 deviceSecret: "c39fe9dc32f97bfd753 ****** b" 、 ホスト: 「iot ****** .mqtt.iothub.aliyuncs.com」 }; // 接続を確立します。 const client = mqtt.getAliyunIotMqttClient (オプション); // IoT Platformからのコマンドをリッスンします。 client.subscribe('/${options.productKey}/${options.de viceName}/user/get') client.on('message', function(topic, message) { console.log("topic" + topic) console.log (「メッセージ」 + メッセージ) }) setInterval(function() { // 環境データを送信します。 const payloadArray = new Buffer.from([0xaa,0x1f,0xc8,0x00,0x00,0x37,0x10,0xff,0x00,0x05,0xd7,0x6b,0x15,0x00,0x1c,0x01,0x34,0x00,0xad,0x04,0xff,0xff,0x04,0x00,0xff,0xff,0xff,0x18,0x00,0x30,0x00、0xff、0x2e] client.publish('/sys/${options.productKey}/${options.de viceName}/thing/model/up_raw', payloadArray, { qos: 0 }); }, 5*1000);
デバイスをIoT Platformに接続すると、デバイスはオンライン状態になります。 デバイスが接続されているインスタンスの [デバイス] ページでステータスを表示できます。
Esensorの [操作] 列で [表示] をクリックします。 次に、[TSLデータ] をクリックします。 この例では、プロダクトのデータ型はカスタムに設定されています。 この場合、送信されたTSLデータは [ステータス] タブに表示されません。
を選択します。 [クラウド実行ログ] タブで、[デバイス間メッセージ] を選択して、16進形式で表示される送信メッセージを表示します。
この例では、次のデータが表示されます。
0xaa1fc800003710ff0005d76b15001c013400ad04ffff0400ffff18003000ff 2e
データ解析スクリプトの作成
IoT Platformコンソールでデータ解析スクリプトを編集、送信、およびデバッグできます。
IoT Platformコンソールにログインします。 管理するインスタンスの名前をクリックします。 [インスタンスの詳細] ページの左側のナビゲーションウィンドウで、[] を選択します。
[製品] ページで製品を見つけ、[操作] 列の [表示] をクリックします。
製品の詳細ページで、[メッセージ分析] をクリックします。
[メッセージ分析] タブで、[スクリプトの編集] フィールドにスクリプトを記述します。
デバイスが使用する通信プロトコルに基づいてスクリプトを変更します。 次の表に、デバイスのメッセージボディ構造を示します。
バイト
説明
補足
12
PM2.5プロパティの下位バイト値
PM2.5プロパティの値を返します。 有効な値: 0 ~ 999 単位: ug/m 3。
13
PM2.5プロパティのハイバイト値
14
温度プロパティの下位バイト値 × 10
temperatureプロパティの値を返します。 有効値: -10 ~ 50。 単位: ℃ 。
15
温度特性 × 10の高いバイト値
16
湿度プロパティの低いバイト値
湿度プロパティの値を返します。 有効な値: 0 ~ 99。 単位: % 。
17
湿度特性の高いバイト値
18
二酸化炭素プロパティの低いバイト値
二酸化炭素プロパティの値を返します。 有効な値: 0 ~ 9999 単位: mg/m 3。
19
二酸化炭素の特性の高いバイト値
22
ホルムアルデヒド特性 × 100の低いバイト値
ホルムアルデヒドプロパティの値を返します。 有効な値: 0 ~ 9.99
23
ホルムアルデヒド特性 × 100の高いバイト値
28
照度プロパティの低いバイト値
照度プロパティの値を返します。 単位: ルクス。
29
照度プロパティの高いバイト値
この例では、デバイスは環境データのみを送信します。 この場合、アップストリームデータを解析するにはrawDataToProtocol() 関数のみを設定する必要があります。 protocolToRawData() 関数を設定する必要はありません。 カスタムデータをプロパティデータ、サービスデータ、またはイベントデータに変換できます。 この例では、カスタムデータをプロパティデータに変換します。
var PROPERTY_REPORT_METHOD = 'thing.event.property.post'; // 上流データを送信します。 次の関数は、カスタム形式のデータをJSON形式のTSLモデルに変換します。 function rawDataToProtocol(bytes) { var uint8Array = new Uint8Array(bytes.length); for (var i = 0; i < bytes.length; i++) { uint8Array[i] = bytes[i] & 0xff; } var dataView = new DataView(uint8Array.buffer, 0); var jsonMap = new Object(); // リクエストメソッドを指定します。 jsonMap['method'] = PROPERTY_REPORT_METHOD; // プロトコルのバージョンを指定します。 値を 1.0 に設定します。 jsonMap['version'] = '1.0 '; // リクエストのIDを指定します。 jsonMap['id'] = new Date().getTime(); var params = {}; // 値12と13はPM2.5プロパティの値です。 params['PM25'] = (dataView.getUint8(13)* 256 + dataView.getUint8(12)); // 値14と15は温度特性の値です。 params['temperature'] = (dataView.getUint8(15)* 256 + dataView.getUint8(14))/10; // 値16と17は湿度特性の値です。 params['humidity'] = (dataView.getUint8(17)* 256 + dataView.getUint8(16)); // 値18と19は、co2プロパティの値です。 params['co2'] = (dataView.getUint8(19)* 256 + dataView.getUint8(18)); // 値22と23はhchoプロパティの値です。 params['hcho'] = (dataView.getUint8(23)* 256 + dataView.getUint8(22))/100; // 値28と29はlightLuxプロパティの値です。 params['lightLux'] = (dataView.getUint8(29)* 256 + dataView.getUint8(28)); jsonMap['params'] = params; jsonMapを返します。} // 下流コマンドを送信します。 次の関数は、JSON形式のTSLモデルをカスタム形式のデータに変換します。 function protocolToRawData(json) { var payloadArray = [1];// デバイスはデータのみを送信でき、IoT Platformからコマンドを受信できません。 payloadArrayを返します。} // カスタム形式のトピックデータをAlink JSONデータに解析します。 function transformPayload(topic, rawData) { var jsonObj = {} jsonObjを返します。}
テストデータ解析。
[シミュレーションタイプ] ドロップダウンリストから [アップストリームされたデバイスデータ] を選択します。
[入力シミュレーション] タブで、フィールドにテストデータを入力します。
デバイスによって送信された16進データをテストデータとして使用できます。 16進データを表示するには、[デバイスログ] ページに移動します。 例: 0xaa1fc800003710ff0005d76b15001c013400ad04ffff0400ffff18003000ff 2e
[実行] をクリックします。
次の図は、[解析結果] タブの結果を示しています。
テストデータがスクリプトによって解析できることを確認したら、[送信] をクリックしてスクリプトをIoT Platformに送信します。
デバイスのデバッグとデータの送信
スクリプトを送信した後、デバイスSDKを使用してスクリプトをデバッグできます。
デバイスがIoT Platformにデータを送信すると、IoT Platformはスクリプトを使用してデータを解析します。
解析されたデータを表示するには、[デバイスの詳細] ページに移動し、
を選択します。アップストリームTSLデータの例
{
"schema": "https://iotx-tsl.oss-ap-southeast-1.aliyuncs.com/schema.json",
"profile": {
"version": "1.0"、
"productKey": "g4 ****"
},
"properties": [
{
"identifier": "lightLux" 、
"name": "照度" 、
"accessMode": "rw",
"required": false,
"dataType": {
"type": "float",
"specs": {
"min": "0",
"max": "10000" 、
"unit": "Lux" 、
"unitName": "単位面積あたりの光束" 、
"ステップ": "0.1"
}
}
},
{
"identifier": "PM25" 、
"name": "PM25" 、
"accessMode": "rw",
"required": false,
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "1000" 、
"unit": "μ g/m ³" 、
"unitName": "立方メートルあたりのマイクログラム" 、
"step": "1"
}
}
},
{
"identifier": "hcho" 、
"name": "ホルムアルデヒド" 、
"accessMode": "rw",
"desc": "HCHOValue" 、
"required": false,
"dataType": {
"type": "float",
"specs": {
"min": "0",
"max": "10",
"unit": "ppm" 、
"unitName": "parts per million" 、
"ステップ": "0.1"
}
}
},
{
"identifier": "co2" 、
"name": "二酸化炭素" 、
"accessMode": "rw",
"required": false,
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "10000" 、
"unit": "mg/m ³" 、
"unitName": "1立方メートルあたりのミリグラム" 、
"step": "1"
}
}
},
{
"identifier": "湿度" 、
"name": "湿度" 、
"accessMode": "rw",
"required": false,
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "100",
"unit": "%" 、
"unitName": "パーセンテージ" 、
"step": "1"
}
}
},
{
"identifier": "temperature" 、
"name": "temperature" 、
"accessMode": "rw",
"desc": "モーターの働く温度" 、
"required": false,
"dataType": {
"type": "float",
"specs": {
"min": "-10" 、
"max": "50" 、
"ユニット": "℃" 、
"ステップ": "0.1"
}
}
}
],
"events": [
{
"identifier": "post",
"name": "post",
"type": "info",
"required": true,
"desc": "submit properties" 、
"method": "thing.event.property.post",
"outputData": [
{
"identifier": "lightLux" 、
"name": "照度" 、
"dataType": {
"type": "float",
"specs": {
"min": "0",
"max": "10000" 、
"unit": "Lux" 、
"unitName": "単位面積あたりの光束" 、
"ステップ": "0.1"
}
}
},
{
"identifier": "PM25" 、
"name": "PM25" 、
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "1000" 、
"unit": "μ g/m ³" 、
"unitName": "立方メートルあたりのマイクログラム" 、
"step": "1"
}
}
},
{
"identifier": "hcho" 、
"name": "ホルムアルデヒド" 、
"dataType": {
"type": "float",
"specs": {
"min": "0",
"max": "10",
"unit": "ppm" 、
"unitName": "parts per million" 、
"ステップ": "0.1"
}
}
},
{
"identifier": "co2" 、
"name": "二酸化炭素" 、
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "10000" 、
"unit": "mg/m ³" 、
"unitName": "1立方メートルあたりのミリグラム" 、
"step": "1"
}
}
},
{
"identifier": "湿度" 、
"name": "湿度" 、
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "100",
"unit": "%" 、
"unitName": "パーセンテージ" 、
"step": "1"
}
}
},
{
"identifier": "temperature" 、
"name": "temperature" 、
"dataType": {
"type": "float",
"specs": {
"min": "-10" 、
"max": "50" 、
"ユニット": "℃" 、
"ステップ": "0.1"
}
}
}
]
}
],
"services": [
{
"identifier": "set",
"name": "set",
"required": true,
"callType": "async",
"desc": "プロパティを設定" 、
"method": "thing.service.property.set",
"inputData": [
{
"identifier": "lightLux" 、
"name": "照度" 、
"dataType": {
"type": "float",
"specs": {
"min": "0",
"max": "10000" 、
"unit": "Lux" 、
"unitName": "単位面積あたりの光束" 、
"ステップ": "0.1"
}
}
},
{
"identifier": "PM25" 、
"name": "PM25" 、
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "1000" 、
"unit": "μ g/m ³" 、
"unitName": "立方メートルあたりのマイクログラム" 、
"step": "1"
}
}
},
{
"identifier": "hcho" 、
"name": "ホルムアルデヒド" 、
"dataType": {
"type": "float",
"specs": {
"min": "0",
"max": "10",
"unit": "ppm" 、
"unitName": "parts per million" 、
"ステップ": "0.1"
}
}
},
{
"identifier": "co2" 、
"name": "二酸化炭素" 、
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "10000" 、
"unit": "mg/m ³" 、
"unitName": "1立方メートルあたりのミリグラム" 、
"step": "1"
}
}
},
{
"identifier": "湿度" 、
"name": "湿度" 、
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "100",
"unit": "%" 、
"unitName": "パーセンテージ" 、
"step": "1"
}
}
},
{
"identifier": "temperature" 、
"name": "temperature" 、
"dataType": {
"type": "float",
"specs": {
"min": "-10" 、
"max": "50" 、
"ユニット": "℃" 、
"ステップ": "0.1"
}
}
}
],
"outputData": []
},
{
"identifier": "get",
"name": "get",
"required": true,
"callType": "async",
"desc": "プロパティを取得" 、
"method": "thing.service.property.get",
"inputData": [
"lightLux" 、
"PM25" 、
"hcho" 、
"co2" 、
「湿度」、
「温度」
],
"outputData": [
{
"identifier": "lightLux" 、
"name": "照度" 、
"dataType": {
"type": "float",
"specs": {
"min": "0",
"max": "10000" 、
"unit": "Lux" 、
"unitName": "単位面積あたりの光束" 、
"ステップ": "0.1"
}
}
},
{
"identifier": "PM25" 、
"name": "PM25" 、
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "1000" 、
"unit": "μ g/m ³" 、
"unitName": "立方メートルあたりのマイクログラム" 、
"step": "1"
}
}
},
{
"identifier": "hcho" 、
"name": "ホルムアルデヒド" 、
"dataType": {
"type": "float",
"specs": {
"min": "0",
"max": "10",
"unit": "ppm" 、
"unitName": "parts per million" 、
"ステップ": "0.1"
}
}
},
{
"identifier": "co2" 、
"name": "二酸化炭素" 、
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "10000" 、
"unit": "mg/m ³" 、
"unitName": "1立方メートルあたりのミリグラム" 、
"step": "1"
}
}
},
{
"identifier": "湿度" 、
"name": "湿度" 、
"dataType": {
"type": "int"、
"specs": {
"min": "0",
"max": "100",
"unit": "%" 、
"unitName": "パーセンテージ" 、
"step": "1"
}
}
},
{
"identifier": "temperature" 、
"name": "temperature" 、
"dataType": {
"type": "float",
"specs": {
"min": "-10" 、
"max": "50" 、
"ユニット": "℃" 、
"ステップ": "0.1"
}
}
}
]
}
]
}