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

73.52% Statements 25/34
50% Branches 2/4
60% Functions 9/15
70% Lines 21/30

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                                                40x 5x     5x 5x           5x   5x     5x 5x 5x 5x   5x 5x 5x 5x     5x 12x     12x 12x             5x 7x     7x                                                      
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.css'
})
export class SignalCardComponent implements OnChanges, OnDestroy{
  private messageService = inject(MessageService);
 
  @Input() signalId: string;
  signal: Signal | null = null;
  device: Device | null = null;
  lastSample: SignalSample | null ;
  valueSubscription: Subscription;
  numberSamplesSubscription: Subscription;
  signalSubscription: Subscription;
  delay: number;
  sampleStats: SampleStats | null = null;
 
  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;
      this.twinpadApiService.getDevice(this.signal.device_id).subscribe({next: device => 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 => {
        console.log(error);
        this.messageService.add({severity: 'error', summary:  'Command error', detail: error.message });
      }});
    }
 
  }
 
}