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

83.82% Statements 57/68
65.38% Branches 17/26
77.27% Functions 17/22
83.07% Lines 54/65

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 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196                                                                        114x 3x 3x 3x 3x             3x 3x     3x 3x 3x 3x 3x                 2x         2x 2x 2x         1x 1x     1x     2x 2x 2x 2x   7x 5x 3x 3x     2x           2x           2x 2x   2x       2x 2x 2x         2x                                                     4x       4x       6x 6x   6x         18x         5x     1x           3x 3x   3x 3x     3x 3x       3x 3x   3x 111x    
import { DatePipe, DecimalPipe } from '@angular/common';
import { HttpErrorResponse } from '@angular/common/http';
import { Component, inject, Input, OnDestroy, OnInit } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ActivatedRoute, Router } from '@angular/router';
 
import { ButtonModule } from 'primeng/button';
import { FloatLabel } from 'primeng/floatlabel';
import { IconFieldModule } from 'primeng/iconfield';
import { InputIconModule } from 'primeng/inputicon';
import { InputTextModule } from 'primeng/inputtext';
import { MultiSelectModule } from 'primeng/multiselect';
import { Table, TableLazyLoadEvent, TableModule, TablePageEvent } from 'primeng/table';
import { TabsModule } from 'primeng/tabs';
import { TagModule } from 'primeng/tag';
import { debounceTime, distinctUntilChanged, Subject, Subscription } from 'rxjs';
 
import { DeviceState } from '../../models/devices';
import { PetriNetworkInterface } from '../../models/petri';
import { ListResponse } from '../../models/response';
import { TwinpadApiService } from '../../services/twinpad-api.service';
import { getSeverity } from '../../utils/utils';
import { ErrorComponent } from '../error/error.component';
import { SignalsGraphsComponent } from '../signals-graphs/signals-graphs.component';
 
interface DeviceProperty {
  name: string;
  label: string;
}
 
@Component({
  selector: 'app-device-states',
  imports: [TableModule, ButtonModule, TagModule, IconFieldModule, InputIconModule, InputTextModule, FormsModule, DatePipe, DecimalPipe, FloatLabel, MultiSelectModule, ErrorComponent, TabsModule, SignalsGraphsComponent],
  templateUrl: './device-states.component.html',
  styleUrl: './device-states.component.scss'
})
export class StatesTableComponent implements OnInit, OnDestroy {
  private route = inject(ActivatedRoute);
  private router = inject(Router);
  private twinpadApiService = inject(TwinpadApiService);
  private filterSubject = new Subject<string>();
  private filterSubscription: Subscription;
 
  @Input() deviceId!: string;
  deviceName: string;
  petriNetwork: PetriNetworkInterface;
  versions: ListResponse<DeviceState>;
  offset: number = 0;
  rows: number = 10;
  deviceProperties: DeviceProperty[];
  selectedProperties: DeviceProperty[];
  sortField: string = "timestamp";
  sortOrder: number = -1;
  defaultRowOptions = [10, 20, 50];
  filteredRowsPerPageOptions: number[] = [];
  placesFilter: string | null = null;
  error: HttpErrorResponse;
  statesError: HttpErrorResponse;
 
  dataSubscription: Subscription;
 
  currentDate: number;
 
  ngOnInit(): void {
    this.deviceProperties = [
      { name: "mode", label: "Mode" },
      { name: "places", label: "Logic states" },
      { name: "load", label: "Load" },
    ];
    this.selectedProperties = this.deviceProperties;
    this.currentDate = Date.now();
    this.filterSubscription = this.filterSubject.pipe(
      debounceTime(700),
      distinctUntilChanged()
    ).subscribe(
      filter => {
        this.placesFilter = filter;
        Iif (this.placesFilter === "") {
          this.placesFilter = null;
        }
        this.getData();
      }
    );
    this.route.params.subscribe(params => {
      this.dataSubscription?.unsubscribe();
      this.deviceId = params['device_id'];
      this.twinpadApiService.devicesBehaviorSubject$.subscribe({
        next: devices => {
          const device = devices.find(device => device?.device_id === this.deviceId);
          if (device !== undefined) {
            this.deviceName = device.name;
            this.petriNetwork = device.petri_network;
          }
          else {
            this.twinpadApiService.getDevice(this.deviceId).subscribe({
              next: device => {
                this.deviceName = device.name;
                this.petriNetwork = device.petri_network;
              },
              error: error => {
                this.error = error;
              }
            });
          }
        }
      });
      this.rows = params['rows'] ? +params['rows'] : 10;
      this.offset = params['offset'] ? +params['offset'] : 0;
 
      this.router.navigate([], {
        queryParams: { signal_id: this.deviceId + "._STATUS" }
      });
    });
    this.updateRowsPerPageOptions();
    Eif (this.versions === undefined) {
      this.getData();
    }
  }
 
  ngOnDestroy(): void {
    this.filterSubscription.unsubscribe();
  }
 
  getSeverity(status: string) {
    return getSeverity(status);
  }
 
  clear(table: Table) {
    table.clear();
  }
 
  onTableEvent(event: TablePageEvent) {
    this.offset = event.first;
    this.rows = event.rows;
 
    this.router.navigate([], {
      queryParams: { offset: this.offset, rows: this.rows },
      queryParamsHandling: 'merge'
    });
  }
 
  onModifiedPropertiesChanged(newModifiedProperties: DeviceProperty[]) {
    this.selectedProperties = newModifiedProperties;
    this.getData();
  }
 
  updateRowsPerPageOptions() {
    this.filteredRowsPerPageOptions = [...new Set([...this.defaultRowOptions, this.rows])].sort((a, b) => a - b);
  }
 
  onFilterPlaces(filter: string) {
    this.filterSubject.next(filter);
  }
 
  getData() {
    this.dataSubscription?.unsubscribe();
    this.dataSubscription = this.twinpadApiService.callPeriodically(
      () => {
        return this.twinpadApiService.getDeviceStates(
          this.deviceId,
          this.rows,
          this.offset,
          this.sortField + ":" + this.sortOrder,
          this.selectedProperties.map(property => property.name),
          this.placesFilter
        );
      }, 5000).subscribe({
        next: (value) => {
          this.versions = value;
        },
        error: (error) => {
          this.statesError = error;
        }
      });
  }
 
  loadEvents(event: TableLazyLoadEvent) {
    Eif (event.first !== null && event.first !== undefined) {
      this.offset = event.first;
    }
    Eif (event.rows !== null && event.rows !== undefined) {
      this.rows = event.rows;
    }
 
    let sortField = event.sortField ?? "";
    Iif (typeof sortField === "object") {
      sortField = sortField[0];
    }
 
    this.sortField = sortField;
    this.sortOrder = event.sortOrder ?? 1;
 
    this.getData();
  }
}