Browse Source

Import edges with update

pull/3811/head
deaflynx 5 years ago
parent
commit
9b27bb2d7c
  1. 92
      ui-ngx/src/app/core/http/entity.service.ts
  2. 8
      ui-ngx/src/app/modules/home/components/import-export/import-dialog-csv.component.ts
  3. 20
      ui-ngx/src/app/modules/home/components/import-export/table-columns-assignment.component.ts

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

@ -14,7 +14,7 @@
/// limitations under the License.
///
import { Injectable } from '@angular/core';
import { Inject, 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';
@ -46,7 +46,7 @@ import { UtilsService } from '@core/services/utils.service';
import { AliasFilterType, EntityAlias, EntityAliasFilter, EntityAliasFilterResult } from '@shared/models/alias.models';
import { entityFields, EntityInfo, ImportEntitiesResultInfo, ImportEntityData } from '@shared/models/entity.models';
import { EntityRelationService } from '@core/http/entity-relation.service';
import { deepClone, isDefined, isDefinedAndNotNull } from '@core/utils';
import { deepClone, generateSecret, guid, 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';
@ -71,6 +71,7 @@ import { alarmFields } from '@shared/models/alarm.models';
import { EdgeService } from "@core/http/edge.service";
import { ruleChainType } from "@shared/models/rule-chain.models";
import { Edge } from '@shared/models/edge.models';
import { WINDOW } from "@core/services/window.service";
@Injectable({
providedIn: 'root'
@ -91,7 +92,8 @@ export class EntityService {
private dashboardService: DashboardService,
private entityRelationService: EntityRelationService,
private attributeService: AttributeService,
private utils: UtilsService
private utils: UtilsService,
@Inject(WINDOW) protected window: Window
) { }
private getEntityObservable(entityType: EntityType, entityId: string,
@ -901,6 +903,15 @@ export class EntityService {
cloudEndpoint: entityData.cloudEndpoint,
routingKey: entityData.routingKey,
secret: entityData.secret
};
if (edge.cloudEndpoint === '') {
edge.cloudEndpoint = this.window.location.origin;
}
if (edge.routingKey === '') {
edge.routingKey = guid();
}
if (edge.secret === '') {
edge.secret = generateSecret(20);
}
saveEntityObservable = this.edgeService.saveEdge(edge, config);
break;
@ -924,30 +935,67 @@ 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;
if (entity.id.entityType === EntityType.EDGE) {
const result: Edge = entity as Edge;
const additionalInfo = result.additionalInfo || {};
if (result.label !== entityData.label ||
result.type !== entityData.type ||
result.cloudEndpoint !== entityData.cloudEndpoint ||
result.edgeLicenseKey !== entityData.edgeLicenseKey ||
result.routingKey !== entityData.routingKey ||
result.secret !== entityData.secret ||
additionalInfo.description !== entityData.description) {
result.type = entityData.type;
if (entityData.label !== '') {
result.label = entityData.label;
}
if (entityData.description !== '') {
result.additionalInfo = additionalInfo;
result.additionalInfo.description = entityData.description;
}
if (entityData.cloudEndpoint !== '') {
result.cloudEndpoint = entityData.cloudEndpoint;
}
if (entityData.edgeLicenseKey !== '') {
result.edgeLicenseKey = entityData.edgeLicenseKey;
}
if (entityData.routingKey !== '') {
result.routingKey = entityData.routingKey;
}
if (entityData.cloudEndpoint !== '') {
result.secret = entityData.secret;
}
tasks.push(this.edgeService.saveEdge(result, config));
}
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;
} else {
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));

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

@ -248,16 +248,16 @@ export class ImportDialogCsvComponent extends DialogComponent<ImportDialogCsvCom
case ImportEntityColumnType.description:
entityData.description = importData.rows[i][j];
break;
case ImportEntityColumnType.edgeLicenseKey:
case ImportEntityColumnType.edgeLicenseKey:
entityData.edgeLicenseKey = importData.rows[i][j];
break;
case ImportEntityColumnType.cloudEndpoint:
case ImportEntityColumnType.cloudEndpoint:
entityData.cloudEndpoint = importData.rows[i][j];
break;
case ImportEntityColumnType.routingKey:
case ImportEntityColumnType.routingKey:
entityData.routingKey = importData.rows[i][j];
break;
case ImportEntityColumnType.secret:
case ImportEntityColumnType.secret:
entityData.secret = importData.rows[i][j];
break;
}

20
ui-ngx/src/app/modules/home/components/import-export/table-columns-assignment.component.ts

@ -126,6 +126,10 @@ export class TableColumnsAssignmentComponent implements OnInit, ControlValueAcce
const isSelectCredentials = this.columns.findIndex((column) => column.type === ImportEntityColumnType.accessToken) > -1;
const isSelectGateway = this.columns.findIndex((column) => column.type === ImportEntityColumnType.isGateway) > -1;
const isSelectDescription = this.columns.findIndex((column) => column.type === ImportEntityColumnType.description) > -1;
const isSelectEdgeLicenseKey = this.columns.findIndex((column) => column.type === ImportEntityColumnType.edgeLicenseKey) > -1;
const isSelectCloudEndpoint = this.columns.findIndex((column) => column.type === ImportEntityColumnType.cloudEndpoint) > -1;
const isSelectRoutingKey = this.columns.findIndex((column) => column.type === ImportEntityColumnType.routingKey) > -1;
const isSelectSecret = this.columns.findIndex((column) => column.type === ImportEntityColumnType.secret) > -1;
const hasInvalidColumn = this.columns.findIndex((column) => !this.columnValid(column)) > -1;
this.valid = isSelectName && isSelectType && !hasInvalidColumn;
@ -143,6 +147,22 @@ export class TableColumnsAssignmentComponent implements OnInit, ControlValueAcce
if (accessTokenColumnType) {
accessTokenColumnType.disabled = isSelectCredentials;
}
const edgeLicenseKeyColumnType = this.columnTypes.find((columnType) => columnType.value === ImportEntityColumnType.edgeLicenseKey);
if (edgeLicenseKeyColumnType) {
edgeLicenseKeyColumnType.disabled = isSelectEdgeLicenseKey;
}
const cloudEndpointColumnType = this.columnTypes.find((columnType) => columnType.value === ImportEntityColumnType.cloudEndpoint);
if (cloudEndpointColumnType) {
cloudEndpointColumnType.disabled = isSelectCloudEndpoint;
}
const routingKeyColumnType = this.columnTypes.find((columnType) => columnType.value === ImportEntityColumnType.routingKey);
if (routingKeyColumnType) {
routingKeyColumnType.disabled = isSelectRoutingKey;
}
const secretColumnType = this.columnTypes.find((columnType) => columnType.value === ImportEntityColumnType.secret);
if (secretColumnType) {
secretColumnType.disabled = isSelectSecret;
}
if (this.propagateChange) {
this.propagateChange(this.columns);
} else {

Loading…
Cancel
Save