import {Injectable} from '@angular/core'; import {Subject, Subscription, BehaviorSubject, Observable} from 'rxjs'; import {HttpClient, HttpParams} from '@angular/common/http'; import {UserService} from './user.service'; //import {User} from './user.model'; @Injectable({providedIn: 'root'}) export class FullnodeService{ private dataStore: { height: number, memoList: string[], addr: string, price: number } = { height: 0, memoList: [], addr: '', price:0 }; private _heightUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.height); private _memoUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.memoList); private _addrUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.addr); private _priceUpdated: BehaviorSubject = new BehaviorSubject(this.dataStore.price); public readonly addrUpdate: Observable = this._addrUpdated.asObservable(); public readonly heightUpdate: Observable = this._heightUpdated.asObservable(); public readonly memoUpdate: Observable = this._memoUpdated.asObservable(); public readonly priceUpdate: Observable = this._priceUpdated.asObservable(); private UserSub: Subscription = new Subscription(); constructor(private http: HttpClient, public userService: UserService){ this.getAddr(); this.getHeight(); this.getMemos(); this.getPrice(); } getHeight(){ let obs = this.http.get<{message: string, height: number}>('http://localhost:3000/api/blockheight'); obs.subscribe((BlockData) => { this.dataStore.height = BlockData.height; this._heightUpdated.next(Object.assign({}, this.dataStore).height); }); return obs; } getPrice(){ let obs = this.http.get<{message: string, price: number}>('http://localhost:3000/api/price'); obs.subscribe((PriceData) => { this.dataStore.price = PriceData.price; console.log(this.dataStore.price); this._priceUpdated.next(Object.assign({},this.dataStore).price); }); return obs; } hexToString(hexString: string) { var str = ''; for (var n=0; n < hexString.length; n +=2) { str += String.fromCharCode(parseInt(hexString.substr(n, 2), 16)); } return str; } getMemos() { this.http.get<{message: string, txs: any}>('http://localhost:3000/api/txs'). subscribe((TxData) => { var memos: string[] = []; //this.addr = TxData.addr; var re = /.*S4ZEC::(.*)\sReply-To:\s(z\w+)/; for (var i=0; i < TxData.txs.length; i++) { var memo = this.hexToString(TxData.txs[i].memo); //console.log(TxData.txs[i].blocktime); if (re.test(memo)){ memos.push(memo); var match = re.exec(memo); if (match != null) { var address = match[2]; var session = match[1]; var blocktime = TxData.txs[i].blocktime; console.log(memo); console.log(`Searching for user for ${session}`); const params = new HttpParams().append('session', session); this.http.get<{message: string, user: any}>('http://localhost:3000/api/getuser', { headers:{}, params: params, observe: 'response'}). subscribe((UserDataResponse) => { console.log(UserDataResponse.status); if (UserDataResponse.status == 200){ console.log(`FS: Found user`); } else { console.log('FS: Did not find user'); this.userService.addUser(address, session, blocktime); } }); } } } this.dataStore.memoList = memos; this._memoUpdated.next(Object.assign({},this.dataStore).memoList); }); } getAddr() { let obs = this.http.get<{message: string, addr: string}>('http://localhost:3000/api/getaddr'); obs.subscribe((AddrData) => { this.dataStore.addr = AddrData.addr; this._addrUpdated.next(Object.assign({}, this.dataStore).addr); }); return obs; } }