1
|
import {Component, OnDestroy, 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
|
import {SensorType} from '../../shared/api/endpoints/models/sensor-type';
|
14
|
import {Subscription} from 'rxjs';
|
15
|
|
16
|
|
17
|
@Component({
|
18
|
selector: 'app-unit',
|
19
|
templateUrl: './unit.component.html',
|
20
|
styleUrls: ['./unit.component.scss']
|
21
|
})
|
22
|
export class UnitComponent implements OnInit, OnDestroy {
|
23
|
|
24
|
preselectedSensors: string;
|
25
|
unitId: number;
|
26
|
viewCount = 0;
|
27
|
data = [];
|
28
|
time = [];
|
29
|
from: Date = moment().hour(0).minutes(0).subtract(7, 'days').toDate();
|
30
|
to: Date = moment().toDate();
|
31
|
today: Date = moment().toDate();
|
32
|
analyticsData: any[] = [];
|
33
|
observationsData: any[] = [];
|
34
|
sensorGroups = [];
|
35
|
selectedSensors: string[] = [];
|
36
|
sensors: Sensor[];
|
37
|
showAggregation = false;
|
38
|
aggregationFunction: AggregationModel[];
|
39
|
selectedAggregationFunction = 'DAY';
|
40
|
useAnalyticsData = false;
|
41
|
dateChanged = false;
|
42
|
sensorTypes: SensorType[];
|
43
|
unitDescription: string;
|
44
|
subscription: Subscription[] = [];
|
45
|
|
46
|
constructor(
|
47
|
private activatedRoute: ActivatedRoute,
|
48
|
private analyticsService: AnalyticsService,
|
49
|
private sensorService: SensorsService,
|
50
|
private toastService: ToastService,
|
51
|
private observationService: ObservationService,
|
52
|
private route: ActivatedRoute,
|
53
|
) {
|
54
|
this.getInitData();
|
55
|
// get unit sensors and prepare them for view
|
56
|
this.sensorService.getUnitSensors({unit_id: this.unitId}).pipe(
|
57
|
tap(sens => {
|
58
|
this.sensors = sens;
|
59
|
this.sensors.sort((a, b) => a.sensorId - b.sensorId);
|
60
|
}),
|
61
|
tap(() => {
|
62
|
if (this.sensors && this.sensors.length > 0) {
|
63
|
this.sensors.forEach(sensor => {
|
64
|
const sensorType = sensor.sensorId.toString().slice(0, 5);
|
65
|
if (!this.sensorGroups.some(group => group === sensorType)) { // create sensor groups only for unit sensors
|
66
|
this.sensorGroups.push(sensorType);
|
67
|
setTimeout(() => {
|
68
|
GraphLoader.getGraph(null, null, null, '#vega_container_' + sensor.sensorId.toString().slice(0, 5),null);
|
69
|
}, 0);
|
70
|
}
|
71
|
});
|
72
|
}
|
73
|
})
|
74
|
).toPromise().then();
|
75
|
}
|
76
|
|
77
|
/**
|
78
|
* Unsubscribe after leaving
|
79
|
*/
|
80
|
ngOnDestroy(): void {
|
81
|
this.subscription.forEach(subs => subs.unsubscribe());
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
* Sets up default data
|
86
|
*/
|
87
|
getInitData() {
|
88
|
this.route.queryParams.subscribe(params => {
|
89
|
if(params.unitDescription) {
|
90
|
this.unitDescription = params.unitDescription;
|
91
|
}
|
92
|
});
|
93
|
this.sensorService.getSensorTypes().toPromise().then(types => this.sensorTypes = types);
|
94
|
this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
|
95
|
this.aggregationFunction = [
|
96
|
{name: 'Hour', code: 'HOUR'},
|
97
|
{name: 'Day', code: 'DAY'},
|
98
|
{name: 'Month', code: 'MONTH'},
|
99
|
{name: 'Year', code: 'YEAR'}
|
100
|
];
|
101
|
}
|
102
|
|
103
|
ngOnInit(): void {
|
104
|
}
|
105
|
|
106
|
/**
|
107
|
* Shows aggregation select box and get data button
|
108
|
*/
|
109
|
aggregationShow() {
|
110
|
this.dateChanged = true;
|
111
|
this.showAggregation = moment(this.to).diff(moment(this.from), 'days') > 7;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Gets data based on selected time range
|
116
|
*/
|
117
|
showGraph(changedDate: boolean = true, changedSensor: string = null) {
|
118
|
if (moment(this.to).diff(moment(this.from), 'days') > 7) {
|
119
|
this.useAnalyticsData = true;
|
120
|
this.showAggregation = true;
|
121
|
const range: Date[] = [this.from, this.to];
|
122
|
this.getAnalytics(range, changedDate, changedSensor);
|
123
|
} else {
|
124
|
this.useAnalyticsData = false;
|
125
|
this.showAggregation = false;
|
126
|
const range: Date[] = [this.from, this.to];
|
127
|
this.getObservations(range, changedDate, changedSensor);
|
128
|
}
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* Gets data from analytics endpoint
|
133
|
* @param range from and to interval
|
134
|
* @param changedDate determines if dates changed so we need refresh all data
|
135
|
* @param changedSensorId if selecting sensor only fetch data for this server
|
136
|
*/
|
137
|
getAnalytics(range: Date[], changedDate: boolean, changedSensorId: string) {
|
138
|
if (changedDate) { // if changed date we need new data for all sensors
|
139
|
this.selectedSensors.forEach(selectSens => {
|
140
|
this.analyticsData = []; //empty analytics data
|
141
|
this.analyticsEndpointRequest(selectSens, range);
|
142
|
});
|
143
|
} else { // add data for selected sensor
|
144
|
this.analyticsEndpointRequest(changedSensorId, range);
|
145
|
}
|
146
|
}
|
147
|
|
148
|
/**
|
149
|
* Endpoint request to get analytics data for sensor
|
150
|
* @param sensorId sensor id to get data
|
151
|
* @param range from and to interval
|
152
|
*/
|
153
|
analyticsEndpointRequest(sensorId: string, range: Date[]) {
|
154
|
this.analyticsService.getAnalytics$Response({unit_id: this.unitId, sensor_id: parseInt(sensorId, 10),
|
155
|
from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
|
156
|
to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: this.selectedAggregationFunction}).pipe(
|
157
|
map((response: HttpResponse<any>) => {
|
158
|
if (response.status === 200) {
|
159
|
return response.body;
|
160
|
} else if (response.status === 204) {
|
161
|
this.toastService.showWarningNoData();
|
162
|
return response.body;
|
163
|
} else {
|
164
|
return false;
|
165
|
}
|
166
|
})
|
167
|
).subscribe(data => {
|
168
|
if (data) {
|
169
|
this.analyticsData.push({sensorId, data: data[sensorId].data, interval: data[sensorId].interval,
|
170
|
sensor: this.sensors.find(sens => sens.sensorId.toString() === sensorId.toString())});
|
171
|
if (data[sensorId].data) {
|
172
|
const groupId = sensorId.slice(0, 5);
|
173
|
const view = '#vega_container_' + groupId;
|
174
|
if (this.selectedSensors.some(sens => sens.toString() === sensorId)) {
|
175
|
// GraphLoader.getAnalyticsGraph(key, data[key].data, data[key].interval, view);
|
176
|
GraphLoader.getGraphWithInterval(this.filteredSelectedSensors(groupId), this.filteredAnalyticsData(groupId), this.filteredAnalyticsData(groupId)[0]['interval'] * 1000, this.filteredSensorsInfos(groupId)[0], view, true);
|
177
|
} else {
|
178
|
// GraphLoader.getAnalyticsGraph(null, null, null, view);
|
179
|
GraphLoader.getGraph(null, null, null, view, null);
|
180
|
}
|
181
|
}
|
182
|
}
|
183
|
}, err => this.toastService.showError(err.error.message));
|
184
|
}
|
185
|
|
186
|
/**
|
187
|
* Check button handler.
|
188
|
* @param sensorId checked sensorId
|
189
|
* @param event event for getting if checked or unchecked
|
190
|
*/
|
191
|
addSensorToGraph(sensorId: string, event) {
|
192
|
const groupId = sensorId.toString().slice(0, 5);
|
193
|
const sensorGroupElement = '#vega_container_' + groupId;
|
194
|
if (!this.selectedSensors.find(sensId => sensId.toString().slice(0, 5) === groupId)) { // if group of sensors is empty show empty graph
|
195
|
// GraphLoader.getAnalyticsGraph(null, null, null, sensorGroupElement);
|
196
|
GraphLoader.getGraph(null, null, null, sensorGroupElement, null);
|
197
|
} else {
|
198
|
if (this.useAnalyticsData) { // use analytics data
|
199
|
if (event.checked) { // if checked > add to graph
|
200
|
if (this.analyticsData.some(sens => sens.sensorId === sensorId)) { // if already data for selected sensor in memory
|
201
|
// GraphLoader.getAnalyticsGraph(sensorId, this.analyticsData.find(sens => sens.sensorId === sensorId).data,
|
202
|
// this.analyticsData.find((sens => sens.sensorId === sensorId).interval, sensorGroupElement);
|
203
|
//GraphLoader.getGraph(this.selectedSensors, this.analyticsData, this.filteredSensorsInfos(groupId), sensorGroupElement, true);
|
204
|
GraphLoader.getGraphWithInterval(this.filteredSelectedSensors(groupId), this.filteredAnalyticsData(groupId), this.filteredAnalyticsData(groupId)[0]['interval'] * 1000, this.filteredSensorsInfos(groupId)[0], sensorGroupElement, true);
|
205
|
|
206
|
} else { // get data from server for added sensor and show graph for selected sensors
|
207
|
this.showGraph(false, sensorId);
|
208
|
}
|
209
|
} else { // remove sensor from graph
|
210
|
// GraphLoader.getAnalyticsGraph(sensorId, this.analyticsData.find(sens => sens.sensorId === sensorId).data,
|
211
|
// this.analyticsData.find(sens => sens.sensorId === sensorId).interval, sensorGroupElement);
|
212
|
GraphLoader.getGraphWithInterval(this.filteredSelectedSensors(groupId), this.filteredAnalyticsData(groupId), this.filteredAnalyticsData(groupId)[0]['interval'] * 1000, this.filteredSensorsInfos(groupId)[0], sensorGroupElement, true);
|
213
|
|
214
|
}
|
215
|
} else { // use observations data
|
216
|
if (event.checked) { // if checked > add to graph
|
217
|
if (this.observationsData.some(sens => sens.sensorId.toString() === sensorId)) { // if already data for selected sensor in memory
|
218
|
GraphLoader.getGraph(this.filteredSelectedSensors(groupId), this.filteredObservationData(groupId),
|
219
|
this.filteredSensorsInfos(groupId), sensorGroupElement, false);
|
220
|
} else { // get data from server for added sensor and show graph for selected sensors
|
221
|
this.showGraph(false, sensorId);
|
222
|
}
|
223
|
} else { // remove sensor from graph
|
224
|
GraphLoader.getGraph(this.filteredSelectedSensors(groupId), this.filteredObservationData(groupId),
|
225
|
this.filteredSensorsInfos(groupId), sensorGroupElement, false);
|
226
|
}
|
227
|
}
|
228
|
}
|
229
|
}
|
230
|
|
231
|
/**
|
232
|
* Filter observations data only fro selected sensors.
|
233
|
* @param sensorGroupId id of changed sensor group
|
234
|
*/
|
235
|
filteredObservationData(sensorGroupId: string): any {
|
236
|
return this.observationsData.filter(sen => this.selectedSensors.includes(sen.sensorId.toString()) &&
|
237
|
sen.sensorId.toString().slice(0, 5) === sensorGroupId);
|
238
|
}
|
239
|
|
240
|
/**
|
241
|
* Filter analytics data only fro selected sensors.
|
242
|
*/
|
243
|
filteredAnalyticsData(sensorGroupId: string): any {
|
244
|
return this.analyticsData.filter(sen => this.selectedSensors.includes(sen.sensorId.toString()) &&
|
245
|
sen.sensorId.toString().slice(0, 5) === sensorGroupId);
|
246
|
}
|
247
|
|
248
|
/**
|
249
|
* Filter only selected sensors for group of sensors
|
250
|
* @param sensorGroupId group of sensors
|
251
|
*/
|
252
|
filteredSelectedSensors(sensorGroupId: string): any {
|
253
|
return this.selectedSensors.filter(sen => sen.toString().slice(0, 5) === sensorGroupId);
|
254
|
}
|
255
|
|
256
|
/**
|
257
|
* Get sensors only for group
|
258
|
* @param sensorGroupId group id
|
259
|
*/
|
260
|
filteredSensorsInfos(sensorGroupId: string): any {
|
261
|
return this.sensors.filter(sen => this.selectedSensors.includes(sen.sensorId.toString()) &&
|
262
|
sen.sensorId.toString().slice(0, 5) === sensorGroupId);
|
263
|
}
|
264
|
|
265
|
/**
|
266
|
* Gets data from observation endpoint
|
267
|
* @param range from and to interval
|
268
|
* @param changedDate determines if dates changed so we need refresh all data
|
269
|
* @param changedSensorId if selecting sensor only fetch data for this server
|
270
|
*/
|
271
|
getObservations(range: Date[], changedDate: boolean, changedSensorId: string) {
|
272
|
if (changedDate) { // if changed date we need new data for all sensors
|
273
|
this.observationsData = []; // empty observation data
|
274
|
this.selectedSensors.forEach(selectSens => {
|
275
|
this.observationEndpointRequest(selectSens, range);
|
276
|
});
|
277
|
} else { // add data for added sensor
|
278
|
this.observationEndpointRequest(changedSensorId, range);
|
279
|
}
|
280
|
}
|
281
|
|
282
|
/**
|
283
|
* Endpoint request to get observation data for sensor
|
284
|
* @param sensorId sensor id to get data
|
285
|
* @param range from and to interval
|
286
|
*/
|
287
|
observationEndpointRequest(sensorId: string, range: Date[]) {
|
288
|
this.observationService.getObservation$Response({
|
289
|
unit_id: this.unitId,
|
290
|
sensor_id: parseInt(sensorId, 10),
|
291
|
from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
|
292
|
to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3)
|
293
|
}).pipe(
|
294
|
map((response: HttpResponse<any>) => {
|
295
|
if (response.status === 200) {
|
296
|
return response.body;
|
297
|
} else if (response.status === 204) {
|
298
|
this.toastService.showWarningNoData();
|
299
|
return response.body;
|
300
|
} else {
|
301
|
return false;
|
302
|
}
|
303
|
})
|
304
|
).subscribe(
|
305
|
observations => {
|
306
|
if (observations) {
|
307
|
const groupId = sensorId.toString().slice(0, 5);
|
308
|
this.observationsData.push({
|
309
|
sensorId, sensor:
|
310
|
this.sensors.find(sens => sens.sensorId.toString() === sensorId.toString()), data: observations
|
311
|
});
|
312
|
const view = '#vega_container_' + sensorId.toString().slice(0, 5);
|
313
|
GraphLoader.getGraph(this.filteredSelectedSensors(groupId), this.filteredObservationData(groupId),
|
314
|
this.filteredSensorsInfos(groupId), view, false);
|
315
|
}
|
316
|
}, err => this.toastService.showError(err.error.message));
|
317
|
}
|
318
|
}
|