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

11.42% Statements 4/35
0% Branches 0/6
0% Functions 0/6
11.76% Lines 4/34

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 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 751x   1x 1x   1x                                                                                                                                          
import * as utils from 'sportident/lib/utils';
import {ShellCommandContext} from '../Shell';
import {BaseCommand, ArgType} from './BaseCommand';
import {getDirectOrRemoteStation} from './getDirectOrRemoteStation';
 
export class SendCommand extends BaseCommand {
    getArgTypes(): ArgType[] {
        return [
            {
                name: 'target',
                choices: ['direct', 'remote'],
            },
            {
                name: 'command',
                regex: /^[0-9a-fA-F]{2}$/,
            },
            {
                name: 'parameters',
                regex: /^[0-9a-fA-F]+$/,
            },
            {
                name: 'number of responses',
                regex: /^[0-9]+$/,
            },
        ];
    }
 
    printUsage(context: ShellCommandContext): void {
        super.printUsage(context);
        context.putString('e.g. send direct F9 01 0\n');
        context.putString('e.g. send remote F9 01 1\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 commandStr = context.args[2].replace(/\s/g, '');
        Iif (commandStr.length !== 2) {
            context.putString(`Command must be one byte, is: ${commandStr}\n`);
            return Promise.resolve();
        }
        const command = parseInt(commandStr, 16);
        const parametersStr = context.args[3].replace(/\s/g, '');
        Iif (parametersStr.length % 2 !== 0) {
            context.putString(`Parameters must be bytes, is: ${parametersStr}\n`);
            return Promise.resolve();
        }
        const parameters = [];
        for (let i = 0; i < parametersStr.length; i += 2) {
            parameters.push(parseInt(parametersStr.slice(i, i + 2), 16));
        }
        const numResp = context.args.length > 4 ? parseInt(context.args[4], 10) : 0;
        return station.sendMessage({
            command: command,
            parameters: parameters,
        }, numResp)
            .then((allResponses: number[][]) => {
                allResponses.forEach((response: number[], index: number) => {
                    context.putString(`Answer[${index}]:\n`);
                    const lines = utils.prettyHex(response, 16).split('\n');
                    lines.forEach((line: string) => {
                        context.putString(` ${line}\n`);
                    });
                });
            });
    }
}