Commit 4aa0b02467604023755da6132dc11c341c4b6e0f

Authored by Volodymyr Babak
1 parent 57bf6c09

Code review updates

... ... @@ -14,7 +14,7 @@
14 14 /// limitations under the License.
15 15 ///
16 16
17   -import { Inject, Injectable } from '@angular/core';
  17 +import { Injectable } from '@angular/core';
18 18 import { EMPTY, forkJoin, Observable, of, throwError } from 'rxjs';
19 19 import { HttpClient } from '@angular/common/http';
20 20 import { PageLink } from '@shared/models/page/page-link';
... ... @@ -33,7 +33,7 @@ import { Store } from '@ngrx/store';
33 33 import { AppState } from '@core/core.state';
34 34 import { Authority } from '@shared/models/authority.enum';
35 35 import { Tenant } from '@shared/models/tenant.model';
36   -import { catchError, concatMap, expand, map, mergeMap, toArray } from 'rxjs/operators';
  36 +import { catchError, concatMap, expand, map, mergeMap, retry, toArray } from 'rxjs/operators';
37 37 import { Customer } from '@app/shared/models/customer.model';
38 38 import { AssetService } from '@core/http/asset.service';
39 39 import { EntityViewService } from '@core/http/entity-view.service';
... ... @@ -52,7 +52,7 @@ import {
52 52 ImportEntityData
53 53 } from '@shared/models/entity.models';
54 54 import { EntityRelationService } from '@core/http/entity-relation.service';
55   -import { deepClone, generateSecret, guid, isDefined, isDefinedAndNotNull } from '@core/utils';
  55 +import { deepClone, isDefined, isDefinedAndNotNull } from '@core/utils';
56 56 import { Asset } from '@shared/models/asset.models';
57 57 import { Device, DeviceCredentialsType } from '@shared/models/device.models';
58 58 import { AttributeService } from '@core/http/attribute.service';
... ... @@ -74,10 +74,9 @@ import {
74 74 StringOperation
75 75 } from '@shared/models/query/query.models';
76 76 import { alarmFields } from '@shared/models/alarm.models';
77   -import { EdgeService } from "@core/http/edge.service";
  77 +import { EdgeService } from '@core/http/edge.service';
78 78 import { Edge } from '@shared/models/edge.models';
79   -import { WINDOW } from "@core/services/window.service";
80   -import { RuleChainType } from "@shared/models/rule-chain.models";
  79 +import { RuleChainType } from '@shared/models/rule-chain.models';
