All files / src/app/components/command-center command-center.component.ts

95.83% Statements 23/24
100% Branches 6/6
91.66% Functions 11/12
100% Lines 19/19

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                                  134x 26x               26x   26x 26x           59x   59x 33x 33x   104x     3962x           10082x       4x 4x 1x 1x   3x   3x     108x    
import { Component, OnInit, inject } from '@angular/core';
 
import { SelectModule } from 'primeng/select';
import { FormsModule } from '@angular/forms';
 
import { first } from 'rxjs';
 
import { SignalCommandComponent } from '../signal-command/signal-command.component';
import { Signal } from '../../models/signals';
import { TwinpadApiService } from '../../services/twinpad-api.service';
 
@Component({
  selector: 'app-command-center',
  imports: [SignalCommandComponent, SelectModule, FormsModule],
  templateUrl: './command-center.component.html',
  styleUrl: './command-center.component.scss'
})
export class CommandCenterComponent implements OnInit {
  private twinpadApiService = inject(TwinpadApiService);
 
  signalIds: string[];
  signalIdsToDisplay: string[];
  _selectedSignalId: string;
  signal: Signal | undefined;
 
  ngOnInit(): void {
    this.twinpadApiService.getSignalsIds().subscribe({
      next: signalIds => {
        this.signalIds = signalIds;
        this.filterSignalIds();
      }
    });
  }
 
  filterSignalIds() {
    this.twinpadApiService.devicesBehaviorSubject$.pipe(first()).subscribe({
      next: devices => {
        if (devices.length === 0) {
          setTimeout(() => this.filterSignalIds(), 50);
          return;
        }
        const downDeviceIds: string[] = devices.filter(device => device.status !== "up").map(device => device.device_id);
        // display only signals of devices who are not down
        // can't filter on devices who are up since some signals don't come from real devices (data storage)
        this.signalIdsToDisplay = this.signalIds.filter(signalId => downDeviceIds.indexOf(signalId.split(".")[0]) === -1);
      }
    });
  }
 
  get selectedSignalId() {
    return this._selectedSignalId;
  }
 
  set selectedSignalId(newValue: string) {
    this._selectedSignalId = newValue;
    if (this._selectedSignalId === null) {
      this.signal = undefined;
      return;
    }
    this.twinpadApiService.getSignal(this._selectedSignalId).subscribe({
      next: signal => {
        this.signal = signal;
      }
    });
  }
}