dashboard.component.ts 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. import { ActivatedRoute, Router } from "@angular/router";
  2. import { Component, OnInit, NgZone, ViewChild } from "@angular/core";
  3. import { Plant } from "src/app/models/plant";
  4. import { Rate } from "src/app/models/rate";
  5. import { PlantsService } from "src/app/services/plants.service";
  6. import { OrganizationsService } from "src/app/services/organizations.service";
  7. import {
  8. latLng,
  9. tileLayer,
  10. marker,
  11. Layer,
  12. icon,
  13. Map,
  14. latLngBounds,
  15. LatLng,
  16. point,
  17. } from "leaflet";
  18. import { MatPaginator } from "@angular/material/paginator";
  19. import { MatSort } from "@angular/material/sort";
  20. import { MatTableDataSource } from "@angular/material/table";
  21. import { AuthService } from "@app/services/auth2.service";
  22. import { of as observableOf, Observable, throwError, from } from "rxjs";
  23. import * as moment from "moment";
  24. import Swal from "sweetalert2";
  25. import { RatesService } from "@app/services/rates.service";
  26. @Component({
  27. selector: "app-dashboard",
  28. templateUrl: "./dashboard.component.html",
  29. styleUrls: ["./dashboard.component.scss"],
  30. })
  31. export class DashboardComponent implements OnInit {
  32. title = "Dashboard";
  33. listData: any;
  34. rows = [];
  35. markers: Layer[] = [];
  36. points: LatLng[] = [];
  37. listAssets: any;
  38. dataSource = new MatTableDataSource(this.listAssets);
  39. displayedColumns: string[] = ["name", "country", "city", "id"];
  40. error: boolean;
  41. listOrganizations: any;
  42. plantId: string;
  43. plant: any;
  44. sub: any;
  45. plantNotFound: boolean;
  46. selectedPlant: any;
  47. meterKeys: any;
  48. meterKeys2: any;
  49. assetKeys: any;
  50. sumarize: number = 0;
  51. totalInstalled: number = 0;
  52. totalMetersInstalled: string;
  53. lastUpdate = new Date().toLocaleString();
  54. i: number;
  55. // Leajflet map
  56. icon = icon({
  57. iconSize: [25, 41],
  58. iconAnchor: [13, 41],
  59. iconUrl: "assets/img/marker-icon.png",
  60. //shadowUrl: 'marker-shadow.png'
  61. });
  62. // Open Street Map definitions
  63. LAYER_OSM = tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
  64. maxZoom: 18,
  65. attribution: "© OpenStreetMap contributors",
  66. });
  67. // Values to bind to Leaflet Directive
  68. options = {
  69. layers: [this.LAYER_OSM],
  70. zoom: 10,
  71. center: latLng([13.661714, -89.25153]),
  72. };
  73. @ViewChild(MatPaginator, { static: true }) paginator: MatPaginator;
  74. @ViewChild(MatSort, { static: true }) sort: MatSort;
  75. userLevel: number;
  76. totalAssetsInstalled: any;
  77. rates: Rate = {
  78. distribuidora: "-",
  79. codigo_tarifa: "-",
  80. tarifa_actual: {
  81. dateMin: null,
  82. dateMax: null,
  83. cargo_energia: null,
  84. cargo_energia_b1: null,
  85. cargo_energia_b2: null,
  86. cargo_energia_b3: null,
  87. cargo_energia_punta: null,
  88. cargo_energia_resto: null,
  89. cargo_energia_valle: null,
  90. },
  91. incremento_porcentaje: {
  92. cargo_energia: null,
  93. cargo_energia_b1: null,
  94. cargo_energia_b2: null,
  95. cargo_energia_b3: null,
  96. cargo_energia_punta: null,
  97. cargo_energia_resto: null,
  98. cargo_energia_valle: null,
  99. },
  100. };
  101. defaultRates: boolean;
  102. constructor(
  103. private plantsService: PlantsService,
  104. private route: ActivatedRoute,
  105. private orgService: OrganizationsService,
  106. private router: Router,
  107. private zone: NgZone,
  108. private authService: AuthService,
  109. private ratesService: RatesService
  110. ) {
  111. //DEMO
  112. this.userLevel = +this.authService.getUserLevel();
  113. Swal.fire({
  114. allowOutsideClick: false,
  115. type: "info",
  116. text: "Espere por favor...",
  117. });
  118. Swal.showLoading();
  119. this.plantsService.getAllAssets().subscribe(
  120. (res) => {
  121. if (res != null) {
  122. this.listAssets = res["data"]["assets"];
  123. this.dataSource.data = this.listAssets;
  124. this.dataSource.paginator = this.paginator;
  125. this.dataSource.sort = this.sort;
  126. this.assetKeys = Object.keys(this.listAssets);
  127. //DEMO
  128. if (this.userLevel == 0) {
  129. this.sumarize = 320;
  130. this.totalAssetsInstalled = 1;
  131. } else {
  132. this.totalAssetsInstalled = this.listAssets.length || 0;
  133. for (let prop in this.assetKeys) {
  134. this.meterKeys2 = Object.keys(
  135. this.listAssets.map((item) => item["meters_installed"])[prop]
  136. );
  137. if (this.meterKeys2.length > 0) {
  138. for (let prop2 in this.meterKeys2) {
  139. this.sumarize += this.listAssets
  140. .map((item) => item["meters_installed"])
  141. [prop].map((response) => response["installedCapacity_kW"])[
  142. prop2
  143. ];
  144. //this.totalMetersInstalled = this.sumarize.toString();
  145. //localStorage.setItem("installedCapacityTotal_kW", this.totalMetersInstalled);
  146. }
  147. }
  148. }
  149. }
  150. }
  151. setTimeout(() => {
  152. if (this.listAssets != undefined) {
  153. this.addMarkers();
  154. this.sendPlantId(this.listAssets[0].id);
  155. }
  156. Swal.close();
  157. }, 1800);
  158. },
  159. (err) => {
  160. Swal.fire({
  161. type: "error",
  162. title: "Error en el servidor",
  163. text: err.message,
  164. });
  165. }
  166. );
  167. }
  168. ngOnInit() {
  169. setTimeout(() => {
  170. Swal.close();
  171. }, 3000);
  172. this.ratesService.getDefaultRate().subscribe((res) => {
  173. this.rates = res["data"];
  174. this.defaultRates = true;
  175. });
  176. var responsiveOptions: any[] = [
  177. [
  178. "screen and (max-width: 640px)",
  179. {
  180. seriesBarDistance: 5,
  181. axisX: {
  182. labelInterpolationFnc: function (value) {
  183. return value[0];
  184. },
  185. },
  186. },
  187. ],
  188. ];
  189. }
  190. ngOnChanges(): void {
  191. if (this.listAssets != undefined) {
  192. this.addMarkers();
  193. this.sendPlantId(this.listAssets[0].id);
  194. }
  195. }
  196. getAsset(id: string) {
  197. return observableOf(this.listAssets.find((e) => e.id === id));
  198. }
  199. addMarkers() {
  200. let lat, long, address, name2;
  201. for (const plant of this.listAssets) {
  202. if (plant["meters_installed"].length > 0) {
  203. // DEMO
  204. if (this.userLevel == 0) {
  205. lat = "13.6613819";
  206. long = "-89.2514334";
  207. address =
  208. "Urbanización Madre Selva Calle Llama del Bosque, Edificio Avante, Local 4-5/4-6, Antiguo Cuscatlán";
  209. name2 = "Inversiones MERELEC S.A de C.V";
  210. } else {
  211. lat = plant["meters_installed"][0].gpsLat;
  212. long = plant["meters_installed"][0].gpsLong;
  213. address = plant.address;
  214. name2 = plant.name;
  215. }
  216. const newMarker = marker([lat, long], { icon: this.icon })
  217. .bindPopup("<b>" + name2 + "</b><br>Dirección: " + address)
  218. .on("click", () => {
  219. this.zone.run(() => {
  220. this.sendPlantId(plant.id);
  221. });
  222. });
  223. this.points.push(latLng([lat, long]));
  224. this.markers.push(newMarker);
  225. }
  226. }
  227. }
  228. sendPlantId(id: string) {
  229. this.plantId = id;
  230. // DEMO
  231. if (this.userLevel == 0) {
  232. this.selectedPlant = {
  233. name: "Inversiones MERELEC S.A de C.V",
  234. country: "El Salvador",
  235. city: "La Libertad",
  236. address:
  237. "Urbanización Madre Selva Calle Llama del Bosque, Edificio Avante, Local 4-5/4-6, Antiguo Cuscatlán",
  238. installedCapacity_kW: 300,
  239. };
  240. this.totalInstalled = 320;
  241. } else {
  242. this.totalInstalled = 0;
  243. this.selectedPlant = this.listAssets.find((e) => e.id === this.plantId);
  244. let keys = Object.keys(
  245. this.selectedPlant.meters_installed.map(
  246. (item) => item["meters_installed"]
  247. )
  248. );
  249. for (let prop2 in keys) {
  250. this.totalInstalled += this.selectedPlant.meters_installed.map(
  251. (response) => response["installedCapacity_kW"]
  252. )[prop2];
  253. }
  254. }
  255. }
  256. onMapReady(map: Map) {
  257. setTimeout(() => {
  258. map.invalidateSize();
  259. }, 0);
  260. const bounds = latLngBounds(this.points);
  261. //map.fitBounds(bounds, {
  262. // padding: point(24, 24),
  263. // maxZoom: 9.5,
  264. // animate: true
  265. //});
  266. }
  267. goToAsset(id: string) {
  268. this.router.navigate(["/assets"], { queryParams: { id: id } });
  269. }
  270. }