2021-10-15 19:14:49 +00:00
|
|
|
import {Injectable} from '@angular/core';
|
2021-10-20 20:51:14 +00:00
|
|
|
import {Subject, Subscription, BehaviorSubject, Observable} from 'rxjs';
|
2021-11-09 18:39:16 +00:00
|
|
|
import {HttpClient, HttpParams, HttpHeaders} from '@angular/common/http';
|
2021-10-15 19:14:49 +00:00
|
|
|
import {UserService} from './user.service';
|
2021-11-22 20:37:45 +00:00
|
|
|
import { Owner } from './owner.model';
|
2021-10-15 19:14:49 +00:00
|
|
|
|
2022-07-12 18:22:17 +00:00
|
|
|
import { ConfigData } from './configdata';
|
|
|
|
|
2022-05-18 20:51:39 +00:00
|
|
|
var Buffer = require('buffer/').Buffer;
|
2021-10-15 19:14:49 +00:00
|
|
|
//import {User} from './user.model';
|
|
|
|
|
|
|
|
@Injectable({providedIn: 'root'})
|
|
|
|
export class FullnodeService{
|
2022-07-12 18:22:17 +00:00
|
|
|
beUrl = ConfigData.Be_URL;
|
2021-10-22 22:20:36 +00:00
|
|
|
private dataStore: { height: number, memoList: string[], addr: string, price: number } = { height: 0, memoList: [], addr: '', price:0 };
|
2021-10-21 15:23:58 +00:00
|
|
|
private _heightUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.height);
|
|
|
|
private _memoUpdated: BehaviorSubject<string[]> = new BehaviorSubject(this.dataStore.memoList);
|
|
|
|
private _addrUpdated: BehaviorSubject<string> = new BehaviorSubject(this.dataStore.addr);
|
2021-10-22 22:20:36 +00:00
|
|
|
private _priceUpdated: BehaviorSubject<number> = new BehaviorSubject(this.dataStore.price);
|
2021-10-20 20:51:14 +00:00
|
|
|
public readonly addrUpdate: Observable<string> = this._addrUpdated.asObservable();
|
|
|
|
public readonly heightUpdate: Observable<number> = this._heightUpdated.asObservable();
|
|
|
|
public readonly memoUpdate: Observable<string[]> = this._memoUpdated.asObservable();
|
2021-10-22 22:20:36 +00:00
|
|
|
public readonly priceUpdate: Observable<number> = this._priceUpdated.asObservable();
|
2021-11-22 20:37:45 +00:00
|
|
|
public readonly ownerUpdate: Observable<Owner>;
|
2021-10-20 20:51:14 +00:00
|
|
|
private UserSub: Subscription = new Subscription();
|
2021-11-09 18:39:16 +00:00
|
|
|
private reqHeaders: HttpHeaders;
|
2021-11-22 20:37:45 +00:00
|
|
|
private owner: Owner = {
|
|
|
|
_id: '',
|
|
|
|
name: '',
|
|
|
|
address: '',
|
|
|
|
currency: 'usd',
|
|
|
|
tax: false,
|
|
|
|
taxValue: 0,
|
|
|
|
vat: false,
|
2022-01-17 20:49:08 +00:00
|
|
|
vatValue: 0,
|
2022-01-28 20:03:35 +00:00
|
|
|
first: '',
|
|
|
|
last: '',
|
2022-01-17 20:49:08 +00:00
|
|
|
email: '',
|
|
|
|
street: '',
|
|
|
|
city: '',
|
|
|
|
state: '',
|
|
|
|
postal: '',
|
|
|
|
phone: '',
|
2022-01-18 22:40:20 +00:00
|
|
|
paid: false,
|
2022-01-19 16:26:25 +00:00
|
|
|
website: '',
|
2022-03-07 17:14:29 +00:00
|
|
|
country: '',
|
2022-05-18 20:51:39 +00:00
|
|
|
zats: false,
|
|
|
|
invoices: false,
|
2022-07-14 16:11:04 +00:00
|
|
|
expiration: new Date(Date.now()).toISOString(),
|
2022-07-18 20:29:27 +00:00
|
|
|
payconf: false,
|
2022-08-31 13:56:06 +00:00
|
|
|
viewkey: '',
|
|
|
|
crmToken: ''
|
2021-11-22 20:37:45 +00:00
|
|
|
};
|
2021-10-15 19:14:49 +00:00
|
|
|
|
|
|
|
constructor(private http: HttpClient, public userService: UserService){
|
2022-07-12 18:22:17 +00:00
|
|
|
var auth = 'Basic ' + Buffer.from(ConfigData.UsrPwd).toString('base64');
|
2022-05-18 20:51:39 +00:00
|
|
|
this.reqHeaders = new HttpHeaders().set('Authorization', auth);
|
2021-11-22 20:37:45 +00:00
|
|
|
this.ownerUpdate = userService.ownerUpdate;
|
2021-10-21 15:23:58 +00:00
|
|
|
this.getAddr();
|
|
|
|
this.getHeight();
|
2021-11-22 20:37:45 +00:00
|
|
|
this.ownerUpdate.subscribe((owner) => {
|
|
|
|
this.owner = owner;
|
|
|
|
this.getPrice(this.owner.currency);
|
|
|
|
});
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-10-20 20:51:14 +00:00
|
|
|
getHeight(){
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.get<{message: string, height: number}>(this.beUrl+'api/blockheight', { headers: this.reqHeaders });
|
2021-10-20 20:51:14 +00:00
|
|
|
obs.subscribe((BlockData) => {
|
2021-10-21 15:23:58 +00:00
|
|
|
this.dataStore.height = BlockData.height;
|
|
|
|
this._heightUpdated.next(Object.assign({}, this.dataStore).height);
|
2021-10-15 19:14:49 +00:00
|
|
|
});
|
|
|
|
|
2021-10-20 20:51:14 +00:00
|
|
|
return obs;
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
|
2021-11-22 20:37:45 +00:00
|
|
|
getPrice(currency: string){
|
|
|
|
//var currency = 'usd';
|
2021-11-02 21:13:24 +00:00
|
|
|
const params = new HttpParams().append('currency', currency);
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.get<{message: string, price: any}>(this.beUrl+'api/price', { headers:this.reqHeaders, params: params, observe: 'response'});
|
2021-10-22 22:20:36 +00:00
|
|
|
obs.subscribe((PriceData) => {
|
2021-11-02 21:13:24 +00:00
|
|
|
if (PriceData.status == 200) {
|
|
|
|
this.dataStore.price = PriceData.body!.price.price;
|
|
|
|
console.log("price", this.dataStore.price);
|
|
|
|
this._priceUpdated.next(Object.assign({},this.dataStore).price);
|
|
|
|
} else {
|
|
|
|
console.log('No price found for currency', currency);
|
|
|
|
}
|
2021-10-22 22:20:36 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
return obs;
|
|
|
|
}
|
2021-10-20 20:51:14 +00:00
|
|
|
|
2021-10-15 19:14:49 +00:00
|
|
|
getAddr() {
|
2021-11-09 18:39:16 +00:00
|
|
|
let obs = this.http.get<{message: string, addr: string}>(this.beUrl+'api/getaddr', { headers: this.reqHeaders });
|
2021-10-21 15:23:58 +00:00
|
|
|
|
|
|
|
obs.subscribe((AddrData) => {
|
|
|
|
this.dataStore.addr = AddrData.addr;
|
|
|
|
this._addrUpdated.next(Object.assign({}, this.dataStore).addr);
|
2021-10-15 19:14:49 +00:00
|
|
|
});
|
2021-10-21 15:23:58 +00:00
|
|
|
|
|
|
|
return obs;
|
2021-10-15 19:14:49 +00:00
|
|
|
}
|
|
|
|
}
|