import _ from 'lodash'; import { BleErrorCode } from 'react-native-ble-plx'; import { base64ToHex, hexToBase64 } from '../../parsing/binary'; import delay from './delay'; import { ProbeEmitter } from './ProbeEmitter'; import { createErrorFromCode, SFPToolboxErrorCode } from '../../error/error'; import { generatePayload } from './generatePayload'; import LocalLogger from '../LocalLogger'; export class DummyProbeEmitter extends ProbeEmitter { timeout: ReturnType | undefined; interval: number; constructor(success, interval = 1500, chunkDelay = 100) { super(); this.interval = interval; const successFunction = async (_command, mockCode) => { const payloadInHex = generatePayload(_command, mockCode); const chunks = _.chunk(payloadInHex, 3 * 20); for (let chunk of chunks) { await delay(chunkDelay); console.log('Received', chunk.join('')); LocalLogger.log('Read', chunk.join('')); this.emit(hexToBase64(chunk.join(''))); } }; const errorFunction = () => { this.error(createErrorFromCode(BleErrorCode.DeviceDisconnected)); }; this.start = (command, mockCode) => { if (this.savedError) { this.error(this.savedError); } console.log('Sent Command: ' + command); LocalLogger.log('Write', base64ToHex(command)); this.timeout = setTimeout( success && mockCode !== 'probeFailure' ? () => successFunction(command, mockCode).catch(err => { if (__DEV__) { console.log('Dummy probe emitter threw an error ', err?.message); } this.error(createErrorFromCode(SFPToolboxErrorCode.Unknown)); }) : errorFunction, this.interval, ); }; } async cancel() { if (this.timeout !== undefined) { clearTimeout(this.timeout); } return Promise.resolve(); } }