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

6.81% Statements 3/44
0% Branches 0/5
0% Functions 0/9
6.81% Lines 3/44

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 75 76 77 78 791x         1x   1x                                                                                                                                              
import si from 'sportident/lib';
import {SiDeviceReceiveEvent} from 'sportident/lib/SiDevice/ISiDevice';
import * as utils from 'sportident/lib/utils';
import {SiExternalApplicationReceiveEvent} from '../ISiExternalApplication';
import {ShellCommandContext} from '../Shell';
import {BaseCommand, ArgType} from './BaseCommand';
 
export class PipeCommand extends BaseCommand {
    getArgTypes(): ArgType[] {
        return [
            {
                name: 'path',
                regex: /^\S+$/,
                description: 'The absolute path of a pipe (on the machine where testbench-server is running)',
            },
        ];
    }
 
    printUsage(context: ShellCommandContext): void {
        super.printUsage(context);
        context.putString('e.g. pipe /tmp/vwin_com1\n');
    }
 
    run(context: ShellCommandContext): Promise<void> {
        const url = context.args[1];
        const device = context.env.device;
        const SiExternalApplication = context.env.externalApplication;
        return new Promise((resolve, reject) => {
            Iif (!SiExternalApplication) {
                reject(new Error('No SiExternalApplication.'));
                return;
            }
            Iif (!device) {
                reject(new Error('No device.'));
                return;
            }
            const externalApplication = new SiExternalApplication(url);
 
            let deviceToApplicationBuffer: number[] = [];
            let applicationToDeviceBuffer: number[] = [];
 
            const onDeviceReceive: utils.EventCallback<SiDeviceReceiveEvent> =
                (e) => {
                    const uint8Data = e.uint8Data;
                    deviceToApplicationBuffer.push(...uint8Data);
                    const {messages, remainder} = si.protocol.parseAll(deviceToApplicationBuffer);
                    messages.forEach((message) => {
                        console.log('SiDevice:', si.protocol.prettyMessage(message));
                    });
                    deviceToApplicationBuffer = remainder;
                    externalApplication.send(uint8Data);
                };
            device.addEventListener('receive', onDeviceReceive);
            const onApplicationReceive: utils.EventCallback<SiExternalApplicationReceiveEvent> =
                (e) => {
                    const uint8Data = e.uint8Data;
                    applicationToDeviceBuffer.push(...uint8Data);
                    const {messages, remainder} = si.protocol.parseAll(applicationToDeviceBuffer);
                    messages.forEach((message) => {
                        console.log('SiExternalApplication:', si.protocol.prettyMessage(message));
                    });
                    applicationToDeviceBuffer = remainder;
                    device.send(uint8Data);
                };
            externalApplication.addEventListener('receive', onApplicationReceive);
 
            context.waitChar().then((char: number) => {
                Iif (char === 27 || char === 3) { // Escape || Ctrl-C
                    device.removeEventListener('receive', onDeviceReceive);
                    externalApplication.removeEventListener('receive', onApplicationReceive);
                    externalApplication.close();
                    context.putString('Piping finished.\n');
                    resolve();
                }
            });
        });
    }
}