Projekt

Obecné

Profil

Stáhnout (2.49 KB) Statistiky
| Větev: | Tag: | Revize:
1
import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
2
import {FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';
3
import {ManagementService} from '../../../shared/api/endpoints/services/management.service';
4
import * as moment from 'moment-timezone';
5
import {map} from 'rxjs/operators';
6
import {HttpResponse} from '@angular/common/http';
7
import {ToastService} from '../../../shared/services/toast.service';
8
import {SensorsService} from '../../../shared/api/endpoints/services/sensors.service';
9

    
10
@Component({
11
  selector: 'app-position-insert-popup',
12
  templateUrl: './position-insert-popup.component.html',
13
  styleUrls: ['./position-insert-popup.component.scss']
14
})
15
export class PositionInsertPopupComponent implements OnInit {
16

    
17
  insertForm: FormGroup;
18

    
19
  @Input() isVisible;
20
  @Input() unitId;
21
  @Output() isVisibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
22

    
23
  constructor(
24
    private formBuilder: FormBuilder,
25
    private sensorService: SensorsService,
26
    private toastService: ToastService,
27
  ) {
28
    this.initForm();
29
  }
30

    
31
  ngOnInit(): void {
32
  }
33
  initForm() {
34
    this.insertForm = this.formBuilder.group({
35
      lat: ['', [Validators.required, Validators.min(-90), Validators.max(90)]],
36
      lon: ['', [Validators.required, Validators.min(-180), Validators.max(180)]],
37
      alt: '',
38
      speed: '',
39
      dop: ''
40
    });
41
  }
42

    
43
  clearFormArray() {
44
    this.insertForm.reset();
45
  }
46

    
47
  /**
48
   * Send insert position request to backend adn handle response.
49
   */
50
  processInsertion() {
51
    if (this.insertForm.valid) {
52
      const lat = this.insertForm.controls.lat.value;
53
      const lon = this.insertForm.controls.lon.value;
54
      const alt = this.insertForm.controls.alt.value;
55
      const speed = this.insertForm.controls.speed.value;
56
      const dop = this.insertForm.controls.dop.value;
57

    
58
      this.sensorService.insertPosition$Response( {
59
        lat, lon, alt, speed, dop, unit_id: this.unitId, date: moment().format('yyyy-MM-DD HH:mm:ssZZ')
60
      }).pipe(
61
        map((response: HttpResponse<any>) => {
62
          if (response.status === 200) {
63
            this.toastService.showSuccessMessage('Position to unit ' + this.unitId + ' inserted!');
64
            this.close();
65
          } else {
66
            this.toastService.showError('Position insertion caused error!');
67
          }
68
        })
69
      ).toPromise().then().catch(err => this.toastService.showError(err.error.message));
70
    }
71
  }
72

    
73
  /**
74
   * Close popup
75
   */
76
  close() {
77
    this.isVisibleChange.emit(false);
78
  }
79
}
(3-3/3)