All files / src/app app.component.ts

79.16% Statements 38/48
66.66% Branches 12/18
73.68% Functions 14/19
76.19% Lines 32/42

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                                                      70x 35x       35x         35x   35x   35x 35x   35x 1269x   111x 111x 98x     13x                   35x   35x                       35x   126x 115x 115x     11x 11x                   115x 115x   95x                   115x 115x     90x 90x 90x                         90x 90x     90x                  
import { Component, OnDestroy, OnInit } from '@angular/core';
import { RouterOutlet, ActivatedRoute, Router, NavigationEnd } from '@angular/router';
import { filter, Subscription } from 'rxjs';
import { NgClass, NgIf } from '@angular/common';
 
 
import { HeaderComponent } from './components/header/header.component';
import { LeftbarComponent } from './components/leftbar/leftbar.component';
import { FooterComponent } from './components/footer/footer.component';
import { ToastModule } from 'primeng/toast';
import { Device } from './models/devices';
import { TwinPadStatus } from './models/status';
import { HttpErrorResponse } from '@angular/common/http';
import { GlobalErrorComponent } from './components/global-error/global-error.component';
import { TwinpadApiService } from './services/twinpad-api.service';
import { Button } from 'primeng/button';
import { LoginService } from './services/login.service';
import { ImagePreloaderService } from './services/resolvers/images.service';
 
 
@Component({
  selector: 'app-root',
  imports: [RouterOutlet, NgIf, HeaderComponent, LeftbarComponent, FooterComponent, ToastModule, GlobalErrorComponent, NgClass, Button],
  providers: [],
  templateUrl: './app.component.html',
  styleUrl: './app.component.css'
})
export class AppComponent implements OnInit, OnDestroy{
  title = 'Twinpad';
  display_bars: boolean;
 
  status: TwinPadStatus;
  cloudStatus: boolean = true;
  devices: Device[];
  cloudStatusSubscription: Subscription;
  devicesSubscription: Subscription;
  error: HttpErrorResponse;
  statusRefreshTime: number = 10;
  nextCloudRequest: number;
  isCollapsed: boolean = false;
 
  constructor(private route: ActivatedRoute, private router: Router, private twinpadApiService: TwinpadApiService, private loginService: LoginService,
    private imagePreloaderService: ImagePreloaderService
  ) {
    router.events
      .pipe(filter(event => event instanceof NavigationEnd))
      .subscribe((_) => {
        const data = route.snapshot.firstChild!.data;
        if ('bars' in data && data['bars']) {
          this.display_bars = true;
        }
        else {
          this.display_bars = false;
        }
      });
  }
 
  toggleSidebar() {
    this.isCollapsed = !this.isCollapsed;
  }
 
  ngOnInit(){
    this.loggedInUserSubscribe();
 
    this.imagePreloaderService.preloadImages([
      '/assets/logo.png',
      '/assets/backgrounds/background4.jpg',
      '/assets/svg/alien-invasion.svg',
      '/assets/svg/towing.svg',
      '/assets/svg/server-down.svg',
      '/assets/svg/bug.svg'
    ]);
 
  }
 
  loggedInUserSubscribe(){
    this.loginService.loggedInUserReplaySubject.subscribe({
      next: (value) => {
        if(value){
          this.statusSubscribe();
          this.deviceSubscribe();
        }
        else {
          this.cloudStatusSubscription?.unsubscribe();
          this.devicesSubscription?.unsubscribe();
        }
      },
      error: (err) => {
        console.log(err);
      }
    });
  }
 
  deviceSubscribe(initialDelay?:number){
    this.devicesSubscription?.unsubscribe();
    this.devicesSubscription = this.twinpadApiService.callPeriodically(() => this.twinpadApiService.getDevices(), 3000, initialDelay)
    .subscribe({
      next: (devices: Device[]) => {this.devices = devices;},
      error: (_) => {
        if(this.isTokenAvailable()) {
          this.deviceSubscribe(2000);
        }
      }
    });
  }
 
  statusSubscribe(initialDelay?:number){
    this.cloudStatusSubscription?.unsubscribe();
    this.cloudStatusSubscription = this.twinpadApiService.callPeriodically(() => this.twinpadApiService.getStatus(), 1000*this.statusRefreshTime, initialDelay)
    .subscribe({
      next: (value: TwinPadStatus) => {
        this.status = value;
        this.cloudStatus = this.status.services.backend == "up";
        Iif(this.isTokenAvailable() && !this.cloudStatus){
          this.nextCloudRequest =  Date.now() + 1000*this.statusRefreshTime;
        }
      },
      error: (_) => {
        if(this.isTokenAvailable()){
          this.statusSubscribe(2000);
        }
      }
    });
  }
 
  isTokenAvailable(){
    const tokenExpired = this.loginService.isTokenValid();
    Iif(!tokenExpired) {
      window.location.reload();
    }
    return true;
  }
 
  ngOnDestroy(): void {
    this.cloudStatusSubscription?.unsubscribe();
    this.devicesSubscription?.unsubscribe();
  }
 
}