All files / src/app/components/device-from-deployer device-from-deployer.component.ts

66.66% Statements 26/39
66.66% Branches 4/6
59.09% Functions 13/22
65.78% Lines 25/38

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                                        112x         2x   2x 2x 2x     2x 3x 3x 3x 2x 2x   2x         1x 1x 1x           1x   1x                 1x   1x                                       1x                       1x   1x 1x                     1x 110x      
import { Component, inject, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute, Router, RouterLink } from '@angular/router';
import { Subscription } from 'rxjs';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { DeviceFromDeployer } from '../../models/devices';
import { NgClass } from '@angular/common';
import { LoaderComponent } from '../loader/loader.component';
import { MessageService } from 'primeng/api';
import { FormsModule } from '@angular/forms';
import { FloatLabel } from 'primeng/floatlabel';
import { InputTextModule } from 'primeng/inputtext';
import { HttpErrorResponse } from '@angular/common/http';
import { ErrorComponent } from '../error/error.component';
 
@Component({
  selector: 'app-device-from-deployer',
  imports: [LoaderComponent, RouterLink, NgClass, FormsModule, FloatLabel, InputTextModule, ErrorComponent],
  templateUrl: './device-from-deployer.component.html',
  styleUrl: './device-from-deployer.component.scss'
})
export class DeviceFromDeployerComponent implements OnInit, OnDestroy {
  deviceDeployerId: string;
  deviceId: string;
  device: DeviceFromDeployer;
  deviceError: HttpErrorResponse;
  private messageService = inject(MessageService);
  deviceSubscription: Subscription;
  twinpadApiService = inject(TwinpadApiService);
  route = inject(ActivatedRoute);
  router = inject(Router);
 
  ngOnInit() {
    this.route.params.subscribe(params => {
      this.deviceDeployerId = params['device_deployer_id'];
      this.deviceId = params['device_id'];
      if (this.deviceId !== "new"){
        this.deviceSubscription = this.twinpadApiService.callPeriodically(
          () => this.twinpadApiService.getDeviceFromDeployer(this.deviceDeployerId, this.deviceId), 5000)
          .subscribe({
            next: device => this.device = device,
            error: error => this.deviceError = error
          });
      }
      else{
        this.device = new DeviceFromDeployer();
        this.device.name = "";
        this.device.description = "";
      }
    });
  }
 
  startDevice(device_id: string) {
    this.twinpadApiService.startDevice(this.deviceDeployerId, device_id).subscribe({
      "next": device => {
        this.messageService.add({ severity: 'success', summary: 'Start device success', detail: device.device_id });
      },
      "error": error => {
        this.messageService.add({ severity: 'error', summary: 'Start device failed', detail: error.error });
      }
    });
  }
 
  stopDevice(device_id: string) {
    this.twinpadApiService.stopDevice(this.deviceDeployerId, device_id).subscribe({
      "next": device => {
        this.messageService.add({ severity: 'success', summary: 'Stop device success', detail: device.device_id });
      },
      "error": error => {
        this.messageService.add({ severity: 'error', summary: 'Stop device failed', detail: error.error });
      }
    });
  }
 
  restartDevice(device_id: string) {
    this.twinpadApiService.restartDevice(this.deviceDeployerId, device_id).subscribe({
      "next": device => {
        this.messageService.add({ severity: 'success', summary: 'Restart device success', detail: device.device_id });
      },
      "error": error => {
        this.messageService.add({ severity: 'error', summary: 'Restart device failed', detail: error.error });
      }
    });
  }
 
  deleteDevice(device_id: string) {
    this.twinpadApiService.deleteDevice(this.deviceDeployerId, device_id).subscribe({
      "next": device => {
        this.messageService.add({ severity: 'info', summary: 'Device is deleted', detail: device.device_id });
        this.router.navigate(["/device-deployers", this.deviceDeployerId]);
      },
      "error": error => {
        this.messageService.add({ severity: 'error', summary: 'Delete device failed', detail: error.error });
      }
    });
  }
 
  addDevice(){
    this.twinpadApiService.addDeviceFromDeployer(this.deviceDeployerId, this.device).subscribe(
      {next: device=>{
        this.messageService.add({ severity: 'success', summary: 'Device created' });
        this.router.navigate(["/device-deployers/", this.deviceDeployerId, "devices", device.device_id]);},
       error: error => {
        let detail = "";
        if (error.status == 400){
          detail = "Invalid name";
        }
        this.messageService.add({severity: 'error', summary:  'Cannot create device', detail: detail });}
      });
  }
 
  ngOnDestroy(){
    this.deviceSubscription?.unsubscribe();
  }
 
}