All files / src/app/components/signal-card signal-card.component.ts

81.25% Statements 39/48
64.28% Branches 9/14
72.22% Functions 13/18
79.06% Lines 34/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                                                101x 10x     10x 10x             10x   10x   10x     10x 10x 10x 10x   10x 10x 9x 9x 4x 10x   5x 9x     15x 6x   33x 15x 15x           10x 25x     24x 24x             10x 16x     15x                 3x 3x                                       388x      
import { Component, inject, OnChanges, OnDestroy } from '@angular/core';
import { Input } from '@angular/core';
import { SampleStats, Signal } from '../../models/signals';
import { RouterLink } from '@angular/router';
import { NgIf } from '@angular/common';
import { SignalCommandComponent } from '../signal-command/signal-command.component';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { SignalSample } from '../../models/signals';
import { Subscription } from 'rxjs';
import { DatePipe } from '@angular/common';
import { DecimalPipe } from '@angular/common';
import { MessageService } from 'primeng/api';
import { ValuePipe } from '../../pipes/value.pipe';
import { NumberSuffixPipe } from '../../pipes/number-suffix.pipe';
import { Device } from '../../models/devices';
 
 
@Component({
    selector: 'app-signal-card',
    imports: [RouterLink, SignalCommandComponent, NgIf, DatePipe, DecimalPipe, ValuePipe, NumberSuffixPipe],
    providers: [DecimalPipe],
    templateUrl: './signal-card.component.html',
    styleUrl: './signal-card.component.scss'
})
export class SignalCardComponent implements OnChanges, OnDestroy{
  private messageService = inject(MessageService);
 
  @Input() signalId: string;
  signal: Signal | null = null;
  device: Device | null = null;
  lastSample: SignalSample | null ;
  deviceSubscription: Subscription;
  valueSubscription: Subscription;
  numberSamplesSubscription: Subscription;
  signalSubscription: Subscription;
  delay: number;
  sampleStats: SampleStats | null = null;
  dataType: string;
  enumOptions: string = "";
 
  constructor(private twinpadApiService:TwinpadApiService){}
 
  ngOnChanges(){
    this.signal = null;
    this.device = null;
    this.lastSample = null;
    this.sampleStats = null;
 
    this.signalSubscription?.unsubscribe();
    this.signalSubscription = this.twinpadApiService.getSignal(this.signalId).subscribe({next: (signal)=>{
      this.signal = signal;
      if (this.signal.isEnum()) {
        this.dataType = `enum[${this.signal.enumType()}]`;
        this.signal.stringEnumOptions().forEach(enumOption => this.enumOptions += `- ${enumOption}\n`);
      }
      else { this.dataType = this.signal.data_type; }
      this.deviceSubscription = this.twinpadApiService.devicesBehaviorSubject$.subscribe({
        next: devices => {
          // ensure that once the device is loaded, we unsub from it
          if (this.deviceSubscription!== undefined && this.device !== undefined) {
            this.deviceSubscription.unsubscribe();
          }
          const device = devices.find(device => device?.device_id === this.signal?.device_id);
          Eif (device !== undefined) {
            this.device = device;
          }
        }
      });
    }});
 
    this.valueSubscription?.unsubscribe();
    this.valueSubscription = this.twinpadApiService.callPeriodically(() => this.twinpadApiService.getSignalValue(this.signalId), 1000)
      .subscribe({
        next: (response) => {
          this.lastSample = response;
          this.delay = this.lastSample.delay();
        },
        error: () => {
          this.lastSample = null;
        },
      });
 
    this.numberSamplesSubscription?.unsubscribe();
    this.numberSamplesSubscription = this.twinpadApiService.callPeriodically(() => this.twinpadApiService.getSignalNumberSamples(this.signalId), 2000)
      .subscribe({
        next: (response) => {
          this.sampleStats = response;
        },
        error: () => {
          this.lastSample = null;
        },
      });
  }
 
  ngOnDestroy(): void {
    this.valueSubscription?.unsubscribe();
    this.numberSamplesSubscription?.unsubscribe();
  }
 
  unForce(){
    if (this.signal !== null){
      this.twinpadApiService.sendSignalUnforce(this.signal).subscribe({next: _ => {
        this.messageService.add({severity: 'success', summary:  'Unforce success', detail: this.signal?.signal_id+' reset to normal value ' });
      },
      error: error => {
        let severity: string = "error";
        if (error.status === 504) {
          severity = "warn";
        }
        this.messageService.add({severity: severity, summary:  'Command error', detail: error.error.detail });
      }});
    }
 
  }
 
  asNumber(value: number | string) {
    return Number.parseFloat(value.toString());
  }
}