Commit 0710d272a1bb1b9121e73b8ed1705c5e57c81760

Authored by nickAS21
1 parent af57637d

lwm2m: front fix bug create device lwm2mConfig default

... ... @@ -84,16 +84,18 @@
84 84 </mat-form-field>
85 85 <mat-form-field class="mat-block">
86 86 <mat-label translate>device.lwm2m-value</mat-label>
87   - <textarea matInput formControlName="credentialsValue" rows="10" required></textarea>
  87 + <textarea matInput formControlName="credentialsValue" rows="10" required
  88 + [matTooltip]="lwm2mCredentialsValueTip(deviceCredentialsFormGroup.get('credentialsValue').hasError('jsonError'))"
  89 + matTooltipPosition="above"
  90 + ></textarea>
88 91 <mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('required')">
89 92 {{ 'device.lwm2m-value-required' | translate }}
90 93 </mat-error>
91 94 <mat-error *ngIf="deviceCredentialsFormGroup.get('credentialsValue').hasError('jsonError')">
92   - {{ 'device.lwm2m-value-json-error' | translate }}
  95 + {{ 'device.lwm2m-value-format-error' | translate }}
93 96 </mat-error>
94 97 <div mat-dialog-actions fxLayoutAlign="center center">
95 98 <button mat-raised-button color="primary"
96   - matTooltip="{{'device.lwm2m-value-edit-tip' | translate }}"
97 99 (click)="openSecurityInfoLwM2mDialog($event)"
98 100 >
99 101 {{'device.lwm2m-value-edit' | translate }}
... ...
... ... @@ -37,11 +37,13 @@ import {Subscription} from 'rxjs';
37 37 import {distinctUntilChanged} from 'rxjs/operators';
38 38 import {SecurityConfigComponent} from '@home/pages/device/lwm2m/security-config.component';
39 39 import {
  40 + ClientSecurityConfig,
40 41 DEFAULT_END_POINT,
41 42 DeviceCredentialsDialogLwm2mData,
42 43 END_POINT,
43 44 getDefaultSecurityConfig,
44   - JSON_ALL_CONFIG, SecurityConfigModels, validateSecurityConfig
  45 + JSON_ALL_CONFIG,
  46 + validateSecurityConfig
45 47 } from '@home/pages/device/lwm2m/security-config.models';
46 48 import {TranslateService} from '@ngx-translate/core';
47 49 import {MatDialog} from '@angular/material/dialog';
... ... @@ -129,6 +131,8 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
129 131 let credentialsValue = null;
130 132 if (value.credentialsType === DeviceCredentialsType.MQTT_BASIC) {
131 133 credentialsBasic = JSON.parse(value.credentialsValue) as DeviceCredentialMQTTBasic;
  134 + } else if (value.credentialsType === DeviceCredentialsType.LWM2M_CREDENTIALS) {
  135 + credentialsValue = JSON.parse(JSON.stringify(value.credentialsValue)) as ClientSecurityConfig;
132 136 } else {
133 137 credentialsValue = value.credentialsValue;
134 138 }
... ... @@ -178,7 +182,7 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
178 182 credentialsTypeChanged(): void {
179 183 this.deviceCredentialsFormGroup.patchValue({
180 184 credentialsId: null,
181   - credentialsValue: null,
  185 + credentialsValue: JSON.stringify(getDefaultSecurityConfig(), null, 2),
182 186 credentialsBasic: {clientId: '', userName: '', password: ''}
183 187 });
184 188 this.updateValidators();
... ... @@ -203,7 +207,7 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
203 207 this.deviceCredentialsFormGroup.get('credentialsBasic').disable({emitEvent: false});
204 208 break;
205 209 case DeviceCredentialsType.LWM2M_CREDENTIALS:
206   - this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required, this.jsonValidator]);
  210 + this.deviceCredentialsFormGroup.get('credentialsValue').setValidators([Validators.required, this.lwm2mConfigJsonValidator]);
