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
|
});
|
70
|
}
|
71
|
})
|
72
|
).toPromise().then();
|
73
|
}
|
74
|
|
75
|
ngOnInit(): void {
|
76
|
}
|
77
|
|
78
|
showGraph(changedDate: boolean = true, changedSensor: number = null) {
|
79
|
const range: Date[] = [moment().subtract(7, 'days').toDate(), moment().toDate()];
|
80
|
|
81
|
if (this.from && !this.to || !this.from && this.to) {
|
82
|
return;
|
83
|
}
|
84
|
|
85
|
if (this.from && this.to) {
|
86
|
range[0] = this.from;
|
87
|
range[1] = this.to;
|
88
|
}
|
89
|
|
90
|
if (moment(range[1]).diff(moment(range[0]), 'days') > 7) {
|
91
|
this.useAnalyticsData = true;
|
92
|
this.showAggregation = true;
|
93
|
this.getAnalytics(range, changedDate, changedSensor);
|
94
|
} else {
|
95
|
this.useAnalyticsData = false;
|
96
|
this.showAggregation = false;
|
97
|
this.getObservations(range, changedDate, changedSensor);
|
98
|
}
|
99
|
}
|
100
|
|
101
|
getAnalytics(range: Date[], changedDate: boolean, changedSensor: number) {
|
102
|
if (changedDate) {
|
103
|
this.selectedSensors.forEach(selectSens => {
|
104
|
this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: selectSens,
|
105
|
from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
|
106
|
to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
|
107
|
map((response: HttpResponse<any>) => {
|
108
|
if (response.status === 200) {
|
109
|
return response.body;
|
110
|
} else if (response.status === 204) {
|
111
|
GraphLoader.getGraph(null, null, null, '#vega_container_' + selectSens.toString().slice(0, 5));
|
112
|
this.toastService.showWarningNoData();
|
113
|
return response.body;
|
114
|
} else {
|
115
|
return false;
|
116
|
}
|
117
|
})
|
118
|
).subscribe(data => {
|
119
|
if (data) {
|
120
|
this.analyticsData.push({sensorId: selectSens, data: data[selectSens].data, interval: data[selectSens].interval});
|
121
|
const objectKeys = Object.keys(data);
|
122
|
for(const key of objectKeys ) {
|
123
|
if (data[key].data) {
|
124
|
const view = '#vega_container_' + key.slice(0, 5);
|
125
|
if (this.selectedSensors.some(sens => sens.toString() === key)) {
|
126
|
GraphLoader.getGraph(key, data[key].data, data[key].interval, view);
|
127
|
} else {
|
128
|
GraphLoader.getGraph(null, null, null, view);
|
129
|
}
|
130
|
}
|
131
|
}
|
132
|
}
|
133
|
}, err => this.toastService.showError(err.error.message));
|
134
|
});
|
135
|
} else {
|
136
|
this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: changedSensor,
|
137
|
from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
|
138
|
to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
|
139
|
map((response: HttpResponse<any>) => {
|
140
|
if (response.status === 200) {
|
141
|
return response.body;
|
142
|
} else if (response.status === 204) {
|
143
|
this.toastService.showWarningNoData();
|
144
|
return response.body;
|
145
|
} else {
|
146
|
return false;
|
147
|
}
|
148
|
})
|
149
|
).subscribe(data => {
|
150
|
if (data) {
|
151
|
this.analyticsData.push({sensorId: changedSensor, data: data[changedSensor].data, interval: data[changedSensor].interval});
|
152
|
const objectKeys = Object.keys(data);
|
153
|
for(const key of objectKeys ) {
|
154
|
if (data[key].data) {
|
155
|
const view = '#vega_container_' + key.slice(0, 5);
|
156
|
if (this.selectedSensors.some(sens => sens.toString() === key)) {
|
157
|
GraphLoader.getGraph(key, data[key].data, data[key].interval, view);
|
158
|
} else {
|
159
|
GraphLoader.getGraph(null, null, null, view);
|
160
|
}
|
161
|
}
|
162
|
}
|
163
|
}
|
164
|
}, err => this.toastService.showError(err.error.message));
|
165
|
}
|
166
|
}
|
167
|
|
168
|
/**
|
169
|
* Check button handler.
|
170
|
* @param sensorId checked sensorId
|
171
|
* @param event event for getting if checked or unchecked
|
172
|
*/
|
173
|
addSensorToGraph(sensorId: number, event) {
|
174
|
const groupId = sensorId.toString().slice(0, 5);
|
175
|
const sensorGroupElement = '#vega_container_' + groupId;
|
176
|
if (!this.selectedSensors.find(sensId => sensId.toString().slice(0, 5) === groupId)) { // if group of sensors is empty show empty graph
|
177
|
GraphLoader.getGraph(null, null, null, sensorGroupElement);
|
178
|
} else {
|
179
|
if (this.useAnalyticsData) { // use analytics data
|
180
|
if (event.checked) { // if checked > add to graph
|
181
|
if (this.analyticsData.some(sens => sens.sensorId === sensorId)) { // if already data for selected sensor in memory
|
182
|
GraphLoader.getGraph(sensorId, this.analyticsData.find(sens => sens.sensorId === sensorId).data,
|
183
|
this.analyticsData.find(sens => sens.sensorId === sensorId).interval, sensorGroupElement);
|
184
|
} else { // get data from server for added sensor and show graph for selected sensors
|
185
|
this.showGraph(false, sensorId);
|
186
|
}
|
187
|
} else { // remove sensor from graph
|
188
|
GraphLoader.getGraph(sensorId, this.analyticsData.find(sens => sens.sensorId === sensorId).data,
|
189
|
this.analyticsData.find(sens => sens.sensorId === sensorId).interval, sensorGroupElement);
|
190
|
}
|
191
|
} else { // use observations data
|
192
|
if (event.checked) { // if checked > add to graph
|
193
|
if (this.observationsData.some(sens => sens.sensorId === sensorId)) { // if already data for selected sensor in memory
|
194
|
GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(groupId), sensorGroupElement)
|
195
|
} else { // get data from server for added sensor and show graph for selected sensors
|
196
|
this.showGraph(false, sensorId);
|
197
|
}
|
198
|
} else { // remove sensor from graph
|
199
|
GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(groupId), sensorGroupElement)
|
200
|
}
|
201
|
}
|
202
|
}
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* Filter observations data only fro selected sensors.
|
207
|
* @param sensorGroupId id of changed sensor group
|
208
|
*/
|
209
|
filteredObservationData(sensorGroupId: string): any {
|
210
|
return this.observationsData.filter(sen => this.selectedSensors.includes(sen.sensorId.toString()) &&
|
211
|
sen.sensorId.toString().slice(0, 5) === sensorGroupId);
|
212
|
}
|
213
|
|
214
|
/**
|
215
|
* Filter analytics data only fro selected sensors.
|
216
|
*/
|
217
|
filteredAnalyticsData(): any {
|
218
|
return this.analyticsData.filter(sen => this.selectedSensors.includes(sen.sensorId.toString()));
|
219
|
}
|
220
|
|
221
|
private getObservations(range: Date[], changedDate: boolean, changedSensorId: number) {
|
222
|
if (changedDate) { // if changed date we need new data for all sensors
|
223
|
this.observationsData = []; // empty observation data
|
224
|
this.selectedSensors.forEach(selectSens => {
|
225
|
this.observationService.getObservation$Response({
|
226
|
unit_id: this.unitId,
|
227
|
sensor_id: selectSens,
|
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
|
tap((observations) => {
|
242
|
if (observations) {
|
243
|
this.observationsData.push({sensorId: selectSens, sensor:
|
244
|
this.sensors.find(sens => sens.sensorId.toString() === selectSens.toString()), data: observations});
|
245
|
}
|
246
|
}),
|
247
|
tap(() => {
|
248
|
if (this.observationsData && this.observationsData.length > 1) {
|
249
|
const view = '#vega_container_' + selectSens.toString().slice(0, 5);
|
250
|
setTimeout(() => {
|
251
|
console.log(this.selectedSensors);
|
252
|
GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(selectSens.toString().slice(0, 5)), view);
|
253
|
}, 0);
|
254
|
}
|
255
|
})
|
256
|
).toPromise().then().catch(err => this.toastService.showError(err));
|
257
|
});
|
258
|
} else {
|
259
|
this.observationService.getObservation$Response({
|
260
|
unit_id: this.unitId,
|
261
|
sensor_id: changedSensorId,
|
262
|
from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
|
263
|
to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
|
264
|
}).pipe(
|
265
|
map((response: HttpResponse<any>) => {
|
266
|
if (response.status === 200) {
|
267
|
return response.body;
|
268
|
} else if (response.status === 204) {
|
269
|
this.toastService.showWarningNoData();
|
270
|
return response.body;
|
271
|
} else {
|
272
|
return false;
|
273
|
}
|
274
|
})
|
275
|
).subscribe(
|
276
|
observations => {
|
277
|
this.observationsData.push({sensorId: changedSensorId, sensor:
|
278
|
this.sensors.find(sens => sens.sensorId.toString() === changedSensorId.toString()), data: observations});
|
279
|
const view = '#vega_container_' + changedSensorId.toString().slice(0, 5);
|
280
|
if (observations) {
|
281
|
GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(changedSensorId.toString().slice(0, 5)), view);
|
282
|
} else {
|
283
|
GraphLoader.getMultilineGraph(null, null, null);
|
284
|
}
|
285
|
}, err => this.toastService.showError(err.error.message));
|
286
|
}
|
287
|
}
|
288
|
}
|