All files / tools/ReFlex.TrackingServer/ClientApp/src/shared/services processing.service.ts

9.58% Statements 7/73
60% Branches 3/5
0% Functions 0/37
8.45% Lines 6/71

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 1943x                 3x 3x 3x 3x         3x                                                                                                                                                                                                                                                                                                                                                                
import { HttpClient, HttpResponse } from '@angular/common/http';
import { inject, Injectable } from '@angular/core';
import {
  Interaction,
  InteractionFrame,
  InteractionHistory,
  JsonSimpleValue,
  RemoteProcessingServiceSettings
} from '@reflex/shared-types';
import { fromEventPattern, Observable, using } from 'rxjs';
import { LogService } from 'src/app/log/log.service';
import { BASE_URL } from 'src/shared/tokens/base-url.token';
import { SignalRBaseService } from './signalR.base.service';
 
@Injectable({
  providedIn: 'root'
})
export class ProcessingService extends SignalRBaseService<string> {
  private readonly http = inject(HttpClient);
  private readonly baseUrl: string;
 
  private readonly processingRoute = 'api/Processing/';
  private readonly isLoopRunningRoute = `${this.processingRoute}IsLoopRunning/`;
  private readonly getIntervalRoute = `${this.processingRoute}GetInterval/`;
  private readonly getRemoteProcessorSettingsRoute = `${this.processingRoute}GetRemoteProcessorSettings/`;
  private readonly getObserverTypesRoute = `${this.processingRoute}GetObserverTypes/`;
  private readonly getSelectedObserverTypeRoute = `${this.processingRoute}GetObserverType/`;
  private readonly setIntervalRoute = `${this.processingRoute}SetUpdateInterval`;
  private readonly setRemoteProcessorSettingsRoute = `${this.processingRoute}SetRemoteProcessorSettings/`;
  private readonly setObserverTypeRoute = `${this.processingRoute}SelectObserverType`;
  private readonly toggleInteractionProcessingRoute = `${this.processingRoute}ToggleInteractionProcessing/`;
 
 
  public constructor() {
    const baseUrl = inject(BASE_URL);
    const logService = inject(LogService);
 
    super(`${baseUrl}prochub`, 'processingState', logService);
 
    this.baseUrl = baseUrl;
  }
 
  public getIsLoopRunning(): Observable<HttpResponse<JsonSimpleValue>> {
    return this.http.get<HttpResponse<JsonSimpleValue>>(
      this.baseUrl + this.isLoopRunningRoute
    );
  }
 
  public getInterval(): Observable<number> {
    return this.http.get<number>(this.baseUrl + this.getIntervalRoute);
  }
 
  public getRemoteProcessorSettings(): Observable<RemoteProcessingServiceSettings> {
    return this.http.get<RemoteProcessingServiceSettings>(
      this.baseUrl + this.getRemoteProcessorSettingsRoute
    );
  }
 
  public getObserverTypes(): Observable<Array<string>> {
    return this.http.get<Array<string>>(
      this.baseUrl + this.getObserverTypesRoute
    );
  }
 
  public getSelectedObserverType(): Observable<number> {
    return this.http.get<number>(
      this.baseUrl + this.getSelectedObserverTypeRoute
    );
  }
 
  public setInterval(
    interval: number
  ): Observable<HttpResponse<JsonSimpleValue>> {
    const args: JsonSimpleValue = { name: 'UpdateInterval', value: interval };
 
    return this.http.post<JsonSimpleValue>(
      this.baseUrl + this.setIntervalRoute,
      args,
      { observe: 'response' }
    );
  }
 
  public setRemoteProcessorSettings(
    settings: RemoteProcessingServiceSettings
  ): Observable<HttpResponse<RemoteProcessingServiceSettings>> {
    return this.http.post<RemoteProcessingServiceSettings>(
      this.baseUrl + this.setRemoteProcessorSettingsRoute,
      settings,
      { observe: 'response' }
    );
  }
 
  public setObserverType(
    observerType: string
  ): Observable<HttpResponse<JsonSimpleValue>> {
    const args: JsonSimpleValue = { name: 'ObserverType', value: observerType };
 
    return this.http.post<JsonSimpleValue>(
      this.baseUrl + this.setObserverTypeRoute,
      args,
      { observe: 'response' }
    );
  }
 
  public toggleProcessing(): Observable<HttpResponse<JsonSimpleValue>> {
    return this.http.put<JsonSimpleValue>(
      this.baseUrl + this.toggleInteractionProcessingRoute,
      { headers: this.headers },
      { observe: 'response' }
    );
  }
 
  public getInteractions(): Observable<Array<Interaction>> {
    const interactions$ = fromEventPattern<Array<Interaction>>(
      (handler) => this.connection.on('interactions', handler),
      (handler) => this.connection.off('interactions', handler)
    );
 
    return using(
      () => {
        this.connection.send('StartInteractions').catch((error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        });
 
        return {
          // @todo: check if this is correct
          // eslint-disable-next-line @typescript-eslint/no-misused-promises
          unsubscribe: async () =>
            this.connection.send('StopInteractions').catch((error) => {
              console.error(error);
              this.logService.sendErrorLog(`${error}`);
            })
        };
      },
      () => interactions$
    );
  }
 
  public getFrames(): Observable<Array<InteractionFrame>> {
    const frames$ = fromEventPattern<Array<InteractionFrame>>(
      (handler) => this.connection.on('frames', handler),
      (handler) => this.connection.off('frames', handler)
    );
 
    return using(
      () => {
        this.connection.send('StartInteractionFrames').catch((error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        });
 
        return {
          // @todo: check if this is correct
          // eslint-disable-next-line @typescript-eslint/no-misused-promises
          unsubscribe: async () =>
            this.connection.send('StopInteractionFrames').catch((error) => {
              console.error(error);
              this.logService.sendErrorLog(`${error}`);
            })
        };
      },
      () => frames$
    );
  }
 
  public getHistory(): Observable<Array<InteractionHistory>> {
    const history$ = fromEventPattern<Array<InteractionHistory>>(
      (handler) => this.connection.on('history', handler),
      (handler) => this.connection.off('history', handler)
    );
 
    return using(
      () => {
        this.connection.send('StartInteractionHistory').catch((error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        });
 
        return {
          // @todo: check if this is correct
          // eslint-disable-next-line @typescript-eslint/no-misused-promises
          unsubscribe: async () =>
            this.connection.send('StopInteractionHistory').catch((error) => {
              console.error(error);
              this.logService.sendErrorLog(`${error}`);
            })
        };
      },
      () => history$
    );
  }
}