All files / src/app/network network.component.ts

100% Statements 60/60
100% Branches 1/1
96.66% Functions 29/30
100% Lines 58/58

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                                                1x   10x 10x   10x   10x 10x   10x             10x         10x 9x 9x   1x 1x         10x 10x 10x         9x 8x 8x 8x 8x 8x 8x   1x 1x     9x   8x   1x 1x         4x 4x   3x 3x   1x 1x 1x         4x   3x   1x 1x         3x   2x   1x 1x         3x   2x   1x 1x         3x   2x   1x 1x                 2x     1x 1x         8x 8x 8x        
import { Component, OnDestroy, OnInit } from '@angular/core';
import { Subscription } from 'rxjs';
import { NetworkingService } from 'src/shared/services/networking.service';
import { SettingsService } from 'src/shared/services/settingsService';
import { LogService } from '../log/log.service';
import { NetworkInterface, NetworkSettings } from '@reflex/shared-types';
import { PanelHeaderComponent, ValueSelectionComponent, ValueSliderComponent, ValueTextComponent } from '@reflex/angular-components/dist';
import { CommonModule } from '@angular/common';
import { FormsModule } from '@angular/forms';
import { TuioComponent } from './tuio/tuio.component';
 
@Component({
  selector: 'app-network',
  templateUrl: './network.component.html',
  imports: [
    CommonModule,
    FormsModule,
    PanelHeaderComponent,
    ValueSelectionComponent,
    ValueTextComponent,
    ValueSliderComponent,
    TuioComponent
  ]
})
export class NetworkComponent implements OnInit, OnDestroy {
 
  public statusText = '';
  public errorText = '';
 
  public isBroadcasting = false;
 
  public networkInterfaces: Array<string> = [];
  public selectedInterfaceIdx = -1;
 
  public networkSettings: NetworkSettings = { networkInterfaceType: NetworkInterface.None, port: 0, endpoint: '', address: '', interval: 100 };
 
  private statusTextSubscription?: Subscription;
  private statusValuesSubscription?: Subscription;
 
  private settingsSubscription?: Subscription;
 
  public constructor(private readonly settingsService: SettingsService, private readonly networkService: NetworkingService, private readonly logService: LogService) {
 
  }
 
  public ngOnInit(): void {
    this.settingsSubscription = this.settingsService.getSettings().subscribe((result) => {
      this.networkSettings = result.networkSettingValues;
      this.fetchCurrentValues();
    }, (error) => {
      console.error(error);
      this.logService.sendErrorLog(`${error}`);
    });
  }
 
  public ngOnDestroy(): void {
    this.settingsSubscription?.unsubscribe();
    this.statusTextSubscription?.unsubscribe();
    this.statusValuesSubscription?.unsubscribe();
  }
 
  public fetchCurrentValues(): void {
 
    this.statusValuesSubscription = this.networkService.getStatusValues().subscribe((result) => {
      this.networkInterfaces = result.interfaces;
      this.networkSettings.address = result.address;
      this.networkSettings.port = result.port;
      this.networkSettings.endpoint = result.endpoint;
      this.updateInterfaceType(result.selectedInterface);
      this.isBroadcasting = result.isActive;
    }, (error) => {
      console.error(error);
      this.logService.sendErrorLog(`${error}`);
    });
 
    this.statusTextSubscription = this.networkService.getStatus()
      .subscribe((result) => {
        this.statusText = result;
      }, (error) => {
        console.error(error);
        this.logService.sendErrorLog(`${error}`);
      });
  }
 
  public isBroadcastingChanged(): void {
    this.errorText = '';
    this.networkService.toggleBroadcasting()
      .subscribe((result) => {
        console.log(`Networking status toggle - result:  ${result.status} - ${result.body?.value}`);
        this.isBroadcasting = result.body?.value as boolean;
      }, (error) => {
        this.isBroadcasting = false;
        this.errorText = `${error} - ${JSON.stringify(error, null, 3)}`;
        this.logService.sendErrorLog(this.errorText);
      });
  }
 
  public setNetworkInterface(): void {
    this.networkService.setNetworkInterface(this.networkInterfaces[this.selectedInterfaceIdx])
      .subscribe((result) => {
        console.log(`response to change NetworkInterface Type: ${result.status} - ${result.body?.value}`);
      }, (error: unknown) => {
        console.error(error);
        this.logService.sendErrorLog(`${error}`);
      });
  }
 
  public setAddress(): void {
    this.networkService.setAddress(this.networkSettings.address)
      .subscribe((result) => {
        console.log(`response to change address: ${result.status} - ${result.body?.value}`);
      }, (error: unknown) => {
        console.error(error);
        this.logService.sendErrorLog(`${error}`);
      });
  }
 
  public setPort(): void {
    this.networkService.setPort(this.networkSettings.port)
      .subscribe((result) => {
        console.log(`response to change port: ${result.status} - ${result.body?.value}`);
      }, (error: unknown) => {
        console.error(error);
        this.logService.sendErrorLog(`${error}`);
      });
  }
 
  public setEndpoint(): void {
    this.networkService.setEndpoint(this.networkSettings.endpoint)
      .subscribe((result) => {
        console.log(`response to change endpoint: ${result.status} - ${result.body?.value}`);
      }, (error: unknown) => {
        console.error(error);
        this.logService.sendErrorLog(`${error}`);
      });
  }
 
  public setInterval(): void {
    // Todo: implement on server side
  }
 
  public saveNetworkSettings(): void {
    this.networkService.saveSettings()
      .subscribe(() => {
      }, (error) => {
        this.errorText = `Cannot save settings: ${error} - ${JSON.stringify(error, null, 3)}`;
        this.logService.sendErrorLog(this.errorText);
      });
  }
 
  public updateInterfaceType(idx: number): void {
    this.selectedInterfaceIdx = idx;
    if (this.networkSettings) {
      this.networkSettings.networkInterfaceType = idx as NetworkInterface;
    }
  }
}