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

72.28% Statements 60/83
59.37% Branches 19/32
63.15% Functions 12/19
70.12% Lines 54/77

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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                                                62x                     3x     3x   3x 3x     3x             2x 2x     4x 1x 1x   4x 3x 1x     2x         2x     2x 2x 2x                     1x 1x 1x 1x 1x                                                   1x               1x 1x 1x 1x         1x 11x 11x 11x     1x 1x 1x 27x 11x 11x 11x         1x 11x 11x       2x 2x 2x 44x 44x 22x   44x 22x       2x   1x               2x      
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';
import { HttpErrorResponse } from '@angular/common/http';
import { Device } from '../../models/devices';
import { ErrorComponent } from '../error/error.component';
 
@Component({
    selector: 'app-pid',
    imports: [DrawerModule, SignalCardComponent, NgIf, EmptyPlaceholderComponent, ErrorComponent],
    providers: [],
    templateUrl: './pid.component.html',
    styleUrl: './pid.component.scss'
})
export class PidComponent implements OnInit, OnDestroy{
  @ViewChild('pidCanvas', { static: true }) pidCanvas!: ElementRef<HTMLCanvasElement>;
  private context!: CanvasRenderingContext2D;
  pid: PID;
  @Input() deviceId: string;
  deviceSubscription: Subscription;
  canvas: Canvas;
  signalValues: SignalSample[];
  signalsSubscription: Subscription;
  device_id: string;
  commandSignal: Signal | undefined;
  commandLoading: boolean = true;
 
  statusSignal: Signal | undefined;
  statusLoading: boolean = true;
 
  displayToolbar: boolean = false;
  hideCanvas: boolean = false;
  error: HttpErrorResponse;
 
  constructor(private twinpadApiService: TwinpadApiService, private route: ActivatedRoute){}
 
  showToolbar(){
    this.displayToolbar = true;
  }
 
  ngOnInit(){
    this.device_id = this.route.snapshot.params['device_id'] || this.deviceId;
    this.deviceSubscription = this.twinpadApiService.devicesBehaviorSubject$.subscribe({
      next: devices => {
        // If device subscription is not undefined and pid is not undefined, no need to refresh device again, so unsub
        if (this.deviceSubscription !== undefined && this.pid !== undefined) {
          this.deviceSubscription.unsubscribe();
          return;
        }
        const device = devices.find(device => device.device_id === this.device_id);
        if (device !== undefined) {
          this.setupPid(device);
        }
        else {
          this.twinpadApiService.getDevice(this.device_id).subscribe({
            next: device => {
              this.setupPid(device);
            },
            error: error => {
              this.error = error;
            }
          });
          this.pidCanvas.nativeElement.hidden = true;
          this.hideCanvas = true;
          return;
        }
      },
      error: _ => {
        this.pidCanvas.nativeElement.hidden = true;
        this.hideCanvas = true;
      }
    });
  }
 
  setupPid(device: Device) {
    Eif (device['pid'] !== undefined && device['pid'] !== null){
      this.context = this.pidCanvas.nativeElement.getContext('2d')!;
      this.pid = PID.deserialize(device['pid']);
      this.canvas = new Canvas(this.context, this.pid);
      this.pid.selectedComponent.subscribe({
        next: (component) => {
          if (component !== null && component.commandSignalId !== undefined && component.commandSignalId !== null){
            this.twinpadApiService.getSignal(component.commandSignalId).subscribe({next: (value) => {
              this.commandSignal = value;
              this.commandLoading = false;
            }});
            this.showToolbar();
          }
          else {
            this.commandSignal = undefined;
            this.commandLoading = false;
          }
          if (component !== null && component.statusSignalId !== undefined && component.statusSignalId !== null){
            this.twinpadApiService.getSignal(component.statusSignalId).subscribe({next: (value) => {
              this.statusSignal = value;
              this.statusLoading = false;
            }});
            this.showToolbar();
          }
          else {
            this.statusSignal = undefined;
            this.statusLoading = false;
          }
        }
      });
      this.drawPid();
    }
    else {
      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();
  }
}