import { Injectable } from '@angular/core' ; import { HttpClient } from '@angular/common/http' ; import { UserService } from 'src/app/core/services/user.service' ; import CryptoJS from 'crypto-js' ; import { NoticeService } from 'src/app/core/services/notice.service' ; import { API } from 'src/app/configs/api.config' ; import { DataService } from 'src/app/core/services/data.service' ; @ Injectable ( { providedIn : 'root' } ) export class DevcenterService { get uuid ( ) { return this . dataService . auth . uuid ; } get token ( ) { return this . dataService . auth . token ; } datakeyList = [ ] ; proDeviceList = [ ] ; userLevel ; dataKeyNum ; dataKeyMaxNum ; proDeviceNum ; proDeviceMaxNum ; diyDeviceNum ; diyDeviceMaxNum ; expiryDate ; vender ; developerInfo = { auth : { vender : "" , verified : 0 } , dataKeyMaxNum : 0 , dataKeyNum : 0 , diyDeviceMaxNum : 0 , diyDeviceNum : 0 , expiryDate : 0 , proDeviceMaxNum : 0 , proDeviceNum : 0 , userLevel : 0 , } ; currentProDevice ; constructor ( private http , private userService , private noticeService , private dataService ) { } async getUserLevel ( ) { return this . http . get ( API . DEV_CENTER . USER_LEVEL , { params : { uuid : this . uuid , token : this . token } } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { this . developerInfo = data . detail ; return true ; } return false ; } ) . catch ( this . handleError ) ; } async getDataKeys ( ) { return this . http . get ( API . DEV_CENTER . DATAKEYS , { params : { uuid : this . uuid , token : this . token } } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { this . datakeyList = [ ] ; let datakeys = data . detail ; for ( let deviceId in datakeys ) { let item = { deviceId : deviceId , keys : datakeys [ deviceId ] } ; this . datakeyList . push ( item ) ; } return true ; } return false ; } ) . catch ( this . handleError ) ; } delDataKey ( deviceId , key ) { return this . http . delete ( API . DEV_CENTER . DATAKEYS , { params : { uuid : this . uuid , token : this . token , deviceName : deviceId , dataKeys : JSON . stringify ( [ key ] ) } } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { this . dataKeyNum = this . dataKeyNum - 1 ; return true ; } return false ; } ) . catch ( this . handleError ) ; } addProDevice ( config ) { return this . http . post ( API . DEV_CENTER . PRODEVICE , { "uuid" : this . uuid , 'token' : this . token , "conf" : JSON . stringify ( config ) } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { return true ; } else { return false ; } } ) . catch ( this . handleError ) ; } delProDevice ( deviceType , password ) { return this . http . delete ( API . DEV_CENTER . PRODEVICE , { params : { uuid : this . uuid , token : this . token , deviceType : deviceType , password : CryptoJS . SHA256 ( password ) } } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { this . getProDevices ( ) ; return true ; } return false ; } ) . catch ( this . handleError ) ; } getProDevices ( ) { return this . http . get ( API . DEV_CENTER . PRODEVICE , { params : { uuid : this . uuid , token : this . token , } } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { this . proDeviceList = data . detail ; return true ; } return false ; } ) . catch ( this . handleError ) ; } getProDeviceAuthKey ( deviceType ) { return this . http . get ( API . DEV_CENTER . PRODEVICE_KEY , { params : { uuid : this . uuid , token : this . token , deviceType : deviceType } } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { return data . detail . typeKey ; } return 'error' ; } ) . catch ( this . handleError ) ; } getProDeviceConfig ( deviceType ) { this . currentProDevice = null ; return this . http . get ( API . DEV_CENTER . PRODEVICE_CONFIG , { params : { uuid : this . uuid , token : this . token , deviceType : deviceType } } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { this . currentProDevice = data . detail ; return true ; } return false ; } ) . catch ( this . handleError ) ; } getProDeviceLayouter ( deviceType ) { return this . http . get ( API . DEV_CENTER . PRODEVICE_LAYOUTER , { params : { uuid : this . uuid , token : this . token , deviceType : deviceType } } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { return data . detail ; } return false ; } ) . catch ( this . handleError ) ; } setProDeviceConfig ( deviceType , config ) { console . log ( deviceType ) ; console . log ( config ) ; return this . http . post ( API . DEV_CENTER . PRODEVICE_CONFIG , { uuid : this . uuid , token : this . token , deviceType : deviceType , conf : JSON . stringify ( config ) } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { return true ; } else { return false ; } } ) . catch ( this . handleError ) ; } isAuthenticated ( ) { return this . http . get ( API . DEV_CENTER . USER_AUTH , { params : { uuid : this . uuid , token : this . token } } ) . toPromise ( ) . then ( response => { console . log ( response ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { return ( data . detail . verified == 1 ? true : false ) ; } } ) . catch ( this . handleError ) ; } async uploadAuthInfo ( authInfo ) { await this . noticeService . showLoading ( 'upload' ) ; let formData = new FormData ( ) ; formData . append ( 'uuid' , this . userService . uuid ) ; formData . append ( 'token' , this . userService . token ) ; formData . append ( 'vender' , authInfo . vender ) ; formData . append ( 'idCard' , authInfo . idCard ) ; if ( authInfo . type == 'enterprise' ) formData . append ( 'charter' , authInfo . charter ) ; return this . http . post ( API . DEV_CENTER . USER_AUTH , formData ) . toPromise ( ) . then ( response => { console . log ( response ) ; this . noticeService . hideLoading ( ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { console . log ( "上传成功" ) ; return true ; } else { console . log ( "上传失败" ) ; return false ; } } ) . catch ( this . handleError ) ; } async publicProDevice ( deviceType ) { await this . noticeService . showLoading ( 'upload' ) ; let formData = new FormData ( ) ; formData . append ( 'uuid' , this . userService . uuid ) ; formData . append ( 'token' , this . userService . token ) ; formData . append ( 'deviceType' , deviceType ) ; return this . http . post ( API . DEV_CENTER . PUBLIC_PRODEVICE , formData ) . toPromise ( ) . then ( response => { console . log ( response ) ; this . noticeService . hideLoading ( ) ; let data = JSON . parse ( JSON . stringify ( response ) ) ; if ( data . message == 1000 ) { console . log ( "发布成功" ) ; return true ; } else { console . log ( "发布失败" ) ; return false ; } } ) . catch ( this . handleError ) ; } private handleError ( error ) { console . error ( 'An error occurred' , error ) ; return false ; } }