All files / src/app/tracking/depth-image depth-image.component.ts

96.55% Statements 56/58
100% Branches 18/18
90.47% Functions 19/21
96.36% Lines 53/55

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                                    1x   1x           6x   6x   6x 6x   6x   6x       6x       6x           6x   6x   6x   6x       6x     18x     18x         18x       3x 3x         30x           5x   5x 1x     5x 1x 1x           6x     18x 8x   10x     10x     9x 8x         9x           6x       6x 6x 6x           3x 1x     2x 1x   1x         8x                   8x   8x       10x 2x         9x 1x     8x   8x        
import { CommonModule } from '@angular/common';
import { Component, ElementRef, EventEmitter, Inject, Input, OnDestroy, OnInit, Output, Renderer2, ViewChild } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { DepthCameraState } from '@reflex/shared-types';
import { BehaviorSubject, NEVER, Observable, Subscription, combineLatest, Subject } from 'rxjs';
import { distinctUntilChanged, map, switchMap, tap } from 'rxjs/operators';
import { TrackingService } from 'src/shared/services/tracking.service';
import { WebSocketService } from 'src/shared/services/webSocket.service';
 
@Component({
  selector: 'app-depth-image',
  templateUrl: './depth-image.component.html',
  styleUrls: ['./depth-image.component.scss'],
  imports: [
    CommonModule,
    FormsModule
  ]
})
export class DepthImageComponent implements OnInit, OnDestroy {
 
  private static readonly fullScreenClassName = 'fullScreen';
 
  @ViewChild('depthImageRenderContainer')
  public container?: ElementRef;
 
  @Output()
  public fullScreenChanged = new EventEmitter<boolean>();
 
  public imageData = '';
 
  public livePreview = false;
  public numFramesReceived = 0;
 
  private pfullScreen = false;
 
  private readonly livePreview$ = new BehaviorSubject<boolean>(false);
 
  private readonly shouldShowLiveView$: Observable<boolean>;
 
  private plivePreviewEnabled = false;
 
  private depthImageSubscription?: Subscription;
 
  private readonly address = 'depthImage';
 
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
  private socket?: Subject<MessageEvent>;
 
  // eslint-disable-next-line new-cap
  public constructor(@Inject('BASE_URL') private readonly baseUrl: string, private readonly angularRenderer: Renderer2, private readonly wsService: WebSocketService, private readonly trackingService: TrackingService) {
 
    this.baseUrl = this.baseUrl.replace('http', 'ws');
 
    const isTracking$ = this.trackingService.getStatus()
      .pipe(
        map((status) => status.depthCameraStateName === DepthCameraState[DepthCameraState.Streaming]),
        distinctUntilChanged()
      );
 
    this.shouldShowLiveView$ = combineLatest(isTracking$, this.livePreview$)
      .pipe(
        tap(([isTracking, livePreview]) => {
          console.debug(`tracking: ${isTracking}, livePreview: ${livePreview}`);
 
        }),
        map(([isTracking, livePreview]) => livePreview && isTracking)
      );
  }
 
  public get fullScreen(): boolean {
    return this.pfullScreen;
  }
 
  public set fullScreen(fs: boolean) {
    this.pfullScreen = fs;
    this.fullScreenChanged.emit(this.pfullScreen);
  }
 
  // eslint-disable-next-line @typescript-eslint/member-ordering
  public get livePreviewEnabled(): boolean {
    return this.plivePreviewEnabled;
  }
 
  // eslint-disable-next-line @typescript-eslint/member-ordering
  @Input()
  public set livePreviewEnabled(value: boolean) {
    this.plivePreviewEnabled = value;
 
    if (!value) {
      this.numFramesReceived = 0;
    }
 
    if (!this.plivePreviewEnabled && this.livePreview) {
      this.livePreview = false;
      this.livePreviewChanged();
    }
  }
 
 
  public ngOnInit(): void {
    this.depthImageSubscription = this.shouldShowLiveView$
      .pipe(
        switchMap((showLiveView) => {
          if (showLiveView) {
            return this.startSocket();
          } else {
            this.stopSocket();
          }
 
          return NEVER.pipe();
        }),
        tap((result) => {
          if (result.data !== undefined && result.data !== null) {
            this.numFramesReceived++;
          }
        })
      )
      .subscribe(
        (result) => this.updateDepthImage(result),
        (error) => console.error(error)
      );
  }
 
  public ngOnDestroy(): void {
    this.depthImageSubscription?.unsubscribe();
  }
 
  public livePreviewChanged(): void {
    this.livePreview$.next(this.livePreview);
    this.trackingService.setDepthImagePreviewState(this.livePreview).subscribe(
      () => this.livePreview$.next(this.livePreview)
    );
  }
 
  public updateSize(): void {
 
    if (this.container === undefined) {
      return;
    }
 
    if (this.fullScreen) {
      this.angularRenderer.addClass(this.container.nativeElement, DepthImageComponent.fullScreenClassName);
    } else {
      this.angularRenderer.removeClass(this.container.nativeElement, DepthImageComponent.fullScreenClassName);
    }
  }
 
  private startSocket(): Observable<MessageEvent> {
    this.socket = this.wsService.createSocket({
      url: `${this.baseUrl}${this.address}`,
      deserializer: (value) => value
    });
 
    // this._socket = webSocket({
    //   url: `${this.baseUrl}${this._address}`,
    //   deserializer: (value) => value
    // });
 
    this.socket.next(new MessageEvent('message', { data: 'Start' }));
 
    return this.wsService.getResponseStream();
  }
 
  private stopSocket(): void {
    if (this.socket) {
      this.socket.complete();
    }
  }
 
  private updateDepthImage(result: MessageEvent): void {
    if (result.data === undefined || result.data === null) {
      return;
    }
 
    this.imageData = result.data as string;
 
    this.socket?.next(new MessageEvent('message', { data: 'continue' }));
  }
 
}