dashboard.component.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. import { ActivatedRoute, Router } from '@angular/router';
  2. import { Component, OnInit, Input, Output, EventEmitter, OnDestroy, NgZone } from '@angular/core';
  3. import { Plant } from 'src/app/models/plant';
  4. import { PlantsService } from 'src/app/services/plants.service';
  5. import { OrganizationsService } from 'src/app/services/organizations.service';
  6. import { latLng, tileLayer, marker, Layer, icon, Map, latLngBounds, LatLng, point } from 'leaflet';
  7. import { of as observableOf, Observable, throwError, from } from 'rxjs';
  8. import * as moment from 'moment';
  9. import Swal from 'sweetalert2';
  10. @Component({
  11. selector: 'app-dashboard',
  12. templateUrl: './dashboard.component.html',
  13. styleUrls: ['./dashboard.component.scss']
  14. })
  15. export class DashboardComponent implements OnInit {
  16. title = "Dashboard";
  17. listData: any;
  18. rows = [];
  19. markers: Layer[] = [];
  20. points: LatLng[] = [];
  21. listOrganizations: any;
  22. error: boolean;
  23. plantId: string;
  24. plant: any;
  25. sub: any;
  26. plantNotFound: boolean;
  27. selectedPlant: any;
  28. totalMetersInstalled: string;
  29. lastUpdate = new Date().toLocaleString();
  30. i: number;
  31. // Leajflet map
  32. icon = icon({
  33. iconSize: [25, 41],
  34. iconAnchor: [13, 41],
  35. iconUrl: 'assets/img/marker-icon.png',
  36. //shadowUrl: 'marker-shadow.png'
  37. });
  38. // Open Street Map definitions
  39. LAYER_OSM = tileLayer(
  40. 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
  41. {
  42. maxZoom: 18,
  43. attribution: '© OpenStreetMap contributors'
  44. }
  45. );
  46. // Values to bind to Leaflet Directive
  47. options = {
  48. layers: [this.LAYER_OSM],
  49. zoom: 10,
  50. center: latLng([13.661714, -89.251530])
  51. };
  52. constructor(
  53. private plantsService: PlantsService,
  54. private route: ActivatedRoute,
  55. private orgService: OrganizationsService,
  56. private router: Router,
  57. private zone: NgZone) {
  58. Swal.fire({
  59. allowOutsideClick: false,
  60. type: 'info',
  61. text: 'Espere por favor...'
  62. });
  63. Swal.showLoading();
  64. this.plantsService.getAssets().subscribe(res => {
  65. this.listData = res["assets"];
  66. }, (err) => {
  67. Swal.fire({
  68. type: 'error',
  69. title: 'Error en el servidor',
  70. text: "No su pudo obtener la informacion"
  71. });
  72. });
  73. if (localStorage.getItem("installedCapacityTotal_kW") == undefined) {
  74. this.plantsService.getInstalledCapacity().subscribe(res => {
  75. this.totalMetersInstalled = res["installedCapacityTotal_kW"];
  76. localStorage.setItem("totalMetersInstalled", this.totalMetersInstalled);
  77. });
  78. }
  79. else {
  80. this.totalMetersInstalled = localStorage.getItem("installedCapacityTotal_kW");
  81. }
  82. }
  83. ngOnInit(): void {
  84. var responsiveOptions: any[] = [
  85. ['screen and (max-width: 640px)', {
  86. seriesBarDistance: 5,
  87. axisX: {
  88. labelInterpolationFnc: function (value) {
  89. return value[0];
  90. }
  91. }
  92. }]
  93. ];
  94. setTimeout(() => {
  95. if (this.listData != undefined) {
  96. this.addMarkers();
  97. }
  98. Swal.close();
  99. }, 2500);
  100. }
  101. getAsset(id: string) {
  102. return observableOf(this.listData.find(e => e.id === id));
  103. }
  104. addMarkers() {
  105. let lat, long, address, name2;
  106. for (const plant of this.listData) {
  107. if (localStorage.getItem("email") == "inverlec@grupomerelec.com") {
  108. lat = "13.6613819";
  109. long = "-89.2514334";
  110. address = "Urbanización Madre Selva Calle Llama del Bosque, Edificio Avante, Local 4-5/4-6, Antiguo Cuscatlán";
  111. name2 = "Inversiones MERELEC S.A de C.V";
  112. }
  113. else {
  114. lat = plant.latitud;
  115. long = plant.longitud;
  116. address = plant.address;
  117. name2 = plant.name;
  118. }
  119. const newMarker = marker(
  120. [lat, long],
  121. { icon: this.icon })
  122. .bindPopup('<b>' + name2 + '</b><br>Dirección: ' + address)
  123. .on('click', () => {
  124. this.zone.run(() => {
  125. this.sendPlantId(plant.id);
  126. });
  127. });
  128. this.points.push(latLng([lat, long]));
  129. this.markers.push(newMarker);
  130. }
  131. }
  132. sendPlantId(id: string) {
  133. this.plantId = id;
  134. if (localStorage.getItem("email") == "inverlec@grupomerelec.com") {
  135. this.selectedPlant = {
  136. name: "Inversiones MERELEC S.A de C.V",
  137. country: "El Salvador",
  138. city: "La Libertad",
  139. address: "Urbanización Madre Selva Calle Llama del Bosque, Edificio Avante, Local 4-5/4-6, Antiguo Cuscatlán",
  140. installedCapacity_kW: 300
  141. }
  142. }
  143. else {
  144. this.selectedPlant = this.listData.find(e => e.id === this.plantId);
  145. }
  146. }
  147. onMapReady(map: Map) {
  148. setTimeout(() => {
  149. map.invalidateSize();
  150. }, 0);
  151. const bounds = latLngBounds(this.points);
  152. map.fitBounds(bounds, {
  153. padding: point(24, 24),
  154. maxZoom: 9.5,
  155. animate: true
  156. });
  157. }
  158. goToAsset(id: string) {
  159. this.router.navigate(['/assets'], { queryParams: { id: id } });
  160. }
  161. }