import { createErrorFromCode, SFPToolboxErrorCode } from '../../error/error'; import base64ToArrayBuffer from 'base64-arraybuffer'; import _ from 'lodash'; export class ResetClerk { resolve: Function; reject: Function; onBeginResetting: Function; result: number | null; resetCommand: string; constructor(resolver: Function, rejecter: Function, onBeginReading: Function, resetCommand: string) { this.resolve = resolver; this.reject = rejecter; this.onBeginResetting = onBeginReading; this.result = null; this.resetCommand = resetCommand; } appendString(aString: string) { if (this.getResult() == null) { this.onBeginResetting?.(); } const response = Array.from(new Uint8Array(base64ToArrayBuffer.decode(aString))); const result = response.length ? response[0] : 1; const commandInResponse = response.slice(1, 3); const originalCommand = Array.from(new Uint8Array(base64ToArrayBuffer.decode(this.resetCommand))).slice( 0, 2, ); if (result === 0 && _.isEqual(commandInResponse, originalCommand)) { this.resolve(); } else { this.reject(createErrorFromCode(SFPToolboxErrorCode.ResetWentWrong)); } } getResult() { return this.result; } }