sensor.component.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import { Component, OnDestroy, OnInit } 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. @Component({
  13. selector: 'app-sensor',
  14. templateUrl: './sensor.component.html',
  15. styleUrls: ['./sensor.component.scss']
  16. })
  17. export class SensorComponent implements OnInit, OnDestroy {
  18. sensorId: number;
  19. unitId: number;
  20. data = [];
  21. time = [];
  22. from: Date = moment().hour(0).minutes(0).subtract(7, 'days').toDate();
  23. to: Date = moment().toDate();
  24. sensor: Sensor;
  25. dateChanged = false;
  26. unitDescription: string;
  27. today: Date = moment().toDate();
  28. subscription: Subscription[] = [];
  29. constructor(
  30. private activatedRoute: ActivatedRoute,
  31. private observationService: ObservationService,
  32. private toastService: ToastService,
  33. private sensorsService: SensorsService,
  34. private route: ActivatedRoute,
  35. ) {
  36. this.getInitData();
  37. this.sensorsService.getUnitSensors({ unit_id: this.unitId }).subscribe(sensors => {
  38. if (sensors) {
  39. this.sensor = sensors.filter(value => value.sensorId === this.sensorId)[0];
  40. this.showGraph(); // show default graph
  41. }
  42. }, err => this.toastService.showError(err.error.message));
  43. }
  44. /**
  45. * Sets up default data
  46. */
  47. getInitData() {
  48. this.route.queryParams.subscribe(params => {
  49. if (params.unitName) {
  50. this.unitDescription = params.unitName;
  51. }
  52. });
  53. this.sensorId = parseInt(this.activatedRoute.snapshot.paramMap.get('sensorId'), 10);
  54. this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
  55. }
  56. /**
  57. * Unsubscribe after leaving
  58. */
  59. ngOnDestroy(): void {
  60. this.subscription.forEach(subs => subs.unsubscribe());
  61. }
  62. ngOnInit(): void {
  63. }
  64. /**
  65. * Shows get data button
  66. */
  67. onDateChanged(): void {
  68. this.dateChanged = true;
  69. }
  70. /**
  71. * Gets data based on selected time range
  72. */
  73. showGraph() {
  74. const range: Date[] = [this.from, this.to];
  75. this.getObservations(range);
  76. }
  77. /**
  78. * Get data from observation endpoint
  79. * @param range from and to interval
  80. */
  81. getObservations(range: Date[]) {
  82. this.observationService.getObservation$Response({
  83. unit_id: this.unitId,
  84. sensor_id: this.sensorId,
  85. from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
  86. to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
  87. }).pipe(
  88. map((response: HttpResponse<any>) => {
  89. if (response.status === 200) {
  90. return response.body;
  91. } else if (response.status === 204) {
  92. this.toastService.showWarningNoData();
  93. return response.body;
  94. } else {
  95. return false;
  96. }
  97. })
  98. ).subscribe(
  99. observations => {
  100. if (observations) {
  101. GraphLoader.getGraph(this.sensorId, observations, this.sensor, '#view', false);
  102. } else {
  103. GraphLoader.getGraph(null, null, null, '#view', null);
  104. }
  105. }, err => this.toastService.showError(err.error.message));
  106. }
  107. }