This repository has been archived by the owner on Apr 23, 2022. It is now read-only.
/
IoTHubDevice.ts
153 lines (133 loc) · 4.51 KB
/
IoTHubDevice.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
import * as iothub from 'azure-iothub';
import {Guid} from 'guid-typescript';
import * as vscode from 'vscode';
import {ConfigHandler} from '../configHandler';
import {ConfigKey} from '../constants';
import {getExtension} from './Apis';
import {ComponentInfo, DependencyConfig} from './AzureComponentConfig';
import {AzureUtility} from './AzureUtility';
import {extensionName} from './Interfaces/Api';
import {Component, ComponentType} from './Interfaces/Component';
import {Provisionable} from './Interfaces/Provisionable';
export class IoTHubDevice implements Component, Provisionable {
private componentType: ComponentType;
private channel: vscode.OutputChannel;
private componentId: string;
get id() {
return this.componentId;
}
dependencies: DependencyConfig[] = [];
constructor(channel: vscode.OutputChannel) {
this.componentType = ComponentType.IoTHubDevice;
this.channel = channel;
this.componentId = Guid.create().toString();
}
name = 'IoT Hub Device';
getComponentType(): ComponentType {
return this.componentType;
}
async load(): Promise<boolean> {
return true;
}
async create(): Promise<boolean> {
return true;
}
async provision(): Promise<boolean> {
const iotHubConnectionString =
ConfigHandler.get<string>(ConfigKey.iotHubConnectionString);
if (!iotHubConnectionString) {
throw new Error(
'Unable to find IoT Hub connection in the project. Please retry Azure Provision.');
}
const selection = await vscode.window.showQuickPick(
getProvisionIothubDeviceSelection(iotHubConnectionString),
{ignoreFocusOut: true, placeHolder: 'Provision IoTHub Device'});
if (!selection) {
return false;
}
const toolkit = getExtension(extensionName.Toolkit);
if (toolkit === undefined) {
const error = new Error(
'Azure IoT Hub Toolkit is not installed. Please install it from Marketplace.');
throw error;
}
let device = null;
try {
switch (selection.detail) {
case 'select':
device = await toolkit.azureIoTExplorer.getDevice(
null, iotHubConnectionString, this.channel);
if (device === undefined) {
return false;
} else {
await ConfigHandler.update(
ConfigKey.iotHubDeviceConnectionString,
device.connectionString);
}
break;
case 'create':
device = await toolkit.azureIoTExplorer.createDevice(
false, iotHubConnectionString, this.channel);
if (device === undefined) {
return false;
} else {
await ConfigHandler.update(
ConfigKey.iotHubDeviceConnectionString,
device.connectionString);
}
break;
default:
break;
}
return true;
} catch (error) {
throw error;
}
}
updateConfigSettings(componentInfo?: ComponentInfo): void {}
}
async function getProvisionIothubDeviceSelection(
iotHubConnectionString: string) {
let provisionIothubDeviceSelection: vscode.QuickPickItem[];
const deviceNumber = await getDeviceNumber(iotHubConnectionString);
if (deviceNumber > 0) {
provisionIothubDeviceSelection = [
{
label: 'Select an existing IoT Hub device',
description: 'Select an existing IoT Hub device',
detail: 'select'
},
{
label: 'Create a new IoT Hub device',
description: 'Create a new IoT Hub device',
detail: 'create'
}
];
} else {
provisionIothubDeviceSelection = [{
label: 'Create a new IoT Hub device',
description: 'Create a new IoT Hub device',
detail: 'create'
}];
}
return provisionIothubDeviceSelection;
}
async function getDeviceNumber(iotHubConnectionString: string) {
return new Promise(
(resolve: (value: number) => void, reject: (error: Error) => void) => {
const registry: iothub.Registry =
iothub.Registry.fromConnectionString(iotHubConnectionString);
registry.list((err, list) => {
if (err) {
return reject(err);
}
if (list === undefined) {
return resolve(0);
} else {
return resolve(list.length);
}
});
});
}