|
|
|
@ -20,7 +20,9 @@ import { |
|
|
|
FormBuilder, |
|
|
|
FormGroup, |
|
|
|
NG_VALIDATORS, |
|
|
|
NG_VALUE_ACCESSOR, ValidationErrors, Validator, |
|
|
|
NG_VALUE_ACCESSOR, |
|
|
|
ValidationErrors, |
|
|
|
Validator, |
|
|
|
Validators |
|
|
|
} from '@angular/forms'; |
|
|
|
import { |
|
|
|
@ -34,10 +36,9 @@ import { |
|
|
|
ServerSecurityConfig |
|
|
|
} from './lwm2m-profile-config.models'; |
|
|
|
import { DeviceProfileService } from '@core/http/device-profile.service'; |
|
|
|
import { of, Subject } from 'rxjs'; |
|
|
|
import { map, mergeMap, takeUntil, tap } from 'rxjs/operators'; |
|
|
|
import { Subject } from 'rxjs'; |
|
|
|
import { mergeMap, takeUntil, tap } from 'rxjs/operators'; |
|
|
|
import { Observable } from 'rxjs/internal/Observable'; |
|
|
|
import { deepClone } from '@core/utils'; |
|
|
|
|
|
|
|
@Component({ |
|
|
|
selector: 'tb-profile-lwm2m-device-config-server', |
|
|
|
@ -61,7 +62,7 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc |
|
|
|
private disabled = false; |
|
|
|
private destroy$ = new Subject(); |
|
|
|
|
|
|
|
private securityDefaultConfig: ServerSecurityConfig; |
|
|
|
private isDataLoadedIntoCache = false; |
|
|
|
|
|
|
|
serverFormGroup: FormGroup; |
|
|
|
securityConfigLwM2MType = securityConfigMode; |
|
|
|
@ -82,12 +83,13 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc |
|
|
|
ngOnInit(): void { |
|
|
|
this.serverFormGroup = this.fb.group({ |
|
|
|
host: ['', Validators.required], |
|
|
|
port: [this.isBootstrapServer ? DEFAULT_PORT_BOOTSTRAP_NO_SEC : DEFAULT_PORT_SERVER_NO_SEC, [Validators.required, Validators.min(0)]], |
|
|
|
port: [this.isBootstrapServer ? DEFAULT_PORT_BOOTSTRAP_NO_SEC : DEFAULT_PORT_SERVER_NO_SEC, |
|
|
|
[Validators.required, Validators.min(1), Validators.max(65535), Validators.pattern('[0-9]*')]], |
|
|
|
securityMode: [securityConfigMode.NO_SEC], |
|
|
|
serverPublicKey: ['', Validators.required], |
|
|
|
clientHoldOffTime: ['', [Validators.required, Validators.min(0)]], |
|
|
|
serverId: ['', [Validators.required, Validators.min(0)]], |
|
|
|
bootstrapServerAccountTimeout: ['', [Validators.required, Validators.min(0)]], |
|
|
|
serverPublicKey: [''], |
|
|
|
clientHoldOffTime: ['', [Validators.required, Validators.min(0), Validators.pattern('[0-9]*')]], |
|
|
|
serverId: ['', [Validators.required, Validators.min(1), Validators.max(65534), Validators.pattern('[0-9]*')]], |
|
|
|
bootstrapServerAccountTimeout: ['', [Validators.required, Validators.min(0), Validators.pattern('[0-9]*')]], |
|
|
|
}); |
|
|
|
this.serverFormGroup.get('securityMode').valueChanges.pipe( |
|
|
|
tap(securityMode => this.updateValidate(securityMode)), |
|
|
|
@ -113,7 +115,7 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc |
|
|
|
this.serverFormGroup.patchValue(serverData, {emitEvent: false}); |
|
|
|
this.updateValidate(serverData.securityMode); |
|
|
|
} |
|
|
|
if (!this.securityDefaultConfig){ |
|
|
|
if (!this.isDataLoadedIntoCache){ |
|
|
|
this.getLwm2mBootstrapSecurityInfo().subscribe(value => { |
|
|
|
if (!serverData) { |
|
|
|
this.serverFormGroup.patchValue(value); |
|
|
|
@ -176,37 +178,11 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc |
|
|
|
} |
|
|
|
|
|
|
|
private getLwm2mBootstrapSecurityInfo(securityMode = securityConfigMode.NO_SEC): Observable<ServerSecurityConfig> { |
|
|
|
if (this.securityDefaultConfig) { |
|
|
|
return of(this.processingBootstrapSecurityInfo(this.securityDefaultConfig, securityMode)); |
|
|
|
} |
|
|
|
return this.deviceProfileService.getLwm2mBootstrapSecurityInfo(this.isBootstrapServer).pipe( |
|
|
|
map(securityInfo => { |
|
|
|
this.securityDefaultConfig = securityInfo; |
|
|
|
return this.processingBootstrapSecurityInfo(securityInfo, securityMode); |
|
|
|
}) |
|
|
|
return this.deviceProfileService.getLwm2mBootstrapSecurityInfoBySecurityType(this.isBootstrapServer, securityMode).pipe( |
|
|
|
tap(() => this.isDataLoadedIntoCache = true) |
|
|
|
); |
|
|
|
} |
|
|
|
|
|
|
|
private processingBootstrapSecurityInfo(securityConfig: ServerSecurityConfig, securityMode: securityConfigMode): ServerSecurityConfig { |
|
|
|
const config = deepClone(securityConfig); |
|
|
|
switch (securityMode) { |
|
|
|
case securityConfigMode.PSK: |
|
|
|
config.port = config.securityPort; |
|
|
|
config.host = config.securityHost; |
|
|
|
config.serverPublicKey = ''; |
|
|
|
break; |
|
|
|
case securityConfigMode.RPK: |
|
|
|
case securityConfigMode.X509: |
|
|
|
config.port = config.securityPort; |
|
|
|
config.host = config.securityHost; |
|
|
|
break; |
|
|
|
case securityConfigMode.NO_SEC: |
|
|
|
config.serverPublicKey = ''; |
|
|
|
break; |
|
|
|
} |
|
|
|
return config; |
|
|
|
} |
|
|
|
|
|
|
|
validate(): ValidationErrors | null { |
|
|
|
return this.serverFormGroup.valid ? null : { |
|
|
|
serverFormGroup: true |
|
|
|
|