JavaScript code snippet to send/monitor D2C message
This commit is contained in:
Родитель
41826adb08
Коммит
810e2079f6
|
@ -3,6 +3,7 @@ typings/**
|
|||
out/test/**
|
||||
test/**
|
||||
src/**
|
||||
images/**
|
||||
**/*.map
|
||||
.gitignore
|
||||
tsconfig.json
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
### 0.0.8 (2017-02-28)
|
||||
### 0.0.10 (2017-03-06)
|
||||
* JavaScript code snippet to send/monitor D2C message
|
||||
|
||||
### 0.0.9 (2017-02-28)
|
||||
* Show verbose info when monitoring messages
|
||||
* Output refinement for JSON object
|
||||
|
||||
|
|
29
README.md
29
README.md
|
@ -10,6 +10,8 @@ Toolkit makes Azure IoT Development easier. For more awesome Azure IoT projects
|
|||
|
||||
[x] Monitor device-to-cloud messages
|
||||
|
||||
[x] Code snippet to send/monitor D2C message for IoT Hub
|
||||
|
||||
[x] Send messages to Azure Event Hub
|
||||
|
||||
[x] Monitor Event Hub messages
|
||||
|
@ -53,6 +55,17 @@ Toolkit makes Azure IoT Development easier. For more awesome Azure IoT projects
|
|||
|
||||
![Monitor](images/monitor.gif)
|
||||
|
||||
* Code Snippets
|
||||
|
||||
![Snippet](images/snippet.gif)
|
||||
|
||||
| Trigger | Content |
|
||||
| ---- | ---- |
|
||||
| iotSendD2CMessage | Send D2C message to IoT Hub |
|
||||
| iotMonitorD2CMessage | Monitor D2C message for IoT Hub |
|
||||
> After code snippet is created, you need to install corresponding npm package (e.g. [azure-iot-device-mqtt](https://www.npmjs.com/package/azure-iot-device-mqtt)) to run the code snippet.
|
||||
> If you want to 'Run Code' directly, you need to install [Code Runner](https://marketplace.visualstudio.com/items?itemName=formulahendry.code-runner).
|
||||
|
||||
* Send/monitor messages for Azure Event Hub
|
||||
|
||||
![Event Hub](images/event-hub.gif)
|
||||
|
@ -65,14 +78,14 @@ Toolkit makes Azure IoT Development easier. For more awesome Azure IoT projects
|
|||
|
||||
![Remote](images/remote.gif)
|
||||
|
||||
| Config | description |
|
||||
| ---- | ---- |
|
||||
| azure-iot-toolkit.localFolder | The folder of current machine to deploy |
|
||||
| azure-iot-toolkit.remoteFolder | The folder of remote machine to deploy |
|
||||
| azure-iot-toolkit.host | The hostname or IP address of remote machine |
|
||||
| azure-iot-toolkit.username | The username of remote machine |
|
||||
| azure-iot-toolkit.password | The password of remote machine |
|
||||
| azure-iot-toolkit.command | The command to run in remote machine |
|
||||
| Config | description |
|
||||
| ---- | ---- |
|
||||
| azure-iot-toolkit.localFolder | The folder of current machine to deploy |
|
||||
| azure-iot-toolkit.remoteFolder | The folder of remote machine to deploy |
|
||||
| azure-iot-toolkit.host | The hostname or IP address of remote machine |
|
||||
| azure-iot-toolkit.username | The username of remote machine |
|
||||
| azure-iot-toolkit.password | The password of remote machine |
|
||||
| azure-iot-toolkit.command | The command to run in remote machine |
|
||||
|
||||
* Discover Ethernet, USB serial, WiFi devices
|
||||
1. Install Node.js or install [device-discovery-cli](https://github.com/Azure/device-discovery-cli):
|
||||
|
|
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 246 KiB |
Двоичный файл не отображается.
После Ширина: | Высота: | Размер: 249 KiB |
27
package.json
27
package.json
|
@ -1,15 +1,17 @@
|
|||
{
|
||||
"name": "azure-iot-toolkit",
|
||||
"displayName": "Azure IoT Toolkit",
|
||||
"description": "Interact with Azure IoT Hub and Azure Event Hub; IoT Device Management; Discover Ethernet, USB serial, WiFi connected devices; Deploy and run in remote machine (e.g. Raspberry Pi, Arduino)",
|
||||
"version": "0.0.9",
|
||||
"description": "Interact with Azure IoT Hub and Azure Event Hub; IoT Device Management; IoT Hub Code Snippets; Discover Ethernet, USB serial, WiFi connected devices; Deploy and run in remote machine (e.g. Raspberry Pi, Arduino)",
|
||||
"version": "0.0.10",
|
||||
"publisher": "formulahendry",
|
||||
"icon": "images/logo.png",
|
||||
"engines": {
|
||||
"vscode": "^1.0.0"
|
||||
},
|
||||
"categories": [
|
||||
"Other"
|
||||
"Other",
|
||||
"Languages",
|
||||
"Snippets"
|
||||
],
|
||||
"keywords": [
|
||||
"azure",
|
||||
|
@ -28,16 +30,7 @@
|
|||
"url": "https://github.com/formulahendry/vscode-azure-iot-toolkit.git"
|
||||
},
|
||||
"activationEvents": [
|
||||
"onCommand:azure-iot-toolkit.sendD2CMessage",
|
||||
"onCommand:azure-iot-toolkit.startMonitorIoTHubMessage",
|
||||
"onCommand:azure-iot-toolkit.sendMessageToEventHub",
|
||||
"onCommand:azure-iot-toolkit.startMonitorEventHubMessage",
|
||||
"onCommand:azure-iot-toolkit.listDevice",
|
||||
"onCommand:azure-iot-toolkit.createDevice",
|
||||
"onCommand:azure-iot-toolkit.deleteDevice",
|
||||
"onCommand:azure-iot-toolkit.discoverDevice",
|
||||
"onCommand:azure-iot-toolkit.deploy",
|
||||
"onCommand:azure-iot-toolkit.run"
|
||||
"*"
|
||||
],
|
||||
"main": "./out/src/extension",
|
||||
"contributes": {
|
||||
|
@ -258,7 +251,13 @@
|
|||
"description": "The command to run in remote machine"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"snippets": [
|
||||
{
|
||||
"language": "javascript",
|
||||
"path": "./snippets/javascript.json"
|
||||
}
|
||||
]
|
||||
},
|
||||
"scripts": {
|
||||
"vscode:prepublish": "tsc -p ./",
|
||||
|
|
|
@ -0,0 +1,73 @@
|
|||
{
|
||||
"Send D2C message": {
|
||||
"prefix": "iotSendD2CMessage",
|
||||
"description": "Send D2C message",
|
||||
"body": [
|
||||
"'use strict';",
|
||||
"",
|
||||
"var clientFromConnectionString = require('azure-iot-device-mqtt').clientFromConnectionString;",
|
||||
"var Message = require('azure-iot-device').Message;",
|
||||
"var connectionString = '<deviceConnectionString>';",
|
||||
"var client = clientFromConnectionString(connectionString);",
|
||||
"",
|
||||
"function printResultFor(op, client) {",
|
||||
"\treturn function printResult(err, res) {",
|
||||
"\t\tif (err) console.log(op + ' error: ' + err.toString());",
|
||||
"\t\tif (res) console.log(op + ' status: ' + res.constructor.name);",
|
||||
"\t\tclient.close();",
|
||||
"\t};",
|
||||
"}",
|
||||
"",
|
||||
"var connectCallback = function (err) {",
|
||||
"\tif (err) {",
|
||||
"\t\tconsole.log('Could not connect: ' + err);",
|
||||
"\t} else {",
|
||||
"\t\tconsole.log('Client connected');",
|
||||
"\t\tvar data = 'Hello IoT Hub!';",
|
||||
"\t\tvar message = new Message(data);",
|
||||
"\t\tconsole.log('Sending message: ' + message.getData());",
|
||||
"\t\tclient.sendEvent(message, printResultFor('send', client));",
|
||||
"\t}",
|
||||
"};",
|
||||
"",
|
||||
"client.open(connectCallback);"
|
||||
]
|
||||
},
|
||||
"Monitor D2C message": {
|
||||
"prefix": "iotMonitorD2CMessage",
|
||||
"description": "Monitor D2C message",
|
||||
"body": [
|
||||
"'use strict';",
|
||||
"",
|
||||
"var EventHubClient = require('azure-event-hubs').Client;",
|
||||
"",
|
||||
"var connectionString = '<iotHubConnectionString>';",
|
||||
"var printError = function (err) {",
|
||||
"\tconsole.log(err.message);",
|
||||
"};",
|
||||
"",
|
||||
"var printMessage = function (message) {",
|
||||
"\tconsole.log('Message received: ');",
|
||||
"\tconsole.log(JSON.stringify(message.body));",
|
||||
"\tconsole.log('');",
|
||||
"};",
|
||||
"",
|
||||
"var client = EventHubClient.fromConnectionString(connectionString);",
|
||||
"console.log('Start monitoring IoT Hub...')",
|
||||
"client.open()",
|
||||
"\t.then(client.getPartitionIds.bind(client))",
|
||||
"\t.then(function (partitionIds) {",
|
||||
"\t\treturn partitionIds.map(function (partitionId) {",
|
||||
"\t\t\treturn client.createReceiver('$$Default', partitionId, {",
|
||||
"\t\t\t\t'startAfterTime': Date.now()",
|
||||
"\t\t\t}).then(function (receiver) {",
|
||||
"\t\t\t\tconsole.log('Created partition receiver: ' + partitionId)",
|
||||
"\t\t\t\treceiver.on('errorReceived', printError);",
|
||||
"\t\t\t\treceiver.on('message', printMessage);",
|
||||
"\t\t\t});",
|
||||
"\t\t});",
|
||||
"\t})",
|
||||
"\t.catch(printError);"
|
||||
]
|
||||
}
|
||||
}
|
|
@ -6,6 +6,7 @@ import { DeviceDiscoverer } from "./deviceDiscoverer";
|
|||
import { DeviceExplorer } from "./deviceExplorer";
|
||||
import { EventHubMessageExplorer } from "./eventHubMessageExplorer";
|
||||
import { IoTHubMessageExplorer } from "./iotHubMessageExplorer";
|
||||
import { SnippetManager } from "./snippetManager";
|
||||
|
||||
export class AzureIoTExplorer {
|
||||
private _iotHubMessageExplorer: IoTHubMessageExplorer;
|
||||
|
@ -13,6 +14,7 @@ export class AzureIoTExplorer {
|
|||
private _deviceExplorer: DeviceExplorer;
|
||||
private _deviceDiscoverer: DeviceDiscoverer;
|
||||
private _deviceController: DeviceController;
|
||||
private _snippetManager: SnippetManager;
|
||||
|
||||
constructor(context: vscode.ExtensionContext) {
|
||||
let outputChannel = vscode.window.createOutputChannel("Azure IoT Toolkit");
|
||||
|
@ -22,6 +24,7 @@ export class AzureIoTExplorer {
|
|||
this._deviceExplorer = new DeviceExplorer(outputChannel, appInsightsClient);
|
||||
this._deviceDiscoverer = new DeviceDiscoverer(context, outputChannel, appInsightsClient);
|
||||
this._deviceController = new DeviceController(outputChannel, appInsightsClient);
|
||||
this._snippetManager = new SnippetManager(outputChannel, appInsightsClient);
|
||||
}
|
||||
|
||||
public sendD2CMessage(): void {
|
||||
|
@ -71,4 +74,8 @@ export class AzureIoTExplorer {
|
|||
public run(): void {
|
||||
this._deviceController.run();
|
||||
}
|
||||
|
||||
public replaceConnectionString(event: vscode.TextDocumentChangeEvent): void {
|
||||
this._snippetManager.replaceConnectionString(event);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -53,6 +53,8 @@ export function activate(context: vscode.ExtensionContext) {
|
|||
azureIoTExplorer.run();
|
||||
});
|
||||
|
||||
vscode.workspace.onDidChangeTextDocument((event) => azureIoTExplorer.replaceConnectionString(event));
|
||||
|
||||
context.subscriptions.push(sendD2CMessage);
|
||||
context.subscriptions.push(startMonitorIoTHubMessage);
|
||||
context.subscriptions.push(stopMonitorIoTHubMessage);
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
"use strict";
|
||||
import * as vscode from "vscode";
|
||||
import { AppInsightsClient } from "./appInsightsClient";
|
||||
import { BaseExplorer } from "./baseExplorer";
|
||||
import { Constants } from "./constants";
|
||||
import { Utility } from "./utility";
|
||||
|
||||
export class SnippetManager extends BaseExplorer {
|
||||
private connectionStringKeys = [Constants.DeviceConnectionStringKey, Constants.IotHubConnectionStringKey];
|
||||
|
||||
constructor(outputChannel: vscode.OutputChannel, appInsightsClient: AppInsightsClient) {
|
||||
super(outputChannel, appInsightsClient);
|
||||
}
|
||||
|
||||
public replaceConnectionString(event: vscode.TextDocumentChangeEvent): void {
|
||||
let changedText = event.contentChanges[0].text;
|
||||
if (/\r|\n/.exec(changedText)) {
|
||||
let editor = vscode.window.activeTextEditor;
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
let document = editor.document;
|
||||
let text = document.getText();
|
||||
let config = Utility.getConfiguration();
|
||||
this.connectionStringKeys.forEach((connectionStringKey) => {
|
||||
let connectionStringValue = config.get<string>(connectionStringKey);
|
||||
let connectionStringKeyWithAngleBracket = this.getTextWithAngleBracket(connectionStringKey);
|
||||
if (changedText.indexOf(connectionStringKeyWithAngleBracket) > -1
|
||||
&& connectionStringValue && !connectionStringValue.startsWith("<<insert")) {
|
||||
let offset = text.indexOf(connectionStringKeyWithAngleBracket);
|
||||
while (offset > -1) {
|
||||
editor.edit((editBuilder) => {
|
||||
editBuilder.replace(new vscode.Range(document.positionAt(offset),
|
||||
document.positionAt(offset + connectionStringKeyWithAngleBracket.length)),
|
||||
connectionStringValue);
|
||||
})
|
||||
offset = text.indexOf(connectionStringKeyWithAngleBracket, offset + 1);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
private getTextWithAngleBracket(text: string): string {
|
||||
return "<" + text + ">";
|
||||
}
|
||||
}
|
Загрузка…
Ссылка в новой задаче