import { Injectable } from '@angular/core'; import { Observable, Subject } from 'rxjs'; import { take, map, share, filter, tap } from 'rxjs/operators'; import { ProtocolService } from '@ucap-webmessenger/protocol'; import { SVC_TYPE_OPTION, SSVC_TYPE_OPTION_VIEW_REQ, SSVC_TYPE_OPTION_UPD_REQ, SSVC_TYPE_OPTION_REG_VIEW_REQ, SSVC_TYPE_OPTION_REG_UPD_REQ, SSVC_TYPE_OPTION_CALL_VIEW_REQ, SSVC_TYPE_OPTION_CALL_UPD_REQ, SSVC_TYPE_OPTION_REG_UPD_RES } from '../types/service'; import { encodeView, decodeView, ViewRequest, ViewResponse } from '../protocols/view'; import { UpdateRequest, UpdateResponse, encodeUpdate, decodeUpdate } from '../protocols/update'; import { RegViewRequest, RegViewResponse, encodeRegView, decodeRegView } from '../protocols/reg-view'; import { RegUpdateRequest, RegUpdateResponse, encodeRegUpdate, decodeRegUpdate, decodeRegUpdateNotification, RegUpdateNotification } from '../protocols/reg-update'; import { CallViewRequest, CallViewResponse, encodeCallView, decodeCallView } from '../protocols/call-view'; import { CallUpdateRequest, CallUpdateResponse, encodeCallUpdate, decodeCallUpdate } from '../protocols/call-update'; type Notifications = RegUpdateNotification; @Injectable({ providedIn: 'root' }) export class OptionProtocolService { private notificationSubject: Subject; public notification$: Observable; constructor(private protocolService: ProtocolService) { this.notificationSubject = new Subject(); this.notification$ = this.notificationSubject.asObservable().pipe(share()); this.protocolService.serverMessage .pipe( filter(message => message.serviceType === SVC_TYPE_OPTION), tap(message => { switch (message.subServiceType) { case SSVC_TYPE_OPTION_REG_UPD_RES: { this.notificationSubject.next( decodeRegUpdateNotification(message) ); } break; default: break; } }) ) .subscribe(); } public view(req: ViewRequest): Observable { return this.protocolService .call(SVC_TYPE_OPTION, SSVC_TYPE_OPTION_VIEW_REQ, ...encodeView(req)) .pipe( take(1), map(res => decodeView(res)) ); } public update(req: UpdateRequest): Observable { return this.protocolService .call(SVC_TYPE_OPTION, SSVC_TYPE_OPTION_UPD_REQ, ...encodeUpdate(req)) .pipe( take(1), map(res => decodeUpdate(res)) ); } public regView(req: RegViewRequest): Observable { return this.protocolService .call( SVC_TYPE_OPTION, SSVC_TYPE_OPTION_REG_VIEW_REQ, ...encodeRegView(req) ) .pipe( take(1), map(res => decodeRegView(res)) ); } public regUpdate(req: RegUpdateRequest): Observable { return this.protocolService .call( SVC_TYPE_OPTION, SSVC_TYPE_OPTION_REG_UPD_REQ, ...encodeRegUpdate(req) ) .pipe( take(1), map(res => decodeRegUpdate(res)) ); } public callView(req: CallViewRequest): Observable { return this.protocolService .call( SVC_TYPE_OPTION, SSVC_TYPE_OPTION_CALL_VIEW_REQ, ...encodeCallView(req) ) .pipe( take(1), map(res => decodeCallView(res)) ); } public callUpdate(req: CallUpdateRequest): Observable { return this.protocolService .call( SVC_TYPE_OPTION, SSVC_TYPE_OPTION_CALL_UPD_REQ, ...encodeCallUpdate(req) ) .pipe( take(1), map(res => decodeCallUpdate(res)) ); } }