calculator.service.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. import {HttpClient} from '@angular/common/http';
  2. import {Injectable} from '@angular/core';
  3. import {Subject} from 'rxjs';
  4. import {catchError} from 'rxjs/operators';
  5. import {HsConfig} from 'hslayers-ng';
  6. import {FieldService} from './field.service';
  7. import {ZonesService} from './zones.service';
  8. export type Index = 'EVI' | 'RVI4S1';
  9. @Injectable({providedIn: 'root'})
  10. export class CalculatorService {
  11. availableDates: Array<string>;
  12. availableProducts = ['EVI', 'RVI4S1'];
  13. dateRangeSelects: Subject<{date: string}> = new Subject();
  14. dateCalendarSelects: Subject<{date: string}> = new Subject();
  15. selectedDate;
  16. //selectedProduct;
  17. serviceBaseUrl = 'https://fieldcalc.lesprojekt.cz/';
  18. private _datesLoading: boolean;
  19. private _zonesLoading: boolean;
  20. constructor(
  21. private fieldService: FieldService,
  22. private hsConfig: HsConfig,
  23. private httpClient: HttpClient,
  24. private zonesService: ZonesService
  25. ) {
  26. this.dateRangeSelects.subscribe(({date}) => {
  27. this.selectedDate = date;
  28. });
  29. }
  30. noDates(): boolean {
  31. return this.availableDates === undefined;
  32. }
  33. /**
  34. * Call 'get_dates' API method
  35. */
  36. async getDates({product}: {product: Index}) {
  37. this.availableDates = undefined;
  38. this._datesLoading = true;
  39. try {
  40. const data = await this.httpClient
  41. .get<{dates: string[]}>(
  42. (this.proxyEnabled() ? this.hsConfig.proxyPrefix : '') +
  43. this.serviceBaseUrl +
  44. 'get_dates?' +
  45. 'product=' +
  46. product +
  47. '&centroid=' +
  48. JSON.stringify(this.fieldService.getSelectedFieldCentroid())
  49. )
  50. .toPromise();
  51. this._datesLoading = false;
  52. console.log('data received!');
  53. console.log(data);
  54. this.availableDates = data.dates;
  55. /* Any previously selected date must be cleaned up
  56. * so it won't get sent to the API as a wrong param
  57. */
  58. this.selectedDate = undefined;
  59. } catch (err) {
  60. this._datesLoading = false;
  61. console.error('Somethin fucked up!');
  62. console.log(err);
  63. }
  64. }
  65. get datesLoading() {
  66. return this._datesLoading;
  67. }
  68. get zonesLoading() {
  69. return this._zonesLoading;
  70. }
  71. /**
  72. * Call 'get_zones' API method
  73. */
  74. async getZones({product}: {product: Index}) {
  75. this._zonesLoading = true;
  76. try {
  77. const data = await this.httpClient
  78. .post(
  79. (this.proxyEnabled() ? this.hsConfig.proxyPrefix : '') +
  80. this.serviceBaseUrl +
  81. 'get_zones',
  82. {
  83. product,
  84. date: this.selectedDate,
  85. format: 'geojson',
  86. geojson: this.fieldService.getSelectedFieldGeoJSON(),
  87. },
  88. {
  89. headers: {
  90. 'Content-Type': 'application/json',
  91. 'Accept': 'application/json',
  92. },
  93. }
  94. )
  95. .toPromise();
  96. console.log('data received!');
  97. console.log(data);
  98. this._zonesLoading = false;
  99. this.zonesService.updateZones(data);
  100. } catch (err) {
  101. this._zonesLoading = false;
  102. console.error('Somethin fucked up!');
  103. console.log(err);
  104. }
  105. }
  106. private proxyEnabled(): boolean {
  107. return this.hsConfig.useProxy === undefined || this.hsConfig.useProxy;
  108. }
  109. }