integration azure
1.0.0
นี่คือตัวอย่างการรวมเครือข่ายสิ่งต่าง ๆ กับ Azure IoT Hub คุณสมบัติการรวมนี้สร้างอุปกรณ์ใน Azure IoT Hub Device Registry รวมถึงการส่งเหตุการณ์จากข้อความอัปลิงค์
การรวมนี้ต้องใช้ชื่อคีย์นโยบายการเข้าถึงที่ใช้ร่วมกันพร้อมกับการอ่านและเขียนและการเชื่อมต่ออุปกรณ์ ในตัวอย่างนี้เราใช้นโยบาย Iothubowner ซึ่งเปิดใช้งานการอนุญาตเหล่านี้โดยค่าเริ่มต้น
'use strict' ;
const ttnazureiot = require ( 'ttn-azure-iothub' ) ;
// Replace with your region, app ID and access key
const region = '<insert region>' ;
const appId = '<insert app ID>' ;
const accessKey = '<insert access key>' ;
// Replace with your Azure IoT Hub name, key name and key
const hubName = '<insert hub name>' ;
const keyName = 'iothubowner' ;
const key = '<insert key>' ;
const bridge = new ttnazureiot . Bridge ( region , appId , accessKey , hubName , keyName , key ) ;
bridge . on ( 'ttn-connect' , ( ) => {
console . log ( 'TTN connected' ) ;
} ) ;
bridge . on ( 'error' , err => {
console . warn ( 'Error' , err ) ;
} ) ;
bridge . on ( 'message' , data => {
console . log ( 'Message' , data ) ;
} ) ; เมื่อสร้างและเริ่มต้น Bridge คุณสามารถระบุตัวเลือก:
const options = { } ;
const bridge = new ttnazureiot . Bridge ( region , appID , accessKey , hubName , keyName , key , options ) ;createMessage ฟังก์ชั่นในการสร้างข้อความ โดยค่าเริ่มต้นข้อความคือการรวมกันของผลลัพธ์ของ fields ฟังก์ชันเพย์โหลด, รหัสอุปกรณ์ที่ไม่ซ้ำกันและเวลาเซิร์ฟเวอร์:
options . createMessage = function ( deviceId , message ) {
const metadata = {
deviceId : deviceId ,
time : message . metadata . time ,
raw : message . payload_raw
} ;
return Object . assign ( { } , message . payload_fields , metadata ) ;
} หมายเหตุ: หากผลลัพธ์ของฟังก์ชั่นเพย์โหลดของคุณมีฟิลด์ deviceId หรือ time ฟิลด์เหล่านี้จะถูกเขียนทับโดยข้อมูลเมตา ใช้ฟังก์ชั่น createMessage ที่กำหนดเองเพื่อใช้ชื่อฟิลด์ที่กำหนดเอง