|
|
|
@ -14,7 +14,7 @@ |
|
|
|
/// limitations under the License.
|
|
|
|
///
|
|
|
|
|
|
|
|
import { Component, EventEmitter, forwardRef, Input, OnInit, Output } from '@angular/core'; |
|
|
|
import { Component, EventEmitter, forwardRef, Input, OnDestroy, OnInit, Output } from '@angular/core'; |
|
|
|
import { |
|
|
|
AbstractControl, |
|
|
|
ControlValueAccessor, |
|
|
|
@ -24,13 +24,13 @@ import { |
|
|
|
NG_VALIDATORS, |
|
|
|
NG_VALUE_ACCESSOR |
|
|
|
} from '@angular/forms'; |
|
|
|
import { of, Subscription } from 'rxjs'; |
|
|
|
import { of, Subject } from 'rxjs'; |
|
|
|
import { ServerSecurityConfig } from '@home/components/profile/device/lwm2m/lwm2m-profile-config.models'; |
|
|
|
import { TranslateService } from '@ngx-translate/core'; |
|
|
|
import { DialogService } from '@core/services/dialog.service'; |
|
|
|
import { MatDialog } from '@angular/material/dialog'; |
|
|
|
import { Lwm2mBootstrapAddConfigServerDialogComponent } from '@home/components/profile/device/lwm2m/lwm2m-bootstrap-add-config-server-dialog.component'; |
|
|
|
import { mergeMap } from 'rxjs/operators'; |
|
|
|
import { mergeMap, takeUntil } from 'rxjs/operators'; |
|
|
|
import { DeviceProfileService } from '@core/http/device-profile.service'; |
|
|
|
import { Lwm2mSecurityType } from '@shared/models/lwm2m-security-config.models'; |
|
|
|
|
|
|
|
@ -50,7 +50,7 @@ import { Lwm2mSecurityType } from '@shared/models/lwm2m-security-config.models'; |
|
|
|
} |
|
|
|
] |
|
|
|
}) |
|
|
|
export class Lwm2mBootstrapConfigServersComponent implements OnInit, ControlValueAccessor { |
|
|
|
export class Lwm2mBootstrapConfigServersComponent implements OnInit, ControlValueAccessor, OnDestroy { |
|
|
|
|
|
|
|
bootstrapConfigServersFormGroup: UntypedFormGroup; |
|
|
|
|
|
|
|
@ -72,8 +72,7 @@ export class Lwm2mBootstrapConfigServersComponent implements OnInit, ControlValu |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private valueChangeSubscription: Subscription = null; |
|
|
|
|
|
|
|
private destroy$ = new Subject<void>(); |
|
|
|
private propagateChange = (v: any) => { }; |
|
|
|
|
|
|
|
constructor(public translate: TranslateService, |
|
|
|
@ -94,9 +93,17 @@ export class Lwm2mBootstrapConfigServersComponent implements OnInit, ControlValu |
|
|
|
this.bootstrapConfigServersFormGroup = this.fb.group({ |
|
|
|
serverConfigs: this.fb.array([]) |
|
|
|
}); |
|
|
|
this.bootstrapConfigServersFormGroup.valueChanges.pipe( |
|
|
|
takeUntil(this.destroy$) |
|
|
|
).subscribe(() => this.updateModel()); |
|
|
|
} |
|
|
|
|
|
|
|
ngOnDestroy() { |
|
|
|
this.destroy$.next(); |
|
|
|
this.destroy$.complete(); |
|
|
|
} |
|
|
|
|
|
|
|
serverConfigsFromArray(): UntypedFormArray { |
|
|
|
get serverConfigsFromArray(): UntypedFormArray { |
|
|
|
return this.bootstrapConfigServersFormGroup.get('serverConfigs') as UntypedFormArray; |
|
|
|
} |
|
|
|
|
|
|
|
@ -110,24 +117,22 @@ export class Lwm2mBootstrapConfigServersComponent implements OnInit, ControlValu |
|
|
|
} |
|
|
|
|
|
|
|
writeValue(serverConfigs: Array<ServerSecurityConfig> | null): void { |
|
|
|
if (this.valueChangeSubscription) { |
|
|
|
this.valueChangeSubscription.unsubscribe(); |
|
|
|
} |
|
|
|
const serverConfigsControls: Array<AbstractControl> = []; |
|
|
|
if (serverConfigs) { |
|
|
|
serverConfigs.forEach((serverConfig) => { |
|
|
|
serverConfigsControls.push(this.fb.control(serverConfig)); |
|
|
|
}); |
|
|
|
} |
|
|
|
this.bootstrapConfigServersFormGroup.setControl('serverConfigs', this.fb.array(serverConfigsControls)); |
|
|
|
if (this.disabled) { |
|
|
|
this.bootstrapConfigServersFormGroup.disable({emitEvent: false}); |
|
|
|
if (serverConfigs?.length === this.serverConfigsFromArray.length) { |
|
|
|
this.serverConfigsFromArray.patchValue(serverConfigs, {emitEvent: false}); |
|
|
|
} else { |
|
|
|
this.bootstrapConfigServersFormGroup.enable({emitEvent: false}); |
|
|
|
const serverConfigsControls: Array<AbstractControl> = []; |
|
|
|
if (serverConfigs) { |
|
|
|
serverConfigs.forEach((serverConfig) => { |
|
|
|
serverConfigsControls.push(this.fb.control(serverConfig)); |
|
|
|
}); |
|
|
|
} |
|
|
|
this.bootstrapConfigServersFormGroup.setControl('serverConfigs', this.fb.array(serverConfigsControls), {emitEvent: false}); |
|
|
|
if (this.disabled) { |
|
|
|
this.bootstrapConfigServersFormGroup.disable({emitEvent: false}); |
|
|
|
} else { |
|
|
|
this.bootstrapConfigServersFormGroup.enable({emitEvent: false}); |
|
|
|
} |
|
|
|
} |
|
|
|
this.valueChangeSubscription = this.bootstrapConfigServersFormGroup.valueChanges.subscribe(() => { |
|
|
|
this.updateModel(); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
trackByParams(index: number): number { |
|
|
|
@ -147,7 +152,7 @@ export class Lwm2mBootstrapConfigServersComponent implements OnInit, ControlValu |
|
|
|
true |
|
|
|
).subscribe((result) => { |
|
|
|
if (result) { |
|
|
|
this.serverConfigsFromArray().removeAt(index); |
|
|
|
this.serverConfigsFromArray.removeAt(index); |
|
|
|
} |
|
|
|
}); |
|
|
|
} |
|
|
|
@ -169,7 +174,7 @@ export class Lwm2mBootstrapConfigServersComponent implements OnInit, ControlValu |
|
|
|
addServerConfigObs.subscribe((serverConfig) => { |
|
|
|
if (serverConfig) { |
|
|
|
serverConfig.securityMode = Lwm2mSecurityType.NO_SEC; |
|
|
|
this.serverConfigsFromArray().push(this.fb.control(serverConfig)); |
|
|
|
this.serverConfigsFromArray.push(this.fb.control(serverConfig)); |
|
|
|
this.updateModel(); |
|
|
|
} else { |
|
|
|
this.isTransportWasRunWithBootstrap = false; |
|
|
|
@ -196,7 +201,7 @@ export class Lwm2mBootstrapConfigServersComponent implements OnInit, ControlValu |
|
|
|
} |
|
|
|
|
|
|
|
private isBootstrapAdded(): boolean { |
|
|
|
const serverConfigsArray = this.serverConfigsFromArray().getRawValue(); |
|
|
|
const serverConfigsArray = this.serverConfigsFromArray.getRawValue(); |
|
|
|
for (let i = 0; i < serverConfigsArray.length; i++) { |
|
|
|
if (serverConfigsArray[i].bootstrapServerIs) { |
|
|
|
return true; |
|
|
|
@ -207,15 +212,15 @@ export class Lwm2mBootstrapConfigServersComponent implements OnInit, ControlValu |
|
|
|
|
|
|
|
private removeBootstrapServerConfig(): void { |
|
|
|
if (this.bootstrapConfigServersFormGroup) { |
|
|
|
const bootstrapServerIndex = this.serverConfigsFromArray().getRawValue().findIndex(server => server.bootstrapServerIs === true); |
|
|
|
const bootstrapServerIndex = this.serverConfigsFromArray.getRawValue().findIndex(server => server.bootstrapServerIs === true); |
|
|
|
if (bootstrapServerIndex !== -1) { |
|
|
|
this.serverConfigsFromArray().removeAt(bootstrapServerIndex); |
|
|
|
this.serverConfigsFromArray.removeAt(bootstrapServerIndex); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private updateModel() { |
|
|
|
const serverConfigs: Array<ServerSecurityConfig> = this.serverConfigsFromArray().value; |
|
|
|
const serverConfigs: Array<ServerSecurityConfig> = this.serverConfigsFromArray.value; |
|
|
|
this.propagateChange(serverConfigs); |
|
|
|
} |
|
|
|
} |
|
|
|
|