Commit ca940ba1b6619802b9c1b6abcf26e0a1e3416588

Authored by Vladyslav_Prykhodko
1 parent a8e09391

UI: Refactoring LwM2M

... ... @@ -75,15 +75,15 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro
75 75 }
76 76
77 77 private propagateChange = (v: any) => {
78   - };
  78 + }
79 79
80 80 constructor(private store: Store<AppState>,
81 81 private fb: FormBuilder,
82 82 private deviceProfileService: DeviceProfileService,
83 83 @Inject(WINDOW) private window: Window) {
84 84 this.lwm2mDeviceProfileFormGroup = this.fb.group({
85   - objectIds: [{}, Validators.required],
86   - observeAttrTelemetry: [{clientLwM2M: []}, Validators.required],
  85 + objectIds: [[], Validators.required],
  86 + observeAttrTelemetry: [null, Validators.required],
87 87 shortId: [null, Validators.required],
88 88 lifetime: [null, Validators.required],
89 89 defaultMinPeriod: [null, Validators.required],
... ... @@ -95,10 +95,10 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro
95 95 this.lwm2mDeviceConfigFormGroup = this.fb.group({
96 96 configurationJson: [null, Validators.required]
97 97 });
98   - this.lwm2mDeviceProfileFormGroup.valueChanges.subscribe(() => {
  98 + this.lwm2mDeviceProfileFormGroup.valueChanges.subscribe((value) => {
99 99 console.warn('main form');
100 100 if (!this.disabled) {
101   - this.updateModel();
  101 + this.updateDeviceProfileValue(value);
102 102 }
103 103 });
104 104 this.lwm2mDeviceConfigFormGroup.valueChanges.subscribe(() => {
... ... @@ -159,7 +159,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro
159 159 const objectsList = value.objectsList;
160 160 this.lwm2mDeviceProfileFormGroup.patchValue({
161 161 objectIds: value,
162   - observeAttrTelemetry: {clientLwM2M: this.getObserveAttrTelemetryObjects(objectsList)},
  162 + observeAttrTelemetry: this.getObserveAttrTelemetryObjects(objectsList),
163 163 shortId: this.configurationValue.bootstrap.servers.shortId,
164 164 lifetime: this.configurationValue.bootstrap.servers.lifetime,
165 165 defaultMinPeriod: this.configurationValue.bootstrap.servers.defaultMinPeriod,
... ... @@ -173,9 +173,8 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro
173 173
174 174 private updateModel = (): void => {
175 175 let configuration: DeviceProfileTransportConfiguration = null;
176   - if (this.lwm2mDeviceConfigFormGroup.valid) {
177   - this.upDateValueToJson();
178   - configuration = this.lwm2mDeviceConfigFormGroup.getRawValue().configurationJson;
  176 + if (this.lwm2mDeviceConfigFormGroup.valid && this.lwm2mDeviceProfileFormGroup.valid) {
  177 + configuration = this.lwm2mDeviceConfigFormGroup.value.configurationJson;
179 178 configuration.type = DeviceTransportType.LWM2M;
180 179 }
181 180 this.propagateChange(configuration);
... ... @@ -183,89 +182,31 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro
183 182
184 183 private updateObserveAttrTelemetryObjectFormGroup = (objectsList: ObjectLwM2M[]): void => {
185 184 this.lwm2mDeviceProfileFormGroup.patchValue({
186   - observeAttrTelemetry: {clientLwM2M: this.getObserveAttrTelemetryObjects(objectsList)}
  185 + observeAttrTelemetry: this.getObserveAttrTelemetryObjects(objectsList)
187 186 },
188 187 {emitEvent: false});
189   - this.lwm2mDeviceProfileFormGroup.get('observeAttrTelemetry').markAsPristine({
190   - onlySelf: true
191   - });
192   - }
193   -
194   - private upDateValueToJson = (): void => {
195   - this.upDateValueToJsonTab0();
196   - this.upDateValueToJsonTab1();
197   - }
198   -
199   - private upDateValueToJsonTab0 = (): void => {
200   - if (!this.lwm2mDeviceProfileFormGroup.get('observeAttrTelemetry').pristine) {
201   - this.upDateObserveAttrTelemetryFromGroupToJson(
202   - this.lwm2mDeviceProfileFormGroup.get('observeAttrTelemetry').value.clientLwM2M
203   - );
204   - this.lwm2mDeviceProfileFormGroup.get('observeAttrTelemetry').markAsPristine({
205   - onlySelf: true
206   - });
207   - this.upDateJsonAllConfig();
208   - }
209   - }
210   -
211   - private upDateValueToJsonTab1 = (): void => {
212   - this.upDateValueServersToJson();
213   - if (!this.lwm2mDeviceProfileFormGroup.get('bootstrapServer').pristine) {
214   - this.configurationValue.bootstrap.bootstrapServer = this.lwm2mDeviceProfileFormGroup.get('bootstrapServer').value;
215   - this.lwm2mDeviceProfileFormGroup.get('bootstrapServer').markAsPristine({
216   - onlySelf: true
217   - });
218   - this.upDateJsonAllConfig();
219   - }
220   - if (!this.lwm2mDeviceProfileFormGroup.get('lwm2mServer').pristine) {
221   - this.configurationValue.bootstrap.lwm2mServer = this.lwm2mDeviceProfileFormGroup.get('lwm2mServer').value;
222   - this.lwm2mDeviceProfileFormGroup.get('lwm2mServer').markAsPristine({
223   - onlySelf: true
224   - });
225   - this.upDateJsonAllConfig();
226   - }
227   - }
228   -
229   - private upDateValueServersToJson = (): void => {
230   - const bootstrapServers = this.configurationValue.bootstrap.servers;
231   - if (!this.lwm2mDeviceProfileFormGroup.get('shortId').pristine) {
232   - bootstrapServers.shortId = this.lwm2mDeviceProfileFormGroup.get('shortId').value;
233   - this.lwm2mDeviceProfileFormGroup.get('shortId').markAsPristine({
234   - onlySelf: true
235   - });
236   - this.upDateJsonAllConfig();
237   - }
238   - if (!this.lwm2mDeviceProfileFormGroup.get('lifetime').pristine) {
239   - bootstrapServers.lifetime = this.lwm2mDeviceProfileFormGroup.get('lifetime').value;
240   - this.lwm2mDeviceProfileFormGroup.get('lifetime').markAsPristine({
241   - onlySelf: true
242   - });
243   - this.upDateJsonAllConfig();
244   - }
245   - if (!this.lwm2mDeviceProfileFormGroup.get('defaultMinPeriod').pristine) {
246   - bootstrapServers.defaultMinPeriod = this.lwm2mDeviceProfileFormGroup.get('defaultMinPeriod').value;
247   - this.lwm2mDeviceProfileFormGroup.get('defaultMinPeriod').markAsPristine({
248   - onlySelf: true
249   - });
250   - this.upDateJsonAllConfig();
251   - }
252   - if (!this.lwm2mDeviceProfileFormGroup.get('notifIfDisabled').pristine) {
253   - bootstrapServers.notifIfDisabled = this.lwm2mDeviceProfileFormGroup.get('notifIfDisabled').value;
254   - this.lwm2mDeviceProfileFormGroup.get('notifIfDisabled').markAsPristine({
255   - onlySelf: true
256   - });
257   - this.upDateJsonAllConfig();
258   - }
259   - if (!this.lwm2mDeviceProfileFormGroup.get('binding').pristine) {
260   - bootstrapServers.binding = this.lwm2mDeviceProfileFormGroup.get('binding').value;
261   - this.lwm2mDeviceProfileFormGroup.get('binding').markAsPristine({
262   - onlySelf: true
263   - });
  188 + // this.lwm2mDeviceProfileFormGroup.get('observeAttrTelemetry').markAsPristine({
  189 + // onlySelf: true
  190 + // });
  191 + }
  192 +
  193 + private updateDeviceProfileValue(config): void {
  194 + if (this.lwm2mDeviceProfileFormGroup.valid) {
  195 + this.upDateObserveAttrTelemetryFromGroupToJson(config.observeAttrTelemetry.clientLwM2M);
  196 + this.configurationValue.bootstrap.bootstrapServer = config.bootstrapServer;
  197 + this.configurationValue.bootstrap.lwm2mServer = config.lwm2mServer;
  198 + const bootstrapServers = this.configurationValue.bootstrap.servers;
  199 + bootstrapServers.shortId = config.shortId;
  200 + bootstrapServers.lifetime = config.lifetime;
  201 + bootstrapServers.defaultMinPeriod = config.defaultMinPeriod;
  202 + bootstrapServers.notifIfDisabled = config.notifIfDisabled;
  203 + bootstrapServers.binding = config.binding;
264 204 this.upDateJsonAllConfig();
  205 + this.updateModel();
265 206 }
266 207 }
267 208
268   - private getObserveAttrTelemetryObjects = (listObject: ObjectLwM2M[]): ObjectLwM2M [] => {
  209 + private getObserveAttrTelemetryObjects = (listObject: ObjectLwM2M[]): object => {
269 210 const clientObserveAttrTelemetry = listObject;
270 211 if (this.configurationValue[this.observeAttr]) {
271 212 const observeArray = this.configurationValue[this.observeAttr][this.observe];
... ... @@ -291,7 +232,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro
291 232 clientObserveAttrTelemetry.forEach(obj => {
292 233 obj.instances.sort((a, b) => a.id - b.id);
293 234 });
294   - return clientObserveAttrTelemetry;
  235 + return {clientLwM2M: clientObserveAttrTelemetry};
295 236 }
296 237
297 238 private includesNotZeroInstance = (attribute: string[], telemetry: string[]): boolean => {
... ... @@ -480,7 +421,7 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro
480 421 return pathParameterIndexes;
481 422 }
482 423
483   - private getObjectsFromJsonAllConfig = (): number [] => {
  424 + private getObjectsFromJsonAllConfig = (): number[] => {
484 425 const objectsIds = new Set<number>();
485 426 if (this.configurationValue[this.observeAttr]) {
486 427 if (this.configurationValue[this.observeAttr][this.observe]) {
... ... @@ -503,20 +444,17 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro
503 444 }
504 445
505 446 private upDateJsonAllConfig = (): void => {
506   - this.lwm2mDeviceProfileFormGroup.patchValue({
  447 + this.lwm2mDeviceConfigFormGroup.patchValue({
507 448 configurationJson: this.configurationValue
508 449 }, {emitEvent: false});
509   - this.lwm2mDeviceProfileFormGroup.markAsPristine({
510   - onlySelf: true
511   - });
512 450 }
513 451
514 452 addObjectsList = (value: ObjectLwM2M[]): void => {
515   - this.updateObserveAttrTelemetryObjectFormGroup(deepClone(value));
  453 + this.updateObserveAttrTelemetryObjectFormGroup(value);
516 454 }
517 455
518 456 removeObjectsList = (value: ObjectLwM2M): void => {
519   - const objectsOld = deepClone(this.lwm2mDeviceProfileFormGroup.get('observeAttrTelemetry').value.clientLwM2M);
  457 + const objectsOld = this.lwm2mDeviceProfileFormGroup.get('observeAttrTelemetry').value.clientLwM2M;
520 458 const isIdIndex = (element) => element.id === value.id;
521 459 const index = objectsOld.findIndex(isIdIndex);
522 460 if (index >= 0) {
... ... @@ -526,25 +464,23 @@ export class Lwm2mDeviceProfileTransportConfigurationComponent implements Contro
526 464 this.removeObserveAttrTelemetryFromJson(this.observe, value.id);
527 465 this.removeObserveAttrTelemetryFromJson(this.telemetry, value.id);
528 466 this.removeObserveAttrTelemetryFromJson(this.attribute, value.id);
529   - this.removeObserveAttrTelemetryFromJson(this.attribute, value.id);
530 467 this.removeKeyNameFromJson(value.id);
531 468 this.upDateJsonAllConfig();
532 469 }
533 470
534 471 private removeObserveAttrTelemetryFromJson = (observeAttrTel: string, id: number): void => {
535   - const isIdIndex = (element) => Array.from(element.substring(1).split('/'), Number)[0] === id;
  472 + const isIdIndex = (element: string) => element.startsWith(`/${id}`);
536 473 let index = this.configurationValue[this.observeAttr][observeAttrTel].findIndex(isIdIndex);
537 474 while (index >= 0) {
538 475 this.configurationValue[this.observeAttr][observeAttrTel].splice(index, 1);
539   - index = this.configurationValue[this.observeAttr][observeAttrTel].findIndex(isIdIndex);
  476 + index = this.configurationValue[this.observeAttr][observeAttrTel].findIndex(isIdIndex, index);
540 477 }
541 478 }
542 479
543 480 private removeKeyNameFromJson = (id: number): void => {
544 481 const keyNameJson = this.configurationValue[this.observeAttr][this.keyName];
545 482 Object.keys(keyNameJson).forEach(key => {
546   - const idKey = Array.from(key.substring(1).split('/'), Number)[0];
547   - if (idKey === id) {
  483 + if (key.startsWith(`/${id}`)) {
548 484 delete keyNameJson[key];
549 485 }
550 486 });
... ...
... ... @@ -25,6 +25,7 @@ import { ObjectLwM2M } from './profile-config.models';
25 25 import { TranslateService } from '@ngx-translate/core';
26 26 import { DeviceProfileService } from '@core/http/device-profile.service';
27 27 import { Direction } from '@shared/models/page/sort-order';
  28 +import { isDefined } from '@core/utils';
28 29
29 30 @Component({
30 31 selector: 'tb-profile-lwm2m-object-list',
... ... @@ -114,7 +115,9 @@ export class Lwm2mObjectListComponent implements ControlValueAccessor, OnInit, V
114 115 this.disabled = isDisabled;
115 116 if (isDisabled) {
116 117 this.lwm2mListFormGroup.disable({emitEvent: false});
117   - this.clear();
  118 + if (isDefined(this.objectInput)) {
  119 + this.clear();
  120 + }
118 121 } else {
119 122 this.lwm2mListFormGroup.enable({emitEvent: false});
120 123 }
... ... @@ -144,7 +147,7 @@ export class Lwm2mObjectListComponent implements ControlValueAccessor, OnInit, V
144 147 this.lwm2mListFormGroup.get('objectsList').setValue(this.objectsList);
145 148 this.addList.next(this.objectsList);
146 149 }
147   - this.propagateChange(this.modelValue);
  150 + // this.propagateChange(this.modelValue);
148 151 this.clear();
149 152 }
150 153
... ... @@ -159,7 +162,7 @@ export class Lwm2mObjectListComponent implements ControlValueAccessor, OnInit, V
159 162 if (!this.modelValue.length) {
160 163 this.modelValue = null;
161 164 }
162   - this.propagateChange(this.modelValue);
  165 + // this.propagateChange(this.modelValue);
163 166 this.clear();
164 167 }
165 168 }
... ...
... ... @@ -124,7 +124,9 @@ export class Lwm2mObserveAttrTelemetryComponent implements ControlValueAccessor
124 124 }
125 125
126 126 writeValue(value: any): void {
127   - this.buildClientObjectsLwM2M(value.clientLwM2M);
  127 + if (isDefinedAndNotNull(value)) {
  128 + this.buildClientObjectsLwM2M(value.clientLwM2M);
  129 + }
128 130 }
129 131
130 132 private buildClientObjectsLwM2M = (objectsLwM2M: ObjectLwM2M []): void => {
... ...