207 211 this.deviceCredentialsFormGroup.get('credentialsValue').updateValueAndValidity({emitEvent: false});
208 212 this.deviceCredentialsFormGroup.get('credentialsId').setValidators([]);
209 213 this.deviceCredentialsFormGroup.get('credentialsId').updateValueAndValidity({emitEvent: false});
... ... @@ -284,7 +288,12 @@ export class DeviceCredentialsComponent implements ControlValueAccessor, OnInit,
284 288 return Object.keys(response).length === 0 || JSON.stringify(response) === '[{}]';
285 289 }
286 290
287   - private jsonValidator(control: FormControl) {
  291 + private lwm2mConfigJsonValidator(control: FormControl) {
288 292 return validateSecurityConfig(control.value) ? null: {jsonError: {parsedJson: "error"}};
289 293 }
  294 +
  295 + lwm2mCredentialsValueTip (flag: boolean): string {
  296 + let jsonConfigDef = JSON.stringify(getDefaultSecurityConfig(), null, 2);
  297 + return !flag ? "" : 'Example (mode=\"NoSec\"):\n\r ' + jsonConfigDef;
  298 + }
290 299 }
... ...
... ... @@ -20,7 +20,7 @@
20 20 <mat-tab label="{{ 'device-profile.lwm2m.model-tab' | translate }}">
21 21 <ng-template matTabContent>
22 22 <section [formGroup]="lwm2mDeviceProfileFormGroup">
23   - <div class="mat-padding" style="padding-bottom: 0px">
  23 + <div *ngIf="false" class="mat-padding" style="padding-bottom: 0px">
24 24 <mat-form-field class="mat-block">
25 25 <mat-label>{{ 'device-profile.lwm2m.client-only-observe-after-connect-label' | translate }}</mat-label>
26 26 <mat-select formControlName="clientOnlyObserveAfterConnect"
... ...
... ... @@ -15,23 +15,20 @@
15 15 ///
16 16
17 17
18   -import { Component, Inject, OnInit } from '@angular/core';
19   -import { DialogComponent } from '@shared/components/dialog.component';
20   -import { Store } from '@ngrx/store';
21   -import { AppState } from '@core/core.state';
22   -import { Router } from '@angular/router';
23   -import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
24   -import { FormBuilder, FormGroup, Validators } from '@angular/forms';
25   -import { TranslateService } from '@ngx-translate/core';
  18 +import {Component, Inject, OnInit} from '@angular/core';
  19 +import {DialogComponent} from '@shared/components/dialog.component';
  20 +import {Store} from '@ngrx/store';
  21 +import {AppState} from '@core/core.state';
  22 +import {Router} from '@angular/router';
  23 +import {MAT_DIALOG_DATA, MatDialogRef} from '@angular/material/dialog';
  24 +import {FormBuilder, FormGroup, Validators} from '@angular/forms';
  25 +import {TranslateService} from '@ngx-translate/core';
