From cfa9a5778a9ae7b26c92dc80cb7e4d9265a8a11f Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Tue, 19 Jan 2021 16:24:58 +0200 Subject: [PATCH] Lwm2m: front: refactoring --- .../lwm2m-device-config-server.component.html | 27 ++- .../lwm2m-device-config-server.component.ts | 76 +++--- ...ile-transport-configuration.component.html | 218 +++++++++--------- ...ofile-transport-configuration.component.ts | 132 ++++++----- ...m2m-object-add-instances-list.component.ts | 68 +++--- .../lwm2m-object-add-instances.component.html | 2 +- .../lwm2m-object-add-instances.component.ts | 6 +- .../lwm2m/lwm2m-object-list.component.ts | 82 +++---- ...serve-attr-telemetry-resource.component.ts | 41 ++-- .../lwm2m-observe-attr-telemetry.component.ts | 153 ++++++------ .../device/lwm2m/profile-config.models.ts | 108 ++++----- .../home/pages/device/device.module.ts | 3 +- .../security-config-server.component.html | 57 +++-- .../lwm2m/security-config-server.component.ts | 62 +++-- .../lwm2m/security-config.component.html | 206 +++++++++-------- .../device/lwm2m/security-config.component.ts | 185 +++++++-------- .../device/lwm2m/security-config.models.ts | 54 +++-- .../components/json-object-edit.component.ts | 6 +- .../assets/locale/locale.constant-en_US.json | 10 +- 19 files changed, 778 insertions(+), 718 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-config-server.component.html b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-config-server.component.html index 5f8c1080bc..f0fe7d8dcd 100644 --- a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-config-server.component.html +++ b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-config-server.component.html @@ -90,9 +90,14 @@ serverFormGroup.get('securityMode').value === securityConfigLwM2MType.X509"> {{ 'device-profile.lwm2m.server-public-key' | translate }} - {{serverPublicKey.value?.length || 0}}/{{lenMaxServerPublicKey}} @@ -101,14 +106,18 @@ {{ 'device-profile.lwm2m.required' | translate }} - {{ 'device-profile.lwm2m.client-key' | translate }} - {{ 'device-profile.lwm2m.pattern_hex_dec_182' | translate }} + (serverFormGroup.get('securityMode').value === securityConfigLwM2MType.RPK || + serverFormGroup.get('securityMode').value === securityConfigLwM2MType.X509)"> + {{ 'device-profile.lwm2m.server-public-key' | translate }} + {{ translate.get('device-profile.lwm2m.pattern_hex_dec', { + count: 0}) | async }} - - {{ 'device-profile.lwm2m.client-key' | translate }} - {{ 'device-profile.lwm2m.pattern_hex_dec' | translate }} + + {{ 'device-profile.lwm2m.server-public-key' | translate }} + {{ translate.get('device-profile.lwm2m.pattern_hex_dec', { + count: lenMaxServerPublicKey }) | async }} diff --git a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-config-server.component.ts b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-config-server.component.ts index 01352af36e..e02a4e7c3a 100644 --- a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-config-server.component.ts +++ b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-config-server.component.ts @@ -14,31 +14,28 @@ /// limitations under the License. /// -import { Component, forwardRef, Inject, Input, OnInit } from "@angular/core"; +import { Component, forwardRef, Inject, Input, OnInit } from '@angular/core'; import { ControlValueAccessor, - FormBuilder, FormGroup, NG_VALUE_ACCESSOR, NgModel, Validators -} from "@angular/forms"; + FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators +} from '@angular/forms'; import { SECURITY_CONFIG_MODE, SECURITY_CONFIG_MODE_NAMES, - KEY_PUBLIC_REGEXP_PSK, ServerSecurityConfig, - LEN_MAX_PUBLIC_KEY_PSK, - LEN_MAX_PUBLIC_KEY_RPK_X509, - KEY_PUBLIC_REGEXP_X509, DEFAULT_PORT_BOOTSTRAP_NO_SEC, DEFAULT_PORT_SERVER_NO_SEC, DEFAULT_CLIENT_HOLD_OFF_TIME, - DEFAULT_ID_SERVER -} from "./profile-config.models"; -import { Store } from "@ngrx/store"; -import { AppState } from "@core/core.state"; -import { coerceBooleanProperty } from "@angular/cdk/coercion"; -import { WINDOW } from "@core/services/window.service"; + DEFAULT_ID_SERVER, LEN_MAX_PUBLIC_KEY_RPK, LEN_MAX_PUBLIC_KEY_X509, KEY_REGEXP_HEX_DEC +} from './profile-config.models'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; +import { coerceBooleanProperty } from '@angular/cdk/coercion'; +import { WINDOW } from '@core/services/window.service'; import { pairwise, startWith } from 'rxjs/operators'; import { DeviceProfileService } from '@core/http/device-profile.service'; +import { TranslateService } from '@ngx-translate/core'; @Component({ selector: 'tb-profile-lwm2m-device-config-server', @@ -55,12 +52,14 @@ import { DeviceProfileService } from '@core/http/device-profile.service'; export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAccessor, Validators { private requiredValue: boolean; + valuePrev = null; serverFormGroup: FormGroup; securityConfigLwM2MType = SECURITY_CONFIG_MODE; securityConfigLwM2MTypes = Object.keys(SECURITY_CONFIG_MODE); credentialTypeLwM2MNamesMap = SECURITY_CONFIG_MODE_NAMES; - lenMaxServerPublicKey = LEN_MAX_PUBLIC_KEY_PSK; + lenMinServerPublicKey = 0; + lenMaxServerPublicKey = LEN_MAX_PUBLIC_KEY_RPK; currentSecurityMode = null; @@ -80,6 +79,7 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc } constructor(protected store: Store, + public translate: TranslateService, public fb: FormBuilder, private deviceProfileService: DeviceProfileService, @Inject(WINDOW) private window: Window) { @@ -105,7 +105,7 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc ngOnInit(): void { } - updateValueFields(serverData: ServerSecurityConfig): void { + private updateValueFields = (serverData: ServerSecurityConfig): void => { serverData.bootstrapServerIs = this.bootstrapServerIs; this.serverFormGroup.patchValue(serverData, {emitEvent: false}); this.serverFormGroup.get('bootstrapServerIs').disable(); @@ -113,40 +113,38 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc this.updateValidate(securityMode); } - updateValidate(securityMode: SECURITY_CONFIG_MODE): void { + private updateValidate = (securityMode: SECURITY_CONFIG_MODE): void => { switch (securityMode) { case SECURITY_CONFIG_MODE.NO_SEC: - this.serverFormGroup.get('serverPublicKey').setValidators([]); + this.setValidatorsNoSecPsk(); break; case SECURITY_CONFIG_MODE.PSK: - this.lenMaxServerPublicKey = LEN_MAX_PUBLIC_KEY_PSK; - this.serverFormGroup.get('serverPublicKey').setValidators([]); + this.setValidatorsNoSecPsk(); break; case SECURITY_CONFIG_MODE.RPK: - this.lenMaxServerPublicKey = LEN_MAX_PUBLIC_KEY_RPK_X509; - this.serverFormGroup.get('serverPublicKey').setValidators([Validators.required, Validators.pattern(KEY_PUBLIC_REGEXP_PSK)]); + this.lenMinServerPublicKey = LEN_MAX_PUBLIC_KEY_RPK; + this.lenMaxServerPublicKey = LEN_MAX_PUBLIC_KEY_RPK; + this.setValidatorsRpkX509(); break; case SECURITY_CONFIG_MODE.X509: - this.lenMaxServerPublicKey = LEN_MAX_PUBLIC_KEY_RPK_X509; - this.serverFormGroup.get('serverPublicKey').setValidators([Validators.required, Validators.pattern(KEY_PUBLIC_REGEXP_X509)]); + this.lenMinServerPublicKey = 0; + this.lenMaxServerPublicKey = LEN_MAX_PUBLIC_KEY_X509; + this.setValidatorsRpkX509(); break; } this.serverFormGroup.updateValueAndValidity(); - // this.checkValueWithNewValidate(); } - // checkValueWithNewValidate(): void { - // this.serverFormGroup.patchValue({ - // host: this.serverFormGroup.get('host').value, - // port: this.serverFormGroup.get('port').value, - // bootstrapServerIs: this.serverFormGroup.get('bootstrapServerIs').value, - // serverPublicKey: this.serverFormGroup.get('serverPublicKey').value, - // clientHoldOffTime: this.serverFormGroup.get('clientHoldOffTime').value, - // serverId: this.serverFormGroup.get('serverId').value, - // bootstrapServerAccountTimeout: this.serverFormGroup.get('bootstrapServerAccountTimeout').value, - // }, - // {emitEvent: true}); - // } + private setValidatorsNoSecPsk = (): void => { + this.serverFormGroup.get('serverPublicKey').setValidators([]); + } + + private setValidatorsRpkX509 = (): void => { + this.serverFormGroup.get('serverPublicKey').setValidators([Validators.required, + Validators.pattern(KEY_REGEXP_HEX_DEC), + Validators.minLength(this.lenMinServerPublicKey), + Validators.maxLength(this.lenMaxServerPublicKey)]); + } writeValue(value: any): void { if (value) { @@ -160,7 +158,7 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc this.propagateChange = fn; } - private propagateChangeState(value: any): void { + private propagateChangeState = (value: any): void => { if (value !== undefined) { if (this.valuePrev === null) { this.valuePrev = 'init'; @@ -190,7 +188,7 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc registerOnTouched(fn: any): void { } - getServerGroup(): FormGroup { + private getServerGroup = (): FormGroup => { const port = this.bootstrapServerIs ? DEFAULT_PORT_BOOTSTRAP_NO_SEC : DEFAULT_PORT_SERVER_NO_SEC; return this.fb.group({ host: [this.window.location.hostname, this.required ? [Validators.required] : []], @@ -204,7 +202,7 @@ export class Lwm2mDeviceConfigServerComponent implements OnInit, ControlValueAcc }); } - getLwm2mBootstrapSecurityInfo(mode: string) { + private getLwm2mBootstrapSecurityInfo = (mode: string): void => { this.deviceProfileService.getLwm2mBootstrapSecurityInfo(mode, this.serverFormGroup.get('bootstrapServerIs').value).subscribe( (serverSecurityConfig) => { this.serverFormGroup.patchValue({ diff --git a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-profile-transport-configuration.component.html b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-profile-transport-configuration.component.html index 5e9c5d27f4..3eadd43c53 100644 --- a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-profile-transport-configuration.component.html +++ b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-profile-transport-configuration.component.html @@ -19,118 +19,126 @@
-
- - -
-
- - -
+ +
+ + +
+
+ + +
+
-
- - - - -
{{ 'device-profile.lwm2m.servers' | translate | uppercase }}
-
-
-
-
- - {{ 'device-profile.lwm2m.short-id' | translate }} - - - {{ 'device-profile.lwm2m.short-id' | translate }} - {{ 'device-profile.lwm2m.required' | translate }} - - - - {{ 'device-profile.lwm2m.lifetime' | translate }} - - - {{ 'device-profile.lwm2m.lifetime' | translate }} - {{ 'device-profile.lwm2m.required' | translate }} - - + +
+ + + + +
{{ 'device-profile.lwm2m.servers' | translate | uppercase }}
+
+
+
+
+ + {{ 'device-profile.lwm2m.short-id' | translate }} + + + {{ 'device-profile.lwm2m.short-id' | translate }} + {{ 'device-profile.lwm2m.required' | translate }} + + + + {{ 'device-profile.lwm2m.lifetime' | translate }} + + + {{ 'device-profile.lwm2m.lifetime' | translate }} + {{ 'device-profile.lwm2m.required' | translate }} + + +
+
+ + {{ 'device-profile.lwm2m.default-min-period' | translate }} + + + {{ 'device-profile.lwm2m.default-min-period' | translate }} + {{ 'device-profile.lwm2m.required' | translate }} + + + + {{ 'device-profile.lwm2m.binding' | translate }} + + + {{ 'device-profile.lwm2m.binding' | translate }} + {{ 'device-profile.lwm2m.required' | translate }} + + +
+
+ + {{ 'device-profile.lwm2m.notif-if-disabled' | translate }} + +
-
- - {{ 'device-profile.lwm2m.default-min-period' | translate }} - - - {{ 'device-profile.lwm2m.default-min-period' | translate }} - {{ 'device-profile.lwm2m.required' | translate }} - - - - {{ 'device-profile.lwm2m.binding' | translate }} - - - {{ 'device-profile.lwm2m.binding' | translate }} - {{ 'device-profile.lwm2m.required' | translate }} - - + + + + + + +
{{ 'device-profile.lwm2m.bootstrap-server' | translate | uppercase }}
+
+
+
+ +
-
- - {{ 'device-profile.lwm2m.notif-if-disabled' | translate }} - + + + + + + +
{{ 'device-profile.lwm2m.lwm2m-server' | translate | uppercase }}
+
+
+
+ +
-
-
-
- - - - -
{{ 'device-profile.lwm2m.bootstrap-server' | translate | uppercase }}
-
-
-
- - -
-
-
- - - - -
{{ 'device-profile.lwm2m.lwm2m-server' | translate | uppercase }}
-
-
-
- - -
-
-
-
+
+
+
+
-
- - -
+ +
+ + +
+
diff --git a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-profile-transport-configuration.component.ts b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-profile-transport-configuration.component.ts index 9071fc4a2b..b1f87568ad 100644 --- a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-profile-transport-configuration.component.ts +++ b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-device-profile-transport-configuration.component.ts @@ -34,10 +34,10 @@ import { OBSERVE_ATTR, TELEMETRY, ObjectLwM2M, getDefaultProfileConfig, KEY_NAME, Instance, ProfileConfigModels, ResourceLwM2M -} from "./profile-config.models"; -import { DeviceProfileService } from "@core/http/device-profile.service"; -import { deepClone, isUndefined } from "@core/utils"; -import { WINDOW } from "@core/services/window.service"; +} from './profile-config.models'; +import { DeviceProfileService } from '@core/http/device-profile.service'; +import { deepClone, isUndefined } from '@core/utils'; +import { WINDOW } from '@core/services/window.service'; import { JsonObject } from '@angular/compiler-cli/ngcc/src/packages/entry_point'; import { isNotNullOrUndefined } from 'codelyzer/util/isNotNullOrUndefined'; @@ -65,7 +65,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro bootstrapServers: string; bootstrapServer: string; lwm2mServer: string; - sortFunction = this.sortObjectKeyPathJson; + sortFunction: {}; get required(): boolean { return this.requiredValue; @@ -76,7 +76,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro this.requiredValue = coerceBooleanProperty(value); } - private propagateChange = (v: any) => { }; + private propagateChange = (v: any) => { }; constructor(private store: Store, private fb: FormBuilder, @@ -109,6 +109,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro } ngOnInit() { + this.sortFunction = this.sortObjectKeyPathJson; } setDisabledState(isDisabled: boolean): void { @@ -130,22 +131,22 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro this.initWriteValue(); } - private initWriteValue(): void { + private initWriteValue = (): void => { const modelValue = {objectIds: null, objectsList: []}; modelValue.objectIds = this.getObjectsFromJsonAllConfig(); if (modelValue.objectIds !== null) { this.deviceProfileService.getLwm2mObjects(modelValue.objectIds).subscribe( - (objectsList) => { - modelValue.objectsList = objectsList; - this.updateWriteValue(modelValue); - } + (objectsList) => { + modelValue.objectsList = objectsList; + this.updateWriteValue(modelValue); + } ); } else { this.updateWriteValue(modelValue); } } - private updateWriteValue(value: any): void { + private updateWriteValue = (value: any): void => { const objectsList = deepClone(value.objectsList); this.lwm2mDeviceProfileTransportConfFormGroup.patchValue({ objectIds: value, @@ -161,7 +162,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro {emitEvent: false}); } - private updateModel() { + private updateModel = (): void => { let configuration: DeviceProfileTransportConfiguration = null; if (this.lwm2mDeviceProfileTransportConfFormGroup.valid) { this.upDateValueToJson(); @@ -171,25 +172,25 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro this.propagateChange(configuration); } - private updateObserveAttrTelemetryObjectFormGroup(objectsList: ObjectLwM2M[]) { + private updateObserveAttrTelemetryObjectFormGroup = (objectsList: ObjectLwM2M[]): void => { this.lwm2mDeviceProfileTransportConfFormGroup.patchValue({ - observeAttrTelemetry: {clientLwM2M: this.getObserveAttrTelemetryObjects(objectsList)} - }, - {emitEvent: false}); + observeAttrTelemetry: {clientLwM2M: this.getObserveAttrTelemetryObjects(objectsList)} + }, + {emitEvent: false}); this.lwm2mDeviceProfileTransportConfFormGroup.get('observeAttrTelemetry').markAsPristine({ onlySelf: true }); } - upDateValueToJson(): void { - this.upDateValueToJsonTab_0(); - this.upDateValueToJsonTab_1(); + private upDateValueToJson = (): void => { + this.upDateValueToJsonTab0(); + this.upDateValueToJsonTab1(); } - upDateValueToJsonTab_0(): void { + private upDateValueToJsonTab0 = (): void => { if (!this.lwm2mDeviceProfileTransportConfFormGroup.get('observeAttrTelemetry').pristine) { this.upDateObserveAttrTelemetryFromGroupToJson( - this.lwm2mDeviceProfileTransportConfFormGroup.get('observeAttrTelemetry').value.clientLwM2M + this.lwm2mDeviceProfileTransportConfFormGroup.get('observeAttrTelemetry').value.clientLwM2M ); this.lwm2mDeviceProfileTransportConfFormGroup.get('observeAttrTelemetry').markAsPristine({ onlySelf: true @@ -198,7 +199,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro } } - upDateValueToJsonTab_1(): void { + private upDateValueToJsonTab1 = (): void => { this.upDateValueServersToJson(); if (!this.lwm2mDeviceProfileTransportConfFormGroup.get('bootstrapServer').pristine) { this.configurationValue.bootstrap.bootstrapServer = this.lwm2mDeviceProfileTransportConfFormGroup.get('bootstrapServer').value; @@ -216,7 +217,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro } } - upDateValueServersToJson(): void { + private upDateValueServersToJson = (): void => { const bootstrapServers = this.configurationValue.bootstrap.servers; if (!this.lwm2mDeviceProfileTransportConfFormGroup.get('shortId').pristine) { bootstrapServers.shortId = this.lwm2mDeviceProfileTransportConfFormGroup.get('shortId').value; @@ -255,7 +256,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro } } - getObserveAttrTelemetryObjects(listObject: ObjectLwM2M[]): ObjectLwM2M [] { + private getObserveAttrTelemetryObjects = (listObject: ObjectLwM2M[]): ObjectLwM2M [] => { const clientObserveAttr = deepClone(listObject); if (this.configurationValue[this.observeAttr]) { const observeArray = this.configurationValue[this.observeAttr][this.observe] as Array; @@ -284,12 +285,13 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro return clientObserveAttr; } - includesInstancesNo(attributeArray: Array, telemetryArray: Array): boolean { + private includesInstancesNo = (attributeArray: Array, telemetryArray: Array): boolean => { const isIdIndex = (element) => !element.includes('/0/'); return attributeArray.findIndex(isIdIndex) >= 0 || telemetryArray.findIndex(isIdIndex) >= 0; } - addInstances(attributeArray: Array, telemetryArray: Array, clientObserveAttr: ObjectLwM2M[]): void { + private addInstances = (attributeArray: Array, telemetryArray: Array, + clientObserveAttr: ObjectLwM2M[]): void => { const attr = [] as Array; [...attributeArray].filter(x => (!x.includes('/0/'))).forEach(x => { attr.push(this.convertPathToInstance(x)); @@ -310,42 +312,43 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro }); } - convertPathToInstance(path: string): string { + private convertPathToInstance = (path: string): string => { const newX = Array.from(path.substring(1).split('/'), Number); return [newX[0], newX[1]].join('/'); } - updateObserveAttrTelemetryObjects(isParameter: Array, clientObserveAttr: ObjectLwM2M[], nameParameter: string): void { + private updateObserveAttrTelemetryObjects = (isParameter: Array, clientObserveAttr: ObjectLwM2M[], + nameParameter: string): void => { isParameter.forEach(attr => { const idKeys = Array.from(attr.substring(1).split('/'), Number); clientObserveAttr - .forEach(e => { - if (e.id === idKeys[0]) { - const instance = e.instances.find(itrInstance => itrInstance.id === idKeys[1]); - if (isNotNullOrUndefined(instance)) { - instance.resources.find(resource => resource.id === idKeys[2])[nameParameter] = true; - } + .forEach(e => { + if (e.id === idKeys[0]) { + const instance = e.instances.find(itrInstance => itrInstance.id === idKeys[1]); + if (isNotNullOrUndefined(instance)) { + instance.resources.find(resource => resource.id === idKeys[2])[nameParameter] = true; } - }); + } + }); }); } - updateKeyNameObjects(nameJson: JsonObject, clientObserveAttr: ObjectLwM2M[]): void { + private updateKeyNameObjects = (nameJson: JsonObject, clientObserveAttr: ObjectLwM2M[]): void => { const keyName = JSON.parse(JSON.stringify(nameJson)); Object.keys(keyName).forEach(key => { const idKeys = Array.from(key.substring(1).split('/'), Number); clientObserveAttr - .forEach(e => { - if (e.id === idKeys[0]) { - e.instances - .find(instance => instance.id === idKeys[1]).resources - .find(resource => resource.id === idKeys[2]).keyName = keyName[key]; - } - }); + .forEach(e => { + if (e.id === idKeys[0]) { + e.instances + .find(instance => instance.id === idKeys[1]).resources + .find(resource => resource.id === idKeys[2]).keyName = keyName[key]; + } + }); }); } - upDateObserveAttrTelemetryFromGroupToJson(val: ObjectLwM2M[]): void { + private upDateObserveAttrTelemetryFromGroupToJson = (val: ObjectLwM2M[]): void => { const observeArray: Array = []; const attributeArray: Array = []; const telemetryArray: Array = []; @@ -405,19 +408,22 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro this.updateKeyName(); } - sortObjectKeyPathJson(key, value) { + sortObjectKeyPathJson = (key: string, value: object): object => { if (key === 'keyName') { return Object.keys(value).sort((a, b) => { const aLC = Array.from(a.substring(1).split('/'), Number); const bLC = Array.from(b.substring(1).split('/'), Number); return aLC[0] === bLC[0] ? aLC[1] - bLC[1] : aLC[0] - bLC[0]; - }).reduce((r, k) => r[k] = value[k], {}); + }).reduce((obj, keySort) => { + obj[keySort] = value[keySort]; + return obj; + }, {}); } else { return value; } } - updateKeyName(): void { + private updateKeyName = (): void => { const paths = new Set(); if (this.configurationValue[this.observeAttr][this.attribute]) { this.configurationValue[this.observeAttr][this.attribute].forEach(path => { @@ -431,20 +437,20 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro } const keyNameNew = {}; paths.forEach(path => { - const pathParameter = this.findIndexsForIds(path); + const pathParameter = this.findIndexesForIds(path); if (pathParameter.length === 3) { keyNameNew[path] = this.lwm2mDeviceProfileTransportConfFormGroup.get('observeAttrTelemetry').value - .clientLwM2M[pathParameter[0]].instances[pathParameter[1]].resources[pathParameter[2]][this.keyName]; + .clientLwM2M[pathParameter[0]].instances[pathParameter[1]].resources[pathParameter[2]][this.keyName]; } }); this.configurationValue[this.observeAttr][this.keyName] = this.sortObjectKeyPathJson('keyName', keyNameNew); } - findIndexsForIds(path: string): number[] { + private findIndexesForIds = (path: string): number[] => { const pathParameter = Array.from(path.substring(1).split('/'), Number); const pathParameterIndexes: number[] = []; const objectsOld = deepClone( - this.lwm2mDeviceProfileTransportConfFormGroup.get('observeAttrTelemetry').value.clientLwM2M) as ObjectLwM2M[]; + this.lwm2mDeviceProfileTransportConfFormGroup.get('observeAttrTelemetry').value.clientLwM2M) as ObjectLwM2M[]; let isIdIndex = (element) => element.id === pathParameter[0]; const objIndex = objectsOld.findIndex(isIdIndex); if (objIndex >= 0) { @@ -463,7 +469,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro return pathParameterIndexes; } - getObjectsFromJsonAllConfig(): number [] { + private getObjectsFromJsonAllConfig = (): number [] => { const objectsIds = new Set(); if (this.configurationValue[this.observeAttr]) { if (this.configurationValue[this.observeAttr][this.observe]) { @@ -485,7 +491,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro return (objectsIds.size > 0) ? Array.from(objectsIds) : null; } - upDateJsonAllConfig(): void { + private upDateJsonAllConfig = (): void => { this.lwm2mDeviceProfileTransportConfFormGroup.patchValue({ configurationJson: this.configurationValue }, {emitEvent: false}); @@ -494,11 +500,11 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro }); } - addObjectsList(value: ObjectLwM2M[]): void { + addObjectsList = (value: ObjectLwM2M[]): void => { this.updateObserveAttrTelemetryObjectFormGroup(deepClone(value)); } - removeObjectsList(value: ObjectLwM2M): void { + removeObjectsList = (value: ObjectLwM2M): void => { const objectsOld = deepClone(this.lwm2mDeviceProfileTransportConfFormGroup.get('observeAttrTelemetry').value.clientLwM2M); const isIdIndex = (element) => element.id === value.id; const index = objectsOld.findIndex(isIdIndex); @@ -514,7 +520,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro this.upDateJsonAllConfig(); } - removeObserveAttrTelemetryFromJson(observeAttrTel: string, id: number): void { + private removeObserveAttrTelemetryFromJson = (observeAttrTel: string, id: number): void => { const isIdIndex = (element) => Array.from(element.substring(1).split('/'), Number)[0] === id; let index = this.configurationValue[this.observeAttr][observeAttrTel].findIndex(isIdIndex); while (index >= 0) { @@ -523,12 +529,12 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro } } - removeKeyNameFromJson(id: number): void { - const keyNmaeJson = this.configurationValue[this.observeAttr][this.keyName]; - Object.keys(keyNmaeJson).forEach(key => { + private removeKeyNameFromJson = (id: number): void => { + const keyNameJson = this.configurationValue[this.observeAttr][this.keyName]; + Object.keys(keyNameJson).forEach(key => { const idKey = Array.from(key.substring(1).split('/'), Number)[0]; if (idKey === id) { - delete keyNmaeJson[key]; + delete keyNameJson[key]; } }); } @@ -538,10 +544,10 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro if (!isPath) { isPath = this.findPathInJson(path, this.telemetry); } - return (isPath) ? true : false; + return !!isPath; } - findPathInJson(path: string, side: string): string { + private findPathInJson = (path: string, side: string): string => { if (this.configurationValue[this.observeAttr]) { if (this.configurationValue[this.observeAttr][side]) { return this.configurationValue[this.observeAttr][side].find( diff --git a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-object-add-instances-list.component.ts b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-object-add-instances-list.component.ts index 44d9066440..acbebca313 100644 --- a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-object-add-instances-list.component.ts +++ b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-object-add-instances-list.component.ts @@ -21,44 +21,45 @@ import { OnInit, ViewChild, ElementRef, -} from "@angular/core"; +} from '@angular/core'; import { ControlValueAccessor, FormBuilder, FormGroup, NG_VALUE_ACCESSOR, Validators -} from "@angular/forms"; -import { coerceBooleanProperty } from "@angular/cdk/coercion"; -import { Store } from "@ngrx/store"; -import { AppState } from "@core/core.state"; +} from '@angular/forms'; +import { coerceBooleanProperty } from '@angular/cdk/coercion'; +import { Store } from '@ngrx/store'; +import { AppState } from '@core/core.state'; import { MatChipList } from '@angular/material/chips'; import { INSTANCES_ID_VALUE_MAX, INSTANCES_ID_VALUE_MIN -} from "./profile-config.models"; -import { TranslateService } from "@ngx-translate/core"; -import { DeviceProfileService } from "@core/http/device-profile.service"; +} from './profile-config.models'; +import { TranslateService } from '@ngx-translate/core'; +import { DeviceProfileService } from '@core/http/device-profile.service'; @Component({ selector: 'tb-profile-lwm2m-object-add-instances-list', templateUrl: './lwm2m-object-add-instances-list.component.html', styleUrls: ['./lwm2m-object-add-instances-list.component.scss'], providers: [{ - provide: NG_VALUE_ACCESSOR, - useExisting: forwardRef(() => Lwm2mObjectAddInstancesListComponent), - multi: true - }] + provide: NG_VALUE_ACCESSOR, + useExisting: forwardRef(() => Lwm2mObjectAddInstancesListComponent), + multi: true + }] }) export class Lwm2mObjectAddInstancesListComponent implements ControlValueAccessor, OnInit, Validators { - lwm2mObjectListFormGroup: FormGroup; private requiredValue: boolean; - private instancesIdsList: Set | null; - filteredObjectsList: Array; private disabled = false as boolean; private dirty = false as boolean; - instanceIdValueMin = INSTANCES_ID_VALUE_MIN as number - instanceIdValueMax = INSTANCES_ID_VALUE_MAX as number + + lwm2mObjectListFormGroup: FormGroup; + instancesIdsList: Set | null; + filteredObjectsList: Array; + instanceIdValueMin = INSTANCES_ID_VALUE_MIN as number; + instanceIdValueMax = INSTANCES_ID_VALUE_MAX as number; get required(): boolean { return this.requiredValue; @@ -76,8 +77,7 @@ export class Lwm2mObjectAddInstancesListComponent implements ControlValueAccesso @ViewChild('instanceIdInput') instanceIdInput: ElementRef; @ViewChild('chipList', {static: true}) chipList: MatChipList; - private propagateChange = (v: any) => { - }; + private propagateChange = (v: any) => { }; constructor(private store: Store, public translate: TranslateService, @@ -89,7 +89,7 @@ export class Lwm2mObjectAddInstancesListComponent implements ControlValueAccesso }); } - updateValidators() { + private updateValidators = (): void => { this.lwm2mObjectListFormGroup.get('instanceIdInput').setValidators([ Validators.min(this.instanceIdValueMin), Validators.max(this.instanceIdValueMax)]); @@ -128,41 +128,41 @@ export class Lwm2mObjectAddInstancesListComponent implements ControlValueAccesso this.dirty = false; } - add(value: number): void { + add = (value: number): void => { if (!isNaN(value) && this.lwm2mObjectListFormGroup.get('instanceIdInput').valid) { this.instancesIdsList.add(value); this.lwm2mObjectListFormGroup.get('instancesIdsList').setValue(this.instancesIdsList); this.propagateChange(this.instancesIdsList); - this.dirty = true + this.dirty = true; } this.clear(); } - remove(object: number) { + remove = (object: number): void => { this.instancesIdsList.delete(object); this.lwm2mObjectListFormGroup.get('instancesIdsList').setValue(this.instancesIdsList); this.propagateChange(this.instancesIdsList); - this.dirty = true + this.dirty = true; this.clear(); } + // + // displayFn(object?: number): number | undefined { + // return object ? object : undefined; + // } - displayFn(object?: number): number | undefined { - return object ? object : undefined; - } - - clear() { + private clear = (): void => { this.lwm2mObjectListFormGroup.get('instanceIdInput').patchValue(null, {emitEvent: true}); - this.instanceIdInput.nativeElement.value = ""; + this.instanceIdInput.nativeElement.value = ''; setTimeout(() => { this.instanceIdInput.nativeElement.blur(); this.instanceIdInput.nativeElement.focus(); }, 0); } - onkeydown(e: KeyboardEvent) { - if (e.keyCode == 189 || e.keyCode == 187 || e.keyCode == 109 || e.keyCode == 107) { + onkeydown = (e: KeyboardEvent): boolean => { + if (e.keyCode === 189 || e.keyCode === 187 || e.keyCode === 109 || e.keyCode === 107) { return false; - } else if (e.keyCode == 8) { + } else if (e.keyCode === 8) { if (this.lwm2mObjectListFormGroup.get('instanceIdInput').value == null) { this.clear(); } @@ -170,7 +170,7 @@ export class Lwm2mObjectAddInstancesListComponent implements ControlValueAccesso } } - onFocus() { + onFocus = (): void => { if (this.dirty) { this.lwm2mObjectListFormGroup.get('instanceIdInput').updateValueAndValidity({onlySelf: true, emitEvent: true}); this.dirty = false; diff --git a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-object-add-instances.component.html b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-object-add-instances.component.html index b8d10689e1..6dafeb6024 100644 --- a/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-object-add-instances.component.html +++ b/ui-ngx/src/app/modules/home/components/profile/device/lwm2m/lwm2m-object-add-instances.component.html @@ -42,7 +42,7 @@