Projekt

Obecné

Profil

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

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

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