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

81.81% Statements 45/55
66.66% Branches 12/18
73.68% Functions 14/19
80% Lines 40/50

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                                                122x 12x   12x     12x 12x             12x   12x   12x     12x 12x 12x 12x   12x   12x       12x 12x 12x 12x 6x 14x   6x 12x     20x 8x   47x 20x 18x           12x   12x 18x     18x 18x                     12x 27x     27x 27x                 4x 4x                                       398x 110x    
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 { 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, DatePipe, DecimalPipe, ValuePipe, NumberSuffixPipe],
    providers: [DecimalPipe],
    templateUrl: './signal-card.component.html',
    styleUrl: './signal-card.component.scss'
})
export class SignalCardComponent implements OnChanges, OnDestroy{
  private twinpadApiService = inject(TwinpadApiService);
 
  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 = "";
 
  canControlSignal: boolean = false;
 
  ngOnChanges(){
    this.signal = null;
    this.device = null;
    this.lastSample = null;
    this.sampleStats = null;
 
    this.twinpadApiService.checkIfCanControlSignal(this.signalId).subscribe({
      next: canControlSignal => {
        this.canControlSignal = canControlSignal;
      }
    });
 
    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);
          if (device !== undefined) {
            this.device = device;
          }
        }
      });
    }});
 
    this.createValuesSubcription();
 
    this.numberSamplesSubscription?.unsubscribe();
    this.numberSamplesSubscription = this.twinpadApiService.callPeriodically(() => this.twinpadApiService.getSignalNumberSamples(this.signalId), 2000)
      .subscribe({
        next: (response) => {
          this.sampleStats = response;
          Iif (this.valueSubscription.closed && this.sampleStats.number_samples > 0) {
            this.createValuesSubcription();
          }
        },
        error: () => {
          this.lastSample = null;
        },
      });
  }
 
  createValuesSubcription() {
    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;
        },
      });
  }
 
  ngOnDestroy(): void {
    this.valueSubscription?.unsubscribe();
    this.numberSamplesSubscription?.unsubscribe();
  }
 
  unForce(){
    if (this.signal !== null){
      this.twinpadApiService.sendSignalUnforce(this.signal.signal_id).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());
  }
}