This repository has been archived by the owner on Dec 14, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7dacb27
commit bc605a8
Showing
3 changed files
with
88 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,37 @@ | ||
"use strict"; | ||
'use strict' | ||
|
||
const fs = require('fs'); | ||
const ttnazureiot = require('.'); | ||
const ttn = require('ttn'); | ||
const ttnlog = ttn.Log; | ||
const fs = require('fs') | ||
const ttnazureiot = require('.') | ||
const ttn = require('ttn') | ||
|
||
// TTN related settings | ||
const appId = process.env.TTN_APP_ID; | ||
const processId = process.env.TTN_PROCESS_ID; | ||
const accessKey = process.env.TTN_APP_ACCESS_KEY; | ||
const region = process.env.TTN_REGION; | ||
const appId = process.env.TTN_APP_ID | ||
const processId = process.env.TTN_PROCESS_ID | ||
const accessKey = process.env.TTN_APP_ACCESS_KEY | ||
const region = process.env.TTN_REGION | ||
|
||
// Azure related settings | ||
const hubName = process.env.TTN_AZURE_HUBNAME; | ||
const keyName = process.env.TTN_AZURE_KEYNAME; | ||
const key = process.env.TTN_AZURE_KEY; | ||
const hubName = process.env.TTN_AZURE_HUBNAME | ||
const keyName = process.env.TTN_AZURE_KEYNAME | ||
const key = process.env.TTN_AZURE_KEY | ||
|
||
const mqttCertPath = process.env.TTN_MQTT_CERT || '/etc/ttn/mqtt-ca.pem'; | ||
var options = { | ||
const mqttCertPath = process.env.TTN_MQTT_CERT || '/etc/ttn/mqtt-ca.pem' | ||
|
||
const options = { | ||
protocol: 'mqtts', | ||
ca: fs.readFileSync(mqttCertPath), | ||
}; | ||
} | ||
|
||
var logger = new ttnlog.Logger('azure', appId, processId); | ||
const bridge = new ttnazureiot.Bridge(region, appId, accessKey, hubName, keyName, key, options); | ||
const bridge = new ttnazureiot.Bridge(region, appId, accessKey, hubName, keyName, key, options) | ||
|
||
bridge.on('info', message => { | ||
logger.log(ttnlog.Levels.INFO, message); | ||
}); | ||
console.log('[INFO]', message) | ||
}) | ||
|
||
bridge.on('error', message => { | ||
logger.log(ttnlog.Levels.ERROR, message); | ||
}); | ||
console.warn('[ERROR]', message) | ||
}) | ||
|
||
bridge.on('warn', message => { | ||
logger.log(ttnlog.Levels.WARN, message); | ||
}); | ||
console.warn('[WARN]', message) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,108 +1,108 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
const util = require('util'); | ||
const ttn = require('ttn'); | ||
const iothub = require('azure-iothub'); | ||
const device = require('azure-iot-device'); | ||
const amqp = require('azure-iot-device-amqp'); | ||
const common = require('azure-iot-common'); | ||
const EventEmitter = require('events'); | ||
const util = require('util') | ||
const ttn = require('ttn') | ||
const iothub = require('azure-iothub') | ||
const device = require('azure-iot-device') | ||
const amqp = require('azure-iot-device-amqp') | ||
const common = require('azure-iot-common') | ||
const EventEmitter = require('events') | ||
|
||
const SAK_CONNECTION_STRING = 'HostName=%s.azure-devices.net;SharedAccessKeyName=%s;SharedAccessKey=%s'; | ||
const DEVICE_CONNECTION_STRING = 'HostName=%s.azure-devices.net;DeviceId=%%s;SharedAccessKey=%%s'; | ||
const SAK_CONNECTION_STRING = 'HostName=%s.azure-devices.net;SharedAccessKeyName=%s;SharedAccessKey=%s' | ||
const DEVICE_CONNECTION_STRING = 'HostName=%s.azure-devices.net;DeviceId=%%s;SharedAccessKey=%%s' | ||
|
||
const Bridge = class Bridge extends EventEmitter { | ||
constructor(region, appId, accessKey, hubName, keyName, key, options) { | ||
super(); | ||
options = options || {}; | ||
super() | ||
options = options || {} | ||
|
||
this._createMessage = options.createMessage || function(deviceId, message) { | ||
this._createMessage = options.createMessage || function (deviceId, message) { | ||
const metadata = { | ||
deviceId: deviceId, | ||
time: message.metadata.time, | ||
raw: message.payload_raw.toString('base64') | ||
}; | ||
return Object.assign({}, message.payload_fields, metadata); | ||
} | ||
return Object.assign({}, message.payload_fields, metadata) | ||
} | ||
|
||
if (!appId || !accessKey || !region || !hubName || !keyName || !key) { | ||
throw new Error('Invalid arguments') | ||
} | ||
|
||
this.registry = iothub.Registry.fromConnectionString(util.format(SAK_CONNECTION_STRING, hubName, keyName, key)); | ||
this.deviceConnectionString = util.format(DEVICE_CONNECTION_STRING, hubName); | ||
this.devices = {}; | ||
this.registry = iothub.Registry.fromConnectionString(util.format(SAK_CONNECTION_STRING, hubName, keyName, key)) | ||
this.deviceConnectionString = util.format(DEVICE_CONNECTION_STRING, hubName) | ||
this.devices = {} | ||
|
||
this.ttnClient = new ttn.Client(region, appId, accessKey, options); | ||
this.ttnClient.on('connect', super.emit.bind(this, 'info', 'ttn-connect')); | ||
this.ttnClient.on('error', super.emit.bind(this, 'error')); | ||
this.ttnClient.on('message', this._handleMessage.bind(this)); | ||
this.ttnClient = new ttn.data.MQTT(region, appId, accessKey, options) | ||
this.ttnClient.on('connect', super.emit.bind(this, 'info', 'ttn-connect')) | ||
this.ttnClient.on('error', super.emit.bind(this, 'error')) | ||
this.ttnClient.on('message', this._handleMessage.bind(this)) | ||
} | ||
|
||
_getDevice(deviceId) { | ||
if (this.devices[deviceId]) { | ||
return Promise.resolve(this.devices[deviceId]); | ||
return Promise.resolve(this.devices[deviceId]) | ||
} | ||
|
||
return new Promise((resolve, reject) => { | ||
const device = new iothub.Device(null); | ||
device.deviceId = deviceId; | ||
const device = new iothub.Device(null) | ||
device.deviceId = deviceId | ||
this.registry.create(device, (err, deviceInfo) => { | ||
if (!err) { | ||
resolve(deviceInfo); | ||
} else { | ||
if (err) { | ||
// The device probably exists | ||
this.registry.get(device.deviceId, (err, deviceInfo) => { | ||
if (err) { | ||
reject(err); | ||
reject(err) | ||
} else { | ||
resolve(deviceInfo); | ||
resolve(deviceInfo) | ||
} | ||
}); | ||
}) | ||
} else { | ||
resolve(deviceInfo) | ||
} | ||
}); | ||
}) | ||
}).then(deviceInfo => { | ||
const key = deviceInfo.authentication.symmetricKey.primaryKey; | ||
const connectionString = util.format(this.deviceConnectionString, deviceId, key); | ||
const client = amqp.clientFromConnectionString(connectionString); | ||
const key = deviceInfo.authentication.symmetricKey.primaryKey | ||
const connectionString = util.format(this.deviceConnectionString, deviceId, key) | ||
const client = amqp.clientFromConnectionString(connectionString) | ||
return new Promise((resolve, reject) => { | ||
client.open(err => { | ||
if (err) { | ||
reject(err); | ||
reject(err) | ||
} else { | ||
this.devices[deviceId] = client; | ||
resolve(client); | ||
this.devices[deviceId] = client | ||
resolve(client) | ||
} | ||
}); | ||
}); | ||
}); | ||
}) | ||
}) | ||
}) | ||
} | ||
|
||
_handleMessage(deviceId, data) { | ||
this.emit('info', `${deviceId}: Handling message`); | ||
this.emit('info', `${deviceId}: Handling message`) | ||
|
||
this._getDevice(deviceId).then(deviceInfo => { | ||
const message = JSON.stringify(this._createMessage(deviceId, data)); | ||
this._getDevice(deviceId) | ||
.then(deviceInfo => { | ||
const message = JSON.stringify(this._createMessage(deviceId, data)) | ||
|
||
deviceInfo.sendEvent(new device.Message(message), (err, res) => { | ||
if (err) { | ||
this.emit('warn', `${deviceId}: Could not send event: ${err}. Closing connection`); | ||
deviceInfo.close(err => { | ||
// Delete reference even if close failed | ||
delete this.devices[deviceId]; | ||
}); | ||
this.emit('error', err); | ||
} else { | ||
this.emit('info', `${deviceId}: Handled message ${message}`); | ||
} | ||
}); | ||
}) | ||
.catch(err => { | ||
this.emit('error', `${deviceId}: Could not get device: ${err}`); | ||
}); | ||
deviceInfo.sendEvent(new device.Message(message), (err, res) => { | ||
if (err) { | ||
this.emit('error', `${deviceId}: Could not send event: ${err}. Closing connection`) | ||
deviceInfo.close(err => { | ||
// Delete reference even if close failed | ||
delete this.devices[deviceId] | ||
}) | ||
} else { | ||
this.emit('info', `${deviceId}: Handled message ${message}`) | ||
} | ||
}) | ||
}) | ||
.catch(err => { | ||
this.emit('error', `${deviceId}: Could not get device: ${err}`) | ||
}) | ||
} | ||
} | ||
|
||
module.exports = { | ||
Bridge: Bridge | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,26 @@ | ||
{ | ||
"name": "ttn-azure-iothub", | ||
"version": "1.0.0-5", | ||
"version": "2.0.0", | ||
"description": "The Things Network integration with Azure IoT Hub", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/TheThingsNetwork/azure-integration.git" | ||
"url": "git://github.com/TheThingsNetwork/integration-azure.git" | ||
}, | ||
"author": "Johan Stokking <johan@thethingsnetwork.org>", | ||
"license": "MIT", | ||
"bugs": { | ||
"url": "https://github.com/TheThingsNetwork/azure-integration/issues" | ||
"url": "https://github.com/TheThingsNetwork/integration-azure/issues" | ||
}, | ||
"homepage": "https://github.com/TheThingsNetwork/azure-integration#readme", | ||
"homepage": "https://github.com/TheThingsNetwork/integration-azure#readme", | ||
"dependencies": { | ||
"azure-iot-common": "^1.1.0", | ||
"azure-iot-device": "^1.1.0", | ||
"azure-iot-device-amqp": "^1.1.0", | ||
"azure-iothub": "^1.1.0", | ||
"ttn": "^2.0.0-7" | ||
"ttn": "^2.1.1" | ||
} | ||
} |