Commit 9b27bb2d7ca659eeec0687e38afccd2a6c3a150e

Authored by deaflynx
1 parent 118ba1ad

Import edges with update

... ... @@ -14,7 +14,7 @@
14 14 /// limitations under the License.
15 15 ///
16 16
17   -import { Injectable } from '@angular/core';
  17 +import { Inject, 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';
... ... @@ -46,7 +46,7 @@ import { UtilsService } from '@core/services/utils.service';
46 46 import { AliasFilterType, EntityAlias, EntityAliasFilter, EntityAliasFilterResult } from '@shared/models/alias.models';
47 47 import { entityFields, EntityInfo, ImportEntitiesResultInfo, ImportEntityData } from '@shared/models/entity.models';
48 48 import { EntityRelationService } from '@core/http/entity-relation.service';
49   -import { deepClone, isDefined, isDefinedAndNotNull } from '@core/utils';
  49 +import { deepClone, generateSecret, guid, isDefined, isDefinedAndNotNull } from '@core/utils';
50 50 import { Asset } from '@shared/models/asset.models';
51 51 import { Device, DeviceCredentialsType } from '@shared/models/device.models';
52 52 import { AttributeService } from '@core/http/attribute.service';
... ... @@ -71,6 +71,7 @@ import { alarmFields } from '@shared/models/alarm.models';
71 71 import { EdgeService } from "@core/http/edge.service";
72 72 import { ruleChainType } from "@shared/models/rule-chain.models";
73 73 import { Edge } from '@shared/models/edge.models';
  74 +import { WINDOW } from "@core/services/window.service";
74 75
75 76 @Injectable({
76 77 providedIn: 'root'
... ... @@ -91,7 +92,8 @@ export class EntityService {
91 92 private dashboardService: DashboardService,
92 93 private entityRelationService: EntityRelationService,
93 94 private attributeService: AttributeService,
94   - private utils: UtilsService
  95 + private utils: UtilsService,
  96 + @Inject(WINDOW) protected window: Window
95 97 ) { }
96 98
97 99 private getEntityObservable(entityType: EntityType, entityId: string,
... ... @@ -901,6 +903,15 @@ export class EntityService {
901 903 cloudEndpoint: entityData.cloudEndpoint,
902 904 routingKey: entityData.routingKey,
903 905 secret: entityData.secret
  906 + };
  907 + if (edge.cloudEndpoint === '') {
  908 + edge.cloudEndpoint = this.window.location.origin;
  909 + }
  910 + if (edge.routingKey === '') {
  911 + edge.routingKey = guid();
  912 + }
  913 + if (edge.secret === '') {
  914 + edge.secret = generateSecret(20);
904 915 }
905 916 saveEntityObservable = this.edgeService.saveEdge(edge, config);
906 917 break;
... ... @@ -924,30 +935,67 @@ export class EntityService {
924 935 case EntityType.ASSET:
925 936 findEntityObservable = this.assetService.findByName(entityData.name, config);
926 937 break;
  938 + case EntityType.EDGE:
  939 + findEntityObservable = this.edgeService.findByName(entityData.name, config);
  940 + break;
927 941 }
928 942 return findEntityObservable.pipe(
929 943 mergeMap((entity) => {
930 944 const tasks: Observable<any>[] = [];
931   - const result: Device | Asset = entity as (Device | Asset);
932   - const additionalInfo = result.additionalInfo || {};
933   - if (result.label !== entityData.label ||
934   - result.type !== entityData.type ||
935   - additionalInfo.description !== entityData.description ||
936   - (result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) {
937   - result.label = entityData.label;
938   - result.type = entityData.type;
939   - result.additionalInfo = additionalInfo;
940   - result.additionalInfo.description = entityData.description;
941   - if (result.id.entityType === EntityType.DEVICE) {
942   - result.additionalInfo.gateway = entityData.gateway;
  945 + if (entity.id.entityType === EntityType.EDGE) {
  946 + const result: Edge = entity as Edge;
  947 + const additionalInfo = result.additionalInfo || {};
  948 + if (result.label !== entityData.label ||
  949 + result.type !== entityData.type ||
  950 + result.cloudEndpoint !== entityData.cloudEndpoint ||
  951 + result.edgeLicenseKey !== entityData.edgeLicenseKey ||
  952 + result.routingKey !== entityData.routingKey ||
  953 + result.secret !== entityData.secret ||
  954 + additionalInfo.description !== entityData.description) {
  955 + result.type = entityData.type;
  956 + if (entityData.label !== '') {
  957 + result.label = entityData.label;
  958 + }
  959 + if (entityData.description !== '') {
  960 + result.additionalInfo = additionalInfo;
  961 + result.additionalInfo.description = entityData.description;
  962 + }
  963 + if (entityData.cloudEndpoint !== '') {
  964 + result.cloudEndpoint = entityData.cloudEndpoint;
  965 + }
  966 + if (entityData.edgeLicenseKey !== '') {
  967 + result.edgeLicenseKey = entityData.edgeLicenseKey;
  968 + }
  969 + if (entityData.routingKey !== '') {
  970 + result.routingKey = entityData.routingKey;
  971 + }
  972 + if (entityData.cloudEndpoint !== '') {
  973 + result.secret = entityData.secret;
  974 + }
  975 + tasks.push(this.edgeService.saveEdge(result, config));
943 976 }
944   - switch (result.id.entityType) {
945   - case EntityType.DEVICE:
946   - tasks.push(this.deviceService.saveDevice(result, config));
947   - break;
948   - case EntityType.ASSET:
949   - tasks.push(this.assetService.saveAsset(result, config));
950   - break;
  977 + } else {
  978 + const result: Device | Asset = entity as (Device | Asset);
  979 + const additionalInfo = result.additionalInfo || {};
  980 + if (result.label !== entityData.label ||
  981 + result.type !== entityData.type ||
  982 + additionalInfo.description !== entityData.description ||
  983 + (result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) {
  984 + result.label = entityData.label;
  985 + result.type = entityData.type;
  986 + result.additionalInfo = additionalInfo;
  987 + result.additionalInfo.description = entityData.description;
  988 + if (result.id.entityType === EntityType.DEVICE) {
  989 + result.additionalInfo.gateway = entityData.gateway;
  990 + }
  991 + switch (result.id.entityType) {
  992 + case EntityType.DEVICE:
  993 + tasks.push(this.deviceService.saveDevice(result, config));
  994 + break;
  995 + case EntityType.ASSET:
  996 + tasks.push(this.assetService.saveAsset(result, config));
  997 + break;
  998 + }
951 999 }
952 1000 }
953 1001 tasks.push(this.saveEntityData(entity.id, entityData, config));
... ...
... ... @@ -248,16 +248,16 @@ export class ImportDialogCsvComponent extends DialogComponent<ImportDialogCsvCom
248 248 case ImportEntityColumnType.description:
249 249 entityData.description = importData.rows[i][j];
250 250 break;
251   - case ImportEntityColumnType.edgeLicenseKey:
  251 + case ImportEntityColumnType.edgeLicenseKey:
252 252 entityData.edgeLicenseKey = importData.rows[i][j];
253 253 break;
254   - case ImportEntityColumnType.cloudEndpoint:
  254 + case ImportEntityColumnType.cloudEndpoint:
255 255 entityData.cloudEndpoint = importData.rows[i][j];
256 256 break;
257   - case ImportEntityColumnType.routingKey:
  257 + case ImportEntityColumnType.routingKey:
258 258 entityData.routingKey = importData.rows[i][j];
259 259 break;
260   - case ImportEntityColumnType.secret:
  260 + case ImportEntityColumnType.secret:
261 261 entityData.secret = importData.rows[i][j];
262 262 break;
263 263 }
... ...
... ... @@ -126,6 +126,10 @@ export class TableColumnsAssignmentComponent implements OnInit, ControlValueAcce
126 126 const isSelectCredentials = this.columns.findIndex((column) => column.type === ImportEntityColumnType.accessToken) > -1;
127 127 const isSelectGateway = this.columns.findIndex((column) => column.type === ImportEntityColumnType.isGateway) > -1;
128 128 const isSelectDescription = this.columns.findIndex((column) => column.type === ImportEntityColumnType.description) > -1;
  129 + const isSelectEdgeLicenseKey = this.columns.findIndex((column) => column.type === ImportEntityColumnType.edgeLicenseKey) > -1;
  130 + const isSelectCloudEndpoint = this.columns.findIndex((column) => column.type === ImportEntityColumnType.cloudEndpoint) > -1;
  131 + const isSelectRoutingKey = this.columns.findIndex((column) => column.type === ImportEntityColumnType.routingKey) > -1;
  132 + const isSelectSecret = this.columns.findIndex((column) => column.type === ImportEntityColumnType.secret) > -1;
129 133 const hasInvalidColumn = this.columns.findIndex((column) => !this.columnValid(column)) > -1;
130 134
131 135 this.valid = isSelectName && isSelectType && !hasInvalidColumn;
... ... @@ -143,6 +147,22 @@ export class TableColumnsAssignmentComponent implements OnInit, ControlValueAcce
143 147 if (accessTokenColumnType) {
144 148 accessTokenColumnType.disabled = isSelectCredentials;
145 149 }
  150 + const edgeLicenseKeyColumnType = this.columnTypes.find((columnType) => columnType.value === ImportEntityColumnType.edgeLicenseKey);
  151 + if (edgeLicenseKeyColumnType) {
  152 + edgeLicenseKeyColumnType.disabled = isSelectEdgeLicenseKey;
  153 + }
  154 + const cloudEndpointColumnType = this.columnTypes.find((columnType) => columnType.value === ImportEntityColumnType.cloudEndpoint);
  155 + if (cloudEndpointColumnType) {
  156 + cloudEndpointColumnType.disabled = isSelectCloudEndpoint;
  157 + }
  158 + const routingKeyColumnType = this.columnTypes.find((columnType) => columnType.value === ImportEntityColumnType.routingKey);
  159 + if (routingKeyColumnType) {
  160 + routingKeyColumnType.disabled = isSelectRoutingKey;
  161 + }
  162 + const secretColumnType = this.columnTypes.find((columnType) => columnType.value === ImportEntityColumnType.secret);
  163 + if (secretColumnType) {
  164 + secretColumnType.disabled = isSelectSecret;
  165 + }
146 166 if (this.propagateChange) {
147 167 this.propagateChange(this.columns);
148 168 } else {
... ...