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

76.92% Statements 40/52
64.28% Branches 18/28
80% Functions 8/10
76.47% Lines 39/51

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                                              98x   8x     8x 8x                     8x       12x 12x 1x   12x 8x 8x     4x 4x     12x 12x             4x 4x 2x     4x 4x 1x   3x 2x     1x 1x       4x 3x     1x   4x   4x                         2x   2x   2x                         2x 2x 2x     2x     2x 2x                            
import { Component, inject, Input, OnChanges } from '@angular/core';
import { InputNumberModule } from 'primeng/inputnumber';
import { InputSwitchModule } from 'primeng/inputswitch';
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: [InputSwitchModule, 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;
  boolValue: boolean=false;
  floatValue: number = 0;
  dateValue: Date;
  enumOptions: string[];
  stringValue: string;
  placeholder: string;
  error: HttpErrorResponse;
 
  button_message: string;
 
  precisionNumber: string;
 
  constructor(private twinpadApiService: TwinpadApiService
  ){}
 
  ngOnChanges(){
    this.precisionNumber = "";
    if (this.signal.precision_digits){
      this.precisionNumber = this.signal.precision_digits.toString();
    }
    if(this.signal.type === "command"){
      this.button_message = "Send command";
      this.placeholder = "Value";
    }
    else{
      this.button_message = "Force 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 });
      }
    });
  }
 
  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;
  }
 
 
 
 
 
}