sensor.component.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. showIntervalError: boolean = false;
  30. constructor(
  31. private activatedRoute: ActivatedRoute,
  32. private observationService: ObservationService,
  33. private toastService: ToastService,
  34. private sensorsService: SensorsService,
  35. private route: ActivatedRoute,
  36. ) {
  37. this.getInitData();
  38. this.sensorsService.getUnitSensors({ unit_id: this.unitId }).subscribe(sensors => {
  39. if (sensors) {
  40. this.sensor = sensors.filter(value => value.sensorId === this.sensorId)[0];
  41. this.showGraph(); // show default graph
  42. }
  43. }, err => this.toastService.showError(err.error.message));
  44. }
  45. /**
  46. * Sets up default data
  47. */
  48. getInitData() {
  49. this.route.queryParams.subscribe(params => {
  50. if (params.unitName) {
  51. this.unitDescription = params.unitName;
  52. }
  53. });
  54. this.sensorId = parseInt(this.activatedRoute.snapshot.paramMap.get('sensorId'), 10);
  55. this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
  56. }
  57. /**
  58. * Unsubscribe after leaving
  59. */
  60. ngOnDestroy(): void {
  61. this.subscription.forEach(subs => subs.unsubscribe());
  62. }
  63. ngOnInit(): void {
  64. }
  65. /**
  66. * Shows get data button
  67. */
  68. onDateChanged(): void {
  69. if (moment(this.to).diff(moment(this.from), 'months') > 6){
  70. this.dateChanged = false;
  71. this.showIntervalError = true;
  72. }
  73. else{
  74. this.dateChanged = true;
  75. this.showIntervalError = false;
  76. }
  77. }
  78. /**
  79. * Gets data based on selected time range
  80. */
  81. showGraph() {
  82. const range: Date[] = [this.from, this.to];
  83. this.getObservations(range);
  84. }
  85. /**
  86. * Get data from observation endpoint
  87. * @param range from and to interval
  88. */
  89. getObservations(range: Date[]) {
  90. this.observationService.getObservation$Response({
  91. unit_id: this.unitId,
  92. sensor_id: this.sensorId,
  93. from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
  94. to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
  95. }).pipe(
  96. map((response: HttpResponse<any>) => {
  97. if (response.status === 200) {
  98. return response.body;
  99. } else if (response.status === 204) {
  100. this.toastService.showWarningNoData();
  101. return response.body;
  102. } else {
  103. return false;
  104. }
  105. })
  106. ).subscribe(
  107. observations => {
  108. if (observations) {
  109. GraphLoader.getGraph(this.sensorId, observations, this.sensor, '#view', false);
  110. } else {
  111. GraphLoader.getGraph(null, null, null, '#view', null);
  112. }
  113. }, err => this.toastService.showError(err.error.message));
  114. }
  115. }