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

71.21% Statements 47/66
60.71% Branches 17/28
73.68% Functions 14/19
70.76% Lines 46/65

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                                                125x 15x     15x     15x 15x 15x                         15x 15x     19x 19x 14x 14x     5x 5x     19x   24x         22x 22x 20x     2x         19x 19x       6x       3x   3x   3x                         1x   1x   1x                         5x   5x   5x                         2x 2x   2x 2x                           9x 9x 7x     9x 1x   8x 7x   1x 1x             110x    
import { Component, inject, Input, OnChanges, OnDestroy } from '@angular/core';
import { InputNumberModule } from 'primeng/inputnumber';
import { ToggleSwitchModule } from 'primeng/toggleswitch';
import { Signal } from '../../models/signals';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { FormsModule } from '@angular/forms';
 
import { FloatLabelModule } from 'primeng/floatlabel';
import { ToastModule } from 'primeng/toast';
import { MessageService } from 'primeng/api';
import { Select } from 'primeng/select';
import { InputGroupModule } from 'primeng/inputgroup';
import { InputGroupAddonModule } from 'primeng/inputgroupaddon';
import { DatePicker } from 'primeng/datepicker';
import { HttpErrorResponse } from '@angular/common/http';
import { Subscription } from 'rxjs';
 
@Component({
  selector: 'app-signal-command',
  imports: [ToggleSwitchModule, InputNumberModule, FormsModule, FloatLabelModule, ToastModule, Select, InputGroupModule, InputGroupAddonModule, DatePicker],
  providers: [],
  templateUrl: './signal-command.component.html',
  styleUrl: './signal-command.component.scss'
})
export class SignalCommandComponent implements OnChanges, OnDestroy {
  private twinpadApiService = inject(TwinpadApiService);
 
 
  private messageService = inject(MessageService);
  @Input() signal: Signal;
  @Input() forcedValue: string | number | null;
  @Input() canUnForce: boolean = false;
  boolValue: boolean = false;
  floatValue: number = 0;
  dateValue: Date;
  enumOptions: string[];
  stringValue: string;
  placeholder: string;
  precisionNumber: number;
 
  unForceButtonMessage: string;
  buttonsTooltip: string;
 
  forcingStatusSubscription: Subscription;
 
  error: HttpErrorResponse;
  commandPending: boolean = false;
  canControlSignal: boolean = false;
 
  ngOnChanges() {
    this.precisionNumber = this.signal.precision_digits ?? 12;
    if (this.signal.type === "command") {
      this.unForceButtonMessage = "Unforce command";
      this.placeholder = "Value";
    }
    else {
      this.unForceButtonMessage = "Unforce sensor";
      this.placeholder = "Forced value";
    }
 
    this.forcingStatusSubscription = this.twinpadApiService.callPeriodically(
      () => {
        return this.twinpadApiService.checkIfCanControlSignal(this.signal.signal_id);
      },
      2000,
      0).subscribe({
      next: canControlSignal => {
        this.canControlSignal = canControlSignal;
        if (canControlSignal) {
          this.buttonsTooltip = "";
        }
        else {
          this.buttonsTooltip = "Cannot override other user's forcing";
        }
      }
    });
 
    this.enumOptions = this.signal.stringEnumOptions();
    this.stringValue = this.enumOptions[0];
  }
 
  ngOnDestroy(): void {
    this.forcingStatusSubscription.unsubscribe();
  }
 
  sendCommand() {
    const commandValue = this.getEntryValue();
 
    this.twinpadApiService.sendSignalCommand(this.signal, commandValue, this.forcedValue).subscribe({
      next: response => {
        this.messageService.add({ severity: 'success', summary: 'Command success', detail: response.message });
      },
      error: error => {
        let severity: string = "error";
        if (error.status === 504) {
          severity = "warn";
        }
        this.messageService.add({ severity: severity, summary: 'Command error', detail: error.error.detail });
      }
    });
  }
 
  forceSensor() {
    const forcedValue = this.getEntryValue();
 
    this.twinpadApiService.sendSignalForcedValue(this.signal, forcedValue).subscribe({
      next: response => {
        this.messageService.add({ severity: 'success', summary: 'Forcing successful', detail: response.message });
      },
      error: error => {
        let severity: string = "error";
        if (error.status === 504) {
          severity = "warn";
        }
        this.messageService.add({ severity: severity, summary: 'Forcing error', detail: error.error.detail });
      }
    });
  }
 
  forceCommand() {
    const forcedCommandValue = this.getEntryValue();
 
    this.twinpadApiService.sendSignalForcedValue(this.signal, forcedCommandValue).subscribe({
      next: response => {
        this.messageService.add({ severity: 'success', summary: 'Command forcing successful', detail: response.message });
      },
      error: error => {
        let severity: string = "error";
        if (error.status === 504) {
          severity = "warn";
        }
        this.messageService.add({ severity: severity, summary: 'Command forcing error', detail: error.error.detail });
      }
    });
  }
 
  unForce() {
    this.commandPending = true;
    this.twinpadApiService.sendSignalUnforce(this.signal?.signal_id ?? "").subscribe({
      next: _ => {
        this.commandPending = false;
        this.messageService.add({ severity: 'success', summary: 'Unforcing successful', detail: (this.signal?.signal_id ?? "Unknown signal") + ' reset to normal value' });
      },
      error: error => {
        this.commandPending = false;
        let severity: string = "error";
        if (error.status === 504) {
          severity = "warn";
        }
        this.messageService.add({ severity: severity, summary: 'Unforcing error', detail: error.message });
      }
    });
  }
 
  getEntryValue(): string | number | boolean {
    let signalDataType = this.signal.data_type;
    if (this.signal.isEnum()) {
      signalDataType = this.signal.enumType();
    }
 
    if (signalDataType === "bool") {
      return this.boolValue;
    }
    else if (signalDataType === "str") {
      return this.stringValue;
    }
    else Eif (signalDataType === "epoch") {
      return this.dateValue.getTime() / 1000;
    }
    return this.floatValue;
  }
 
  setDatePickerToNow() {
    this.dateValue = new Date(Date.now());
  }
}