All files / sportident-testbench-shell/src/commands getBackup.ts

16.12% Statements 5/31
0% Branches 0/3
0% Functions 0/7
16.66% Lines 5/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 571x 1x   1x 1x   1x                                                                                                    
import * as utils from 'sportident/lib/utils';
import {proto} from 'sportident/lib/constants';
import {ShellCommandContext} from '../Shell';
import {BaseCommand, ArgType} from './BaseCommand';
import {getDirectOrRemoteStation} from './getDirectOrRemoteStation';
 
export class GetBackupCommand extends BaseCommand {
    getArgTypes(): ArgType[] {
        return [
            {
                name: 'target',
                choices: ['direct', 'remote'],
            },
        ];
    }
 
    printUsage(context: ShellCommandContext): void {
        super.printUsage(context);
        context.putString('e.g. getBackup direct\n');
        context.putString('e.g. getBackup remote\n');
    }
 
    run(context: ShellCommandContext): Promise<void> {
        const device = context.env.device;
        Iif (!device) {
            return Promise.reject(new Error('No device.'));
        }
        const station = getDirectOrRemoteStation(context.args[1], device);
        Iif (station === undefined) {
            context.putString('No such station\n');
            return Promise.resolve();
        }
        const readBlock = (blockIndex: number) => (
            station.sendMessage({
                command: proto.cmd.GET_BACKUP,
                parameters: [0x00, Math.floor(blockIndex / 2) + 1, (blockIndex % 2) * 0x80, 0x80],
            }, 1)
        );
        const allBlocks: number[][] = [];
        const readAllBlocks = (blockIndex: number): Promise<void> => {
            Iif (blockIndex > 26) {
                console.warn(allBlocks.length);
                console.warn(allBlocks.map((block) => utils.prettyHex(block, 16)).join('\n\n'));
                return Promise.resolve();
            }
            return readBlock(blockIndex)
                .then((results: number[][]) => {
                    console.warn(results[0][3] === Math.floor(blockIndex / 2) + 1);
                    console.warn(results[0][4] === (blockIndex % 2) * 0x80);
                    allBlocks.push(results[0].slice(5));
                    return readAllBlocks(blockIndex + 1);
                });
        };
        return readAllBlocks(0);
    }
}