Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Add IoTHub writer module and update the documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
Su Shi committed Mar 20, 2017
1 parent e8fab8d commit c23c553
Show file tree
Hide file tree
Showing 11 changed files with 213 additions and 24 deletions.
23 changes: 23 additions & 0 deletions .editorconfig
@@ -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


1 change: 1 addition & 0 deletions .gitattributes
Expand Up @@ -15,6 +15,7 @@
*.npmrc text eol=lf
.gitattributes text eol=lf
.gitignore text eol=lf
.editorconfig eol=lf


# Denote all files that are truly binary and should not be modified.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -9,4 +9,5 @@ This is a simple tutorial shows Javascript developer how to setup module develop
1. `git clone https://github.com/Azure-Samples/azure-iot-gateway-samples.git`
2. `cd azure-iot-gateway-samples/js`
3. `npm install` to install pre-built core runtime of gateway.
4. `npm start` to start the gateway with pre-defined modules(sensor and printer).
4. `npm run local` to start the gateway with pre-defined modules(sensor and printer).

37 changes: 31 additions & 6 deletions js/README.md
@@ -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`
24 changes: 24 additions & 0 deletions js/app.js
@@ -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();
})();
34 changes: 34 additions & 0 deletions js/gw.cloud.config.json
@@ -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"
}
]
}
16 changes: 6 additions & 10 deletions js/gw.config.json → js/gw.local.config.json
@@ -1,12 +1,9 @@
{
"loaders": [
{
"type": "node",
"name": "node"
}
],
"modules": [
{
"loaders": [{
"type": "node",
"name": "node"
}],
"modules": [{
"name": "node_printer",
"loader": {
"name": "node",
Expand All @@ -27,8 +24,7 @@
"args": null
}
],
"links": [
{
"links": [{
"source": "node_sensor",
"sink": "node_printer"
}
Expand Down
5 changes: 0 additions & 5 deletions js/index.js

This file was deleted.

77 changes: 77 additions & 0 deletions js/modules/iothub_writer.js
@@ -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();
12 changes: 12 additions & 0 deletions js/modules/package.json
@@ -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"
}
}
5 changes: 3 additions & 2 deletions js/package.json
Expand Up @@ -6,9 +6,10 @@
"type": "git",
"url": "https://github.com/Azure-Samples/azure-iot-gateway-sample"
},
"main": "index.js",
"main": "app.js",
"scripts": {
"start": "node index.js"
"local": "node app.js local",
"cloud": "node app.js cloud"
},
"author": "Microsoft Corporation",
"license": "MIT",
Expand Down

0 comments on commit c23c553

Please sign in to comment.