26 26 import {
27 27 BOOTSTRAP_SERVER,
28 28 BOOTSTRAP_SERVERS,
29   - ClientSecurityConfigNoSEC,
30   - ClientSecurityConfigPSK,
31   - ClientSecurityConfigRPK,
32   - ClientSecurityConfigX509,
  29 + ClientSecurityConfig,
33 30 DeviceCredentialsDialogLwm2mData,
34   - getDefaultClientSecurityConfigType,
  31 + getClientSecurityConfig,
35 32 JSON_ALL_CONFIG,
36 33 KEY_REGEXP_HEX_DEC,
37 34 LEN_MAX_PSK,
... ... @@ -41,9 +38,9 @@ import {
41 38 SECURITY_CONFIG_MODE_NAMES,
42 39 SecurityConfigModels
43 40 } from './security-config.models';
44   -import { WINDOW } from '@core/services/window.service';
45   -import { MatTabChangeEvent } from '@angular/material/tabs';
46   -import { MatTab } from '@angular/material/tabs/tab';
  41 +import {WINDOW} from '@core/services/window.service';
  42 +import {MatTabChangeEvent} from '@angular/material/tabs';
  43 +import {MatTab} from '@angular/material/tabs/tab';
47 44
48 45 @Component({
49 46 selector: 'tb-security-config-lwm2m',
... ... @@ -115,20 +112,20 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
115 112 case SECURITY_CONFIG_MODE.NO_SEC:
116 113 break;
117 114 case SECURITY_CONFIG_MODE.PSK:
118   - const clientSecurityConfigPSK = jsonAllConfig.client as ClientSecurityConfigPSK;
  115 + const clientSecurityConfigPSK = jsonAllConfig.client as ClientSecurityConfig;
119 116 this.lwm2mConfigFormGroup.patchValue({
120 117 identityPSK: clientSecurityConfigPSK.identity,
121 118 clientKey: clientSecurityConfigPSK.key,
122 119 }, {emitEvent: false});
123 120 break;
124 121 case SECURITY_CONFIG_MODE.RPK:
125   - const clientSecurityConfigRPK = jsonAllConfig.client as ClientSecurityConfigRPK;
  122 + const clientSecurityConfigRPK = jsonAllConfig.client as ClientSecurityConfig;
126 123 this.lwm2mConfigFormGroup.patchValue({
127 124 clientKey: clientSecurityConfigRPK.key,
128 125 }, {emitEvent: false});
129 126 break;
130 127 case SECURITY_CONFIG_MODE.X509:
131   - const clientSecurityConfigX509 = jsonAllConfig.client as ClientSecurityConfigX509;
  128 + const clientSecurityConfigX509 = jsonAllConfig.client as ClientSecurityConfig;
132 129 this.lwm2mConfigFormGroup.patchValue({
133 130 clientCertificate: clientSecurityConfigX509.x509
134 131 }, {emitEvent: false});
... ... @@ -140,7 +137,7 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
140 137 securityConfigClientModeChanged = (mode: SECURITY_CONFIG_MODE): void => {
141 138 switch (mode) {
142 139 case SECURITY_CONFIG_MODE.NO_SEC:
143   - const clientSecurityConfigNoSEC = getDefaultClientSecurityConfigType(mode) as ClientSecurityConfigNoSEC;
  140 + const clientSecurityConfigNoSEC = getClientSecurityConfig(mode) as ClientSecurityConfig;
144 141 this.jsonAllConfig.client = clientSecurityConfigNoSEC;
145 142 this.lwm2mConfigFormGroup.patchValue({
146 143 jsonAllConfig: this.jsonAllConfig,
... ... @@ -148,8 +145,8 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
148 145 }, {emitEvent: false});
149 146 break;
150 147 case SECURITY_CONFIG_MODE.PSK:
151   - const clientSecurityConfigPSK = getDefaultClientSecurityConfigType(mode, this.lwm2mConfigFormGroup.get('endPoint')
152   - .value) as ClientSecurityConfigPSK;
  148 + const clientSecurityConfigPSK = getClientSecurityConfig(mode, this.lwm2mConfigFormGroup.get('endPoint')
  149 + .value) as ClientSecurityConfig;
153 150 clientSecurityConfigPSK.identity = this.data.endPoint;
154 151 clientSecurityConfigPSK.key = this.lwm2mConfigFormGroup.get('clientKey').value;
155 152 this.jsonAllConfig.client = clientSecurityConfigPSK;
... ... @@ -159,7 +156,7 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
159 156 }, {emitEvent: false});
160 157 break;
161 158 case SECURITY_CONFIG_MODE.RPK:
162   - const clientSecurityConfigRPK = getDefaultClientSecurityConfigType(mode) as ClientSecurityConfigRPK;
  159 + const clientSecurityConfigRPK = getClientSecurityConfig(mode) as ClientSecurityConfig;
163 160 clientSecurityConfigRPK.key = this.lwm2mConfigFormGroup.get('clientKey').value;
164 161 this.jsonAllConfig.client = clientSecurityConfigRPK;
165 162 this.lwm2mConfigFormGroup.patchValue({
... ... @@ -167,7 +164,7 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
167 164 }, {emitEvent: false});
168 165 break;
169 166 case SECURITY_CONFIG_MODE.X509:
170   - this.jsonAllConfig.client = getDefaultClientSecurityConfigType(mode) as ClientSecurityConfigX509;
  167 + this.jsonAllConfig.client = getClientSecurityConfig(mode) as ClientSecurityConfig;
171 168 this.lwm2mConfigFormGroup.patchValue({
172 169 clientCertificate: true
173 170 }, {emitEvent: false});
... ... @@ -236,11 +233,11 @@ export class SecurityConfigComponent extends DialogComponent<SecurityConfigCompo
236 233 if (this.lwm2mConfigFormGroup !== null) {
237 234 if (!this.lwm2mConfigFormGroup.get('endPoint').pristine && this.lwm2mConfigFormGroup.get('endPoint').valid) {
238 235 this.data.endPoint = this.lwm2mConfigFormGroup.get('endPoint').value;
239   - // Client mode == PSK
  236 + /** Client mode == PSK */
240 237 if (this.lwm2mConfigFormGroup.get('securityConfigClientMode').value === SECURITY_CONFIG_MODE.PSK) {
241 238 const endPoint = 'endpoint';
242 239 this.jsonAllConfig.client[endPoint] = this.data.endPoint;
243   - this.jsonAllConfig.client[endPoint].markAsPristine({
  240 + this.lwm2mConfigFormGroup.get('endPoint').markAsPristine({
244 241 onlySelf: true
245 242 });
246 243 this.upDateJsonAllConfig();
... ...
... ... @@ -48,33 +48,14 @@ export const SECURITY_CONFIG_MODE_NAMES = new Map<SECURITY_CONFIG_MODE, string>(
48 48 ]
49 49 );
50 50
51   -export type ClientSecurityConfigType =
52   - ClientSecurityConfigPSK
53   - | ClientSecurityConfigRPK
54   - | ClientSecurityConfigX509
55   - | ClientSecurityConfigNoSEC;
56   -
57   -export interface ClientSecurityConfigPSK {
  51 +export interface ClientSecurityConfig {
58 52 securityConfigClientMode: string;
59 53 endpoint: string;
60 54 identity: string;
61 55 key: string;
62   -}
63   -
64   -export interface ClientSecurityConfigRPK {
65   - securityConfigClientMode: string;
66   - key: string;
67   -}
68   -
69   -export interface ClientSecurityConfigX509 {
70   - securityConfigClientMode: string;
71 56 x509: boolean;
72 57 }
73 58
74   -export interface ClientSecurityConfigNoSEC {
75   - securityConfigClientMode: string;
76   -}
77   -
78 59 export interface ServerSecurityConfig {
79 60 securityMode: string;
80 61 clientPublicKeyOrId?: string;
... ... @@ -87,43 +68,36 @@ interface BootstrapSecurityConfig {
87 68 }
88 69
89 70 export interface SecurityConfigModels {
90   - client: ClientSecurityConfigType;
  71 + client: ClientSecurityConfig;
91 72 bootstrap: BootstrapSecurityConfig;
92 73 }
93 74
94   -export function getDefaultClientSecurityConfigType(securityConfigMode: SECURITY_CONFIG_MODE, endPoint?: string): ClientSecurityConfigType {
95   - let security: ClientSecurityConfigType;
  75 +export function getClientSecurityConfig(securityConfigMode: SECURITY_CONFIG_MODE, endPoint?: string): ClientSecurityConfig {
  76 + let security = getDefaultClientSecurityConfig();
  77 + security.securityConfigClientMode = securityConfigMode.toString();
96 78 switch (securityConfigMode) {
97 79 case SECURITY_CONFIG_MODE.PSK:
98   - security = {
99   - securityConfigClientMode: '',
100   - endpoint: endPoint,
101   - identity: endPoint,
102   - key: ''
103   - };
104   - break;
105   - case SECURITY_CONFIG_MODE.RPK:
106   - security = {
107   - securityConfigClientMode: '',
108   - key: ''
109   - };
  80 + security.endpoint = endPoint;
  81 + security.identity = endPoint;
110 82 break;
111 83 case SECURITY_CONFIG_MODE.X509:
112   - security = {
113   - securityConfigClientMode: '',
114   - x509: true
115   - };
116   - break;
117   - case SECURITY_CONFIG_MODE.NO_SEC:
118   - security = {
119   - securityConfigClientMode: ''
120   - };
  84 + security.x509 = true;
121 85 break;
122 86 }
123   - security.securityConfigClientMode = securityConfigMode.toString();
  87 +
124 88 return security;
125 89 }
126 90
  91 +export function getDefaultClientSecurityConfig(): ClientSecurityConfig {
  92 + return {
  93 + securityConfigClientMode: SECURITY_CONFIG_MODE.NO_SEC.toString(),
  94 + endpoint: '',
  95 + identity: '',
  96 + key: '',
  97 + x509: false
  98 + };
  99 +}
  100 +
127 101 export function getDefaultServerSecurityConfig(): ServerSecurityConfig {
128 102 return {
129 103 securityMode: SECURITY_CONFIG_MODE.NO_SEC.toString(),
... ... @@ -141,7 +115,7 @@ function getDefaultBootstrapSecurityConfig(): BootstrapSecurityConfig {
141 115
142 116 export function getDefaultSecurityConfig(): SecurityConfigModels {
143 117 const securityConfigModels = {
144   - client: getDefaultClientSecurityConfigType(SECURITY_CONFIG_MODE.NO_SEC),
  118 + client: getClientSecurityConfig(SECURITY_CONFIG_MODE.NO_SEC),
145 119 bootstrap: getDefaultBootstrapSecurityConfig()
146 120 };
147 121 return securityConfigModels;
... ... @@ -153,7 +127,7 @@ const isSecurityConfigModels = (p: any): p is SecurityConfigModels =>
153 127 p.hasOwnProperty('bootstrap') &&
154 128 isBootstrapSecurityConfig(p['bootstrap']);
155 129
156   -const isClientSecurityConfigType = (p: any): p is ClientSecurityConfigType =>
  130 +const isClientSecurityConfigType = (p: any): p is ClientSecurityConfig =>
157 131 p.hasOwnProperty('securityConfigClientMode') &&
158 132 p.hasOwnProperty('endpoint') &&
159 133 p.hasOwnProperty('identity') &&
... ...
... ... @@ -888,11 +888,11 @@
888 888 "lwm2m-key-required": "LwM2M Security config key is required.",
889 889 "lwm2m-value": "LwM2M Security config",
890 890 "lwm2m-value-required": "LwM2M Security config value is required.",
891   - "lwm2m-value-json-error": "LwM2M Security config value is not json format.",
  891 + "lwm2m-value-format-error": "Security config value must be in LwM2M Security config format.",
892 892 "lwm2m-endpoint": "Client endpoint/identity",
893 893 "lwm2m-security-info": "Security Config Info",
894 894 "lwm2m-value-edit": "Edit Security config",
895   - "lwm2m-value-edit-tip": "Edit security config json editor",
  895 + "lwm2m-credentials-value-tip": "Edit security config json editor",
896 896 "lwm2m-security-config": {
897 897 "identity": "Client Identity",
898 898 "client-key": "Client Key",
... ...