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: string[] = [];
|
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
|
this.sensors.sort((a, b) => a.sensorId - b.sensorId);
|
59
|
}),
|
60
|
tap(() => {
|
61
|
if (this.sensors && this.sensors.length > 0) {
|
62
|
this.sensors.forEach(sensor => {
|
63
|
const sensorType = sensor.sensorId.toString().slice(0, 5);
|
64
|
if (!this.sensorGroups.some(group => group === sensorType)) {
|
65
|
this.sensorGroups.push(sensorType);
|
66
|
setTimeout(() => {
|
67
|
//GraphLoader.getAnalyticsGraph(null, null, null, '#vega_container_' + sensor.sensorId.toString().slice(0, 5));
|
68
|
GraphLoader.getGraph(null, null, '#vega_container_' + sensor.sensorId.toString().slice(0, 5),null );
|
69
|
}, 0);
|
70
|
}
|
71
|
});
|
72
|
}
|
73
|
})
|
74
|
).toPromise().then();
|
75
|
}
|
76
|
|
77
|
ngOnInit(): void {
|
78
|
}
|
79
|
|
80
|
showGraph(changedDate: boolean = true, changedSensor: string = null) {
|
81
|
const range: Date[] = [moment().subtract(7, 'days').toDate(), moment().toDate()];
|
82
|
|
83
|
if (this.from && !this.to || !this.from && this.to) {
|
84
|
return;
|
85
|
}
|
86
|
|
87
|
if (this.from && this.to) {
|
88
|
range[0] = this.from;
|
89
|
range[1] = this.to;
|
90
|
}
|
91
|
|
92
|
if (moment(range[1]).diff(moment(range[0]), 'days') > 7) {
|
93
|
this.useAnalyticsData = true;
|
94
|
this.showAggregation = true;
|
95
|
this.getAnalytics(range, changedDate, changedSensor);
|
96
|
} else {
|
97
|
this.useAnalyticsData = false;
|
98
|
this.showAggregation = false;
|
99
|
this.getObservations(range, changedDate, changedSensor);
|
100
|
}
|
101
|
}
|
102
|
|
103
|
getAnalytics(range: Date[], changedDate: boolean, changedSensor: string) {
|
104
|
if (changedDate) {
|
105
|
this.selectedSensors.forEach(selectSens => {
|
106
|
this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: parseInt(selectSens, 10),
|
107
|
from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
|
108
|
to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
|
109
|
map((response: HttpResponse<any>) => {
|
110
|
if (response.status === 200) {
|
111
|
return response.body;
|
112
|
} else if (response.status === 204) {
|
113
|
//GraphLoader.getAnalyticsGraph(null, null, null, '#vega_container_' + selectSens.toString().slice(0, 5));
|
114
|
GraphLoader.getGraph(null, null, '#vega_container_' + selectSens.toString().slice(0, 5),null );
|
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.getAnalyticsGraph(key, data[key].data, data[key].interval, view);
|
130
|
//GraphLoader.getGraph(this.selectedSensors, this.filteredAnalyticsData(groupId), view, true);
|
131
|
} else {
|
132
|
GraphLoader.getGraph(null, null, view, null);
|
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: parseInt(changedSensor, 10),
|
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.getAnalyticsGraph(key, data[key].data, data[key].interval, view);
|
162
|
GraphLoader.getGraph(this.selectedSensors, this.analyticsData, view, true);
|
163
|
} else {
|
164
|
//GraphLoader.getAnalyticsGraph(null, null, null, view);
|
165
|
GraphLoader.getGraph(null, null, view, null);
|
166
|
}
|
167
|
}
|
168
|
}
|
169
|
}
|
170
|
}, err => this.toastService.showError(err.error.message));
|
171
|
}
|
172
|
}
|
173
|
|
174
|
/**
|
175
|
* Check button handler.
|
176
|
* @param sensorId checked sensorId
|
177
|
* @param event event for getting if checked or unchecked
|
178
|
*/
|
179
|
addSensorToGraph(sensorId: string, event) {
|
180
|
const groupId = sensorId.toString().slice(0, 5);
|
181
|
const sensorGroupElement = '#vega_container_' + groupId;
|
182
|
if (!this.selectedSensors.find(sensId => sensId.toString().slice(0, 5) === groupId)) { // if group of sensors is empty show empty graph
|
183
|
//GraphLoader.getAnalyticsGraph(null, null, null, sensorGroupElement);
|
184
|
GraphLoader.getGraph(null, null, sensorGroupElement, null);
|
185
|
} else {
|
186
|
if (this.useAnalyticsData) { // use analytics data
|
187
|
if (event.checked) { // if checked > add to graph
|
188
|
if (this.analyticsData.some(sens => sens.sensorId === sensorId)) { // if already data for selected sensor in memory
|
189
|
//GraphLoader.getAnalyticsGraph(sensorId, this.analyticsData.find(sens => sens.sensorId === sensorId).data,
|
190
|
// this.analyticsData.find((sens => sens.sensorId === sensorId).interval, sensorGroupElement);
|
191
|
GraphLoader.getGraph(this.selectedSensors, this.analyticsData, sensorGroupElement, true);
|
192
|
|
193
|
} else { // get data from server for added sensor and show graph for selected sensors
|
194
|
this.showGraph(false, sensorId);
|
195
|
}
|
196
|
} else { // remove sensor from graph
|
197
|
//GraphLoader.getAnalyticsGraph(sensorId, this.analyticsData.find(sens => sens.sensorId === sensorId).data,
|
198
|
// this.analyticsData.find(sens => sens.sensorId === sensorId).interval, sensorGroupElement);
|
199
|
GraphLoader.getGraph(this.selectedSensors, this.analyticsData, sensorGroupElement, true);
|
200
|
|
201
|
}
|
202
|
} else { // use observations data
|
203
|
if (event.checked) { // if checked > add to graph
|
204
|
if (this.observationsData.some(sens => sens.sensorId.toString() === sensorId)) { // if already data for selected sensor in memory
|
205
|
GraphLoader.getGraph(this.selectedSensors, this.filteredObservationData(groupId), sensorGroupElement, false);
|
206
|
//GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(groupId), sensorGroupElement)
|
207
|
} else { // get data from server for added sensor and show graph for selected sensors
|
208
|
this.showGraph(false, sensorId);
|
209
|
}
|
210
|
} else { // remove sensor from graph
|
211
|
//GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(groupId), sensorGroupElement)
|
212
|
GraphLoader.getGraph(this.selectedSensors, this.filteredObservationData(groupId), sensorGroupElement, false);
|
213
|
}
|
214
|
}
|
215
|
}
|
216
|
}
|
217
|
|
218
|
/**
|
219
|
* Filter observations data only fro selected sensors.
|
220
|
* @param sensorGroupId id of changed sensor group
|
221
|
*/
|
222
|
filteredObservationData(sensorGroupId: string): any {
|
223
|
return this.observationsData.filter(sen => this.selectedSensors.includes(sen.sensorId.toString()) &&
|
224
|
sen.sensorId.toString().slice(0, 5) === sensorGroupId);
|
225
|
}
|
226
|
|
227
|
/**
|
228
|
* Filter analytics data only fro selected sensors.
|
229
|
*/
|
230
|
filteredAnalyticsData(sensorGroupId: string): any {
|
231
|
return this.analyticsData.filter(sen => this.selectedSensors.includes(sen.sensorId.toString()) &&
|
232
|
sen.sensorId.toString().slice(0, 5) === sensorGroupId);
|
233
|
}
|
234
|
|
235
|
private getObservations(range: Date[], changedDate: boolean, changedSensorId: string) {
|
236
|
if (changedDate) { // if changed date we need new data for all sensors
|
237
|
this.observationsData = []; // empty observation data
|
238
|
this.selectedSensors.forEach(selectSens => {
|
239
|
this.observationService.getObservation$Response({
|
240
|
unit_id: this.unitId,
|
241
|
sensor_id: parseInt(selectSens, 10),
|
242
|
from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
|
243
|
to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
|
244
|
}).pipe(
|
245
|
map((response: HttpResponse<any>) => {
|
246
|
if (response.status === 200) {
|
247
|
return response.body;
|
248
|
} else if (response.status === 204) {
|
249
|
this.toastService.showWarningNoData();
|
250
|
return response.body;
|
251
|
} else {
|
252
|
return false;
|
253
|
}
|
254
|
}),
|
255
|
tap((observations) => {
|
256
|
if (observations) {
|
257
|
this.observationsData.push({sensorId: selectSens, sensor:
|
258
|
this.sensors.find(sens => sens.sensorId.toString() === selectSens.toString()), data: observations});
|
259
|
}
|
260
|
}),
|
261
|
tap(() => {
|
262
|
if (this.observationsData && this.observationsData.length > 1) {
|
263
|
const view = '#vega_container_' + selectSens.toString().slice(0, 5);
|
264
|
setTimeout(() => {
|
265
|
console.log(this.selectedSensors);
|
266
|
//GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(selectSens.toString().slice(0, 5)), view);
|
267
|
GraphLoader.getGraph(this.selectedSensors, this.filteredObservationData(changedSensorId.toString().slice(0, 5)), view, false);
|
268
|
}, 10);
|
269
|
}
|
270
|
})
|
271
|
).toPromise().then().catch(err => this.toastService.showError(err));
|
272
|
});
|
273
|
} else {
|
274
|
this.observationService.getObservation$Response({
|
275
|
unit_id: this.unitId,
|
276
|
sensor_id: parseInt(changedSensorId, 10),
|
277
|
from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
|
278
|
to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
|
279
|
}).pipe(
|
280
|
map((response: HttpResponse<any>) => {
|
281
|
if (response.status === 200) {
|
282
|
return response.body;
|
283
|
} else if (response.status === 204) {
|
284
|
this.toastService.showWarningNoData();
|
285
|
return response.body;
|
286
|
} else {
|
287
|
return false;
|
288
|
}
|
289
|
})
|
290
|
).subscribe(
|
291
|
observations => {
|
292
|
this.observationsData.push({sensorId: changedSensorId, sensor:
|
293
|
this.sensors.find(sens => sens.sensorId.toString() === changedSensorId.toString()), data: observations});
|
294
|
const view = '#vega_container_' + changedSensorId.toString().slice(0, 5);
|
295
|
if (observations) {
|
296
|
console.log(this.observationsData);
|
297
|
GraphLoader.getGraph(this.selectedSensors, this.filteredObservationData(changedSensorId.toString().slice(0, 5)), view, false);
|
298
|
//GraphLoader.getMultilineGraph(this.selectedSensors, this.filteredObservationData(changedSensorId.toString().slice(0, 5)), view);
|
299
|
} else {
|
300
|
//GraphLoader.getMultilineGraph(null, null, null);
|
301
|
GraphLoader.getGraph(this.selectedSensors, this.filteredObservationData(changedSensorId.toString().slice(0, 5)), view, false);
|
302
|
}
|
303
|
}, err => this.toastService.showError(err.error.message));
|
304
|
}
|
305
|
}
|
306
|
}
|