Skip to content
Snippets Groups Projects
command.service.ts 4.19 KiB
Newer Older
  • Learn to ignore specific revisions
  • paf's avatar
    paf committed
    import {Inject, Injectable, OnDestroy} from '@angular/core';
    import {Subject, Subscription} from 'rxjs';
    
    import {Command, commandKeywords, isKnownCommand, TestStatus} from './test-controller.interfaces';
    import {TestControllerService} from './test-controller.service';
    
    paf's avatar
    paf committed
    import {distinctUntilChanged} from 'rxjs/operators';
    
    import {Uuid} from '../shared/uuid';
    
    import {WebsocketBackendService} from '../shared/websocket-backend.service';
    import {HttpClient} from '@angular/common/http';
    
    export class CommandService extends WebsocketBackendService<Command[]> implements OnDestroy {
    
        public command$: Subject<Command> = new Subject<Command>();
    
    paf's avatar
    paf committed
        private commandSubscription: Subscription;
    
        initialData: [{id: 'primary', keyword: 'debug', arguments: ['on']}];
        pollingEndpoint = 'will_be_set';
        pollingInterval = 5000;
        wsChannelName = 'commands';
        private commandHistory: string[] = [];
    
    
        constructor (
            @Inject('IS_PRODUCTION_MODE') public isProductionMode,
    
            private tcs: TestControllerService,
            @Inject('SERVER_URL') serverUrl: string,
            http: HttpClient
    
                this.setUpGlobalCommandsForDebug();
            }
    
            this.subscribeCommands();
    
    
            this.tcs.testStatus$
                .pipe(distinctUntilChanged())
                .subscribe((testStatus: TestStatus) => {
                    const newPollingEndpoint = `test/${this.tcs.testId}/commands`;
                    if ((testStatus === TestStatus.RUNNING) && (newPollingEndpoint !== this.pollingEndpoint)) {
                        this.pollingEndpoint = newPollingEndpoint;
                        this.observeEndpointAndChannel().subscribe(
                            (commands: Command[]) => {
                                console.log('COMMANDS', commands);
                                if (commands) {
                                    commands.forEach((command: Command) => {
                                        this.command$.next(command);
                                    });
                                }
                            }
                        );
                    }
                });
    
    
    paf's avatar
    paf committed
        ngOnDestroy() {
            this.commandSubscription.unsubscribe();
        }
    
    
        private subscribeCommands() {
    
    paf's avatar
    paf committed
            this.commandSubscription = this.command$
                .pipe(
                    distinctUntilChanged((command1: Command, command2: Command): boolean => (command1.id === command2.id))
                )
                .subscribe((command: Command) => {
    
    
                    if (this.commandHistory.indexOf(command.id) >= 0) {
                        console.warn('command already executed', command);
                    }
    
                    this.commandHistory.push(command.id);
    
    paf's avatar
    paf committed
                    switch (command.keyword) {
                        case 'pause':
                            this.tcs.testStatus$.next(TestStatus.PAUSED);
                            break;
                        case 'resume':
                            this.tcs.testStatus$.next(TestStatus.RUNNING);
                            break;
                        case 'terminate':
                            this.tcs.terminateTest();
                            break;
                        case 'goto':
                            this.tcs.setUnitNavigationRequest(command.arguments[0]);
    
                            break;
                        case 'debug':
                            this.tcs.debugPane = command.arguments[0] !== 'off';
    
                            break;
                        default:
                            console.warn(`command '${command.keyword}' is unknown`);
    
            });
        }
    
        private setUpGlobalCommandsForDebug() {
    
            window['tc'] = {};
            commandKeywords.forEach((keyword: string) => {
    
    paf's avatar
    paf committed
                window['tc'][keyword] = (args) => {this.command(keyword, args, Uuid.create()); };
    
    paf's avatar
    paf committed
        private command(keyword: string, args: string[], id: string): void {
            if (!isKnownCommand(keyword)) {
                console.warn(`Unknown command: ${keyword}`);
    
                return;
            }
    
            if (!this.isProductionMode) {
    
    paf's avatar
    paf committed
                console.log(`Command received: ${keyword}`);
    
            args = (typeof args === 'undefined') ? [] : args;
    
    paf's avatar
    paf committed
            this.command$.next({keyword, arguments: args, id});