Browse Source

Code review updates

pull/3811/head
Volodymyr Babak 6 years ago
parent
commit
4aa0b02467
  1. 278
      ui-ngx/src/app/core/http/entity.service.ts
  2. 9
      ui-ngx/src/app/modules/home/components/import-export/import-dialog-csv.component.ts
  3. 12
      ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts
  4. 8
      ui-ngx/src/app/modules/home/pages/edge/edge.component.ts

278
ui-ngx/src/app/core/http/entity.service.ts

@ -14,7 +14,7 @@
/// limitations under the License.
///
import { Inject, Injectable } from '@angular/core';
import { Injectable } from '@angular/core';
import { EMPTY, forkJoin, Observable, of, throwError } from 'rxjs';
import { HttpClient } from '@angular/common/http';
import { PageLink } from '@shared/models/page/page-link';
@ -33,7 +33,7 @@ import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { Authority } from '@shared/models/authority.enum';
import { Tenant } from '@shared/models/tenant.model';
import { catchError, concatMap, expand, map, mergeMap, toArray } from 'rxjs/operators';
import { catchError, concatMap, expand, map, mergeMap, retry, toArray } from 'rxjs/operators';
import { Customer } from '@app/shared/models/customer.model';
import { AssetService } from '@core/http/asset.service';
import { EntityViewService } from '@core/http/entity-view.service';
@ -52,7 +52,7 @@ import {
ImportEntityData
} from '@shared/models/entity.models';
import { EntityRelationService } from '@core/http/entity-relation.service';
import { deepClone, generateSecret, guid, isDefined, isDefinedAndNotNull } from '@core/utils';
import { deepClone, isDefined, isDefinedAndNotNull } from '@core/utils';
import { Asset } from '@shared/models/asset.models';
import { Device, DeviceCredentialsType } from '@shared/models/device.models';
import { AttributeService } from '@core/http/attribute.service';
@ -74,10 +74,9 @@ import {
StringOperation
} from '@shared/models/query/query.models';
import { alarmFields } from '@shared/models/alarm.models';
import { EdgeService } from "@core/http/edge.service";
import { EdgeService } from '@core/http/edge.service';
import { Edge } from '@shared/models/edge.models';
import { WINDOW } from "@core/services/window.service";
import { RuleChainType } from "@shared/models/rule-chain.models";
import { RuleChainType } from '@shared/models/rule-chain.models';
@Injectable({
providedIn: 'root'
@ -98,8 +97,7 @@ export class EntityService {
private dashboardService: DashboardService,
private entityRelationService: EntityRelationService,
private attributeService: AttributeService,
private utils: UtilsService,
@Inject(WINDOW) protected window: Window
private utils: UtilsService
) { }
private getEntityObservable(entityType: EntityType, entityId: string,
@ -867,37 +865,7 @@ export class EntityService {
public saveEntityParameters(entityType: EntityType, entityData: ImportEntityData, update: boolean,
config?: RequestConfig): Observable<ImportEntitiesResultInfo> {
let saveEntityObservable: Observable<BaseData<EntityId>>;
switch (entityType) {
case EntityType.DEVICE:
const device: Device = {
name: entityData.name,
type: entityData.type,
label: entityData.label,
additionalInfo: {
description: entityData.description
}
};
if (entityData.gateway !== null) {
device.additionalInfo = {
...device.additionalInfo,
gateway: entityData.gateway
};
}
saveEntityObservable = this.deviceService.saveDevice(device, config);
break;
case EntityType.ASSET:
const asset: Asset = {
name: entityData.name,
type: entityData.type,
label: entityData.label,
additionalInfo: {
description: entityData.description
}
};
saveEntityObservable = this.assetService.saveAsset(asset, config);
break;
}
const saveEntityObservable: Observable<BaseData<EntityId>> = this.getSaveEntityObservable(entityType, entityData, config);
return saveEntityObservable.pipe(
mergeMap((entity) => {
return this.saveEntityData(entity.id, entityData, config).pipe(
@ -917,34 +885,14 @@ export class EntityService {
case EntityType.ASSET:
findEntityObservable = this.assetService.findByName(entityData.name, config);
break;
case EntityType.EDGE:
findEntityObservable = this.edgeService.findByName(entityData.name, config);
break;
}
return findEntityObservable.pipe(
mergeMap((entity) => {
const tasks: Observable<any>[] = [];
const result: Device | Asset = entity as (Device | Asset);
const additionalInfo = result.additionalInfo || {};
if (result.label !== entityData.label ||
result.type !== entityData.type ||
additionalInfo.description !== entityData.description ||
(result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) {
result.label = entityData.label;
result.type = entityData.type;
result.additionalInfo = additionalInfo;
result.additionalInfo.description = entityData.description;
if (result.id.entityType === EntityType.DEVICE) {
result.additionalInfo.gateway = entityData.gateway;
}
switch (result.id.entityType) {
case EntityType.DEVICE:
tasks.push(this.deviceService.saveDevice(result, config));
break;
case EntityType.ASSET:
tasks.push(this.assetService.saveAsset(result, config));
break;
}
}
tasks.push(this.saveEntityData(entity.id, entityData, config));
return forkJoin(tasks).pipe(
const updateEntityTasks: Observable<any>[] = this.getUpdateEntityTasks(entityType, entityData, entity, config);
return forkJoin(updateEntityTasks).pipe(
map(() => {
return { update: { entity: 1 } } as ImportEntitiesResultInfo;
}),
@ -960,90 +908,128 @@ export class EntityService {
);
}
public saveEdgeParameters(entityData: ImportEntityData, update: boolean,
config?: RequestConfig): Observable<ImportEntitiesResultInfo> {
const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
const edge: Edge = {
name: edgeEntityData.name,
type: edgeEntityData.type,
label: edgeEntityData.label,
additionalInfo: {
description: edgeEntityData.description
},
edgeLicenseKey: edgeEntityData.edgeLicenseKey,
cloudEndpoint: edgeEntityData.cloudEndpoint,
routingKey: edgeEntityData.routingKey,
secret: edgeEntityData.secret
};
if (edge.cloudEndpoint === '') {
edge.cloudEndpoint = this.window.location.origin;
}
if (edge.routingKey === '') {
edge.routingKey = guid();
}
if (edge.secret === '') {
edge.secret = generateSecret(20);
private getSaveEntityObservable(entityType: EntityType, entityData: ImportEntityData,
config?: RequestConfig): Observable<BaseData<EntityId>> {
let saveEntityObservable: Observable<BaseData<EntityId>>;
switch (entityType) {
case EntityType.DEVICE:
const device: Device = {
name: entityData.name,
type: entityData.type,
label: entityData.label,
additionalInfo: {
description: entityData.description
}
};
if (entityData.gateway !== null) {
device.additionalInfo = {
...device.additionalInfo,
gateway: entityData.gateway
};
}
saveEntityObservable = this.deviceService.saveDevice(device, config);
break;
case EntityType.ASSET:
const asset: Asset = {
name: entityData.name,
type: entityData.type,
label: entityData.label,
additionalInfo: {
description: entityData.description
}
};
saveEntityObservable = this.assetService.saveAsset(asset, config);
break;
case EntityType.EDGE:
const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
const edge: Edge = {
name: edgeEntityData.name,
type: edgeEntityData.type,
label: edgeEntityData.label,
additionalInfo: {
description: edgeEntityData.description
},
edgeLicenseKey: edgeEntityData.edgeLicenseKey,
cloudEndpoint: edgeEntityData.cloudEndpoint,
routingKey: edgeEntityData.routingKey,
secret: edgeEntityData.secret
};
saveEntityObservable = this.edgeService.saveEdge(edge, config);
break;
}
return this.edgeService.saveEdge(edge, config).pipe(
mergeMap((entity) => {
return this.saveEntityData(entity.id, edgeEntityData, config).pipe(
map(() => {
return { create: { entity: 1 } } as ImportEntitiesResultInfo;
}),
catchError(err => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
);
}),
catchError(err => {
if (update) {
return this.edgeService.findByName(edgeEntityData.name, config).pipe(
mergeMap((entity) => {
const tasks: Observable<any>[] = [];
const result: Edge = entity as Edge;
const additionalInfo = result.additionalInfo || {};
if (result.label !== edgeEntityData.label ||
result.type !== edgeEntityData.type ||
result.cloudEndpoint !== edgeEntityData.cloudEndpoint ||
result.edgeLicenseKey !== edgeEntityData.edgeLicenseKey ||
result.routingKey !== edgeEntityData.routingKey ||
result.secret !== edgeEntityData.secret ||
additionalInfo.description !== edgeEntityData.description) {
result.type = edgeEntityData.type;
if (edgeEntityData.label !== '') {
result.label = edgeEntityData.label;
}
if (edgeEntityData.description !== '') {
result.additionalInfo = additionalInfo;
result.additionalInfo.description = edgeEntityData.description;
}
if (edgeEntityData.cloudEndpoint !== '') {
result.cloudEndpoint = edgeEntityData.cloudEndpoint;
}
if (edgeEntityData.edgeLicenseKey !== '') {
result.edgeLicenseKey = edgeEntityData.edgeLicenseKey;
}
if (edgeEntityData.routingKey !== '') {
result.routingKey = edgeEntityData.routingKey;
}
if (edgeEntityData.cloudEndpoint !== '') {
result.secret = edgeEntityData.secret;
}
tasks.push(this.edgeService.saveEdge(result, config));
}
tasks.push(this.saveEntityData(entity.id, edgeEntityData, config));
return forkJoin(tasks).pipe(
map(() => {
return { update: { entity: 1 } } as ImportEntitiesResultInfo;
}),
catchError(updateError => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
);
}),
catchError(findErr => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
);
} else {
return of({ error: { entity: 1 } } as ImportEntitiesResultInfo);
return saveEntityObservable;
}
private getUpdateEntityTasks(entityType: EntityType, entityData: ImportEntityData | EdgeImportEntityData,
entity: BaseData<EntityId>, config?: RequestConfig): Observable<any>[] {
const tasks: Observable<any>[] = [];
let result;
let additionalInfo;
switch (entityType) {
case EntityType.EDGE:
result = entity as Edge;
additionalInfo = result.additionalInfo || {};
const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
if (result.label !== edgeEntityData.label ||
result.type !== edgeEntityData.type ||
result.cloudEndpoint !== edgeEntityData.cloudEndpoint ||
result.edgeLicenseKey !== edgeEntityData.edgeLicenseKey ||
result.routingKey !== edgeEntityData.routingKey ||
result.secret !== edgeEntityData.secret ||
additionalInfo.description !== edgeEntityData.description) {
result.type = edgeEntityData.type;
if (edgeEntityData.label !== '') {
result.label = edgeEntityData.label;
}
if (edgeEntityData.description !== '') {
result.additionalInfo = additionalInfo;
result.additionalInfo.description = edgeEntityData.description;
}
if (edgeEntityData.cloudEndpoint !== '') {
result.cloudEndpoint = edgeEntityData.cloudEndpoint;
}
if (edgeEntityData.edgeLicenseKey !== '') {
result.edgeLicenseKey = edgeEntityData.edgeLicenseKey;
}
if (edgeEntityData.routingKey !== '') {
result.routingKey = edgeEntityData.routingKey;
}
if (edgeEntityData.cloudEndpoint !== '') {
result.secret = edgeEntityData.secret;
}
tasks.push(this.edgeService.saveEdge(result, config));
}
})
);
tasks.push(this.saveEntityData(entity.id, edgeEntityData, config));
break;
case EntityType.ASSET:
case EntityType.DEVICE:
result = entity as (Device | Asset);
additionalInfo = result.additionalInfo || {};
if (result.label !== entityData.label ||
result.type !== entityData.type ||
additionalInfo.description !== entityData.description ||
(result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) {
result.label = entityData.label;
result.type = entityData.type;
result.additionalInfo = additionalInfo;
result.additionalInfo.description = entityData.description;
if (result.id.entityType === EntityType.DEVICE) {
result.additionalInfo.gateway = entityData.gateway;
}
switch (result.id.entityType) {
case EntityType.DEVICE:
tasks.push(this.deviceService.saveDevice(result, config));
break;
case EntityType.ASSET:
tasks.push(this.assetService.saveAsset(result, config));
break;
}
}
tasks.push(this.saveEntityData(entity.id, entityData, config));
break;
}
return tasks;
}
public saveEntityData(entityId: EntityId, entityData: ImportEntityData, config?: RequestConfig): Observable<any> {

9
ui-ngx/src/app/modules/home/components/import-export/import-dialog-csv.component.ts

@ -34,6 +34,7 @@ import {
} from '@home/components/import-export/import-export.models';
import { ImportEntitiesResultInfo, ImportEntityData, EdgeImportEntityData } from '@app/shared/models/entity.models';
import { ImportExportService } from '@home/components/import-export/import-export.service';
import { generateSecret, guid } from '@core/utils';
export interface ImportDialogCsvData {
entityType: EntityType;
@ -281,11 +282,11 @@ export class ImportDialogCsvComponent extends DialogComponent<ImportDialogCsvCom
timeseries: []
};
if (this.entityType === EntityType.EDGE) {
let edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData;
edgeEntityData.edgeLicenseKey = '';
edgeEntityData.cloudEndpoint = '';
edgeEntityData.routingKey = '';
edgeEntityData.secret = '';
edgeEntityData.cloudEndpoint = window.location.origin;
edgeEntityData.routingKey = guid();
edgeEntityData.secret = generateSecret(20);
return edgeEntityData;
} else {
return entityData;

12
ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts

@ -345,11 +345,7 @@ export class ImportExportService {
const importEntitiesObservables: Observable<ImportEntitiesResultInfo>[] = [];
for (let i = 0; i < partSize; i++) {
let saveEntityPromise: Observable<ImportEntitiesResultInfo>;
if (entityType === EntityType.EDGE) {
saveEntityPromise = this.entityService.saveEdgeParameters(entitiesData[i], updateData, config);
} else {
saveEntityPromise = this.entityService.saveEntityParameters(entityType, entitiesData[i], updateData, config);
}
saveEntityPromise = this.entityService.saveEntityParameters(entityType, entitiesData[i], updateData, config);
const importEntityPromise = saveEntityPromise.pipe(
tap((res) => {
if (importEntityCompleted) {
@ -412,7 +408,7 @@ export class ImportExportService {
throw new Error('Invalid rule chain file');
} else if (ruleChainImport.ruleChain.type !== expectedRuleChainType) {
this.store.dispatch(new ActionNotificationShow(
{message: this.translate.instant('rulechain.invalid-rulechain-type-error', { expectedRuleChainType: expectedRuleChainType }),
{message: this.translate.instant('rulechain.invalid-rulechain-type-error', {expectedRuleChainType}),
type: 'error'}));
throw new Error('Invalid rule chain type');
} else {
@ -602,7 +598,7 @@ export class ImportExportService {
private editMissingAliases(widgets: Array<Widget>, isSingleWidget: boolean,
customTitle: string, missingEntityAliases: EntityAliases): Observable<EntityAliases> {
let allowedEntityTypes: Array<EntityType | AliasEntityType> =
const allowedEntityTypes: Array<EntityType | AliasEntityType> =
this.entityService.prepareAllowedEntityTypesList(null, true);
return this.dialog.open<EntityAliasesDialogComponent, EntityAliasesDialogData,
@ -615,7 +611,7 @@ export class ImportExportService {
customTitle,
isSingleWidget,
disableAdd: true,
allowedEntityTypes: allowedEntityTypes
allowedEntityTypes
}
}).afterClosed().pipe(
map((updatedEntityAliases) => {

8
ui-ngx/src/app/modules/home/pages/edge/edge.component.ts

@ -26,7 +26,6 @@ import { NULL_UUID } from '@shared/models/id/has-uuid';
import { ActionNotificationShow } from '@core/notification/notification.actions';
import { generateSecret, guid } from '@core/utils';
import { EntityTableConfig } from '@home/models/entity/entities-table-config.models';
import { WINDOW } from '@core/services/window.service';
@Component({
selector: 'tb-edge',
@ -43,15 +42,14 @@ export class EdgeComponent extends EntityComponent<EdgeInfo> {
protected translate: TranslateService,
@Inject('entity') protected entityValue: EdgeInfo,
@Inject('entitiesTableConfig') protected entitiesTableConfigValue: EntityTableConfig<EdgeInfo>,
public fb: FormBuilder,
@Inject(WINDOW) protected window: Window) {
public fb: FormBuilder) {
super(store, fb, entityValue, entitiesTableConfigValue);
}
ngOnInit() {
this.edgeScope = this.entitiesTableConfig.componentsData.edgeScope;
this.entityForm.patchValue({
cloudEndpoint: this.window.location.origin
cloudEndpoint: window.location.origin
});
super.ngOnInit();
}
@ -94,7 +92,7 @@ export class EdgeComponent extends EntityComponent<EdgeInfo> {
name: entity.name,
type: entity.type,
label: entity.label,
cloudEndpoint: entity.cloudEndpoint ? entity.cloudEndpoint : this.window.location.origin,
cloudEndpoint: entity.cloudEndpoint ? entity.cloudEndpoint : window.location.origin,
edgeLicenseKey: entity.edgeLicenseKey,
routingKey: entity.routingKey,
secret: entity.secret,

Loading…
Cancel
Save