sensor.component.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. import { Component, OnDestroy, OnInit, HostListener } from '@angular/core';
  2. import { ActivatedRoute } from '@angular/router';
  3. import * as moment from 'moment-timezone';
  4. import { GraphLoader } from '../../shared/graph-loading/graphloader';
  5. import { ObservationService } from '../../shared/api/endpoints/services/observation.service';
  6. import { HttpResponse } from '@angular/common/http';
  7. import { map } from 'rxjs/operators';
  8. import { ToastService } from '../../shared/services/toast.service';
  9. import { Sensor } from '../../shared/api/endpoints/models/sensor';
  10. import { SensorsService } from '../../shared/api/endpoints/services/sensors.service';
  11. import { Subscription } from 'rxjs';
  12. import type { View } from 'vega';
  13. @Component({
  14. selector: 'app-sensor',
  15. templateUrl: './sensor.component.html',
  16. styleUrls: ['./sensor.component.scss']
  17. })
  18. export class SensorComponent implements OnInit, OnDestroy {
  19. sensorId: number;
  20. unitId: number;
  21. data = [];
  22. time = [];
  23. from: Date = moment().hour(0).minutes(0).subtract(7, 'days').toDate();
  24. to: Date = moment().toDate();
  25. sensor: Sensor;
  26. dateChanged = false;
  27. unitDescription: string;
  28. today: Date = moment().toDate();
  29. subscription: Subscription[] = [];
  30. showIntervalError = false;
  31. private resizeObserver!: ResizeObserver; // Will watch the container size
  32. graphView;
  33. constructor(
  34. private activatedRoute: ActivatedRoute,
  35. private observationService: ObservationService,
  36. private toastService: ToastService,
  37. private sensorsService: SensorsService,
  38. private route: ActivatedRoute,
  39. ) {
  40. this.getInitData();
  41. this.sensorsService.getUnitSensors({ unit_id: this.unitId }).subscribe(sensors => {
  42. if (sensors) {
  43. this.sensor = sensors.filter(value => value.sensorId === this.sensorId)[0];
  44. this.showGraph(); // show default graph
  45. }
  46. }, err => this.toastService.showError(err.error.message));
  47. }
  48. ngAfterViewInit() {
  49. window.addEventListener('resize', () => console.log('raw'))
  50. }
  51. /*
  52. Fires on every resize event
  53. */
  54. @HostListener('window:resize', ['$event'])
  55. public onWindowResize(event: UIEvent): void {
  56. this.onResize();
  57. }
  58. /**
  59. * Sets up default data
  60. */
  61. getInitData() {
  62. this.route.queryParams.subscribe(params => {
  63. if (params.unitName) {
  64. this.unitDescription = params.unitName;
  65. }
  66. });
  67. this.sensorId = parseInt(this.activatedRoute.snapshot.paramMap.get('sensorId'), 10);
  68. this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
  69. }
  70. /**
  71. * Unsubscribe after leaving
  72. */
  73. ngOnDestroy(): void {
  74. this.subscription.forEach(subs => subs.unsubscribe());
  75. }
  76. ngOnInit(): void {
  77. }
  78. /**
  79. * Shows get data button
  80. */
  81. onDateChanged(): void {
  82. if (moment(this.to).diff(moment(this.from), 'months') > 6){
  83. this.dateChanged = false;
  84. this.showIntervalError = true;
  85. }
  86. else if (this.to < this.from) {
  87. this.dateChanged = false;
  88. this.showIntervalError = true;
  89. }
  90. else{
  91. this.dateChanged = true;
  92. this.showIntervalError = false;
  93. }
  94. }
  95. /**
  96. * Gets data based on selected time range
  97. */
  98. showGraph() {
  99. const range: Date[] = [this.from, this.to];
  100. this.getObservations(range);
  101. }
  102. /**
  103. * Get data from observation endpoint
  104. * @param range from and to interval
  105. */
  106. async getObservations(range: Date[]) {
  107. const box = document.getElementById('view');
  108. const boxWidth = box.getBoundingClientRect().width;
  109. const boxHeight = box.getBoundingClientRect().height;
  110. GraphLoader.setSize(boxWidth - 50, 300);
  111. this.observationService.getObservation$Response({
  112. unit_id: this.unitId,
  113. sensor_id: this.sensorId,
  114. from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
  115. to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
  116. }).pipe(
  117. map((response: HttpResponse<any>) => {
  118. if (response.status === 200) {
  119. return response.body;
  120. } else if (response.status === 204) {
  121. this.toastService.showWarningNoData();
  122. return response.body;
  123. } else {
  124. return false;
  125. }
  126. })
  127. ).subscribe(
  128. async observations => {
  129. if (observations) {
  130. this.graphView = await GraphLoader.getGraph(this.sensorId, observations, this.sensor, '#view', false);
  131. } else {
  132. this.graphView = await GraphLoader.getGraph(null, null, null, '#view', null);
  133. }
  134. }, err => this.toastService.showError(err.error.message));
  135. }
  136. private onResize(): void {
  137. // resize graph width if window changes size
  138. const box = document.getElementById('view');
  139. if (this.graphView) {
  140. const newWidth = box.getBoundingClientRect().width - 50;
  141. this.graphView.width(newWidth).height(300).runAsync();
  142. }
  143. }
  144. }