All files / src/app/components/signals-values-table signals-values-table.component.ts

85.91% Statements 61/71
53.84% Branches 14/26
85% Functions 17/20
84.61% Lines 55/65

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 194 195 196 197 198 199 200 201 202                                                                      93x 2x                                     2x   2x   2x     2x 2x   2x                   2x 2x 2x       2x 4x 4x 2x 2x         2x     2x           1x 1x 1x                                   3x 3x   3x 5x   3x 3x 3x           3x 3x     2x   2x 2x 2x 4x 4x 4x     2x   2x           4x 6x 6x 5x   10x 5x 5x   10x 5x 5x                   1x 1x         3x 3x 1x                         1x        
import { Component, OnDestroy, OnInit, ViewChild, AfterViewInit } from '@angular/core';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { DurationOption, Signal, SignalSample } from '../../models/signals';
import { TableModule, TableRowReorderEvent } from 'primeng/table';
import { MultiSelectModule } from 'primeng/multiselect';
import { FormsModule } from '@angular/forms';
import { DatePipe, NgFor, NgIf } from '@angular/common';
import { Subscription } from 'rxjs';
import { DialogModule } from 'primeng/dialog';
import { SignalCardComponent } from '../signal-card/signal-card.component';
import { ButtonModule } from 'primeng/button';
import { ActivatedRoute, Router } from '@angular/router';
import { MultiSelectSignalsComponent } from '../multi-select-signals/multi-select-signals.component';
import { RefreshRateSelectorComponent } from '../refresh-rate-selector/refresh-rate-selector.component';
import { Badge } from 'primeng/badge';
 
 
interface Column {
  label: string;
  field: string;
  onSample: boolean;
}
 
interface Row {
  signal: Signal;
  sample: SignalSample;
}
 
 
@Component({
  selector: 'app-signals-values-table',
  imports: [TableModule, MultiSelectModule, FormsModule, NgFor, NgIf, DialogModule, SignalCardComponent, ButtonModule, MultiSelectSignalsComponent, RefreshRateSelectorComponent, Badge, DatePipe],
  templateUrl: './signals-values-table.component.html',
  styleUrl: './signals-values-table.component.scss'
})
export class SignalsValuesTableComponent implements OnInit, AfterViewInit, OnDestroy {
  @ViewChild(MultiSelectSignalsComponent) multiSelectSignals!: MultiSelectSignalsComponent;
 
  signalIdsToDisplay: string[];
  signalsById: Map<string, Signal>;
 
  samples: SignalSample[];
  columns: Column[];
  selectedColumns: Column[];
  isLoading: boolean;
  rows: Row[];
  valuesSubscription: Subscription;
  signalDialogVisible: boolean;
  selectedSignal: Signal;
  signalToDeviceName: Map<Signal, string>;
  signalToDeviceSubscription: Map<Signal, Subscription>;
 
  refreshTime: DurationOption;
 
  lastGraphRefreshTs: number;
  isResponseSlowerThanRefresh: boolean = false;
 
  signalsSelectionLimit: number = 50;
 
  constructor(private twinpadApiService: TwinpadApiService, private router: Router, private route: ActivatedRoute) { }
 
  ngOnInit(): void {
    this.rows = [];
    this.signalsById = new Map();
 
    this.columns = [
      { field: 'value', label: 'Value', onSample: true },
      { field: 'forced_value', label: 'Forced value', onSample: true },
      { field: 'unit', label: 'Unit', onSample: false },
      { field: 'frequency', label: 'Frequency', onSample: false },
      { field: 'type', label: 'Type', onSample: false },
      { field: 'precision_digits', label: 'Precision Digits', onSample: false },
      { field: 'status', label: 'Status', onSample: false }
    ];
 
    this.selectedColumns = this.columns.slice(0, 3);
    this.signalToDeviceName = new Map();
    this.signalToDeviceSubscription = new Map();
  }
 
  ngAfterViewInit(): void {
    this.route.queryParams.subscribe(params => {
      const signalIds = params["signal_id"];
      if (signalIds) {
        Eif (signalIds instanceof Array) {
          this.signalIdsToDisplay = signalIds;
        }
        else {
          this.signalIdsToDisplay = [signalIds];
        }
        this.onSignalsChange();
      }
      else {
        this.signalIdsToDisplay = [];
      }
    });
  }
 
  showSignal(signal: Signal) {
    this.selectedSignal = signal;
    this.signalDialogVisible = true;
    this.router.navigate([], {
      fragment: signal.signal_id,
      queryParamsHandling: 'preserve'
    });
  }
 
  hideSignal() {
    this.route.fragment.subscribe(fragment => {
      if (fragment) {
        const el = document.getElementById(fragment);
        if (el) {
          el.scrollIntoView({ behavior: 'smooth', block: 'start' });
        }
      }
    });
  }
 
  onSignalsChange() {
    this.isLoading = true;
    this.twinpadApiService.getSignals(this.signalsSelectionLimit, 0, null, null, this.signalIdsToDisplay, null).subscribe({
      next: signals => {
        for (const signal of signals.items) {
          this.signalsById.set(signal.signal_id, signal);
        }
        this.getDeviceNameFromSignals();
        this.updateValuesSubscription();
        this.isLoading = false;
      }
    });
  }
 
  updateValuesSubscription() {
    this.valuesSubscription?.unsubscribe();
    this.valuesSubscription = this.twinpadApiService.callPeriodically(() => { return this.twinpadApiService.getSignalValues(this.signalIdsToDisplay); }, 1000 * this.refreshTime.duration)
      .subscribe({
        next: values => {
          this.isResponseSlowerThanRefresh = Date.now() - (this.lastGraphRefreshTs + this.refreshTime.duration * 1000) > this.refreshTime.duration * 1000;
 
          this.samples = values;
          const rows = [];
          for (let i = 0; i < this.signalIdsToDisplay.length; i++) {
            const signal = this.signalsById.get(this.signalIdsToDisplay[i]);
            Eif (signal !== undefined) {
              rows.push({ sample: this.samples[i], signal: signal });
            }
          }
          this.rows = rows;
 
          this.lastGraphRefreshTs = Date.now();
        }
      });
  }
 
  getDeviceNameFromSignals() {
    for (const signalId of this.signalIdsToDisplay) {
      const signal = this.signalsById.get(signalId);
      if (signal !== undefined) {
        this.signalToDeviceSubscription.set(signal, this.twinpadApiService.devicesBehaviorSubject$.subscribe({
          next: devices => {
            if (this.signalToDeviceName.get(signal) !== undefined) {
              this.signalToDeviceSubscription.get(signal)?.unsubscribe();
              return;
            }
            const device = devices.find(device => device.device_id === signalId.split('.')[0]);
            Eif (device !== undefined) {
              this.signalToDeviceName.set(signal, device.name);
            }
          }
        })
        );
      }
    }
  }
 
  handleSignalsLoaded() {
    Eif (this.signalToDeviceName.size === 0) {
      this.getDeviceNameFromSignals();
    }
  }
 
  handleRefreshRateChange(selected: DurationOption) {
    this.refreshTime = selected;
    if (this.signalIdsToDisplay) {
      this.onSignalsChange();
    }
  }
 
  onRowReorder(event: TableRowReorderEvent) {
    if (event.dragIndex !== undefined && event.dropIndex !== undefined) {
      const elements = this.signalIdsToDisplay.splice(event.dragIndex, 1);
      this.signalIdsToDisplay.splice(event.dropIndex, 0, elements[0]);
    }
    this.onSignalsChange();
  }
 
  ngOnDestroy() {
    this.valuesSubscription?.unsubscribe();
  }
 
}