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

47.91% Statements 23/48
21.42% Branches 3/14
42.85% Functions 6/14
46.51% Lines 20/43

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                                                                    36x         1x 1x             1x           1x     1x 1x 1x 1x     1x                 1x   1x                                                                                 8x 7x 7x 70x 70x   7x   7x                                     1x        
import { Component, OnDestroy, OnInit } from '@angular/core';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { Signal, SignalSample } from '../../models/signals';
import { ListResponse } from '../../models/response';
import { TableModule, TableRowReorderEvent } from 'primeng/table';
import { MultiSelectModule } from 'primeng/multiselect';
import { FormsModule } from '@angular/forms';
import { 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';
 
 
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],
  templateUrl: './signals-values-table.component.html',
  styleUrl: './signals-values-table.component.css'
})
export class SignalsValuesTableComponent implements OnInit, OnDestroy{
 
  signals: Signal[];
  signalIds: string[];
  signalsById: Map<string, Signal>;
  virtualScrollItemSize: number = 10;
  totalSignals: number = 0;
 
  signalIdsToDisplay: string[];
 
  samples: SignalSample[];
  columns: Column[];
  selectedColumns: Column[];
  isLoading: boolean = true;
  rows: Row[];
  valuesSubscription: Subscription;
  signalDialogVisible: boolean;
  selectedSignal: Signal;
 
  constructor(private twinpadApiService: TwinpadApiService, private router: Router, private route: ActivatedRoute) {}
 
  ngOnInit(): void {
    this.signals = [];
    this.signalIds = [];
    this.signalsById = new Map();
    this.rows = [];
 
 
    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}
      ];
 
    this.selectedColumns = this.columns.slice(0, 3);
 
    this.loadRecursively();
  }
 
  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.valuesSubscription?.unsubscribe();
    this.valuesSubscription = this.twinpadApiService.callPeriodically(() => {return this.twinpadApiService.getSignalValues(this.signalIdsToDisplay);}, 1000)
      .subscribe({next: (values)=>{
        this.samples = values;
        const rows = [];
        for (let i=0; i<this.signalIdsToDisplay.length; i++){
          const signal = this.signalsById.get(this.signalIdsToDisplay[i]);
          if (signal !== undefined){
            rows.push({sample: this.samples[i], signal:signal});
          }
        }
        this.rows = rows;
      }
    });
  }
 
  loadRecursively(){
    this.twinpadApiService.getSignals(this.virtualScrollItemSize, this.signalIds.length, null, null, null).subscribe((data: ListResponse<Signal>) => {
      this.totalSignals = data.total;
      for (const signal of data.items){
        this.signalIds.push(signal.signal_id);
        this.signalsById.set(signal.signal_id, signal);
      }
      Eif(this.signalIds.length < this.totalSignals)
      {
        this.loadRecursively();
      }
    });
  }
 
  handleSelectionChange(selected: string[]) {
    this.signalIdsToDisplay = selected;
    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();
  }
 
}