1 |
370c3423
|
hlavja
|
import {Component, OnInit} from '@angular/core';
|
2 |
|
|
import {ActivatedRoute} from '@angular/router';
|
3 |
|
|
import {AnalyticsService} from '../../shared/api/endpoints/services/analytics.service';
|
4 |
|
|
import {tap} from 'rxjs/operators';
|
5 |
|
|
import {Sensors} from '../../shared/api/endpoints/models/sensors';
|
6 |
|
|
import {AggreationModel} from '../../shared/models/aggreation.model';
|
7 |
|
|
import * as moment from 'moment-timezone';
|
8 |
11efc186
|
hlavja
|
import {GraphLoader} from '../../shared/graph-loading/graphloader';
|
9 |
370c3423
|
hlavja
|
|
10 |
8b522708
|
Štěpán Červenka
|
declare var require: any
|
11 |
370c3423
|
hlavja
|
|
12 |
|
|
@Component({
|
13 |
|
|
selector: 'app-unit',
|
14 |
|
|
templateUrl: './unit.component.html',
|
15 |
|
|
styleUrls: ['./unit.component.scss']
|
16 |
|
|
})
|
17 |
|
|
export class UnitComponent implements OnInit {
|
18 |
|
|
|
19 |
|
|
preselectedSensors: string;
|
20 |
|
|
unitId: number;
|
21 |
8b522708
|
Štěpán Červenka
|
viewCount = 0;
|
22 |
370c3423
|
hlavja
|
data;
|
23 |
11efc186
|
hlavja
|
sensorGroups = [];
|
24 |
370c3423
|
hlavja
|
rangeDates: Date[];
|
25 |
|
|
aggregationFunction: AggreationModel[];
|
26 |
|
|
selectedAggregationFunction: string;
|
27 |
|
|
|
28 |
|
|
constructor(
|
29 |
|
|
private activatedRoute: ActivatedRoute,
|
30 |
|
|
private analyticsService: AnalyticsService
|
31 |
|
|
) {
|
32 |
|
|
this.aggregationFunction = [
|
33 |
|
|
{name: 'Hour', code: 'HOUR'},
|
34 |
|
|
{name: 'Day', code: 'DAY'},
|
35 |
|
|
{name: 'Month', code: 'MONTH'},
|
36 |
|
|
{name: 'Year', code: 'YEAR'}
|
37 |
|
|
];
|
38 |
|
|
}
|
39 |
|
|
|
40 |
|
|
ngOnInit(): void {
|
41 |
|
|
/* this.route.queryParams.subscribe(params => {
|
42 |
|
|
if(params.sensors) {
|
43 |
|
|
this.preselectedSensors = params.sensors;
|
44 |
|
|
}
|
45 |
|
|
});*/
|
46 |
|
|
|
47 |
|
|
this.unitId = parseInt(this.activatedRoute.snapshot.paramMap.get('unitId'), 10);
|
48 |
|
|
|
49 |
|
|
this.showGraph()
|
50 |
|
|
}
|
51 |
|
|
|
52 |
|
|
showGraph() {
|
53 |
|
|
let agg = 'HOUR';
|
54 |
8b522708
|
Štěpán Červenka
|
let range: Date[] = [moment().subtract(6, 'months').toDate(), moment().toDate()];
|
55 |
370c3423
|
hlavja
|
if (this.selectedAggregationFunction) {
|
56 |
|
|
agg = this.selectedAggregationFunction;
|
57 |
|
|
}
|
58 |
|
|
|
59 |
|
|
if (this.rangeDates) {
|
60 |
|
|
range = this.rangeDates;
|
61 |
|
|
}
|
62 |
|
|
|
63 |
|
|
this.analyticsService.getAnalytics({unit_id: this.unitId, sensor_id: null,
|
64 |
|
|
from: moment(range[0]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3),
|
65 |
|
|
to: moment(range[1]).format('yyyy-MM-DD HH:mm:ssZ').slice(0, -3), interval: agg}).pipe(
|
66 |
|
|
tap(data => {
|
67 |
8b522708
|
Štěpán Červenka
|
var objectKeys = Object.keys(data);
|
68 |
|
|
for(const key of objectKeys ) {
|
69 |
72effbbe
|
Štěpán Červenka
|
let element = this.createView();
|
70 |
|
|
GraphLoader.getGraph(key, data[key]["data"], data[key]["interval"], element);
|
71 |
a3ae1cab
|
hlavja
|
}
|
72 |
370c3423
|
hlavja
|
})
|
73 |
|
|
).subscribe();
|
74 |
|
|
}
|
75 |
8b522708
|
Štěpán Červenka
|
|
76 |
|
|
createView() {
|
77 |
|
|
let div = document.createElement("div");
|
78 |
|
|
let name = "view" + this.viewCount;
|
79 |
|
|
div.id = name;
|
80 |
|
|
this.viewCount++;
|
81 |
|
|
|
82 |
|
|
let container = document.getElementById("vega_container");
|
83 |
|
|
container.appendChild(div);
|
84 |
|
|
|
85 |
|
|
return '#' + name;
|
86 |
|
|
}
|
87 |
370c3423
|
hlavja
|
}
|