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

81.13% Statements 43/53
40% Branches 8/20
78.57% Functions 11/14
79.16% Lines 38/48

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                                                                    54x 2x             2x                 2x   2x     2x   2x 4x 4x 2x 2x         2x     2x       2x                 2x       1x 1x 1x                                   5x 6x   5x   5x 5x 5x 5x 5x 5x     5x   5x     5x 5x 5x 5x   5x               3x 3x 3x                         1x        
import { Component, OnDestroy, OnInit, ViewChild } 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 { 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';
 
 
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],
  templateUrl: './signals-values-table.component.html',
  styleUrl: './signals-values-table.component.scss'
})
export class SignalsValuesTableComponent implements OnInit, OnDestroy{
  @ViewChild(MultiSelectSignalsComponent) multiSelectSignals!: MultiSelectSignalsComponent;
 
  signalIdsToDisplay: string[];
 
  samples: SignalSample[];
  columns: Column[];
  selectedColumns: Column[];
  isLoading: boolean = true;
  rows: Row[];
  valuesSubscription: Subscription;
  signalDialogVisible: boolean;
  selectedSignal: Signal;
 
  refreshTime: DurationOption;
 
  lastGraphRefreshTs: number;
  isResponseSlowerThanRefresh: boolean = false;
 
  constructor(private twinpadApiService: TwinpadApiService, private router: Router, private route: ActivatedRoute) {}
 
  ngOnInit(): void {
    this.rows = [];
 
    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 = [];
      }
    });
 
    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);
  }
 
  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 * 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.multiSelectSignals.signalsById.get(this.signalIdsToDisplay[i]);
          Eif (signal !== undefined){
            rows.push({sample: this.samples[i], signal:signal});
          }
        }
        this.rows = rows;
 
        this.lastGraphRefreshTs = Date.now();
      }
    });
    for (const signalId of this.signalIdsToDisplay) {
      const signal = this.multiSelectSignals.signalsById.get(signalId);
      Eif (signal !== undefined) {
        this.twinpadApiService.getDevice(signalId.split('.')[0]).subscribe({
          next: (data) => {
            signal.device = data;
          }
        });
      }
    }
  }
 
  handleRefreshRateChange(selected: DurationOption) {
    this.refreshTime = selected;
    Eif (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();
  }
 
}