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

3.03% Statements 1/33
100% Branches 0/0
0% Functions 0/13
3.03% Lines 1/33

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                    1x                                                                                                                                                                                        
import { Inject, Injectable } from '@angular/core';
import { HttpClient, HttpResponse } from '@angular/common/http';
import { SignalRBaseService } from './signalR.base.service';
import { Observable } from 'rxjs';
import { LogService } from 'src/app/log/log.service';
import { JsonSimpleValue, NetworkAttributes } from '@reflex/shared-types';
 
@Injectable({
  providedIn: 'root'
})
export class NetworkingService extends SignalRBaseService<string> {
 
  private readonly networkingRoute = 'api/Network/';
 
  private readonly getStatusRoute = `${this.networkingRoute}Status`;
 
  private readonly getAddressRoute = `${this.networkingRoute}GetAddress`;
  private readonly getPortRoute = `${this.networkingRoute}GetPort`;
  private readonly getEndpointRoute = `${this.networkingRoute}GetEndpoint`;
 
  private readonly setAddressRoute = `${this.networkingRoute}SetAddress`;
  private readonly setPortRoute = `${this.networkingRoute}SetPort`;
  private readonly setEndpointRoute = `${this.networkingRoute}SetEndpoint`;
 
  private readonly getSelectedNetworkTypeRoute = `${this.networkingRoute}GetNetworkType`;
 
  private readonly selectNetworkTypeRoute = `${this.networkingRoute}SelectNetworkType`;
 
  private readonly getNetworkTypesRoute = `${this.networkingRoute}GetNetworkTypes`;
 
  private readonly toggleNetworkingRoute = `${this.networkingRoute}ToggleNetworking`;
 
  private readonly saveNetworkingSettingsRoute = `${this.networkingRoute}Save`;
 
  public constructor(
    private readonly http: HttpClient,
    // eslint-disable-next-line new-cap
    @Inject('BASE_URL') private readonly baseUrl: string,
    logService: LogService
  ) {
    super(`${baseUrl}nethub`, 'networkingState', logService);
  }
 
  public getStatusValues(): Observable<NetworkAttributes> {
    return this.http.get<NetworkAttributes>(this.baseUrl + this.getStatusRoute, { headers: this.headers });
  }
 
  public getAddress(): Observable<string> {
    return this.http.get(this.baseUrl + this.getAddressRoute, { responseType: 'text' });
  }
 
  public getPort(): Observable<number> {
    return this.http.get<number>(this.baseUrl + this.getPortRoute, { headers: this.headers });
  }
 
  public getEndpoint(): Observable<string> {
    return this.http.get(this.baseUrl + this.getEndpointRoute, { responseType: 'text' });
  }
 
 
  public getNetworkInterfaces(): Observable<Array<string>> {
    return this.http.get<Array<string>>(this.baseUrl + this.getNetworkTypesRoute, { headers: this.headers });
  }
 
  public getSelectedNetworkInterface(): Observable<number> {
    return this.http.get<number>(this.baseUrl + this.getSelectedNetworkTypeRoute, { 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 setEndpoint(endpoint: string): Observable<HttpResponse<JsonSimpleValue>> {
    const args: JsonSimpleValue = { name: 'Endpoint', value: endpoint };
 
    return this.http.post<JsonSimpleValue>(this.baseUrl + this.setEndpointRoute, args, { observe: 'response' });
  }
 
  public setNetworkInterface(observerType: string): Observable<HttpResponse<JsonSimpleValue>> {
    const args: JsonSimpleValue = { name: 'NetworkType', value: observerType };
 
    return this.http.post<JsonSimpleValue>(this.baseUrl + this.selectNetworkTypeRoute, args, { observe: 'response' });
  }
 
  public toggleBroadcasting(): Observable<HttpResponse<JsonSimpleValue>> {
    return this.http.put<JsonSimpleValue>(this.baseUrl + this.toggleNetworkingRoute, { headers: this.headers }, { observe: 'response' });
  }
 
  public saveSettings(): Observable<HttpResponse<JsonSimpleValue>> {
    return this.http.put<JsonSimpleValue>(this.baseUrl + this.saveNetworkingSettingsRoute, { headers: this.headers }, { observe: 'response' });
  }
 
 
}