81 80
82 81 @Injectable({
83 82 providedIn: 'root'
... ... @@ -98,8 +97,7 @@ export class EntityService {
98 97 private dashboardService: DashboardService,
99 98 private entityRelationService: EntityRelationService,
100 99 private attributeService: AttributeService,
101   - private utils: UtilsService,
102   - @Inject(WINDOW) protected window: Window
  100 + private utils: UtilsService
103 101 ) { }
104 102
105 103 private getEntityObservable(entityType: EntityType, entityId: string,
... ... @@ -867,37 +865,7 @@ export class EntityService {
867 865
868 866 public saveEntityParameters(entityType: EntityType, entityData: ImportEntityData, update: boolean,
869 867 config?: RequestConfig): Observable<ImportEntitiesResultInfo> {
870   - let saveEntityObservable: Observable<BaseData<EntityId>>;
871   - switch (entityType) {
872   - case EntityType.DEVICE:
873   - const device: Device = {
874   - name: entityData.name,
875   - type: entityData.type,
876   - label: entityData.label,
877   - additionalInfo: {
878   - description: entityData.description
879   - }
880   - };
881   - if (entityData.gateway !== null) {
882   - device.additionalInfo = {
883   - ...device.additionalInfo,
884   - gateway: entityData.gateway
885   - };
886   - }
887   - saveEntityObservable = this.deviceService.saveDevice(device, config);
888   - break;
889   - case EntityType.ASSET:
890   - const asset: Asset = {
891   - name: entityData.name,
892   - type: entityData.type,
893   - label: entityData.label,
894   - additionalInfo: {
895   - description: entityData.description
896   - }
897   - };
898   - saveEntityObservable = this.assetService.saveAsset(asset, config);
899   - break;
900   - }
  868 + const saveEntityObservable: Observable<BaseData<EntityId>> = this.getSaveEntityObservable(entityType, entityData, config);
901 869 return saveEntityObservable.pipe(
902 870 mergeMap((entity) => {
903 871 return this.saveEntityData(entity.id, entityData, config).pipe(
... ... @@ -917,34 +885,14 @@ export class EntityService {
917 885 case EntityType.ASSET:
918 886 findEntityObservable = this.assetService.findByName(entityData.name, config);
919 887 break;
  888 + case EntityType.EDGE:
  889 + findEntityObservable = this.edgeService.findByName(entityData.name, config);
  890 + break;
920 891 }
921 892 return findEntityObservable.pipe(
922 893 mergeMap((entity) => {
923   - const tasks: Observable<any>[] = [];
924   - const result: Device | Asset = entity as (Device | Asset);
925   - const additionalInfo = result.additionalInfo || {};
926   - if (result.label !== entityData.label ||
927   - result.type !== entityData.type ||
928   - additionalInfo.description !== entityData.description ||
929   - (result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) {
930   - result.label = entityData.label;
931   - result.type = entityData.type;
932   - result.additionalInfo = additionalInfo;
933   - result.additionalInfo.description = entityData.description;
934   - if (result.id.entityType === EntityType.DEVICE) {
935   - result.additionalInfo.gateway = entityData.gateway;
936   - }
937   - switch (result.id.entityType) {
938   - case EntityType.DEVICE:
939   - tasks.push(this.deviceService.saveDevice(result, config));
940   - break;
941   - case EntityType.ASSET:
942   - tasks.push(this.assetService.saveAsset(result, config));
943   - break;
944   - }
945   - }
946   - tasks.push(this.saveEntityData(entity.id, entityData, config));
947   - return forkJoin(tasks).pipe(
  894 + const updateEntityTasks: Observable<any>[] = this.getUpdateEntityTasks(entityType, entityData, entity, config);
  895 + return forkJoin(updateEntityTasks).pipe(
948 896 map(() => {
949 897 return { update: { entity: 1 } } as ImportEntitiesResultInfo;
950 898 }),
... ... @@ -960,90 +908,128 @@ export class EntityService {
960 908 );
961 909 }
962 910
963   - public saveEdgeParameters(entityData: ImportEntityData, update: boolean,
964   - config?: RequestConfig): Observable<ImportEntitiesResultInfo> {
965   - const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
966   - const edge: Edge = {
967   - name: edgeEntityData.name,
968   - type: edgeEntityData.type,
969   - label: edgeEntityData.label,
970   - additionalInfo: {
971   - description: edgeEntityData.description
972   - },
973   - edgeLicenseKey: edgeEntityData.edgeLicenseKey,
974   - cloudEndpoint: edgeEntityData.cloudEndpoint,
975   - routingKey: edgeEntityData.routingKey,
976   - secret: edgeEntityData.secret
977   - };
978   - if (edge.cloudEndpoint === '') {
979   - edge.cloudEndpoint = this.window.location.origin;
980   - }
981   - if (edge.routingKey === '') {
982   - edge.routingKey = guid();
983   - }
984   - if (edge.secret === '') {
985   - edge.secret = generateSecret(20);
  911 + private getSaveEntityObservable(entityType: EntityType, entityData: ImportEntityData,
  912 + config?: RequestConfig): Observable<BaseData<EntityId>> {
  913 + let saveEntityObservable: Observable<BaseData<EntityId>>;
  914 + switch (entityType) {
  915 + case EntityType.DEVICE:
  916 + const device: Device = {
  917 + name: entityData.name,
  918 + type: entityData.type,
  919 + label: entityData.label,
  920 + additionalInfo: {
  921 + description: entityData.description
  922 + }
  923 + };
  924 + if (entityData.gateway !== null) {
  925 + device.additionalInfo = {
  926 + ...device.additionalInfo,
  927 + gateway: entityData.gateway
  928 + };
  929 + }
  930 + saveEntityObservable = this.deviceService.saveDevice(device, config);
  931 + break;
  932 + case EntityType.ASSET:
  933 + const asset: Asset = {
  934 + name: entityData.name,
  935 + type: entityData.type,
  936 + label: entityData.label,
  937 + additionalInfo: {
  938 + description: entityData.description
  939 + }
  940 + };
  941 + saveEntityObservable = this.assetService.saveAsset(asset, config);
  942 + break;
  943 + case EntityType.EDGE:
  944 + const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
  945 + const edge: Edge = {
  946 + name: edgeEntityData.name,
  947 + type: edgeEntityData.type,
  948 + label: edgeEntityData.label,
  949 + additionalInfo: {
  950 + description: edgeEntityData.description
  951 + },
  952 + edgeLicenseKey: edgeEntityData.edgeLicenseKey,
  953 + cloudEndpoint: edgeEntityData.cloudEndpoint,
  954 + routingKey: edgeEntityData.routingKey,
  955 + secret: edgeEntityData.secret
  956 + };
  957 + saveEntityObservable = this.edgeService.saveEdge(edge, config);
  958 + break;
986 959 }
987   - return this.edgeService.saveEdge(edge, config).pipe(
988   - mergeMap((entity) => {
989   - return this.saveEntityData(entity.id, edgeEntityData, config).pipe(
990   - map(() => {
991   - return { create: { entity: 1 } } as ImportEntitiesResultInfo;
992   - }),
993   - catchError(err => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
994   - );
995   - }),
996   - catchError(err => {
997   - if (update) {
998   - return this.edgeService.findByName(edgeEntityData.name, config).pipe(
999   - mergeMap((entity) => {
1000   - const tasks: Observable<any>[] = [];
1001   - const result: Edge = entity as Edge;
1002   - const additionalInfo = result.additionalInfo || {};
1003   - if (result.label !== edgeEntityData.label ||
1004   - result.type !== edgeEntityData.type ||
1005   - result.cloudEndpoint !== edgeEntityData.cloudEndpoint ||
1006   - result.edgeLicenseKey !== edgeEntityData.edgeLicenseKey ||
1007   - result.routingKey !== edgeEntityData.routingKey ||
1008   - result.secret !== edgeEntityData.secret ||
1009   - additionalInfo.description !== edgeEntityData.description) {
1010   - result.type = edgeEntityData.type;
1011   - if (edgeEntityData.label !== '') {
1012   - result.label = edgeEntityData.label;
1013   - }
1014   - if (edgeEntityData.description !== '') {
1015   - result.additionalInfo = additionalInfo;
1016   - result.additionalInfo.description = edgeEntityData.description;
1017   - }
1018   - if (edgeEntityData.cloudEndpoint !== '') {
1019   - result.cloudEndpoint = edgeEntityData.cloudEndpoint;
1020   - }
1021   - if (edgeEntityData.edgeLicenseKey !== '') {
1022   - result.edgeLicenseKey = edgeEntityData.edgeLicenseKey;
1023   - }
1024   - if (edgeEntityData.routingKey !== '') {
1025   - result.routingKey = edgeEntityData.routingKey;
1026   - }
1027   - if (edgeEntityData.cloudEndpoint !== '') {
1028   - result.secret = edgeEntityData.secret;
1029   - }
1030   - tasks.push(this.edgeService.saveEdge(result, config));
1031   - }
1032   - tasks.push(this.saveEntityData(entity.id, edgeEntityData, config));
1033   - return forkJoin(tasks).pipe(
1034   - map(() => {
1035   - return { update: { entity: 1 } } as ImportEntitiesResultInfo;
1036   - }),
1037   - catchError(updateError => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
1038   - );
1039   - }),
1040   - catchError(findErr => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
1041   - );
1042   - } else {
1043   - return of({ error: { entity: 1 } } as ImportEntitiesResultInfo);
  960 + return saveEntityObservable;
  961 +
  962 + }
  963 +
  964 + private getUpdateEntityTasks(entityType: EntityType, entityData: ImportEntityData | EdgeImportEntityData,
  965 + entity: BaseData<EntityId>, config?: RequestConfig): Observable<any>[] {
  966 + const tasks: Observable<any>[] = [];
  967 + let result;
  968 + let additionalInfo;
  969 + switch (entityType) {
  970 + case EntityType.EDGE:
  971 + result = entity as Edge;
  972 + additionalInfo = result.additionalInfo || {};
  973 + const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
  974 + if (result.label !== edgeEntityData.label ||
  975 + result.type !== edgeEntityData.type ||
  976 + result.cloudEndpoint !== edgeEntityData.cloudEndpoint ||
  977 + result.edgeLicenseKey !== edgeEntityData.edgeLicenseKey ||
  978 + result.routingKey !== edgeEntityData.routingKey ||
  979 + result.secret !== edgeEntityData.secret ||
  980 + additionalInfo.description !== edgeEntityData.description) {
  981 + result.type = edgeEntityData.type;
  982 + if (edgeEntityData.label !== '') {
  983 + result.label = edgeEntityData.label;
  984 + }
  985 + if (edgeEntityData.description !== '') {
  986 + result.additionalInfo = additionalInfo;
  987 + result.additionalInfo.description = edgeEntityData.description;
  988 + }
  989 + if (edgeEntityData.cloudEndpoint !== '') {
  990 + result.cloudEndpoint = edgeEntityData.cloudEndpoint;
  991 + }
  992 + if (edgeEntityData.edgeLicenseKey !== '') {
  993 + result.edgeLicenseKey = edgeEntityData.edgeLicenseKey;
  994 + }
  995 + if (edgeEntityData.routingKey !== '') {
  996 + result.routingKey = edgeEntityData.routingKey;
  997 + }
  998 + if (edgeEntityData.cloudEndpoint !== '') {
  999 + result.secret = edgeEntityData.secret;
  1000 + }
  1001 + tasks.push(this.edgeService.saveEdge(result, config));
1044 1002 }
1045   - })
1046   - );
  1003 + tasks.push(this.saveEntityData(entity.id, edgeEntityData, config));
  1004 + break;
  1005 + case EntityType.ASSET:
  1006 + case EntityType.DEVICE:
  1007 + result = entity as (Device | Asset);
  1008 + additionalInfo = result.additionalInfo || {};
  1009 + if (result.label !== entityData.label ||
  1010 + result.type !== entityData.type ||
  1011 + additionalInfo.description !== entityData.description ||
  1012 + (result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) {
  1013 + result.label = entityData.label;
  1014 + result.type = entityData.type;
  1015 + result.additionalInfo = additionalInfo;
  1016 + result.additionalInfo.description = entityData.description;
  1017 + if (result.id.entityType === EntityType.DEVICE) {
  1018 + result.additionalInfo.gateway = entityData.gateway;
  1019 + }
  1020 + switch (result.id.entityType) {
  1021 + case EntityType.DEVICE:
  1022 + tasks.push(this.deviceService.saveDevice(result, config));
  1023 + break;
  1024 + case EntityType.ASSET:
  1025 + tasks.push(this.assetService.saveAsset(result, config));
  1026 + break;
  1027 + }
  1028 + }
  1029 + tasks.push(this.saveEntityData(entity.id, entityData, config));
  1030 + break;
  1031 + }
  1032 + return tasks;
1047 1033 }
1048 1034
1049 1035 public saveEntityData(entityId: EntityId, entityData: ImportEntityData, config?: RequestConfig): Observable<any> {
... ...
... ... @@ -34,6 +34,7 @@ import {
34 34 } from '@home/components/import-export/import-export.models';
35 35 import { ImportEntitiesResultInfo, ImportEntityData, EdgeImportEntityData } from '@app/shared/models/entity.models';
36 36 import { ImportExportService } from '@home/components/import-export/import-export.service';
  37 +import { generateSecret, guid } from '@core/utils';
37 38
38 39 export interface ImportDialogCsvData {
39 40 entityType: EntityType;
... ... @@ -281,11 +282,11 @@ export class ImportDialogCsvComponent extends DialogComponent<ImportDialogCsvCom
281 282 timeseries: []
282 283 };
283 284 if (this.entityType === EntityType.EDGE) {
284   - let edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
  285 + const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
285 286 edgeEntityData.edgeLicenseKey = '';
286   - edgeEntityData.cloudEndpoint = '';
287   - edgeEntityData.routingKey = '';
288   - edgeEntityData.secret = '';
  287 + edgeEntityData.cloudEndpoint = window.location.origin;
  288 + edgeEntityData.routingKey = guid();
  289 + edgeEntityData.secret = generateSecret(20);
289 290 return edgeEntityData;
290 291 } else {
291 292 return entityData;
... ...
... ... @@ -345,11 +345,7 @@ export class ImportExportService {
345 345 const importEntitiesObservables: Observable<ImportEntitiesResultInfo>[] = [];
346 346 for (let i = 0; i < partSize; i++) {
347 347 let saveEntityPromise: Observable<ImportEntitiesResultInfo>;
348   - if (entityType === EntityType.EDGE) {
349   - saveEntityPromise = this.entityService.saveEdgeParameters(entitiesData[i], updateData, config);
350   - } else {
351   - saveEntityPromise = this.entityService.saveEntityParameters(entityType, entitiesData[i], updateData, config);
352   - }
  348 + saveEntityPromise = this.entityService.saveEntityParameters(entityType, entitiesData[i], updateData, config);
353 349 const importEntityPromise = saveEntityPromise.pipe(
354 350 tap((res) => {
355 351 if (importEntityCompleted) {
... ... @@ -412,7 +408,7 @@ export class ImportExportService {
412 408 throw new Error('Invalid rule chain file');
413 409 } else if (ruleChainImport.ruleChain.type !== expectedRuleChainType) {
414 410 this.store.dispatch(new ActionNotificationShow(
415   - {message: this.translate.instant('rulechain.invalid-rulechain-type-error', { expectedRuleChainType: expectedRuleChainType }),
  411 + {message: this.translate.instant('rulechain.invalid-rulechain-type-error', {expectedRuleChainType}),
416 412 type: 'error'}));
417 413 throw new Error('Invalid rule chain type');
418 414 } else {
... ... @@ -602,7 +598,7 @@ export class ImportExportService {
602 598
603 599 private editMissingAliases(widgets: Array<Widget>, isSingleWidget: boolean,
604 600 customTitle: string, missingEntityAliases: EntityAliases): Observable<EntityAliases> {
605   - let allowedEntityTypes: Array<EntityType | AliasEntityType> =
  601 + const allowedEntityTypes: Array<EntityType | AliasEntityType> =
606 602 this.entityService.prepareAllowedEntityTypesList(null, true);
607 603
608 604 return this.dialog.open<EntityAliasesDialogComponent, EntityAliasesDialogData,
... ... @@ -615,7 +611,7 @@ export class ImportExportService {
615 611 customTitle,
616 612 isSingleWidget,
617 613 disableAdd: true,
618   - allowedEntityTypes: allowedEntityTypes
  614 + allowedEntityTypes
619 615 }
620 616 }).afterClosed().pipe(
621 617 map((updatedEntityAliases) => {
... ...
... ... @@ -26,7 +26,6 @@ import { NULL_UUID } from '@shared/models/id/has-uuid';
26 26 import { ActionNotificationShow } from '@core/notification/notification.actions';
27 27 import { generateSecret, guid } from '@core/utils';
28 28 import { EntityTableConfig } from '@home/models/entity/entities-table-config.models';
29   -import { WINDOW } from '@core/services/window.service';
30 29
31 30 @Component({
32 31 selector: 'tb-edge',
... ... @@ -43,15 +42,14 @@ export class EdgeComponent extends EntityComponent<EdgeInfo> {
43 42 protected translate: TranslateService,
44 43 @Inject('entity') protected entityValue: EdgeInfo,
45 44 @Inject('entitiesTableConfig') protected entitiesTableConfigValue: EntityTableConfig<EdgeInfo>,
46   - public fb: FormBuilder,
47   - @Inject(WINDOW) protected window: Window) {
  45 + public fb: FormBuilder) {
48 46 super(store, fb, entityValue, entitiesTableConfigValue);
49 47 }
50 48
51 49 ngOnInit() {
52 50 this.edgeScope = this.entitiesTableConfig.componentsData.edgeScope;
53 51 this.entityForm.patchValue({
54   - cloudEndpoint: this.window.location.origin
  52 + cloudEndpoint: window.location.origin
55 53 });
56 54 super.ngOnInit();
57 55 }
... ... @@ -94,7 +92,7 @@ export class EdgeComponent extends EntityComponent<EdgeInfo> {
94 92 name: entity.name,
95 93 type: entity.type,
96 94 label: entity.label,
97   - cloudEndpoint: entity.cloudEndpoint ? entity.cloudEndpoint : this.window.location.origin,
  95 + cloudEndpoint: entity.cloudEndpoint ? entity.cloudEndpoint : window.location.origin,
98 96 edgeLicenseKey: entity.edgeLicenseKey,
99 97 routingKey: entity.routingKey,
100 98 secret: entity.secret,
... ...