bc-info.component.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. import {
  2. Component,
  3. OnInit,
  4. ViewRef
  5. } from '@angular/core';
  6. import ExtentIteraction, { ExtentEvent } from 'ol/interaction/Extent'
  7. import { shiftKeyOnly } from 'ol/events/condition';
  8. import { transformExtent } from 'ol/proj'
  9. import {
  10. Extent,
  11. getArea
  12. } from 'ol/extent'
  13. import checkDescription from 'iroha-helpers'
  14. import {
  15. HsMapService,
  16. HsPanelComponent,
  17. HsLayoutService,
  18. getAccessRights
  19. } from 'hslayers-ng';
  20. import { BcInfoService } from './bc-info.service';
  21. @Component({
  22. selector: 'blockchain-info',
  23. templateUrl: 'bc-info.component.html',
  24. })
  25. export class BcInfoComponent implements HsPanelComponent, OnInit {
  26. private extentIteraction: ExtentIteraction;
  27. selectingArea: boolean = false;
  28. buyInProgress: boolean = false;
  29. getUserBalanceInProgress: boolean = false;
  30. user: string = 'kunickyd@test';
  31. assetId: string = "coin#test"
  32. userPrivateKey: string = '5ee90333f04f42f457f2d10f1cbd7ec870b041184074dce2b744aa17e67b1e9a';
  33. userBalance: number;
  34. extent: Extent;
  35. price: number;
  36. paymentHash: string;
  37. dataUrl: string;
  38. viewRef: ViewRef;
  39. data: any;
  40. name: string = "bc-panel";
  41. lastTransfers: Array<any> = [
  42. {
  43. hash: "dafsdfagfsdgsrggfsdfasdfgghvccvbgdfgdsf",
  44. user: "kunickyd@test",
  45. timestamp: "20.3.2021 14:35",
  46. extent: "[36, 58, 78, 65]",
  47. amount: "58.5",
  48. expanded: true
  49. },
  50. {
  51. hash: "ddafs65432df456d4fsa54as9d8f4df",
  52. user: "kunickyd@test",
  53. timestamp: "20.3.2021 14:35",
  54. extent: "[36, 58, 78, 65]",
  55. amount: "58.5",
  56. expanded: false
  57. },
  58. {
  59. hash: "54d6afs54df488d8da98f7d75dddafdf5d5f5d5f5d5fd",
  60. user: "kunickyd@test",
  61. timestamp: "20.3.2021 14:35",
  62. extent: "[36, 58, 78, 65]",
  63. amount: "58.5",
  64. expanded: true
  65. }
  66. ];
  67. constructor(
  68. private bcInfoService: BcInfoService,
  69. private mapService: HsMapService,
  70. private hsLayoutService: HsLayoutService
  71. ) {
  72. this.extentIteraction = new ExtentIteraction({ condition: shiftKeyOnly });
  73. //bind event handlers to this scope
  74. this.onMouseUp = this.onMouseUp.bind(this);
  75. this.onExtentChanged = this.onExtentChanged.bind(this);
  76. }
  77. async ngOnInit(): Promise<void> {
  78. await this.refreshUserBalance();
  79. }
  80. async refreshUserBalance(): Promise<void> {
  81. this.getUserBalanceInProgress = true;
  82. this.userBalance = await this.bcInfoService.getUserBalance(this.user, this.userPrivateKey, this.assetId);
  83. this.getUserBalanceInProgress = false;
  84. }
  85. isVisible(): boolean {
  86. return this.hsLayoutService.panelVisible(this.name);
  87. }
  88. onSelectArea(): void {
  89. let map = this.mapService.getMap();
  90. this.selectingArea = true;
  91. map.getTargetElement().addEventListener("mouseup", this.onMouseUp);
  92. this.extentIteraction.on("extentchanged", this.onExtentChanged);
  93. map.addInteraction(this.extentIteraction);
  94. }
  95. async onBuy(): Promise<void> {
  96. if (!confirm("You sure?")) {
  97. return;
  98. }
  99. this.buyInProgress = true;
  100. let transformedExtent = transformExtent(this.extent, this.mapService.getMap().getView().getProjection(), "EPSG:4326");
  101. transformedExtent = this.roundExtentCoords(transformedExtent);
  102. //console.log(transformedExtent);
  103. this.paymentHash = await this.bcInfoService.transferAssets(this.user, this.userPrivateKey, this.assetId, transformedExtent, this.price);
  104. this.refreshUserBalance();
  105. this.dataUrl = await this.bcInfoService.requestData(this.paymentHash, this.user);//"https://eo.lesprojekt.cz/produkty/S2/2020/S2A_MSIL2A_20200422T095031_N9999_R079_T33UXQ_20201127T165009_NDVI.tif"
  106. this.buyInProgress = false;
  107. }
  108. onCancel(): void {
  109. let map = this.mapService.getMap();
  110. this.extent = this.price = this.paymentHash = this.dataUrl = null;
  111. this.extentIteraction.setExtent(null);
  112. map.getTargetElement().removeEventListener("mouseup", this.onMouseUp); //This doesnt seem to work...
  113. this.extentIteraction.un("extentchanged", this.onExtentChanged);
  114. map.removeInteraction(this.extentIteraction);
  115. this.selectingArea = false;
  116. }
  117. async onMouseUp(event): Promise<void> {
  118. if (this.extent) {
  119. this.price = this.roundTwoDecimalPlaces(await this.bcInfoService.getPrice(getArea(this.extent))); //round the price => allow more decimal places in iroha?
  120. }
  121. }
  122. onExtentChanged(event: ExtentEvent): any {
  123. if (event.extent) {
  124. this.extent = this.roundExtentCoords(event.extent); //!! workaround for fixed description length to 64 chars in iroha-helpers !!
  125. }
  126. }
  127. onRequestData(){
  128. window.open("https://eo.lesprojekt.cz/produkty/S2/2020/S2A_MSIL2A_20200422T095031_N9999_R079_T33UXQ_20201127T165009_NDVI.tif");
  129. }
  130. onTest() {
  131. //this.bcInfoService.getUserTransactions(this.user, this.userPrivateKey);
  132. //console.log(this.mapService.getMap().getView().getProjection());
  133. }
  134. //UTILS SECTION - should be separeted in solo file
  135. private roundExtentCoords(extent: Extent): Extent {
  136. let roundedExtent: number[] = new Array<number>(4);
  137. for (let i = 0; i !== extent.length; i++) {
  138. roundedExtent[i] = this.roundTwoDecimalPlaces(extent[i]);
  139. }
  140. return roundedExtent;
  141. }
  142. private roundTwoDecimalPlaces(num: number): number {
  143. return Math.round((num + Number.EPSILON) * 100) / 100;
  144. }
  145. }