|
@@ -0,0 +1,121 @@
|
|
|
|
|
+import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
|
|
|
|
|
+import { FormArray, FormBuilder, FormGroup, Validators } from '@angular/forms';
|
|
|
|
|
+import {HttpResponse} from '@angular/common/http';
|
|
|
|
|
+import {map} from 'rxjs/operators';
|
|
|
|
|
+import {ObservationService} from '../../../api/endpoints/services/observation.service';
|
|
|
|
|
+import { ToastService } from '../../../services/toast.service';
|
|
|
|
|
+import { SensorsService } from '../../../api/endpoints/services/sensors.service';
|
|
|
|
|
+import { DataService } from '../../../api/endpoints/services/data.service';
|
|
|
|
|
+import { Sensor } from '../../../api/endpoints/models/sensor';
|
|
|
|
|
+import * as moment from 'moment-timezone';
|
|
|
|
|
+import { DashboardComponent } from '../../../../dashboard/components/dashboard.component';
|
|
|
|
|
+
|
|
|
|
|
+@Component({
|
|
|
|
|
+ selector: 'app-data-download-popup',
|
|
|
|
|
+ templateUrl: './data-download-popup.component.html',
|
|
|
|
|
+ styleUrls: ['./data-download-popup.component.scss']
|
|
|
|
|
+})
|
|
|
|
|
+export class DataDownloadPopupComponent implements OnInit {
|
|
|
|
|
+
|
|
|
|
|
+ downloadForm: FormGroup;
|
|
|
|
|
+ items: FormArray;
|
|
|
|
|
+ dateFrom: Date = moment().hour(0).minutes(0).subtract(7, 'days').toDate();
|
|
|
|
|
+ dateTo: Date = moment().toDate();
|
|
|
|
|
+
|
|
|
|
|
+ inProgress: Boolean = false;
|
|
|
|
|
+ @Input() isVisible;
|
|
|
|
|
+ @Output() isVisibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
|
|
|
|
|
+ @Input() sensors: Sensor[];
|
|
|
|
|
+
|
|
|
|
|
+ constructor(
|
|
|
|
|
+ private formBuilder: FormBuilder,
|
|
|
|
|
+ private observationService: ObservationService,
|
|
|
|
|
+ private dataService: DataService,
|
|
|
|
|
+ private sensorsService: SensorsService,
|
|
|
|
|
+ private toastService: ToastService
|
|
|
|
|
+ ) {
|
|
|
|
|
+ this.initForm();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ ngOnInit(): void {
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ close() {
|
|
|
|
|
+ this.isVisibleChange.emit(false);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ initForm() {
|
|
|
|
|
+ this.downloadForm = this.formBuilder.group({
|
|
|
|
|
+ from: [this.dateFrom, Validators.required],
|
|
|
|
|
+ to: [this.dateTo, Validators.required],
|
|
|
|
|
+ sensor_id: ['', Validators.required]
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ this.dataService.getData().subscribe(data => {
|
|
|
|
|
+ if (data && data.length > 0) {
|
|
|
|
|
+ let firstUnitId: number = data[0].unit.unitId;
|
|
|
|
|
+ this.sensorsService.getUnitSensors({ unit_id: firstUnitId }).subscribe(sens => {
|
|
|
|
|
+ if (sens) {
|
|
|
|
|
+ this.sensors = sens;
|
|
|
|
|
+ }
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }, err => this.toastService.showError(err.error.message));
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Clear form
|
|
|
|
|
+ */
|
|
|
|
|
+ clearFormArray() {
|
|
|
|
|
+ const frmArray = this.downloadForm?.get('sensors') as FormArray;
|
|
|
|
|
+ if (frmArray) {
|
|
|
|
|
+ frmArray.clear();
|
|
|
|
|
+ }
|
|
|
|
|
+ this.downloadForm.reset();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * Insert unit with sensor and position if form valid
|
|
|
|
|
+ */
|
|
|
|
|
+ processDownload() {
|
|
|
|
|
+ if (this.downloadForm.valid) {
|
|
|
|
|
+
|
|
|
|
|
+ this.inProgress = true;
|
|
|
|
|
+ this.observationService.exportObservations(this.downloadForm.value).pipe(
|
|
|
|
|
+ map((response: HttpResponse<any>) => {
|
|
|
|
|
+ this.inProgress = false;
|
|
|
|
|
+ if (response.status === 200) {
|
|
|
|
|
+ console.log('Export successful');
|
|
|
|
|
+ this.saveAsFile(response.body, this.downloadForm.value.sensor_id + '.csv');
|
|
|
|
|
+ } else {
|
|
|
|
|
+ this.toastService.showError('Data download caused error!');
|
|
|
|
|
+ }
|
|
|
|
|
+ })
|
|
|
|
|
+ ).toPromise().then().catch(err => {
|
|
|
|
|
+ this.inProgress = false;
|
|
|
|
|
+ this.toastService.showError(err.error.message)
|
|
|
|
|
+ });
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ saveAsFile(data, filename) {
|
|
|
|
|
+ let file = new Blob([data], { type: 'text/plain' });
|
|
|
|
|
+ let evt = document.createEvent('MouseEvents');
|
|
|
|
|
+ let link = document.createElement('a');
|
|
|
|
|
+
|
|
|
|
|
+ if (window.navigator && window.navigator.msSaveOrOpenBlob) {
|
|
|
|
|
+ window.navigator.msSaveOrOpenBlob(file, filename);
|
|
|
|
|
+ }
|
|
|
|
|
+ else {
|
|
|
|
|
+ var e = document.createEvent('MouseEvents'),
|
|
|
|
|
+ a = document.createElement('a');
|
|
|
|
|
+
|
|
|
|
|
+ a.download = filename;
|
|
|
|
|
+ a.href = window.URL.createObjectURL(file);
|
|
|
|
|
+ a.dataset.downloadurl = ['text/plain', a.download, a.href].join(':');
|
|
|
|
|
+ e.initEvent('click', true, false);
|
|
|
|
|
+ a.dispatchEvent(e);
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|