All files / tools/ReFlex.TrackingServer/ClientApp/src/app/tracking/depth-image depth-image.component.ts

97.1% Statements 67/69
100% Branches 34/34
90.9% Functions 20/22
96.96% Lines 64/66

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 2133x                                               3x 3x 3x 3x 3x 3x                 59x   3x           7x   7x   7x 7x 7x 7x 7x 7x 7x   7x   7x       7x       7x             7x   7x     7x           7x         21x   21x         24x       3x 3x         54x           6x   6x 1x     6x 1x 1x         7x     21x 10x   11x     11x     10x 9x           10x           7x       7x 7x   7x       3x 1x     2x 1x         1x               10x                   10x   10x       11x 2x         10x 1x     9x   9x      
import { CommonModule } from '@angular/common';
import {
  ChangeDetectionStrategy,
  ChangeDetectorRef,
  Component,
  ElementRef,
  EventEmitter,
  Input,
  OnDestroy,
  OnInit,
  Output,
  Renderer2,
  ViewChild,
  inject
} from '@angular/core';
import { FormsModule } from '@angular/forms';
import { DepthCameraState } from '@reflex/shared-types';
import {
  BehaviorSubject,
  NEVER,
  Observable,
  Subject,
  Subscription,
  combineLatest
} 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';
import { BASE_URL } from 'src/shared/tokens/base-url.token';
import { markViewForCheck } from 'src/shared/util/mark-view-for-check';
 
@Component({
  selector: 'app-depth-image',
  templateUrl: './depth-image.component.html',
  styleUrls: ['./depth-image.component.scss'],
  changeDetection: ChangeDetectionStrategy.Eager,
  imports: [CommonModule, FormsModule]
})
export class DepthImageComponent implements OnInit, OnDestroy {
 
  private static readonly fullScreenClassName = 'fullScreen';
 
  @ViewChild('depthImageRenderContainer')
  public container?: ElementRef<HTMLDivElement>;
 
  @Output()
  public fullScreenChanged = new EventEmitter<boolean>();
 
  public imageData = '';
 
  public livePreview = false;
  public numFramesReceived = 0;
  private readonly changeDetector = inject(ChangeDetectorRef);
  private readonly baseUrl = inject(BASE_URL);
  private readonly angularRenderer = inject(Renderer2);
  private readonly wsService = inject(WebSocketService);
  private readonly trackingService = inject(TrackingService);
 
  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';
 
 
  private socket?: Subject<MessageEvent>;
 
 
  public constructor() {
    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++;
          }
        })
      )
      .pipe(markViewForCheck(this.changeDetector))
      .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' }));
  }
}