|
|
|
@ -14,7 +14,7 @@ |
|
|
|
/// limitations under the License.
|
|
|
|
///
|
|
|
|
|
|
|
|
import { Component, forwardRef, Input, OnInit } from '@angular/core'; |
|
|
|
import { Component, forwardRef, Input, OnDestroy, OnInit } from '@angular/core'; |
|
|
|
import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators } from '@angular/forms'; |
|
|
|
import { Store } from '@ngrx/store'; |
|
|
|
import { AppState } from '@app/core/core.state'; |
|
|
|
@ -24,6 +24,10 @@ import { |
|
|
|
DeviceTransportConfiguration, |
|
|
|
DeviceTransportType |
|
|
|
} from '@shared/models/device.models'; |
|
|
|
import { PowerMode, PowerModeTranslationMap } from '@home/components/profile/device/lwm2m/lwm2m-profile-config.models'; |
|
|
|
import { Subject } from 'rxjs'; |
|
|
|
import { takeUntil } from 'rxjs/operators'; |
|
|
|
import { isDefinedAndNotNull } from '@core/utils'; |
|
|
|
|
|
|
|
@Component({ |
|
|
|
selector: 'tb-coap-device-transport-configuration', |
|
|
|
@ -35,9 +39,12 @@ import { |
|
|
|
multi: true |
|
|
|
}] |
|
|
|
}) |
|
|
|
export class CoapDeviceTransportConfigurationComponent implements ControlValueAccessor, OnInit { |
|
|
|
export class CoapDeviceTransportConfigurationComponent implements ControlValueAccessor, OnInit, OnDestroy { |
|
|
|
|
|
|
|
coapDeviceTransportConfigurationFormGroup: FormGroup; |
|
|
|
coapDeviceTransportForm: FormGroup; |
|
|
|
|
|
|
|
powerMods = Object.values(PowerMode); |
|
|
|
powerModeTranslationMap = PowerModeTranslationMap; |
|
|
|
|
|
|
|
private requiredValue: boolean; |
|
|
|
get required(): boolean { |
|
|
|
@ -51,6 +58,7 @@ export class CoapDeviceTransportConfigurationComponent implements ControlValueAc |
|
|
|
@Input() |
|
|
|
disabled: boolean; |
|
|
|
|
|
|
|
private destroy$ = new Subject(); |
|
|
|
private propagateChange = (v: any) => { }; |
|
|
|
|
|
|
|
constructor(private store: Store<AppState>, |
|
|
|
@ -65,33 +73,82 @@ export class CoapDeviceTransportConfigurationComponent implements ControlValueAc |
|
|
|
} |
|
|
|
|
|
|
|
ngOnInit() { |
|
|
|
this.coapDeviceTransportConfigurationFormGroup = this.fb.group({ |
|
|
|
configuration: [null, Validators.required] |
|
|
|
this.coapDeviceTransportForm = this.fb.group({ |
|
|
|
powerMode: [null], |
|
|
|
edrxCycle: [{disabled: true, value: 0}, [Validators.required, Validators.min(0), Validators.pattern('[0-9]*')]], |
|
|
|
psmActivityTimer: [{disabled: true, value: 0}, [Validators.required, Validators.min(0), Validators.pattern('[0-9]*')]], |
|
|
|
pagingTransmissionWindow: [{disabled: true, value: 0}, [Validators.required, Validators.min(0), Validators.pattern('[0-9]*')]] |
|
|
|
}); |
|
|
|
this.coapDeviceTransportForm.get('powerMode').valueChanges.pipe( |
|
|
|
takeUntil(this.destroy$) |
|
|
|
).subscribe((powerMode: PowerMode) => { |
|
|
|
if (powerMode === PowerMode.E_DRX) { |
|
|
|
this.coapDeviceTransportForm.get('edrxCycle').enable({emitEvent: false}); |
|
|
|
this.coapDeviceTransportForm.get('pagingTransmissionWindow').enable({emitEvent: false}); |
|
|
|
this.disablePSKMode(); |
|
|
|
} else if (powerMode === PowerMode.PSM) { |
|
|
|
this.coapDeviceTransportForm.get('psmActivityTimer').enable({emitEvent: false}); |
|
|
|
this.disableEdrxMode(); |
|
|
|
} else { |
|
|
|
this.disableEdrxMode(); |
|
|
|
this.disablePSKMode(); |
|
|
|
} |
|
|
|
}); |
|
|
|
this.coapDeviceTransportConfigurationFormGroup.valueChanges.subscribe(() => { |
|
|
|
this.coapDeviceTransportForm.valueChanges.pipe( |
|
|
|
takeUntil(this.destroy$) |
|
|
|
).subscribe(() => { |
|
|
|
this.updateModel(); |
|
|
|
}); |
|
|
|
} |
|
|
|
|
|
|
|
ngOnDestroy() { |
|
|
|
this.destroy$.next(); |
|
|
|
this.destroy$.complete(); |
|
|
|
} |
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void { |
|
|
|
this.disabled = isDisabled; |
|
|
|
if (this.disabled) { |
|
|
|
this.coapDeviceTransportConfigurationFormGroup.disable({emitEvent: false}); |
|
|
|
this.coapDeviceTransportForm.disable({emitEvent: false}); |
|
|
|
} else { |
|
|
|
this.coapDeviceTransportConfigurationFormGroup.enable({emitEvent: false}); |
|
|
|
this.coapDeviceTransportForm.enable({emitEvent: false}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
writeValue(value: CoapDeviceTransportConfiguration | null): void { |
|
|
|
this.coapDeviceTransportConfigurationFormGroup.patchValue({configuration: value}, {emitEvent: false}); |
|
|
|
if (isDefinedAndNotNull(value)) { |
|
|
|
this.coapDeviceTransportForm.patchValue(value, {emitEvent: false}); |
|
|
|
} else { |
|
|
|
this.coapDeviceTransportForm.patchValue({ |
|
|
|
powerMode: null, |
|
|
|
edrxCycle: 0, |
|
|
|
psmActivityTimer: 0, |
|
|
|
pagingTransmissionWindow: 0 |
|
|
|
}, {emitEvent: false}); |
|
|
|
} |
|
|
|
if (!this.disabled) { |
|
|
|
this.coapDeviceTransportForm.get('powerMode').updateValueAndValidity({onlySelf: true}); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private updateModel() { |
|
|
|
let configuration: DeviceTransportConfiguration = null; |
|
|
|
if (this.coapDeviceTransportConfigurationFormGroup.valid) { |
|
|
|
configuration = this.coapDeviceTransportConfigurationFormGroup.getRawValue().configuration; |
|
|
|
if (this.coapDeviceTransportForm.valid) { |
|
|
|
configuration = this.coapDeviceTransportForm.value; |
|
|
|
configuration.type = DeviceTransportType.COAP; |
|
|
|
} |
|
|
|
this.propagateChange(configuration); |
|
|
|
} |
|
|
|
|
|
|
|
private disablePSKMode() { |
|
|
|
this.coapDeviceTransportForm.get('psmActivityTimer').disable({emitEvent: false}); |
|
|
|
this.coapDeviceTransportForm.get('psmActivityTimer').reset(0, {emitEvent: false}); |
|
|
|
} |
|
|
|
|
|
|
|
private disableEdrxMode() { |
|
|
|
this.coapDeviceTransportForm.get('edrxCycle').disable({emitEvent: false}); |
|
|
|
this.coapDeviceTransportForm.get('edrxCycle').reset(0, {emitEvent: false}); |
|
|
|
this.coapDeviceTransportForm.get('pagingTransmissionWindow').disable({emitEvent: false}); |
|
|
|
this.coapDeviceTransportForm.get('pagingTransmissionWindow').reset(0, {emitEvent: false}); |
|
|
|
} |
|
|
|
} |
|
|
|
|