Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions src/__tests__/jupyterlab_web_bluetooth_manager.spec.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,31 @@
/**
* Example of [Jest](https://jestjs.io/docs/getting-started) unit tests
*/
import { DropDownRegistry } from '../bluetooth-extension';
import { BluetoothManager } from '../bluetooth/BluetoothManager';

describe('bluetooh-manager', () => {
it('should be tested', () => {
expect(1 + 1).toEqual(2);
});

it('adds newly registered device types to the dialog dropdown', () => {
const bluetoothManager = new BluetoothManager();
const dropdown = new DropDownRegistry(bluetoothManager.deviceTypeRegistry);

expect(dropdown.node.querySelectorAll('option')).toHaveLength(0);

bluetoothManager.deviceTypeRegistry.add({
deviceType: 'Radiacode® 110',
options: {
acceptAllDevices: false,
filters: [{ services: ['e63215e5-7003-49d8-96b0-b024798fb901'] }],
optionalServices: ['e63215e5-7003-49d8-96b0-b024798fb901']
},
factory: async () => undefined as never
});

const options = Array.from(dropdown.node.querySelectorAll('option')).map(
option => option.textContent
);

expect(options).toContain('Radiacode® 110');
});
});
6 changes: 5 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import BluetoothExtensionPlugins from './bluetooth-extension';
import MoveHubExtensionPlugins from './movehub-extension';
import RadiacodeDectectorExtensionPlugins from './radiacode-extension';

const plugins = BluetoothExtensionPlugins.concat(MoveHubExtensionPlugins);
const plugins = BluetoothExtensionPlugins.concat(
MoveHubExtensionPlugins,
RadiacodeDectectorExtensionPlugins
);
export default plugins;
88 changes: 88 additions & 0 deletions src/radiacode-extension/detector.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
import { BluetoothManager } from '../bluetooth/BluetoothManager';
import { DeviceInfo } from './type';
import { buildShortIdentifier } from '../bluetooth-extension';
import {
radiacodeNotifyCharacteristicUUID,
radiacodeServiceUUID,
radiacodeWriteCharacteristicUUID
} from '.';
//import { Buffer } from '../movehub-extension/moveHub/helpers/buffer';
import { ProtocolManager } from './protocol/protocolManager';
import { COMMAND } from './protocol/parsing';

export const defaultDeviceInfo: DeviceInfo = {
err: '',
connected: false,
batteryLevel: undefined,
identifier: '',
primaryMACAddress: ''
};

export class RadiacodeDetector extends BluetoothManager.Device {
public deviceInfo: DeviceInfo;
public notifyCharacteristic: BluetoothRemoteGATTCharacteristic | undefined;
public writeCharacteristic: BluetoothRemoteGATTCharacteristic | undefined;
public protocolManager: ProtocolManager;

constructor(native: BluetoothDevice) {
super(native);
this.deviceInfo = defaultDeviceInfo;
}

async initDevice(): Promise<void> {
this.connected.connect(async (sender, connected: boolean) => {
if (connected) {
this.deviceInfo.connected = connected;
this.isConnected = connected;
}
console.warn('The connection state is', this.isConnected);
this.deviceInfo.identifier = buildShortIdentifier(this.native);
});
this.disconnected.connect(async (sender, disconnected: boolean) => {
if (disconnected) {
this.deviceInfo.connected = false;
this.isConnected = false;
}
console.warn('The connection state is', this.isConnected);
});

this.writeCharacteristic = await this.getCharacteristic(
radiacodeServiceUUID,
radiacodeWriteCharacteristicUUID
);

this.notifyCharacteristic = await this.getCharacteristic(
radiacodeServiceUUID,
radiacodeNotifyCharacteristicUUID
);
this.protocolManager = new ProtocolManager(
this.notifyCharacteristic,
this.writeCharacteristic
);

await this.protocolManager.init();

/* this.protocolManager.readData(new Uint8Array([0x11, 0x05, 0x00, 0x00]), COMMAND.RD_VIRT_SFR, "brightness").then((brightness) => {
console.log(`Read ${brightness} command sent successfully.`);
}).catch(error => {
console.error('Error sending read brightness command:', error);
});*/

/*this.protocolManager.readData(new Uint8Array([0x24, 0x08, 0x00, 0x00]), COMMAND.RD_VIRT_SFR, "temperature").then((temperature) => {
console.log(`Read ${temperature} command sent successfully.`);
}).catch(error => {
console.error('Error sending read temperature command:', error);
});*/

try {
const brightness = await this.protocolManager.readData(
new Uint8Array([0x11, 0x05, 0x00, 0x00]),
COMMAND.RD_VIRT_SFR,
'brightness'
);
console.log(`Read ${brightness} command returned successfully.`);
} catch (error) {
console.error('Error sending read brightness command:', error);
}
}
}
56 changes: 56 additions & 0 deletions src/radiacode-extension/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import {
JupyterFrontEnd,
JupyterFrontEndPlugin
} from '@jupyterlab/application';

import {
IDeviceTypeRegistryItem,
IBluetoothManager,
BluetoothManager
} from '../bluetooth/BluetoothManager';
import { RadiacodeDetector } from './detector';
export const radiacodeServiceUUID = 'e63215e5-7003-49d8-96b0-b024798fb901';
export const radiacodeNotifyCharacteristicUUID =
'e63215e7-7003-49d8-96b0-b024798fb901';
export const radiacodeWriteCharacteristicUUID =
'e63215e6-7003-49d8-96b0-b024798fb901';
export const radiacodeRegistryItem: IDeviceTypeRegistryItem = {
deviceType: 'Radiacode® 110',
options: {
acceptAllDevices: false,
filters: [{ services: [radiacodeServiceUUID] }],
optionalServices: [radiacodeServiceUUID]
},
factory: async (native: BluetoothDevice) => {
const device = new RadiacodeDetector(native);
await device.initDevice();
return device;
}
};

const RadiacodeDetectorRegisterPlugin: JupyterFrontEndPlugin<void> = {
id: 'bluetooth-manager:radiacode-detector-register-plugin',
description:
'Registers the radiacode detector device and provides a factory.',
requires: [IBluetoothManager],
autoStart: true,
activate: (
app: JupyterFrontEnd,
bluetoothManager: BluetoothManager
): void => {
console.log('JupyterLab radiacode-detector-register plugin is activated!');
bluetoothManager.deviceTypeRegistry.added.connect(
async (sender, radiacodeRegistryItem) => {
console.warn(
`New item from category ${radiacodeRegistryItem.deviceType} is added to the deviceType registry.`
);
}
);
bluetoothManager.deviceTypeRegistry.add(radiacodeRegistryItem);
}
};

const RadiacodeDetectorExtensionPlugins: JupyterFrontEndPlugin<any>[] = [
RadiacodeDetectorRegisterPlugin
];
export default RadiacodeDetectorExtensionPlugins;
Loading
Loading