Skip to content

Commit 79034b2

Browse files
authored
Split Client into Client and ModuleClient (#5)
1 parent 2024f9c commit 79034b2

File tree

6 files changed

+1262
-831
lines changed

6 files changed

+1262
-831
lines changed

app/index.js

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,54 @@
1-
"use strict";
1+
'use strict';
22

3-
var Generator = require("yeoman-generator");
4-
var path = require("path");
5-
var yosay = require("yosay");
3+
var Generator = require('yeoman-generator');
4+
var path = require('path');
5+
var yosay = require('yosay');
66

77
module.exports = class extends Generator {
88
constructor(args, opts) {
99
super(args, opts);
1010

11-
this.option("name", {
12-
desc: "Module name",
13-
alias: "n",
11+
this.option('name', {
12+
desc: 'Module name',
13+
alias: 'n',
1414
type: String
1515
});
16-
this.option("repository", {
17-
desc: "Docker image repository for your module",
18-
alias: "r",
16+
this.option('repository', {
17+
desc: 'Docker image repository for your module',
18+
alias: 'r',
1919
type: String
2020
});
2121
}
2222

2323
prompting() {
24-
this.log(yosay("Hey You\r\nWelcome to Azure IoT Edge module generator"));
24+
this.log(yosay('Hey You\r\nWelcome to Azure IoT Edge module generator'));
2525

2626
return this.prompt([
2727
{
28-
type: "input",
29-
name: "name",
30-
message: "What's the name of your module?",
31-
default: "SampleModule",
28+
type: 'input',
29+
name: 'name',
30+
message: 'What\'s the name of your module?',
31+
default: 'SampleModule',
3232
when: () => {
3333
return !this.options.name; // skip the prompt when the value is already passed as a command option
3434
},
3535
validate: (name) => {
3636
if (!name) {
37-
return "Module name could not be empty";
37+
return 'Module name could not be empty';
3838
}
39-
if (name.startsWith("_") || name.endsWith("_")) {
40-
return "Module name must not start or end with the symbol _";
39+
if (name.startsWith('_') || name.endsWith('_')) {
40+
return 'Module name must not start or end with the symbol _';
4141
}
4242
if (name.match(/[^a-zA-Z0-9\_]/)) {
43-
return "Module name must contain only alphanumeric characters or the symbol _";
43+
return 'Module name must contain only alphanumeric characters or the symbol _';
4444
}
4545
return true;
4646
}
4747
},
4848
{
49-
type: "input",
50-
name: "repository",
51-
"message": "What's the Docker image repository for your module?",
49+
type: 'input',
50+
name: 'repository',
51+
message: 'What\'s the Docker image repository for your module?',
5252
when: () => {
5353
return !this.options.repository; // skip the prompt when the value is already passed as a command option
5454
},
@@ -66,13 +66,13 @@ module.exports = class extends Generator {
6666
writing() {
6767
this.log(`Creating ${this.name} module at ${this.repository} ...`);
6868

69-
this._copyStatic("gitignore", ".gitignore");
70-
this._copyStatic("app.js");
71-
this._copyStatic("Dockerfile");
72-
this._copyStatic("Dockerfile.windows-amd64");
69+
this._copyStatic('gitignore', '.gitignore');
70+
this._copyStatic('app.js');
71+
this._copyStatic('Dockerfile');
72+
this._copyStatic('Dockerfile.windows-amd64');
7373

74-
this._copyTemplate("module.json", { repository: this.repository });
75-
this._copyTemplate("package.json", { name: this.name })
74+
this._copyTemplate('module.json', { repository: this.repository });
75+
this._copyTemplate('package.json', { name: this.name })
7676
}
7777

7878
_copyStatic(from, to = undefined) {

app/templates/app.js

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,18 @@
11
'use strict';
22

33
var Transport = require('azure-iot-device-mqtt').Mqtt;
4-
var Client = require('azure-iot-device').Client;
4+
var Client = require('azure-iot-device').ModuleClient;
55
var Message = require('azure-iot-device').Message;
6-
var fs = require('fs');
76

8-
var connectionString = process.env.EdgeHubConnectionString;
9-
var caCertFile = process.env.EdgeModuleCACertificateFile;
10-
11-
var client = Client.fromConnectionString(connectionString, Transport);
12-
console.log('Connection String: ' + connectionString);
13-
14-
client.on('error', function (err) {
15-
console.error(err.message);
16-
});
17-
18-
client.setOptions({
19-
ca: fs.readFileSync(caCertFile).toString('ascii')
20-
}, function (err) {
7+
Client.fromEnvironment(Transport, function (err, client) {
218
if (err) {
229
console.log('error:' + err);
2310
} else {
24-
// connect to the edge instance
11+
client.on('error', function (err) {
12+
console.error(err.message);
13+
});
14+
15+
// connect to the Edge instance
2516
client.open(function (err) {
2617
if (err) {
2718
console.error('Could not connect: ' + err.message);
@@ -30,15 +21,15 @@ client.setOptions({
3021

3122
// Act on input messages to the module.
3223
client.on('inputMessage', function (inputName, msg) {
33-
pipeMessage(inputName, msg);
24+
pipeMessage(client, inputName, msg);
3425
});
3526
}
3627
});
3728
}
3829
});
3930

4031
// This function just pipes the messages without any change.
41-
function pipeMessage(inputName, msg) {
32+
function pipeMessage(client, inputName, msg) {
4233
client.complete(msg, printResultFor('Receiving message'));
4334

4435
if (inputName === 'input1') {

app/templates/package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "<%= name %>",
33
"version": "0.0.1",
44
"dependencies": {
5-
"azure-iot-device": "1.4.0-modules-preview",
6-
"azure-iot-device-mqtt": "1.4.0-modules-preview"
5+
"azure-iot-device": "1.7.0-modules-preview.2",
6+
"azure-iot-device-mqtt": "1.7.0-modules-preview.2"
77
}
88
}

0 commit comments

Comments
 (0)