All files / src/app/processing processing.component.ts

52.47% Statements 53/101
57.14% Branches 4/7
39.53% Functions 17/43
52.47% Lines 53/101

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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283                                                              1x               1x 1x   1x 1x   1x   1x   1x 1x 1x   1x                                     1x 1x 1x 1x 1x     1x                             1x   1x               1x     1x               1x     1x               1x 1x     1x 1x   1x 1x     1x             1x     1x               1x     1x                   1x 1x 1x 1x 1x 1x 1x                                                                                                                                                       1x       1x 1x 1x         1x 1x   1x       1x   1x 1x       1x   1x          
import { Component, Inject, OnDestroy, OnInit, ViewChild } from '@angular/core';
import { NEVER, Observable, Subscription } from 'rxjs';
import { concatMap, tap, startWith, switchMap, map } from 'rxjs/operators';
import { CalibrationService } from 'src/shared/services/calibration.service';
import { ProcessingService } from 'src/shared/services/processing.service';
import { SettingsService } from 'src/shared/services/settingsService';
import { LogService } from '../log/log.service';
import { InteractionsVisualizationComponent } from './interactions-visualization/interactions-visualization.component';
import { InteractionsComponent } from './interactions/interactions.component';
import { CompleteInteractionData, DEFAULT_SETTINGS, Interaction, ObserverType, ProcessingSettings, RemoteProcessingServiceSettings, TrackingServerAppSettings } from '@reflex/shared-types';
import { OptionCheckboxComponent, PanelHeaderComponent, ValueSelectionComponent, ValueSliderComponent } from '@reflex/angular-components/dist';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { HistoryComponent } from './history/history.component';
 
@Component({
  selector: 'app-processing',
  templateUrl: './processing.component.html',
  styleUrls: ['./processing.component.scss'],
  imports: [
    CommonModule,
    FormsModule,
    PanelHeaderComponent,
    ValueSelectionComponent,
    ValueSliderComponent,
    OptionCheckboxComponent,
    InteractionsComponent,
    HistoryComponent,
    InteractionsVisualizationComponent
  ]
})
export class ProcessingComponent implements OnInit, OnDestroy {
 
  @ViewChild('visualizationContainer')
  public visualization?: InteractionsVisualizationComponent;
 
  @ViewChild('interactionsList')
  public interactionsList?: InteractionsComponent;
 
  public statusText = '';
  public eventId = 0;
 
  public selectedProcessorIdx = -1;
  public interval = 0;
 
  public processors: Array<string> = [];
 
  public isInteractionProcessingActive = false;
 
  public remoteSettings: RemoteProcessingServiceSettings = DEFAULT_SETTINGS.remoteProcessingServiceSettingsValues;
  public remoteAddress = '';
  public remotePort = 0;
 
  public processingSettings: ProcessingSettings = {
    interactionType: ObserverType.None,
    intervalDuration: 0
  };
 
  private selectedProcessorSubscription?: Subscription;
  private intervalSubscription?: Subscription;
  private interactionsSubscription?: Subscription;
  private observerSubscription?: Subscription;
  private remoteSettingsSubscription?: Subscription;
  private saveSettingsSubscription?: Subscription;
 
  // private readonly settingsSubscription?: Subscription;
  private statusSubscription?: Subscription;
 
  private readonly saveSettings$: Observable<TrackingServerAppSettings>;
 
  public constructor(
    // eslint-disable-next-line new-cap
    @Inject('BASE_URL') private readonly baseUrl: string,
    private readonly settingsService: SettingsService,
    private readonly processingService: ProcessingService,
    private readonly calibrationService: CalibrationService,
    private readonly logService: LogService
  ) {
 
    this.saveSettings$ = this.settingsService.getSettings().pipe(
      map((result) => {
        const settings = result;
        result.remoteProcessingServiceSettingsValues = this.remoteSettings;
 
        return settings;
      }),
      tap((updatedSettings: TrackingServerAppSettings) => {
        this.settingsService.saveSettings(updatedSettings);
      })
    );
  }
 
