Projekt

Obecné

Profil

Stáhnout (9.81 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
import {ObservationService} from '../../shared/api/endpoints/services/observation.service';
13

    
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
  analyticsData: any[] = [];
31
  observationsData: any[] = [];
32
  sensorGroups = [];
33
  selectedSensors: number[] = [];
34
  sensors: Sensor[];
35
  showAggregation = false;
36
  aggregationFunction: AggregationModel[];
37
  selectedAggregationFunction = 'DAY';
38
  useAnalyticsData = false;
39

    
40
  constructor(
41
    private activatedRoute: ActivatedRoute,
42
    private analyticsService: AnalyticsService,
43
    private sensorService: SensorsService,
44
    private toastService: ToastService,
45
    private observationService: ObservationService
46
  ) {
47
    this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
48
    this.aggregationFunction = [
49
      {name: 'Hour', code: 'HOUR'},
50
      {name: 'Day', code: 'DAY'},
51
      {name: 'Month', code: 'MONTH'},
52
      {name: 'Year', code: 'YEAR'}
53
    ];
54
    this.today = moment().toDate();
55
    this.sensorService.getUnitSensors({unit_id: this.unitId}).pipe(
56
      tap(sens => {
57
        this.sensors = sens;
58
      }),
59
      tap(() => {
60
        if (this.sensors && this.sensors.length > 0) {
61
          this.sensors.forEach(sensor => {
62
            const sensorType = sensor.sensorId.toString().slice(0, 5);
63
            if (!this.sensorGroups.some(group => group === sensorType)) {
64
              this.sensorGroups.push(sensorType);
65
              setTimeout(() => {
66
                GraphLoader.getGraph(null, null, null, '#vega_container_' + sensor.sensorId.toString().slice(0, 5));
67
              }, 0);
68
            }
69
            if (!this.selectedSensors.some(sens => sens.toString().slice(0, 5) === sensorType)) {
70
              this.selectedSensors.push(sensor.sensorId);
71
            }
72
          })
73
          this.showGraph();
74
        }
75
      })
76
    ).toPromise().then();
77
  }
78

    
79
  ngOnInit(): void {
80
  }
81

    
82
  showGraph(changedDate: boolean = true, changedSensor: number = null) {
83
    const range: Date[] = [moment().subtract(7, 'days').toDate(), moment().toDate()];
84

    
85
    if (this.from && !this.to || !this.from && this.to) {
86
      return;
87
    }
88

    
89
    if (this.from && this.to) {
90
      range[0] = this.from;
91
      range[1] = this.to;
92
    }
93

    
94
    if (moment(range[1]).diff(moment(range[0]), 'days') > 7) {
95
      this.useAnalyticsData = true;
96
      this.showAggregation = true;
97
      this.getAnalytics(range, changedDate, changedSensor);
98
    } else {
99
      this.useAnalyticsData = false;
100
      this.showAggregation = false;
101
      this.getObservations(range, changedDate, changedSensor);
102
    }
103
  }
104

    
105
  getAnalytics(range: Date[], changedDate: boolean, changedSensor: number) {
106
    if (changedDate) {
107
      this.selectedSensors.forEach(selectSens => {
108
        this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: selectSens,
109
          from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
110
          to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
111
          map((response: HttpResponse<any>) => {
112
            if (response.status === 200) {
113
              return response.body;
114
            } else if (response.status === 204) {
115
              GraphLoader.getGraph(null, null, null, '#vega_container_' + selectSens.toString().slice(0, 5));
116
              this.toastService.showWarningNoData();
117
              return response.body;
118
            } else {
119
              return false;
120
            }
121
          })
122
        ).subscribe(data => {
123
          if (data) {
124
            this.analyticsData.push({sensorId: selectSens, data: data[selectSens].data, interval: data[selectSens].interval});
125
            const objectKeys = Object.keys(data);
126
            for(const key of objectKeys ) {
127
              if (data[key].data) {
128
                const view = '#vega_container_' + key.slice(0, 5);
129
                if (this.selectedSensors.some(sens => sens.toString() === key)) {
130
                  GraphLoader.getGraph(key, data[key].data, data[key].interval, view);
131
                } else {
132
                  GraphLoader.getGraph(null, null, null, view);
133
                }
134
              }
135
            }
136
          }
137
        }, err => this.toastService.showError(err.error.message));
138
      });
139
    } else  {
140
      this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: changedSensor,
141
        from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
142
        to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
143
        map((response: HttpResponse<any>) => {
144
          if (response.status === 200) {
145
            return response.body;
146
          } else if (response.status === 204) {
147
            this.toastService.showWarningNoData();
148
            return response.body;
149
          } else {
150
            return false;
151
          }
152
        })
153
      ).subscribe(data => {
154
        if (data) {
155
          this.analyticsData.push({sensorId: changedSensor, data: data[changedSensor].data, interval: data[changedSensor].interval});
156
          const objectKeys = Object.keys(data);
157
          for(const key of objectKeys ) {
158
            if (data[key].data) {
159
              const view = '#vega_container_' + key.slice(0, 5);
160
              if (this.selectedSensors.some(sens => sens.toString() === key)) {
161
                GraphLoader.getGraph(key, data[key].data, data[key].interval, view);
162
              } else {
163
                GraphLoader.getGraph(null, null, null, view);
164
              }
165
            }
166
          }
167
        }
168
      }, err => this.toastService.showError(err.error.message));
169
    }
