feat: add HardwareDeployer interface, BLEHardwareDeployer implements it
This commit is contained in:
parent
029efb7464
commit
1b51e75ff0
|
|
@ -4,6 +4,7 @@ import {
|
||||||
BLE_CHAR_SERIAL_UUID,
|
BLE_CHAR_SERIAL_UUID,
|
||||||
CMD_INIT, CMD_DATA, CMD_END, CMD_ACK, CMD_ERR, CMD_SET_BAUD, CMD_RESET,
|
CMD_INIT, CMD_DATA, CMD_END, CMD_ACK, CMD_ERR, CMD_SET_BAUD, CMD_RESET,
|
||||||
CHUNK_SIZE, BLE_TIMEOUT_MS, END_TIMEOUT_MS, END_ACK_INDEX, INIT_ACK_INDEX, MAX_RETRIES,
|
CHUNK_SIZE, BLE_TIMEOUT_MS, END_TIMEOUT_MS, END_ACK_INDEX, INIT_ACK_INDEX, MAX_RETRIES,
|
||||||
|
type HardwareDeployer,
|
||||||
type DeployProgress, type BLEACKResponse
|
type DeployProgress, type BLEACKResponse
|
||||||
} from '$types/deployer';
|
} from '$types/deployer';
|
||||||
|
|
||||||
|
|
@ -12,7 +13,7 @@ function logAck(tag: string, msg: string) {
|
||||||
console.log(`[BLE-ACK#${++_ackNotificationCount}] ${tag}: ${msg}`);
|
console.log(`[BLE-ACK#${++_ackNotificationCount}] ${tag}: ${msg}`);
|
||||||
}
|
}
|
||||||
|
|
||||||
export class BLEHardwareDeployer {
|
export class BLEHardwareDeployer implements HardwareDeployer {
|
||||||
private server: BluetoothRemoteGATTServer | null = null;
|
private server: BluetoothRemoteGATTServer | null = null;
|
||||||
private service: BluetoothRemoteGATTService | null = null;
|
private service: BluetoothRemoteGATTService | null = null;
|
||||||
private flashingChar: BluetoothRemoteGATTCharacteristic | null = null;
|
private flashingChar: BluetoothRemoteGATTCharacteristic | null = null;
|
||||||
|
|
@ -46,6 +47,10 @@ export class BLEHardwareDeployer {
|
||||||
return this.server?.connected ?? false;
|
return this.server?.connected ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
get deviceName(): string | null {
|
||||||
|
return this.device?.name ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
private withTimeout<T>(promise: Promise<T>, ms: number, label: string): Promise<T> {
|
private withTimeout<T>(promise: Promise<T>, ms: number, label: string): Promise<T> {
|
||||||
return Promise.race([
|
return Promise.race([
|
||||||
promise,
|
promise,
|
||||||
|
|
|
||||||
|
|
@ -16,6 +16,26 @@ export interface DeployProgress {
|
||||||
bytesPerSecond?: number;
|
bytesPerSecond?: number;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Abstraction untuk hardware deployer (BLE atau USB).
|
||||||
|
* DeployTab menggunakan interface ini, switching instance berdasarkan mode.
|
||||||
|
*/
|
||||||
|
export interface HardwareDeployer {
|
||||||
|
checkSupport(): boolean;
|
||||||
|
readonly isConnected: boolean;
|
||||||
|
readonly deviceName: string | null;
|
||||||
|
pair(): Promise<void>;
|
||||||
|
deployHex(hexContent: string, onProgress: (p: DeployProgress) => void): Promise<void>;
|
||||||
|
deployBinary?(base64Binary: string, onProgress: (p: DeployProgress) => void): Promise<void>;
|
||||||
|
startSerialMonitor(onData: (text: string) => void): Promise<void>;
|
||||||
|
stopSerialMonitor(): void;
|
||||||
|
sendSerialInput(text: string): Promise<void>;
|
||||||
|
setBaudRate(baud: number): Promise<void>;
|
||||||
|
resetArduino(): Promise<void>;
|
||||||
|
disconnect(): void;
|
||||||
|
onDisconnected(cb: () => void): void;
|
||||||
|
}
|
||||||
|
|
||||||
export interface BLEACKResponse {
|
export interface BLEACKResponse {
|
||||||
command: number;
|
command: number;
|
||||||
index: number;
|
index: number;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue