All files / src/shared/services tuio.service.ts

2.04% Statements 1/49
100% Branches 0/0
0% Functions 0/24
2.08% Lines 1/48

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                      1x                                                                                                                                                                                                                                                                                                                                                              
import { Inject, Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { SignalRBaseService } from './signalR.base.service';
import { fromEventPattern, Observable, using } from 'rxjs';
import { concatMap, share, skipWhile } from 'rxjs/operators';
import { LogService } from 'src/app/log/log.service';
import { JsonSimpleValue, TuioConfiguration, TuioPackageDetails } from '@reflex/shared-types';
 
@Injectable({
  providedIn: 'root'
})
export class TuioService extends SignalRBaseService<string> {
  private readonly tuioRoute = 'api/Tuio/';
 
  private readonly getConfigurationRoute = `${this.tuioRoute}GetTuioConfiguration`;
 
  private readonly getIsBroadcastingRoute = `${this.tuioRoute}IsBroadcasting`;
 
  private readonly getTransportProtocolsRoute = `${this.tuioRoute}GetTransportProtocols`;
  private readonly getTuioVersionsRoute = `${this.tuioRoute}GetTuioProtocolVersions`;
  private readonly getTuioInterpretationsRoute = `${this.tuioRoute}GetTuioInterpretations`;
 
  private readonly setAddressRoute = `${this.tuioRoute}SetAddress`;
  private readonly setPortRoute = `${this.tuioRoute}SetPort`;
 
  private readonly selectTransportProtocolRoute = `${this.tuioRoute}SelectTransportProtocol`;
  private readonly selectTuioProtocolRoute = `${this.tuioRoute}SelectTuioProtocol`;
  private readonly selectTuioInterpretationRoute = `${this.tuioRoute}SelectTuioInterpretation`;
 
  private readonly toggleBroadcastRoute = `${this.tuioRoute}ToggleBroadcast`;
 
  private readonly saveTuioSettingsRoute = `${this.tuioRoute}Save`;
 
  private readonly packages$: Observable<TuioPackageDetails>;
  private readonly startPackagesAfterConnected$: Observable<void>;
 
  public constructor(
    private readonly http: HttpClient,
    logService: LogService,
    // eslint-disable-next-line new-cap
    @Inject('BASE_URL') private readonly baseUrl: string
  ) {
    super(`${baseUrl}tuiohub`, 'tuioState', logService);
 
    this.packages$ = fromEventPattern<TuioPackageDetails>(
      (handler) => this.connection.on('currentPackage', handler),
      (handler) => this.connection.off('currentPackage', handler)
    )
      .pipe(
        share()
      );
 
    // send 'startState' only after 'isConnected' emits true
    this.startPackagesAfterConnected$ = this.isConnected.pipe(
      skipWhile((value) => !value),
      concatMap(async () => this.connection.send('startPackageDetails'))
    );
  }
 
  public getPackages(): Observable<TuioPackageDetails> {
    return using(
      () => {
        this.startPackagesAfterConnected$.subscribe(() => {}, (error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        });
 
        // eslint-disable-next-line @typescript-eslint/no-misused-promises
        return { unsubscribe: async () => this.connection.send('stopPackageDetails').catch((error) => {
          console.error(error);
          this.logService.sendErrorLog(`${error}`);
        }) };
      },
      () => this.packages$
    );
  }
 
  public getIsBroadcasting(): Observable<HttpResponse<JsonSimpleValue>> {
    return this.http.get<JsonSimpleValue>(
      this.baseUrl + this.getIsBroadcastingRoute,
      { observe: 'response' }
    );
  }
 
  public getConfig(): Observable<TuioConfiguration> {
    return this.http.get<TuioConfiguration>(
      this.baseUrl + this.getConfigurationRoute,
      { headers: this.headers }
    );
  }
 
  public getTransportProtocols(): Observable<Array<string>> {
    return this.http.get<Array<string>>(
      this.baseUrl + this.getTransportProtocolsRoute,
      { headers: this.headers }
    );
  }
 
  public getTuioProtocolVersions(): Observable<Array<string>> {
    return this.http.get<Array<string>>(
      this.baseUrl + this.getTuioVersionsRoute,
      { headers: this.headers }
    );
  }
 
  public getTuioInterpretations(): Observable<Array<string>> {
    return this.http.get<Array<string>>(
      this.baseUrl + this.getTuioInterpretationsRoute,
      { headers: this.headers }
    );
  }
 
  public setAddress(
    address: string
  ): Observable<HttpResponse<JsonSimpleValue>> {
    const args: JsonSimpleValue = { name: 'Address', value: address };
 
    return this.http.post<JsonSimpleValue>(
      this.baseUrl + this.setAddressRoute,
      args,
      { observe: 'response' }
    );
  }
 
  public setPort(port: number): Observable<HttpResponse<JsonSimpleValue>> {
    const args: JsonSimpleValue = { name: 'Port', value: port };
 
    return this.http.post<JsonSimpleValue>(
      this.baseUrl + this.setPortRoute,
      args,
      { observe: 'response' }
    );
  }
 
  public setTransportProtocol(
    observerType: string
  ): Observable<HttpResponse<JsonSimpleValue>> {
    const args: JsonSimpleValue = { name: 'TransportProtocol', value: observerType };
 
    return this.http.post<JsonSimpleValue>(
      this.baseUrl + this.selectTransportProtocolRoute,
      args,
      { observe: 'response' }
    );
  }
 
  public setTuioProtocolVersion(
    observerType: string
  ): Observable<HttpResponse<JsonSimpleValue>> {
    const args: JsonSimpleValue = { name: 'ProtocolVersion', value: observerType };
 
    return this.http.post<JsonSimpleValue>(
      this.baseUrl + this.selectTuioProtocolRoute,
      args,
      { observe: 'response' }
    );
  }
 
  public setTuioInterpretation(
    observerType: string
  ): Observable<HttpResponse<JsonSimpleValue>> {
    const args: JsonSimpleValue = { name: 'TuioInterpretation', value: observerType };
 
    return this.http.post<JsonSimpleValue>(
      this.baseUrl + this.selectTuioInterpretationRoute,
      args,
      { observe: 'response' }
    );
  }
 
  public toggleBroadcasting(): Observable<HttpResponse<JsonSimpleValue>> {
    return this.http.put<JsonSimpleValue>(
      this.baseUrl + this.toggleBroadcastRoute,
      { headers: this.headers },
      { observe: 'response' }
    );
  }
 
  public saveSettings(): Observable<HttpResponse<JsonSimpleValue>> {
    return this.http.put<JsonSimpleValue>(
      this.baseUrl + this.saveTuioSettingsRoute,
      { headers: this.headers },
      { observe: 'response' }
    );
  }
}