Преглед изворни кода

transform extent for data extraction & experiments

kunickyd пре 3 година
родитељ
комит
df7ac201c6

+ 1 - 0
src/app/bc-info/bc-info.component.html

@@ -8,6 +8,7 @@ Data URL: {{bcInfoService.dataUrl}} -->
 <div [hidden]="!isVisible()" class="mainpanel" style="overflow: auto;">
     <hs-panel-header name="bc-panel" [title]="'Blockchain shop'"></hs-panel-header>
     <div class="card-body">
+        <button type="button" class="btn btn-danger" (click)="onTest()">Test</button>
         <span>
             <p><b>User:</b> {{user}}</p>
             <p><b>Asset:</b> {{assetId}}</p>

+ 23 - 13
src/app/bc-info/bc-info.component.ts

@@ -6,6 +6,7 @@ import {
 
 import ExtentIteraction, { ExtentEvent } from 'ol/interaction/Extent'
 import { shiftKeyOnly } from 'ol/events/condition';
+import { transformExtent } from 'ol/proj'
 import {
   Extent,
   getArea
@@ -55,14 +56,14 @@ export class BcInfoComponent implements HsPanelComponent, OnInit {
 
     //bind event handlers to this scope
     this.onMouseUp = this.onMouseUp.bind(this);
-    this.onExtentChanged = this.onExtentChanged.bind(this);    
+    this.onExtentChanged = this.onExtentChanged.bind(this);
   }
 
   async ngOnInit(): Promise<void> {
     await this.refreshUserBalance();
   }
 
-  async refreshUserBalance(): Promise<void>{
+  async refreshUserBalance(): Promise<void> {
     this.getUserBalanceInProgress = true;
     this.userBalance = await this.bcInfoService.getUserBalance(this.user, this.userPrivateKey, this.assetId);
     this.getUserBalanceInProgress = false;
@@ -84,24 +85,28 @@ export class BcInfoComponent implements HsPanelComponent, OnInit {
   }
 
   async onBuy(): Promise<void> {
-    if(!confirm("You sure?")){
+    if (!confirm("You sure?")) {
       return;
     }
 
     this.buyInProgress = true;
 
-    this.paymentHash = await this.bcInfoService.transferAssets(this.user, this.userPrivateKey, this.assetId, this.extent, this.price);
+    let transformedExtent = transformExtent(this.extent, this.mapService.getMap().getView().getProjection(), "EPSG:4326");
+    transformedExtent = this.roundExtentCoords(transformedExtent);
+    //console.log(transformedExtent);
+
+    this.paymentHash = await this.bcInfoService.transferAssets(this.user, this.userPrivateKey, this.assetId, transformedExtent, this.price);
     this.refreshUserBalance();
-    this.dataUrl = "https://eo.lesprojekt.cz/produkty/S2/2020/S2A_MSIL2A_20200422T095031_N9999_R079_T33UXQ_20201127T165009_NDVI.tif" //await this.bcInfoService.requestData(this.paymentHash, this.user);
+    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"
 
-    this.buyInProgress = false; 
+    this.buyInProgress = false;
   }
 
   onCancel(): void {
     let map = this.mapService.getMap();
 
     this.extent = this.price = this.paymentHash = this.dataUrl = null;
-    this.extentIteraction.setExtent(null);    
+    this.extentIteraction.setExtent(null);
 
     map.getTargetElement().removeEventListener("mouseup", this.onMouseUp); //This doesnt seem to work...
     this.extentIteraction.un("extentchanged", this.onExtentChanged);
@@ -113,28 +118,33 @@ export class BcInfoComponent implements HsPanelComponent, OnInit {
 
   async onMouseUp(event): Promise<void> {
     if (this.extent) {
-      this.price =  this.roundTwoDecimalPlaces(await this.bcInfoService.getPrice(getArea(this.extent))); //round the price => allow more decimal places in iroha?
+      this.price = this.roundTwoDecimalPlaces(await this.bcInfoService.getPrice(getArea(this.extent))); //round the price => allow more decimal places in iroha?
     }
   }
 
   onExtentChanged(event: ExtentEvent): any {
-    if(event.extent){
+    if (event.extent) {
       this.extent = this.roundExtentCoords(event.extent); //!! workaround for fixed description length to 64 chars in iroha-helpers !!
-    }    
+    }
+  }
+
+  onTest() {
+    this.bcInfoService.getUserTransactions(this.user, this.userPrivateKey);
+    //console.log(this.mapService.getMap().getView().getProjection());
   }
 
   //UTILS SECTION - should be separeted in solo file
-  private roundExtentCoords(extent: Extent): Extent{
+  private roundExtentCoords(extent: Extent): Extent {
     let roundedExtent: number[] = new Array<number>(4);
 
-    for(let i = 0; i !== extent.length; i++ ){
+    for (let i = 0; i !== extent.length; i++) {
       roundedExtent[i] = this.roundTwoDecimalPlaces(extent[i]);
     }
 
     return roundedExtent;
   }
 
-  private roundTwoDecimalPlaces(num: number): number{
+  private roundTwoDecimalPlaces(num: number): number {
     return Math.round((num + Number.EPSILON) * 100) / 100;
   }
 }

+ 40 - 12
src/app/bc-info/bc-info.service.ts

@@ -9,14 +9,14 @@ import { AppConfigService, BcConfig } from '../app.config.service';
 import { Buffer } from 'buffer';
 
 @Injectable({ providedIn: 'root' })
-export class BcInfoService {  
+export class BcInfoService {
   private readonly commandService: CommandService;
   private readonly queryService: QueryService;
   private readonly basicAuthHeaders: HttpHeaders;
 
   private readonly _bcConfig: BcConfig;
 
-  
+
   // extent = [ //bottom left and top right point should be enough
   //   [48.5, 17],
   //   [49, 17],
@@ -24,7 +24,7 @@ export class BcInfoService {
   //   [48.5, 16.5]
   // ];
 
-  constructor(private httpClient: HttpClient, private appSettings: AppConfigService ) {
+  constructor(private httpClient: HttpClient, private appSettings: AppConfigService) {
     this._bcConfig = appSettings.config;
 
     this.commandService = new CommandService(this._bcConfig.IROHA_ADDRESS);
@@ -50,14 +50,42 @@ export class BcInfoService {
         pageSize: 10
       });
 
-      let balance: number = parseFloat(data[0].balance);      
+      let balance: number = parseFloat(data[0].balance);
       return balance;
 
-    } catch (err) {      
+    } catch (err) {
       console.error(err)
     }
   }
 
+  async getUserTransactions(user: string, userPrivateKey: string) {
+    let queryOptions = {
+      privateKey: userPrivateKey,
+      creatorAccountId: user,
+      queryService: this.queryService,
+      timeoutLimit: 10000
+    };
+
+    let response = await queries.getAccountTransactions(queryOptions,
+      {
+        accountId: user,
+        pageSize: 10,
+        firstTxHash: undefined,
+        ordering:
+        {          
+          field: undefined,
+          direction: undefined
+        },
+        firstTxTime: undefined,
+        lastTxTime: undefined,
+        firstTxHeight: undefined,
+        lastTxHeight: undefined,
+      }
+    );
+
+    console.log(response);
+  }
+
   async transferAssets(user: string, userPrivateKey: string, assetId: string, extent: number[], amount: number): Promise<string> {
 
     let commandOptions = {
@@ -75,7 +103,7 @@ export class BcInfoService {
         destAccountId: this._bcConfig.ADMIN_USER,
         assetId,
         description: JSON.stringify({ extent }),
-        amount: String(amount), 
+        amount: String(amount),
       });
 
       return response["txHash"][0];
@@ -85,15 +113,15 @@ export class BcInfoService {
     }
   }
 
-  async getPrice(area: number) : Promise<number>{
-    let response = await this.httpClient.post(this._bcConfig.CHAIN4ALL_SERVICE_URL + '/price', { area }, { headers: this.basicAuthHeaders} ).toPromise();
-    
+  async getPrice(area: number): Promise<number> {
+    let response = await this.httpClient.post(this._bcConfig.CHAIN4ALL_SERVICE_URL + '/price', { area }, { headers: this.basicAuthHeaders }).toPromise();
+
     return response["price"];
   }
 
-  async requestData(paymentHash: string, user: string) : Promise<string>{
-    let response = await this.httpClient.post(this._bcConfig.CHAIN4ALL_SERVICE_URL + '/buy', { txHash: paymentHash, user }, { headers: this.basicAuthHeaders}).toPromise();
-    
+  async requestData(paymentHash: string, user: string): Promise<string> {
+    let response = await this.httpClient.post(this._bcConfig.CHAIN4ALL_SERVICE_URL + '/buy', { txHash: paymentHash, user }, { headers: this.basicAuthHeaders }).toPromise();
+
     return response["dataUrl"];
   }
 }