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

83.33% Statements 85/102
66.03% Branches 35/53
80.95% Functions 17/21
82.29% Lines 79/96

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                                                74x                     3x     3x     3x   3x 3x     3x     3x       2x 2x     4x 1x 1x   4x 3x 1x     2x         2x 2x 2x                         1x       1x 1x 1x 1x 1x       1x   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 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       1x                     2x      
import { Component, OnInit, ElementRef, ViewChild, OnDestroy, Input } 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 { 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;
 
  sensor: Sensor | undefined;
  sensorLoading: 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;
            }
          });
        }
      },
      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;
          }
          Iif (item.statusSignalId !== undefined && item.statusSignalId !== null) {
            this.twinpadApiService.getSignal(item.statusSignalId).subscribe({
              next: (value) => {
                this.statusSignal = value;
                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();
  }
 
  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();
  }
}