dashboard.component.ts 5.1 KB

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