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

73.43% Statements 47/64
59.37% Branches 19/32
76.92% Functions 10/13
73.01% Lines 46/63

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                                              100x   9x     9x 9x 9x                   9x   9x       12x 12x 7x 7x 7x     5x 5x 5x     12x 12x         4x 4x 2x     4x 4x 1x   3x 2x     1x 1x       4x 3x     1x   4x   4x                         3x   3x   3x                         1x 1x 1x   1x 1x                             3x 3x 3x     3x     3x 3x                  
import { Component, inject, Input, OnChanges } 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 { NgIf } from '@angular/common';
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';
 
@Component({
    selector: 'app-signal-command',
    imports: [ToggleSwitchModule, InputNumberModule, FormsModule, NgIf, FloatLabelModule, ToastModule, Select, InputGroupModule, InputGroupAddonModule, DatePicker],
    providers: [],
    templateUrl: './signal-command.component.html',
    styleUrl: './signal-command.component.scss'
})
export class SignalCommandComponent implements OnChanges{
 
  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;
 
  forceButtonMessage: string;
  unForceButtonMessage: string;
  error: HttpErrorResponse;
  commandPending: boolean = false;
 
  constructor(private twinpadApiService: TwinpadApiService
  ){}
 
  ngOnChanges(){
    this.precisionNumber = this.signal.precision_digits ?? 12;
    if(this.signal.type === "command"){
      this.forceButtonMessage = "Send command";
      this.unForceButtonMessage = "Unforce command";
      this.placeholder = "Value";
    }
    else{
      this.forceButtonMessage = "Force sensor";
      this.unForceButtonMessage = "Unforce sensor";
      this.placeholder = "Forced value";
    }
 
    this.enumOptions = this.signal.stringEnumOptions();
    this.stringValue = this.enumOptions[0];
  }
 
  sendCommand(){
    let value: string | number | boolean;
    let signalDataType = this.signal.data_type;
    if (this.signal.isEnum()){
      signalDataType = this.signal.enumType();
    }
 
    value = this.floatValue;
    if (signalDataType === "bool"){
      value = this.boolValue;
    }
    else if (signalDataType === "str"){
      value = this.stringValue;
    }
 
    else Eif (signalDataType === "epoch"){
      value = this.dateValue.getTime() / 1000;
    }
 
    let request;
    if (this.signal.type === "command"){
      request = this.twinpadApiService.sendSignalCommand(this.signal, value, this.forcedValue);
    }
    else{
      request = this.twinpadApiService.sendSignalForcedValue(this.signal, value);
    }
    request.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 });
      }
    });
  }
 
  forceCommand(){
    const value = this.getSignalType();
 
    this.twinpadApiService.sendSignalForcedValue(this.signal, value).subscribe({
      next: response => {
          this.messageService.add({severity: 'success', summary:  'Command force success', detail: response.message });
      },
      error: error => {
        let severity: string = "error";
        if (error.status === 504) {
          severity = "warn";
        }
        this.messageService.add({severity: severity, summary:  'Command force error', detail: error.error.detail });
      }
    });
  }
 
  unForce(){
    Eif (this.signal !== null){
      this.commandPending = true;
      this.twinpadApiService.sendSignalUnforce(this.signal).subscribe({
        next: _ => {
          this.commandPending = false;
          this.messageService.add({severity: 'success', summary:  'Unforce success', detail: this.signal?.signal_id+' 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:  'Command error', detail: error.message });
        }
      });
    }
  }
 
  getSignalType(){
    let signalDataType = this.signal.data_type;
    Eif (this.signal.isEnum()){
      signalDataType = this.signal.enumType();
    }
 
    Iif (signalDataType === "bool"){
      return this.boolValue;
    }
    else Eif (signalDataType === "str"){
      return this.stringValue;
    }
 
    else if (signalDataType === "epoch"){
      return this.dateValue.getTime() / 1000;
    }
    return this.floatValue;
  }
}