position-insert-popup.component.ts 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
  2. import {FormArray, UntypedFormBuilder, UntypedFormGroup, 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. @Component({
  10. selector: 'app-position-insert-popup',
  11. templateUrl: './position-insert-popup.component.html',
  12. styleUrls: ['./position-insert-popup.component.scss']
  13. })
  14. export class PositionInsertPopupComponent implements OnInit {
  15. insertForm: UntypedFormGroup;
  16. @Input() isVisible;
  17. @Input() unitId;
  18. @Output() isVisibleChange: EventEmitter<boolean> = new EventEmitter<boolean>();
  19. constructor(
  20. private formBuilder: UntypedFormBuilder,
  21. private sensorService: SensorsService,
  22. private toastService: ToastService,
  23. ) {
  24. this.initForm();
  25. }
  26. ngOnInit(): void {
  27. }
  28. initForm() {
  29. this.insertForm = this.formBuilder.group({
  30. lat: ['', [Validators.required, Validators.min(-90), Validators.max(90)]],
  31. lon: ['', [Validators.required, Validators.min(-180), Validators.max(180)]],
  32. alt: '',
  33. speed: '',
  34. dop: ''
  35. });
  36. }
  37. clearFormArray() {
  38. this.insertForm.reset();
  39. }
  40. /**
  41. * Send insert position request to backend adn handle response.
  42. */
  43. processInsertion() {
  44. if (this.insertForm.valid) {
  45. const lat = this.insertForm.controls.lat.value;
  46. const lon = this.insertForm.controls.lon.value;
  47. const alt = this.insertForm.controls.alt.value;
  48. const speed = this.insertForm.controls.speed.value;
  49. const dop = this.insertForm.controls.dop.value;
  50. this.sensorService.insertPosition$Response( {
  51. lat, lon, alt, speed, dop, unit_id: this.unitId, date: moment().format('yyyy-MM-DD HH:mm:ssZZ')
  52. }).pipe(
  53. map((response: HttpResponse<any>) => {
  54. if (response.status === 200) {
  55. this.toastService.showSuccessMessage('Position to unit ' + this.unitId + ' inserted!');
  56. this.close();
  57. } else {
  58. this.toastService.showError('Position insertion caused error!');
  59. }
  60. })
  61. ).toPromise().then().catch(err => this.toastService.showError(err.error.message));
  62. }
  63. }
  64. /**
  65. * Close popup
  66. */
  67. close() {
  68. this.isVisibleChange.emit(false);
  69. }
  70. }