170
  }
171

    
172
  addSensorToGraph(sensorId: number) {
173
    if (!this.selectedSensors.some(sensId => sensId.toString().slice(0, 5) === sensorId.toString().slice(0, 5))) {
174
      GraphLoader.getGraph(null, null, null, '#vega_container_' + sensorId.toString().slice(0, 5));
175
    } else {
176
      if (this.useAnalyticsData) {
177
        if (this.analyticsData.some(sens => sens.sensorId === sensorId)) {
178
          GraphLoader.getGraph(sensorId, this.analyticsData.find(sens => sens.sensorId === sensorId).data,
179
            this.analyticsData.find(sens => sens.sensorId === sensorId).interval, '#vega_container_' + sensorId.toString().slice(0, 5));
180
        } else {
181
          this.showGraph(false, sensorId);
182
        }
183
      } else {
184
        if (this.observationsData.some(sens => sens.sensorId === sensorId)) {
185
          GraphLoader.getObservationGraph(sensorId,
186
            this.observationsData.find(sens => sens.sensorId === sensorId).data, '#vega_container_' + sensorId.toString().slice(0, 5));
187
        } else {
188
          this.showGraph(false, sensorId);
189
        }
190
      }
191
    }
192
  }
193

    
194
  private getObservations(range: Date[], changedDate: boolean, changedSensor: number) {
195
    if (changedDate) {
196
      this.selectedSensors.forEach(selectSens => {
197
        this.observationService.getObservation$Response({
198
          unit_id: this.unitId,
199
          sensor_id: selectSens,
200
          from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
201
          to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
202
        }).pipe(
203
          map((response: HttpResponse<any>) => {
204
            if (response.status === 200) {
205
              return response.body;
206
            } else if (response.status === 204) {
207
              this.toastService.showWarningNoData();
208
              return response.body;
209
            } else {
210
              return false;
211
            }
212
          })
213
        ).subscribe(
214
          observations => {
215
            this.observationsData.push({sensorId: selectSens, data: observations});
216
            const view = '#vega_container_' + selectSens.toString().slice(0, 5);
217
            if (observations) {
218
              GraphLoader.getObservationGraph(selectSens, observations, view);
219
            } else {
220
              GraphLoader.getObservationGraph(null, null, null);
221
            }
222
          }, err => this.toastService.showError(err.error.message));
223
      });
224
    } else {
225
      this.observationService.getObservation$Response({
226
        unit_id: this.unitId,
227
        sensor_id: changedSensor,
228
        from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
229
        to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
230
      }).pipe(
231
        map((response: HttpResponse<any>) => {
232
          if (response.status === 200) {
233
            return response.body;
234
          } else if (response.status === 204) {
235
            this.toastService.showWarningNoData();
236
            return response.body;
237
          } else {
238
            return false;
239
          }
240
        })
241
      ).subscribe(
242
        observations => {
243
          this.observationsData.push({sensorId: this.selectedSensors[0], data: observations});
244
          const view = '#vega_container_' + changedSensor.toString().slice(0, 5);
245
          if (observations) {
246
            GraphLoader.getObservationGraph(changedSensor, observations, view);
247
          } else {
248
            GraphLoader.getObservationGraph(null, null, null);
249
          }
250
        }, err => this.toastService.showError(err.error.message));
251
    }
252
  }
253
}
(3-3/3)