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

87.96% Statements 95/108
67.92% Branches 36/53
85.71% Functions 18/21
87.37% Lines 90/103

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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230                                                114x 5x 5x                       5x     5x     5x   5x 5x       3x       4x 4x     7x 1x 1x   10x 6x 2x     4x         4x 4x 4x                         2x       2x 2x 2x 2x 2x       2x   2x     2x   1x 1x 1x 1x 1x   1x 1x     1x           1x 1x   1x 1x               1x   1x   1x 1x 1x 1x   1x 1x 1x 1x 1x                       2x 2x 2x 2x 8x 8x 8x   2x 18x 18x 18x     2x 2x 2x 56x 22x 22x 22x         2x 26x 26x       3x 3x 3x 72x 72x 39x   72x 33x       3x   2x       1x 1x 1x       2x                     3x 109x    
import { Component, OnInit, ElementRef, ViewChild, OnDestroy, Input, inject } from '@angular/core';
import { PID, PIDInterface, 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 { HttpErrorResponse } from '@angular/common/http';
import { Device } from '../../models/devices';
import { ErrorComponent } from '../error/error.component';
 
@Component({
  selector: 'app-pid',
  imports: [DrawerModule, SignalCardComponent, EmptyPlaceholderComponent, ErrorComponent],
  providers: [],
  templateUrl: './pid.component.html',
  styleUrl: './pid.component.scss'
})
export class PidComponent implements OnInit, OnDestroy {
  private twinpadApiService = inject(TwinpadApiService);
  private route = inject(ActivatedRoute);
 
  @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;
 
  sensor: Sensor | undefined;
  sensorLoading: boolean = true;
 
  displayToolbar: boolean = false;
  hideCanvas: boolean = false;
  error: HttpErrorResponse;
 
  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;
            }
          });
        }
      },
      error: _ => {
        this.pidCanvas.nativeElement.hidden = true;
        this.hideCanvas = true;
      }
    });
  }
 
  setupPid(device: Device) {
    Iif (device.pid === undefined || device.pid === null || this.isPidEmpty(device.pid)) {
      this.hideCanvas = true;
      return;
    }
    this.context = this.pidCanvas.nativeElement.getContext('2d')!;
    this.pid = PID.deserialize(device['pid']);
    this.canvas = new Canvas(this.context, this.pid);
    this.handlePidClick();
    this.drawPid();
  }
 
  handlePidClick() {
    this.pid.selectedItem.subscribe({
      next: (item) => {
        Iif (item === null) {
          return;
        }
        if ((item as PIDComponent).commandSignalId !== undefined) {
          // hide sensor signal
          this.sensor = undefined;
          this.sensorLoading = false;
          item = item as PIDComponent;
          Eif (item.commandSignalId !== undefined && item.commandSignalId !== null) {
            this.twinpadApiService.getSignal(item.commandSignalId).subscribe({
              next: (value) => {
                this.commandSignal = value;
                this.commandLoading = false;
              }
            });
            this.showToolbar();
          }
          else {
            this.commandSignal = undefined;
            this.commandLoading = false;
          }
          Eif (item.statusSignalId !== undefined && item.statusSignalId !== null) {
            this.twinpadApiService.getSignal(item.statusSignalId).subscribe({
              next: (value) => {
                this.statusSignal = value;
                this.statusLoading = false;
              }
            });
          }
          else {
            this.statusSignal = undefined;
            this.statusLoading = false;
          }
          this.showToolbar();
        }
        else Eif ((item as Sensor).signal_id !== undefined) {
          // hide command and status signals
          this.commandSignal = undefined;
          this.commandLoading = false;
          this.statusSignal = undefined;
          this.statusLoading = false;
 
          item = item as Sensor;
          Eif (item.signal_id !== undefined && item.signal_id !== null) {
            this.sensor = item;
            this.sensorLoading = false;
            this.showToolbar();
          }
          else {
            this.sensor = undefined;
            this.sensorLoading = false;
          }
        }
      }
    });
  }
 
  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();
  }
 
  hideSignal() {
    this.commandSignal = undefined;
    this.statusSignal = undefined;
    this.sensor = undefined;
  }
 
  isPidEmpty(pid: PIDInterface): boolean {
    return (pid.components?.length ?? 0) === 0 &&
      (pid.pipes?.length ?? 0) === 0 &&
      (pid.sensors?.length ?? 0) === 0 &&
      (pid.tap_sensors?.length ?? 0) === 0;
  }
 
  preventScroll(event: WheelEvent) {
    event.preventDefault();
  }
 
  ngOnDestroy() {
    this.signalsSubscription?.unsubscribe();
  }
}