feat: add HardwareDeployer interface, BLEHardwareDeployer implements it

This commit is contained in:
a2nr 2026-07-17 08:03:42 +07:00
parent 029efb7464
commit 1b51e75ff0
2 changed files with 26 additions and 1 deletions

View File

@ -4,6 +4,7 @@ import {
BLE_CHAR_SERIAL_UUID,
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,
type HardwareDeployer,
type DeployProgress, type BLEACKResponse
} from '$types/deployer';
@ -12,7 +13,7 @@ function logAck(tag: string, msg: string) {
console.log(`[BLE-ACK#${++_ackNotificationCount}] ${tag}: ${msg}`);
}
export class BLEHardwareDeployer {
export class BLEHardwareDeployer implements HardwareDeployer {
private server: BluetoothRemoteGATTServer | null = null;
private service: BluetoothRemoteGATTService | null = null;
private flashingChar: BluetoothRemoteGATTCharacteristic | null = null;
@ -46,6 +47,10 @@ export class BLEHardwareDeployer {
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> {
return Promise.race([
promise,

View File

@ -16,6 +16,26 @@ export interface DeployProgress {
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 {
command: number;
index: number;