Skip to content

Commit c22240b

Browse files
Improve bluetoothManager class.
1 parent ea5fff9 commit c22240b

5 files changed

Lines changed: 55 additions & 68 deletions

File tree

src/bluetooth-extension/index.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
export namespace CommandIDs {
1717
export const openDeviceRegistryDialog =
1818
'bluetooth-manager:open-dialog-for-devices-registry';
19-
export const disconnectDevice = 'bluetooth-manager:disconnect-device';
19+
export const disconnect = 'bluetooth-manager:disconnect-device';
2020
}
2121

2222
export function buildCompleteIdentifier(native: BluetoothDevice): string {
@@ -67,13 +67,13 @@ const BluetoothSidebarPlugin: JupyterFrontEndPlugin<void> = {
6767
const openDeviceRegistryDialogLabel = trans.__('Add a Device');
6868
let runningItemsList: Array<IRunningSessions.IRunningItem>;
6969

70-
app.commands.addCommand(CommandIDs.disconnectDevice, {
70+
app.commands.addCommand(CommandIDs.disconnect, {
7171
execute: args => {
7272
const selectedDevice = bluetoothManager.deviceList.find(
7373
device => device.native.id === (args.deviceID as string)
7474
);
7575
if (selectedDevice) {
76-
bluetoothManager.disconnectDevice(selectedDevice);
76+
bluetoothManager.disconnect(selectedDevice);
7777
return selectedDevice;
7878
} else {
7979
throw new Error('No device provided or device is invalid');
@@ -87,16 +87,16 @@ const BluetoothSidebarPlugin: JupyterFrontEndPlugin<void> = {
8787
execute: async () => {
8888
showDialog({
8989
title: 'Select device type',
90-
body: new DropDownRegistry(bluetoothManager.registry),
90+
body: new DropDownRegistry(bluetoothManager.deviceTypeRegistry),
9191
buttons: [
9292
Dialog.okButton({ label: 'Select' }),
9393
Dialog.cancelButton({ label: 'Cancel' })
9494
]
9595
}).then(async result => {
9696
if (result.button.accept) {
97-
bluetoothManager.registry.itemsList.forEach(async item => {
97+
bluetoothManager.deviceTypeRegistry.itemsList.forEach(async item => {
9898
if (item.deviceType === result.value) {
99-
await bluetoothManager.connectDevice(item);
99+
await bluetoothManager.connect(item);
100100
} else {
101101
console.warn('There is no corresponding item in the registry!');
102102
}
@@ -151,7 +151,7 @@ export class DropDownRegistry
151151
extends Widget
152152
implements Dialog.IBodyWidget<string>
153153
{
154-
constructor(registry: BluetoothManager.DeviceRegistry) {
154+
constructor(registry: BluetoothManager.DeviceTypeRegistry) {
155155
super();
156156
this._selectList = document.createElement('select');
157157
this.node.appendChild(this._selectList);
@@ -169,7 +169,7 @@ export class DropDownRegistry
169169
}
170170

171171
private _selectList: HTMLSelectElement;
172-
public registry: BluetoothManager.DeviceRegistry;
172+
public registry: BluetoothManager.DeviceTypeRegistry;
173173
}
174174

175175
const BluetoothExtensionPlugins: JupyterFrontEndPlugin<any>[] = [

src/bluetooth/BluetoothDeviceRunningItem.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { buildCompleteIdentifier } from '../bluetooth-extension';
55
import { Menu } from '@lumino/widgets';
66
import { CommandRegistry } from '@lumino/commands';
77

8-
export const disconnectDevice = 'bluetooth-manager:disconnect-device';
8+
export const disconnect = 'bluetooth-manager:disconnect-device';
99

1010
export class BluetoothDeviceRunningItem
1111
implements IRunningSessions.IRunningItem
@@ -58,7 +58,7 @@ export class BluetoothDeviceRunningItem
5858
}
5959

6060
shutdown() {
61-
this.bluetoothManager.disconnectDevice(this._device);
61+
this.bluetoothManager.disconnect(this._device);
6262
}
6363

6464
private _device: BluetoothManager.Device;

src/bluetooth/BluetoothManager.ts

Lines changed: 35 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ export class BluetoothManager implements IBluetoothManager {
1515
);
1616
this.registeredByAPlugin = new Signal<
1717
this,
18-
BluetoothManager.DeviceRegistry
18+
BluetoothManager.DeviceTypeRegistry
1919
>(this);
20-
this._registry = new BluetoothManager.DeviceRegistry();
20+
this._deviceTypeRegistry = new BluetoothManager.DeviceTypeRegistry();
2121
this._deviceList = [];
2222
this._identifierRegistry = [];
2323
}
@@ -26,54 +26,50 @@ export class BluetoothManager implements IBluetoothManager {
2626
return this._deviceList;
2727
}
2828

29-
get registry(): BluetoothManager.DeviceRegistry {
30-
return this._registry;
29+
get deviceTypeRegistry(): BluetoothManager.DeviceTypeRegistry {
30+
return this._deviceTypeRegistry;
3131
}
3232

33-
get identifierRegistry(): Array<string> {
34-
return this._identifierRegistry;
35-
}
36-
37-
async connectDevice(
38-
registryItem: IDeviceRegistryItem
33+
async connect(
34+
registryItem: IDeviceTypeRegistryItem
3935
): Promise<BluetoothManager.Device | undefined> {
4036
const native = await this.requestDevice(registryItem);
4137
if (native) {
4238
const device = await registryItem.factory(native);
4339
if (device && device.isConnected) {
44-
this.addDeviceToList(device);
40+
this._addDeviceToList(device);
4541
device.disconnected.connect(async () => {
46-
this.removeDeviceFromList(device);
42+
this._removeDeviceFromList(device);
4743
});
4844
return device;
4945
}
5046
}
5147
}
5248

53-
async disconnectDevice(device: BluetoothManager.Device) {
49+
async disconnect(device: BluetoothManager.Device) {
5450
await device.disconnect();
5551
device.dispose();
5652
}
5753

5854
// Method to add a device to the list
59-
addDeviceToList(device: BluetoothManager.Device): void {
55+
private _addDeviceToList(device: BluetoothManager.Device): void {
6056
const identifier = buildCompleteIdentifier(device.native);
61-
if (this.identifierRegistry.includes(identifier) === false) {
57+
if (this._identifierRegistry.includes(identifier) === false) {
6258
this._deviceList.push(device);
63-
this.identifierRegistry.push(identifier);
59+
this._identifierRegistry.push(identifier);
6460
} else {
65-
console.warn('The device is already in the identifierRegistry');
61+
console.warn('The device is already in the registry of identifiers');
6662
}
6763
// Emit the signal when the list changes
6864
this.deviceListChanged.emit(this._deviceList);
6965
}
7066

7167
// Method to remove a device from the list
72-
removeDeviceFromList(device: BluetoothManager.Device): void {
68+
private _removeDeviceFromList(device: BluetoothManager.Device): void {
7369
const index = this._deviceList.indexOf(device);
7470
if (index > -1) {
7571
this._deviceList.splice(index, 1);
76-
this.identifierRegistry.splice(index, 1);
72+
this._identifierRegistry.splice(index, 1);
7773
// Emit the signal when the list changes
7874
this.deviceListChanged.emit(this._deviceList);
7975
}
@@ -82,20 +78,11 @@ export class BluetoothManager implements IBluetoothManager {
8278

8379
removeAllDevices() {
8480
this._deviceList.forEach((device, index) => {
85-
this.removeDeviceFromList(device);
81+
this._removeDeviceFromList(device);
8682
this.deviceListChanged.emit(this._deviceList);
8783
});
8884
}
8985

90-
register(registryItem: IDeviceRegistryItem) {
91-
this._registry.add(registryItem);
92-
this.registeredByAPlugin.emit(this._registry);
93-
console.warn(
94-
`New item from category ${registryItem.deviceType} is added to the registry.`
95-
);
96-
return this._registry;
97-
}
98-
9986
async checkWebBluetoothSupport(): Promise<boolean> {
10087
const isWebBluetoothSupported: boolean = navigator.bluetooth ? true : false;
10188
if (isWebBluetoothSupported === false) {
@@ -109,7 +96,7 @@ export class BluetoothManager implements IBluetoothManager {
10996
}
11097

11198
async requestDevice(
112-
registryItem: IDeviceRegistryItem
99+
registryItem: IDeviceTypeRegistryItem
113100
): Promise<BluetoothDevice | undefined> {
114101
const isWebBluetoothSupported = await this.checkWebBluetoothSupport();
115102
if (isWebBluetoothSupported) {
@@ -126,9 +113,9 @@ export class BluetoothManager implements IBluetoothManager {
126113
public deviceListChanged: Signal<this, Array<BluetoothManager.Device>>;
127114
public registeredByAPlugin: Signal<
128115
BluetoothManager,
129-
BluetoothManager.DeviceRegistry
116+
BluetoothManager.DeviceTypeRegistry
130117
>;
131-
private _registry: BluetoothManager.DeviceRegistry;
118+
private _deviceTypeRegistry: BluetoothManager.DeviceTypeRegistry;
132119
private _identifierRegistry: Array<string>;
133120
}
134121

@@ -283,18 +270,18 @@ export namespace BluetoothManager {
283270
}
284271
}
285272

286-
export class DeviceRegistry implements IDeviceRegistry {
287-
private _registry: Array<IDeviceRegistryItem>;
288-
public registryItem: IDeviceRegistryItem;
273+
export class DeviceTypeRegistry implements IDeviceTypeRegistry {
274+
private _deviceTypeRegistry: Array<IDeviceTypeRegistryItem>;
275+
public registryItem: IDeviceTypeRegistryItem;
289276
constructor() {
290-
this._registry = [];
277+
this._deviceTypeRegistry = [];
291278
}
292279

293-
add(registryItem: IDeviceRegistryItem) {
294-
this._registry.push(registryItem);
280+
add(registryItem: IDeviceTypeRegistryItem) {
281+
this._deviceTypeRegistry.push(registryItem);
295282
}
296-
get itemsList(): Array<IDeviceRegistryItem> {
297-
return this._registry;
283+
get itemsList(): Array<IDeviceTypeRegistryItem> {
284+
return this._deviceTypeRegistry;
298285
}
299286
}
300287
}
@@ -303,33 +290,29 @@ export namespace BluetoothManager {
303290
* Interface for the bluetooth manager.
304291
*/
305292
export interface IBluetoothManager {
306-
addDeviceToList(Device: BluetoothManager.Device): void;
307-
removeDeviceFromList(Device: BluetoothManager.Device): void;
308293
removeAllDevices(Devices: Array<BluetoothManager.Device>): void;
309-
register(registryItem: IDeviceRegistryItem): BluetoothManager.DeviceRegistry;
310-
connectDevice(registryItem: IDeviceRegistryItem): any;
311-
disconnectDevice(device: BluetoothManager.Device): void;
294+
connect(registryItem: IDeviceTypeRegistryItem): any;
295+
disconnect(device: BluetoothManager.Device): void;
312296
deviceListChanged: Signal<BluetoothManager, Array<BluetoothManager.Device>>;
313297
registeredByAPlugin: Signal<
314298
BluetoothManager,
315-
BluetoothManager.DeviceRegistry
299+
BluetoothManager.DeviceTypeRegistry
316300
>;
317301
get deviceList(): Array<BluetoothManager.Device>;
318-
get registry(): BluetoothManager.DeviceRegistry;
319-
get identifierRegistry(): Array<string>;
302+
get deviceTypeRegistry(): BluetoothManager.DeviceTypeRegistry;
320303
}
321304

322-
export interface IDeviceRegistryItem {
305+
export interface IDeviceTypeRegistryItem {
323306
deviceType: string;
324307
factory: (
325308
native: BluetoothDevice
326309
) => Promise<BluetoothManager.Device | undefined>;
327310
options: IDeviceOptions;
328311
}
329312

330-
export interface IDeviceRegistry {
331-
add: (registryItem: IDeviceRegistryItem) => void;
332-
get itemsList(): Array<IDeviceRegistryItem>;
313+
export interface IDeviceTypeRegistry {
314+
add: (registryItem: IDeviceTypeRegistryItem) => void;
315+
get itemsList(): Array<IDeviceTypeRegistryItem>;
333316
}
334317

335318
export const IBluetoothManager = new Token<IBluetoothManager>(

src/movehub-extension/index.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ITranslator } from '@jupyterlab/translation';
66
import { IThemeManager, MainAreaWidget } from '@jupyterlab/apputils';
77
import { Toolbar } from '@jupyterlab/ui-components';
88
import {
9-
IDeviceRegistryItem,
9+
IDeviceTypeRegistryItem,
1010
IBluetoothManager,
1111
BluetoothManager
1212
} from '../bluetooth/BluetoothManager';
@@ -27,7 +27,7 @@ export const connectMoveHub = 'bluetooth-manager:connect-movehub';
2727
export const disconnectMoveHub = 'bluetooth-manager:disconnect-movehub';
2828
export const moveHubServiceUUID = '00001623-1212-efde-1623-785feabcd123';
2929
export const moveHubCharacteristicUUID = '00001624-1212-efde-1623-785feabcd123';
30-
export const movehubRegistryItem: IDeviceRegistryItem = {
30+
export const movehubRegistryItem: IDeviceTypeRegistryItem = {
3131
deviceType: 'LEGO® Move Hub',
3232
options: {
3333
acceptAllDevices: false,
@@ -56,7 +56,11 @@ const MoveHubRegisterPlugin: JupyterFrontEndPlugin<void> = {
5656
bluetoothManager: BluetoothManager
5757
): void => {
5858
console.log('JupyterLab move-hub-register plugin is activated!');
59-
bluetoothManager.register(movehubRegistryItem);
59+
bluetoothManager.deviceTypeRegistry.add(movehubRegistryItem);
60+
bluetoothManager.registeredByAPlugin.emit(bluetoothManager.deviceTypeRegistry);
61+
console.warn(
62+
`New item from category ${movehubRegistryItem} is added to the registry.`
63+
);
6064
}
6165
};
6266

@@ -125,7 +129,7 @@ const LEGOMoveHubControlPanelPlugin: JupyterFrontEndPlugin<void> = {
125129
device => device.native.id === (args.deviceID as string)
126130
);
127131
if (selectedDevice && selectedDevice instanceof MoveHub) {
128-
bluetoothManager.disconnectDevice(selectedDevice);
132+
bluetoothManager.disconnect(selectedDevice);
129133
return selectedDevice;
130134
} else {
131135
throw new Error('No device provided or device is invalid');
@@ -151,7 +155,7 @@ const LEGOMoveHubControlPanelPlugin: JupyterFrontEndPlugin<void> = {
151155

152156
app.commands.addCommand(connectMoveHub, {
153157
execute: args => {
154-
const newDevice = bluetoothManager.connectDevice(movehubRegistryItem);
158+
const newDevice = bluetoothManager.connect(movehubRegistryItem);
155159
return newDevice;
156160
},
157161
caption: 'Connect MoveHub',

src/movehub-extension/widget.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ export class MoveHubModel extends DOMWidgetModel {
174174
console.log('not connected yet');*/
175175
if (identifier === '') {
176176
this.movehub =
177-
await MoveHubModel.bluetoothManager.connectDevice(movehubRegistryItem);
177+
await MoveHubModel.bluetoothManager.connect(movehubRegistryItem);
178178
} else {
179179
const selectedDevice = MoveHubModel.bluetoothManager.deviceList.find(
180180
device => device.native.id === identifier

0 commit comments

Comments
 (0)