This repository has been archived by the owner on Nov 16, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add IoTHub writer module and update the documentation.
- Loading branch information
Su Shi
committed
Mar 20, 2017
1 parent
e8fab8d
commit c23c553
Showing
11 changed files
with
213 additions
and
24 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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
# top-most EditorConfig file | ||
root = true | ||
|
||
# Unix-style newlines with a newline ending every file | ||
[*] | ||
end_of_line = lf | ||
insert_final_newline = true | ||
|
||
# Matches multiple files with brace expansion notation | ||
# Set default charset | ||
[*] | ||
charset = utf-8 | ||
|
||
# 2 space indentation | ||
[*] | ||
indent_style = space | ||
indent_size = 2 | ||
|
||
# Tab indentation (no size specified) | ||
[*] | ||
indent_style = space | ||
|
||
|
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
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
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,16 +1,41 @@ | ||
# Simple Gateway Modules | ||
This sample simply shows: | ||
- How to write modules(sensor.js, printer.js) in Javascript. | ||
- How to custmize the gateway runtime(gw.config.json). | ||
- How to initialize and start the gateway application in Javascript(index.js). | ||
- How to write modules(sensor.js, printer.js, iothub_writer.js) in Javascript. | ||
- How to custmize the gateway runtime(gw.[local|cloud].config.json). | ||
- How to initialize and start the gateway application in Javascript(app.js). | ||
|
||
# Technical Details | ||
## Callbacks | ||
Each module(sensor/printer) needs to implement callbacks for different events: | ||
Each module(sensor/printer/iothub_writer) needs to implement callbacks for different events: | ||
1. create - module is created. | ||
2. start - module starts. | ||
3. receive - receive message from broker. | ||
4. destroy - modules is destroyed. | ||
## Data pipeline | ||
The configuration JSON file(gw.config.json) to initialize each gateway instance will need to define | ||
the data links for different module pair<sensor, printer>. | ||
The configuration JSON file(gw.[local|cloud].config.json) to initialize each gateway instance will | ||
need to define the data links for different module pairs. | ||
## Connect to Azure IoT Hub | ||
iothub_writer module shows you how to establish connection between gateway application and azure iot | ||
hub. Below are the steps to configure and run it: | ||
1. Go to [Azure IoT Hub portal](https://azure.microsoft.com/en-us/services/iot-hub/) to create an | ||
IoT hub instance with your own Azure subscription account to connecte your gateway application. | ||
2. Update `gw.cloud.config.json` by replacing `<IoT Hub device connection string>` (in the | ||
`iothub_writer` module config JSON (*shown below*)) with your actual IoT Hub device connection | ||
string | ||
``` | ||
{ | ||
"name": "iothub_writer", | ||
"loader": { | ||
"name": "node", | ||
"entrypoint": { | ||
"main.path": "modules/iothub_writer.js" | ||
} | ||
}, | ||
"args": { | ||
"connection_string": "<IoT Hub device connection string>" | ||
} | ||
} | ||
``` | ||
3. `cd modules & npm install` | ||
4. `cd .. & npm install` | ||
5. `npm run cloud` |
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 |
---|---|---|
@@ -0,0 +1,24 @@ | ||
(function() { | ||
'use strict'; | ||
|
||
const Gateway = require('azure-iot-gateway'); | ||
let config_path = null; | ||
|
||
// node app.js [local | cloud ] | ||
if (process.argv.length < 3) { | ||
throw 'Calling pattern should be node app.js [local | cloud].'; | ||
} | ||
|
||
const option = process.argv[2]; | ||
|
||
if (option === 'local') { | ||
config_path = './gw.local.config.json'; | ||
} else if (option === 'cloud') { | ||
config_path = './gw.cloud.config.json'; | ||
} else { | ||
throw 'Invalid option to start app.js !'; | ||
} | ||
|
||
const gw = new Gateway(config_path); | ||
gw.run(); | ||
})(); |
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 |
---|---|---|
@@ -0,0 +1,34 @@ | ||
{ | ||
"loaders": [{ | ||
"type": "node", | ||
"name": "node" | ||
}], | ||
"modules": [{ | ||
"name": "node_sensor", | ||
"loader": { | ||
"name": "node", | ||
"entrypoint": { | ||
"main.path": "modules/sensor.js" | ||
} | ||
}, | ||
"args": null | ||
}, | ||
{ | ||
"name": "iothub_writer", | ||
"loader": { | ||
"name": "node", | ||
"entrypoint": { | ||
"main.path": "modules/iothub_writer.js" | ||
} | ||
}, | ||
"args": { | ||
"connection_string": "<IoT Hub device connection string>" | ||
} | ||
} | ||
], | ||
"links": [{ | ||
"source": "node_sensor", | ||
"sink": "node_printer" | ||
} | ||
] | ||
} |
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
This file was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,77 @@ | ||
'use strict'; | ||
|
||
let Protocol = require('azure-iot-device-amqp').Amqp; | ||
let Client = require('azure-iot-device').Client; | ||
let Message = require('azure-iot-device').Message; | ||
|
||
class IotHubWriterModule { | ||
constructor() { | ||
this.iothub_client = null; | ||
this.connected = false; | ||
} | ||
|
||
on_connect(err) { | ||
if (err) { | ||
console.error(`Could not connect to IoT Hub. Error: ${err.message}`); | ||
} else { | ||
this.connected = true; | ||
this.iothub_client.on('error', this.on_error.bind(this)); | ||
this.iothub_client.on('disconnect', this.on_disconnect.bind(this)); | ||
} | ||
} | ||
|
||
on_error(err) { | ||
console.error(`Azure IoT Hub error: ${err.message}`); | ||
} | ||
|
||
on_disconnect() { | ||
console.log('Got disconnected from Azure IoT Hub.'); | ||
this.connected = false; | ||
} | ||
|
||
create(broker, configuration) { | ||
this.broker = broker; | ||
this.configuration = configuration; | ||
|
||
if (this.configuration && this.configuration.connection_string) { | ||
// open a connection to the IoT Hub | ||
this.iothub_client = Client.fromConnectionString(this.configuration.connection_string, Protocol); | ||
this.iothub_client.open(this.on_connect.bind(this)); | ||
|
||
return true; | ||
} else { | ||
console.error('This module requires the connection string to be passed in via configuration.'); | ||
return false; | ||
} | ||
} | ||
|
||
receive(message) { | ||
if (this.connected) { | ||
var m = new Message(message.content ? message.content.buffer : []); | ||
if (message.properties) { | ||
for (var prop in message.properties) { | ||
m.properties.add(prop, message.properties[prop]); | ||
} | ||
} | ||
|
||
this.iothub_client.sendEvent(m, err => { | ||
if (err) { | ||
console.error(`An error occurred when sending message to Azure IoT Hub: ${err.toString()}`); | ||
} | ||
}); | ||
} | ||
} | ||
|
||
destroy() { | ||
console.log('iothub_writer.destroy'); | ||
if (this.connected) { | ||
this.iothub_client.close(err => { | ||
if (err) { | ||
console.error(`An error occurred when disconnecting from Azure IoT Hub: ${err.toString()}`); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
|
||
module.exports = new IotHubWriterModule(); |
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 |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"name": "azure-iot-gateway-samples", | ||
"version": "0.1.0", | ||
"description": "", | ||
"main": "iothub_writer.js", | ||
"author": "Microsoft", | ||
"license": "MIT", | ||
"dependencies": { | ||
"azure-iot-device": "^1.0.6", | ||
"azure-iot-device-amqp": "^1.0.6" | ||
} | ||
} |
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