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

90.47% Statements 19/21
100% Branches 2/2
90% Functions 9/10
90% Lines 18/20

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                                            40x 5x     5x   5x       5x 5x       4x 4x 4x 4x 3x 3x     1x             1x 1x 1x                     4x 4x        
import { Component, inject, OnDestroy, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { Signal } from '../../models/signals';
import { TableModule } from 'primeng/table';
import { ToastModule } from 'primeng/toast';
import { MessageService } from 'primeng/api';
import { NgIf, NgClass } from '@angular/common';
import { Subscription } from 'rxjs';
import { SignalsTableComponent } from '../signals-table/signals-table.component';
import { Device, Mode } from '../../models/devices';
import { DeviceCardComponent } from '../device-card/device-card.component';
import { HttpErrorResponse } from '@angular/common/module.d-CnjH8Dlt';
import { ErrorComponent } from '../error/error.component';
 
@Component({
    selector: 'app-device',
    imports: [TableModule, ToastModule, NgIf, NgClass, SignalsTableComponent, DeviceCardComponent, ErrorComponent],
    providers: [],
    templateUrl: './device.component.html',
    styleUrl: './device.component.css'
})
export class DeviceComponent implements OnInit, OnDestroy {
  private messageService = inject(MessageService);
  deviceId: string;
  device: Device;
  private deviceSubscription: Subscription = new Subscription;
  private routeSub: Subscription;
  isLoading: boolean = false;
  signals: Signal[];
  error: HttpErrorResponse;
 
  constructor(private twinpadApiService: TwinpadApiService,
    private route: ActivatedRoute
    ) {}
 
  ngOnInit() {
    this.routeSub = this.route.params.subscribe(params => {
      this.deviceId = params['device_id'];
      this.deviceSubscription?.unsubscribe();
      this.deviceSubscription = this.twinpadApiService.getDevice(this.deviceId).subscribe({'next': data => {
        this.device = data;
        this.isLoading = false;
      },
    'error': error=>{
      this.error = error;
    }});
    });
  }
 
 
  changeMode(newMode:Mode){
    this.twinpadApiService.changeDeviceMode(this.device, newMode).subscribe(_ => {
      this.isLoading = true;
      this.messageService.add({severity: 'success', summary:  'Mode switch sent', detail: this.device.name+' should switch to '+newMode.name+' mode' });
    },
    error => {
      console.log(error);
      this.messageService.add({severity: 'error', summary:  'Mode switch error', detail: error.message });
    }
 
    );
  }
 
  ngOnDestroy(): void {
    this.deviceSubscription?.unsubscribe();
    this.routeSub?.unsubscribe();
  }
 
}