Projekt

Obecné

Profil

Stáhnout (3.82 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {Component, OnInit} from '@angular/core';
2
import {ActivatedRoute} from '@angular/router';
3
import {AnalyticsService} from '../../shared/api/endpoints/services/analytics.service';
4
import {map, tap} from 'rxjs/operators';
5
import {AggregationModel} from '../../shared/models/aggregationModel';
6
import * as moment from 'moment-timezone';
7
import {GraphLoader} from '../../shared/graph-loading/graphloader';
8
import {SensorsService} from '../../shared/api/endpoints/services/sensors.service';
9
import {HttpResponse} from '@angular/common/http';
10
import {ToastService} from '../../shared/services/toast.service';
11
import {Sensor} from '../../shared/api/endpoints/models/sensor';
12

    
13
declare var require: any
14

    
15
@Component({
16
  selector: 'app-unit',
17
  templateUrl: './unit.component.html',
18
  styleUrls: ['./unit.component.scss']
19
})
20
export class UnitComponent implements OnInit {
21

    
22
  preselectedSensors: string;
23
  unitId: number;
24
  viewCount = 0;
25
  data = [];
26
  time = [];
27
  from: Date;
28
  to: Date;
29
  today: Date;
30
  sensorGroups = [];
31
  selectedSensors = [];
32
  sensors: Sensor[];
33
  showAggregation = false;
34
  aggregationFunction: AggregationModel[];
35
  selectedAggregationFunction = 'HOUR';
36

    
37
  constructor(
38
    private activatedRoute: ActivatedRoute,
39
    private analyticsService: AnalyticsService,
40
    private sensorService: SensorsService,
41
    private toastService: ToastService
42
  ) {
43
    this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
44
    this.aggregationFunction = [
45
      {name: 'Hour', code: 'HOUR'},
46
      {name: 'Day', code: 'DAY'},
47
      {name: 'Month', code: 'MONTH'},
48
      {name: 'Year', code: 'YEAR'}
49
    ];
50
    this.today = moment().toDate();
51
    this.sensorService.getUnitSensors({unit_id: this.unitId}).subscribe(
52
      sensors => {
53
        this.sensors = sensors;
54
        if (sensors) {
55
          sensors.forEach(sensor => {
56
            const sensorType = sensor.sensorId.toString().slice(0, 5);
57
            if (!this.sensorGroups.some(group => group === sensorType)) {
58
              this.sensorGroups.push(sensorType);
59
            }
60
            if (!this.selectedSensors.some(sens => sens.toString().slice(0, 5) === sensorType)) {
61
              this.selectedSensors.push(sensor.sensorId);
62
            }
63
          })
64
        }
65
      }
66
    )
67
  }
68

    
69
  ngOnInit(): void {
70
    setTimeout(() => {
71
      this.showGraph();
72
    }, 0);
73
  }
74

    
75
  showGraph() {
76
    const range: Date[] = [moment().subtract(6, 'months').toDate(), moment().toDate()];
77

    
78
    if (this.from && !this.to || !this.from && this.to) {
79
      return;
80
    }
81

    
82
    if (this.from && this.to) {
83
      range[0] = this.from;
84
      range[1] = this.to;
85
    }
86

    
87
    if (moment(range[1]).diff(moment(range[0]), 'days') > 7) {
88
      this.showAggregation = true;
89
      this.getAnalytics(range);
90
    } else {
91
      this.showAggregation = false;
92
      // this.getObservations(range);
93
    }
94
  }
95

    
96
  getAnalytics(range: Date[]) {
97
    this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: null,
98
      from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
99
      to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
100
      map((response: HttpResponse<any>) => {
101
        if (response.status === 200) {
102
          return response.body;
103
        } else if (response.status === 204) {
104
          this.toastService.showWarningNoData();
105
          return response.body;
106
        } else {
107
          return false;
108
        }
109
      })
110
    ).subscribe(data => {
111
        const objectKeys = Object.keys(data);
112
        for(const key of objectKeys ) {
113
          if (data[key].data) {
114
            const view = '#vega_container_' + key.slice(0, 5);
115
            if (this.selectedSensors.some(sens => sens.toString() === key)) {
116
              GraphLoader.getGraph(key, data[key].data, data[key].interval, view);
117
            }
118
          }
119
        }
120
      });
121
  }
122
}
(3-3/3)