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 height = 0; private memoList: string[] = []; private addr: string = ''; private _heightUpdated: BehaviorSubject = new BehaviorSubject(this.height); private _memoUpdated: BehaviorSubject = new BehaviorSubject(this.memoList); private _addrUpdated: BehaviorSubject = new BehaviorSubject(this.addr); public readonly addrUpdate: Observable = this._addrUpdated.asObservable(); public readonly heightUpdate: Observable = this._heightUpdated.asObservable(); public readonly memoUpdate: Observable = this._memoUpdated.asObservable(); private UserSub: Subscription = new Subscription(); constructor(private http: HttpClient, public userService: UserService){ } getHeight(){ let obs = this.http.get<{message: string, height: number}>('http://localhost:3000/api/blockheight'); obs.subscribe((BlockData) => { this.height = BlockData.height; this._heightUpdated.next(this.height); }); return obs; } //getHeightUpdateListener() { //return this.heightUpdated; //} 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}`); this.userService.getUser(session); this.UserSub = this.userService.getZaddrUpdateListener(). subscribe((zAddr) => { if (zAddr.length == 0) { console.log('FS: getMemos new adding user'); this.userService.addUser(address, session, blocktime); } else { console.log('FS: getMemos found user'); } }); } } } this.memoList = memos; this._memoUpdated.next(this.memoList); }); } //getMemoUpdateListener() { //return this.memoUpdated; //} getAddr() { this.http.get<{message: string, addr: string}>('http://localhost:3000/api/getaddr'). subscribe((AddrData) => { this.addr = AddrData.addr; this._addrUpdated.next(this.addr); }); } //getAddrUpdateListener() { //return this.addrUpdated; //} }