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

16.66% Statements 5/30
0% Branches 0/3
0% Functions 0/6
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 57 58 59 60 611x 1x   1x 1x   1x                                                                                                            
import {ISiStationStorageFields, siStationStorageLocations} from 'sportident/lib/SiStation/BaseSiStation';
import {SiFieldValue} from 'sportident/lib/storage/SiFieldValue';
import {ShellCommandContext} from '../Shell';
import {BaseCommand, ArgType} from './BaseCommand';
import {getDirectOrRemoteStation} from './getDirectOrRemoteStation';
 
export class SetInfoCommand extends BaseCommand {
    getArgTypes(): ArgType[] {
        return [
            {
                name: 'target',
                choices: ['direct', 'remote'],
            },
            {
                name: 'information name',
                choices: Object.keys(siStationStorageLocations),
            },
            {
                name: 'new value',
                regex: /^\S+$/,
            },
        ];
    }
 
    printUsage(context: ShellCommandContext): void {
        super.printUsage(context);
        context.putString('e.g. setInfo direct code 10\n');
        context.putString('e.g. setInfo remote mode Readout\n');
        context.putString('e.g. setInfo d beeps false\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 infoName = context.args[2];
        const newValue = context.args[3];
        Iif (!(infoName in siStationStorageLocations)) {
            context.putString(`No such info: ${infoName}.\n`);
        }
        const validInfoName = infoName as keyof ISiStationStorageFields;
        const field = station.getField(validInfoName);
        const fieldValue = SiFieldValue.fromString<any>(field, newValue);
        const validFieldValue = fieldValue as SiFieldValue<ISiStationStorageFields[typeof validInfoName]>;
        return station.atomically(() => {
            station.setInfo(validInfoName, validFieldValue);
        })
            .then(() => station.readInfo())
            .then(() => {
                const infoValue = station.getInfo(validInfoName);
                context.putString(`${validInfoName}: ${infoValue}\n`);
            });
    }
}