import { HttpClient } from '@angular/common/http' ; import { Injectable } from '@angular/core' ; import { BehaviorSubject } from 'rxjs' ; import { API } from 'src/app/configs/api.config' ; import { UserService } from 'src/app/core/services/user.service' ; import { DataService } from 'src/app/core/services/data.service' ; import { BlinkerResponse } from 'src/app/core/model/response.model' ; @ Injectable ( { providedIn : 'root' } ) export class ShareService { get uuid ( ) { return this . dataService . auth . uuid ; } get token ( ) { return this . dataService . auth . token ; } get shareData ( ) { return this . dataService . share ; } set shareData ( data ) { this . dataService . share = data ; } get sharedDeviceList ( ) { let list = [ ] ; for ( let deviceItem of this . shareData . shared ) { list . push ( deviceItem . deviceName ) ; } return list ; } loaded = new BehaviorSubject ( false ) ; constructor ( private http , private userService , private dataService ) { } init ( ) { } shareDevice2User ( device , phone ) { let sharedata = { } ; sharedata [ device . deviceName ] = [ phone ] ; return this . http . post < BlinkerResponse > ( API . SHARE . SHARE_DEVIE , { uuid : this . uuid , token : this . token , devices : [ sharedata ] } ) . toPromise ( ) . then ( resp => { console . log ( resp ) ; if ( resp . message == 1000 ) { this . shareData = resp . detail ; return true ; } return false ; } ) . catch ( this . handleError ) ; } deleteShareDevice2User ( deviceName , uuid ) { let sharedata = { } ; sharedata [ deviceName ] = [ uuid ] ; return this . http . post < BlinkerResponse > ( API . SHARE . DEL_SHARE , { uuid : this . uuid , token : this . token , devices : JSON . stringify ( [ sharedata ] ) } ) . toPromise ( ) . then ( resp => { console . log ( resp ) ; if ( resp . message == 1000 ) { this . shareData = resp . detail ; return true ; } return false ; } ) . catch ( this . handleError ) ; } getShareList ( ) { return this . http . get < BlinkerResponse > ( API . SHARE . SHARE_LIST , { params : { uuid : this . uuid , token : this . token } } ) . toPromise ( ) . then ( resp => { console . log ( resp ) ; if ( resp . message == 1000 ) { this . shareData = resp . detail ; return true ; } return false ; } ) . catch ( this . handleError ) ; } acceptSharedDevice ( taskId ) { return this . http . post < BlinkerResponse > ( API . SHARE . ACCEPT_SHARED , { uuid : this . uuid , token : this . token , tasks : JSON . stringify ( [ taskId ] ) } ) . toPromise ( ) . then ( resp => { console . log ( resp ) ; if ( resp . message == 1000 ) { this . shareData = resp . detail ; return true ; } return false ; } ) . catch ( this . handleError ) ; } refuseSharedDevice ( taskId ) { return this . http . post < BlinkerResponse > ( API . SHARE . REFUSE_SHARED , { uuid : this . uuid , token : this . token , tasks : JSON . stringify ( [ taskId ] ) } ) . toPromise ( ) . then ( resp => { console . log ( resp ) ; if ( resp . message == 1000 ) { this . shareData = resp . detail ; return true ; } return false ; } ) . catch ( this . handleError ) ; } deleteSharedDevice ( deviceName ) { return this . http . post < BlinkerResponse > ( API . SHARE . DEL_SHARED , { uuid : this . uuid , token : this . token , devices : JSON . stringify ( [ deviceName ] ) } ) . toPromise ( ) . then ( resp => { console . log ( resp ) ; if ( resp . message == 1000 ) { this . shareData = resp . detail ; return true ; } return false ; } ) . catch ( this . handleError ) ; } private handleError ( error ) { console . error ( 'An error occurred' , error ) ; return false ; } }