Skip to content
This repository has been archived by the owner on Apr 23, 2022. It is now read-only.

Add simulator, add device option to command filter #336

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
7 changes: 7 additions & 0 deletions resources/boardlist.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
"detailInfo": "Raspberry Pi",
"helpUrl": "https://www.raspberrypi.org/",
"exampleUrl": "https://raw.githubusercontent.com/VSChina/azureiotdevkit_tools/gallery/workbench-example-raspi.json"
},
{
"name": "Simulator",
"id": "simulator",
"detailInfo": "Simulate the connection between device and IoT hub",
"helpUrl": "https://www.raspberrypi.org/",
Copy link
Member

@Sneezry Sneezry Jul 12, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

helpUrl should be changed to IoT Workbench repo URL

"exampleUrl": "https://raw.githubusercontent.com/ab-sin-the/my-test-folder/master/simulator-example.json"
}
]
}
Empty file added resources/simulator/basic.js
Empty file.
8 changes: 8 additions & 0 deletions resources/simulator/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "iot-workbench-raspi",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace iot-workbench-raspi with other string. raspi stands for Raspberry Pi

"version": "0.1.0",
"main": "app.js",
"dependencies": {
"azure-iot-device-mqtt": "^1.4.3"
}
}
33 changes: 33 additions & 0 deletions resources/simulator/simple_iothub.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
const clientFromConnectionString = require('azure-iot-device-mqtt').clientFromConnectionString;
const Message = require('azure-iot-device').Message;
const connectionString = require('./config.json').connectionString;
const client = clientFromConnectionString(connectionString);

client.open(err => {
if (err) {
console.error('Could not connect: ' + err);
process.exit(1);
} else {
const data = {
topic: 'iot'
};
const message = new Message(JSON.stringify(data));

setInterval(() => {
client.sendEvent(message, err => {
if (err) {
console.warn('Send message to IoT Hub failed: ' + err.toString());
} else {
console.log('Message sent to IoT Hub.');
}
});
}, 2000);

client.on('message', msg => {
console.log(msg);
client.complete(msg, () => {
console.log('completed');
});
});
}
});
25 changes: 25 additions & 0 deletions resources/simulator/template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"templates": [
{
"description": "",
"detail": "Create project with device only code.",
"label": "Device only",
"type": "Basic",
"sketch": "basic.js"
},
{
"description": "",
"detail": "Create project that connects to Azure IoT Hub, such as sending sensor data to cloud.",
"label": "With Azure IoT Hub",
"type": "IotHub",
"sketch": "simple_iothub.js"
},
{
"description": "",
"detail": "Create project that connects to Azure IoT Hub and processes device data further in Azure Functions.",
"label": "With Azure Functions",
"type": "AzureFunctions",
"sketch": "simple_iothub.js"
}
]
}
51 changes: 15 additions & 36 deletions src/DeviceOperator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import {TelemetryContext} from './telemetry';
import {Board, BoardQuickPickItem} from './Models/Interfaces/Board';
import {ArduinoPackageManager} from './ArduinoPackageManager';
import {BoardProvider} from './boardProvider';
import {ConfigHandler} from './configHandler';
import {ConfigKey} from './constants';

export class DeviceOperator {
async compile(
Expand Down Expand Up @@ -69,45 +71,22 @@ export class DeviceOperator {
context: vscode.ExtensionContext, channel: vscode.OutputChannel,
telemetryContext: TelemetryContext) {
const boardProvider = new BoardProvider(context);
const boardItemList: BoardQuickPickItem[] = [];
const boards = boardProvider.list.filter(board => board.installation);
boards.forEach((board: Board) => {
boardItemList.push({
name: board.name,
id: board.id,
detailInfo: board.detailInfo,
label: board.name,
description: board.detailInfo,
});
});

const boardSelection = await vscode.window.showQuickPick(boardItemList, {
ignoreFocusOut: true,
matchOnDescription: true,
matchOnDetail: true,
placeHolder: 'Select a board',
});

if (!boardSelection) {
telemetryContext.properties.errorMessage = 'Board selection canceled.';
telemetryContext.properties.result = 'Canceled';
return false;
} else {
telemetryContext.properties.board = boardSelection.label;
try {
const board = boardProvider.find({id: boardSelection.id});

if (board) {
await ArduinoPackageManager.installBoard(board);
}
} catch (error) {
throw new Error(`Device package for ${
boardSelection.label} installation failed: ${error.message}`);
const boardId = ConfigHandler.get<string>(ConfigKey.boardId);
const board = boardProvider.find({id: boardId});
if (board === undefined) {
throw new Error(`Board Id ${boardId} not found.`);
}
try {
telemetryContext.properties.board = board.name;
if (board) {
await ArduinoPackageManager.installBoard(board);
}
} catch (error) {
throw new Error(`Device package for ${board.name} installation failed: ${
error.message}`);
}

vscode.window.showInformationMessage(
`Device package for ${boardSelection.label} has been installed.`);
`Device package for ${board.name} has been installed.`);
return true;
}
}
6 changes: 6 additions & 0 deletions src/Models/Interfaces/CommandItem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ export interface CommandItem extends vscode.QuickPickItem {
* the specific field.
*/
only?: string;
/**
* Show the menu item when only the
* deviceId of current workspace is in
* variable 'deviceIds'
*/
deviceIds?: string[];
}
3 changes: 2 additions & 1 deletion src/Models/Interfaces/Device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import {Uploadable} from './Uploadable';
export enum DeviceType {
MXChip_AZ3166 = 1,
IoT_Button = 2,
Raspberry_Pi = 3
Raspberry_Pi = 3,
Simulator = 4
}

export interface Device extends Component, Compilable, Uploadable {
Expand Down
9 changes: 9 additions & 0 deletions src/Models/IoTProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {IoTButtonDevice} from './IoTButtonDevice';
import {IoTHub} from './IoTHub';
import {IoTHubDevice} from './IoTHubDevice';
import {RaspberryPiDevice} from './RaspberryPiDevice';
import {Simulator} from './Simulator';

const constants = {
deviceDefaultFolderName: 'Device',
Expand Down Expand Up @@ -101,6 +102,10 @@ export class IoTProject {
const device = new RaspberryPiDevice(
this.extensionContext, deviceLocation, this.channel);
this.componentList.push(device);
} else if (boardId === Simulator.boardId) {
const device =
new Simulator(this.extensionContext, deviceLocation, this.channel);
this.componentList.push(device);
}
}

Expand Down Expand Up @@ -266,6 +271,10 @@ export class IoTProject {
device = new RaspberryPiDevice(
this.extensionContext, deviceDir, this.channel,
projectTemplateItem.sketch);
} else if (boardId === Simulator.boardId) {
device = new Simulator(
this.extensionContext, deviceDir, this.channel,
projectTemplateItem.sketch);
} else {
throw new Error('The specified board is not supported.');
}
Expand Down
Loading