  public ngOnInit(): void {
 
    this.statusSubscription = this.processingService.getStatus().subscribe(
      (result) => {
        this.statusText = result;
      },
      (error) => {
        console.error(error);
        this.logService.sendErrorLog(`${error}`);
      }
    );
 
    this.observerSubscription = this.processingService.getObserverTypes()
      .subscribe(
        (result) => {
          this.processors = result;
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
 
    this.selectedProcessorSubscription = this.processingService.getSelectedObserverType()
      .subscribe(
        (result) => {
          this.updateProcessor(result);
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
 
    const interactions$ = this.processingService.getInteractions();
    this.interactionsSubscription = this.processingService.getStatus()
      .pipe(
        tap((processing) => {
          this.isInteractionProcessingActive = processing === 'Active';
          this.updateStatusText();
        }),
        switchMap((processing) => processing ? interactions$ : NEVER.pipe<Array<Interaction>>(startWith([]))),
        concatMap((raw) => this.calibrationService.computeCalibratedAbsolutePosition(raw))
      )
      .subscribe(
        (result) => this.updateInteractions(result),
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
 
    this.intervalSubscription = this.processingService.getInterval()
      .subscribe(
        (result) => {
          this.updateInterval(result);
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
 
    this.remoteSettingsSubscription = this.processingService.getRemoteProcessorSettings()
      .subscribe(
        (result) => {
          this.updateRemoteSettings(result);
        },
        (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }
      );
  }
 
  public ngOnDestroy(): void {
    this.statusSubscription?.unsubscribe();
    this.selectedProcessorSubscription?.unsubscribe();
    this.intervalSubscription?.unsubscribe();
    this.interactionsSubscription?.unsubscribe();
    this.observerSubscription?.unsubscribe();
    this.remoteSettingsSubscription?.unsubscribe();
    this.saveSettingsSubscription?.unsubscribe();
  }
 
  public isInteractionProcessingActiveChanged(): void {
    this.processingService.toggleProcessing()
      .subscribe((result) => {
        console.log(`Processing status toggle - result:  ${result.status} - ${result.body?.value}`);
        this.isInteractionProcessingActive = result.body?.value as boolean;
 
      }, (error) => {
        console.error(error);
        this.logService.sendErrorLog(`${error}`);
      });
  }
 
  public setProcessor(): void {
    this.processingService.setObserverType(this.processors[this.selectedProcessorIdx]).subscribe((result) => {
      console.log(`response to change Observer Type: ${result.status} - ${result.body?.value}`);
    }, (error) => {
      console.error(error);
      this.logService.sendErrorLog(`${error}`);
    });
  }
 
  public setInterval(): void {
    this.processingService.setInterval(this.interval).subscribe((result) => {
      console.log(`Response to change interval request: ' + ${result.status} - Value: ${result.body?.value}`);
    }, (error) => {
      console.error(error);
      this.logService.sendErrorLog(`${error}`);
    });
  }
 
  public setAddress(): void {
    this.updateAddress();
  }
 
  public setPort(): void {
    this.updateAddress();
  }
 
  public updateAddress(): void {
    const completeAddress = `${this.remoteAddress}:${this.remotePort}`;
    try {
      const url = new URL(completeAddress);
      Iif (url) {
        this.remoteSettings.address = completeAddress;
        this.updateSettings();
      }
    } catch (exc) {
      console.warn(`Invalid URL: ${completeAddress}`);
    }
  }
 
  public updateSettings(): void {
    this.processingService.setRemoteProcessorSettings(this.remoteSettings).subscribe((result) => {
      console.log(`Response to set remote processing settings request: ' + ${result.status} - Value: ${JSON.stringify(result.body)}`);
    }, (error) => {
      console.error(error);
      this.logService.sendErrorLog(`${error}`);
    });
  }
 
  public saveRemoteProcessingSettings(): void {
    this.saveSettingsSubscription = this.saveSettings$.subscribe(
      (result) => {
        console.log(`saved settings:  ${JSON.stringify(result)}`);
      },
      (error) => {
        console.error(error);
        this.logService.sendErrorLog(`${error}`);
      }
    );
  }
 
  private updateStatusText(): void {
    this.statusText = this.isInteractionProcessingActive ? 'Active' : 'Inactive';
  }
 
  private updateProcessor(idx: number): void {
    this.selectedProcessorIdx = idx;
    if (this.processingSettings) {
      this.processingSettings.interactionType = idx as ObserverType;
    }
  }
 
  private updateInterval(interval: number): void {
    if (this.processingSettings) {
      this.processingSettings.intervalDuration = interval;
    }
    this.interval = interval;
  }
 
  private updateInteractions(interactions: CompleteInteractionData): void {
    this.eventId++;
 
    this.interactionsList?.updateInteractions(interactions);
    this.visualization?.updateCalibratedInteractions(interactions);
  }
 
  private updateRemoteSettings(settings: RemoteProcessingServiceSettings): void {
    this.remoteSettings = settings;
 
    const url = new URL(this.remoteSettings.address);
    this.remoteAddress = `${url.protocol}//${url.hostname}`;
    this.remotePort = Number.parseFloat(url.port);
  }
}