All files / src/app/components/pid pid.component.ts

72.72% Statements 48/66
54.16% Branches 13/24
60% Functions 9/15
70.49% Lines 43/61

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                                          36x                   1x     1x   1x 1x   1x             1x 1x   1x 1x 1x 1x 1x                                   1x                           1x 1x 1x 1x         1x 11x 11x 11x     1x 1x 1x 26x 11x 11x 11x         1x 11x 11x       2x 2x 2x 44x 44x 22x   44x 22x       2x   1x               1x      
import { Component, OnInit, ElementRef, ViewChild, OnDestroy, Input } from '@angular/core';
import { PID, Sensor } from '../../models/pid';
import { Component as PIDComponent } from '../../models/pid';
import { Canvas } from '../../models/canvas';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { Signal, SignalSample } from '../../models/signals';
import { Subscription } from 'rxjs';
import { ActivatedRoute } from '@angular/router';
import { EmptyPlaceholderComponent } from '../empty-placeholder/empty-placeholder.component';
 
import { DrawerModule } from 'primeng/drawer';
import { SignalCardComponent } from '../signal-card/signal-card.component';
import { NgIf } from '@angular/common';
 
@Component({
    selector: 'app-pid',
    imports: [DrawerModule, SignalCardComponent, NgIf, EmptyPlaceholderComponent],
    providers: [TwinpadApiService],
    templateUrl: './pid.component.html',
    styleUrl: './pid.component.css'
})
export class PidComponent implements OnInit, OnDestroy{
  @ViewChild('pidCanvas', { static: true }) pidCanvas!: ElementRef<HTMLCanvasElement>;
  private context!: CanvasRenderingContext2D;
  pid: PID;
  @Input() deviceId: string;
  canvas: Canvas;
  signalValues: SignalSample[];
  signalsSubscription: Subscription;
  device_id: string;
  commandSignal: Signal;
  commandLoading: boolean = true;
 
  statusSignal: Signal;
  statusLoading: boolean = true;
 
  displayToolbar: boolean = false;
  hideCanvas: boolean = false;
 
  constructor(private twinpadApiService: TwinpadApiService, private route: ActivatedRoute){}
 
  showToolbar(){
    this.displayToolbar = true;
  }
 
  ngOnInit(){
    this.device_id = this.route.snapshot.params['device_id'] || this.deviceId;
    this.twinpadApiService.getDevice(this.device_id).subscribe({
      next: data => {
        Eif (data['pid'] !== undefined && data['pid'] !== null){
          this.context = this.pidCanvas.nativeElement.getContext('2d')!;
          this.pid = PID.deserialize(data['pid']);
          this.canvas = new Canvas(this.context, this.pid);
          this.pid.selectedComponent.subscribe({
            next: (component) => {
              if (component !== null && component.commandSignalId !== undefined){
                this.twinpadApiService.getSignal(component.commandSignalId).subscribe({next: (value) => {
                  this.commandSignal = value;
                  this.commandLoading = false;
                }});
                this.showToolbar();
              }
              if (component !== null && component.statusSignalId !== undefined){
                this.twinpadApiService.getSignal(component.statusSignalId).subscribe({next: (value) => {
                  this.statusSignal = value;
                  this.statusLoading = false;
                }});
                this.showToolbar();
              }
            }
          });
          this.drawPid();
        }
        else {
          this.hideCanvas = true;
        }
      },
      error: _ => {
        this.pidCanvas.nativeElement.hidden = true;
        this.hideCanvas = true;
      }
    });
  }
 
  drawPid(){
    const signalIds: string[] = [];
    const indexToSensor: {[id: number] : Sensor} = {};
    let isensor = 0;
    for (const sensor of this.pid.sensors){
      signalIds.push(sensor.signal_id);
      indexToSensor[isensor] = sensor;
      isensor += 1;
    }
    for (const sensor of this.pid.tapSensors){
      signalIds.push(sensor.signal_id);
      indexToSensor[isensor] = sensor;
      isensor += 1;
    }
 
    let icomponent = isensor;
    const indexToComponent: {[id: number] : PIDComponent} = {};
    for (const component of this.pid.components){
      if (component.statusSignalId !== undefined){
        signalIds.push(component.statusSignalId);
        indexToComponent[icomponent] = component;
        icomponent += 1;
      }
    }
 
    // Get signals units
    for (let i=0; i<this.pid.sensors.length+this.pid.tapSensors.length; i++){
      this.twinpadApiService.getSignal(signalIds[i]).subscribe(
        signal => {indexToSensor[i].unit = signal.unit;}
      );
    }
 
    this.signalsSubscription = this.twinpadApiService.callPeriodically(() => this.twinpadApiService.getSignalValues(signalIds), 2000).subscribe(value => {
      this.signalValues = value;
      for (let i=0; i<signalIds.length; i++){
        Eif (this.signalValues[i] !== null){
          if (i in indexToSensor){
            indexToSensor[i].sample = this.signalValues[i];
          }
          if (i in indexToComponent){
            indexToComponent[i].statusSample = this.signalValues[i];
          }
        }
      }
      this.canvas.draw();
    });
    this.canvas.draw();
  }
 
  preventScroll(event: WheelEvent) {
    event.preventDefault();
  }
 
  ngOnDestroy(){
    this.signalsSubscription?.unsubscribe();
  }
}