Showing
1 changed file
with
130 additions
and
57 deletions
... | ... | @@ -45,6 +45,7 @@ import { DataKey, Datasource, DatasourceType, KeyInfo } from '@app/shared/models |
45 | 45 | import { UtilsService } from '@core/services/utils.service'; |
46 | 46 | import { AliasFilterType, EntityAlias, EntityAliasFilter, EntityAliasFilterResult } from '@shared/models/alias.models'; |
47 | 47 | import { |
48 | + EdgeImportEntityData, | |
48 | 49 | EntitiesKeysByQuery, |
49 | 50 | entityFields, |
50 | 51 | EntityInfo, |
... | ... | @@ -76,6 +77,7 @@ import { |
76 | 77 | import { alarmFields } from '@shared/models/alarm.models'; |
77 | 78 | import { EdgeService } from "@core/http/edge.service"; |
78 | 79 | import { RuleChainType } from "@shared/models/rule-chain.models"; |
80 | +import { Edge } from '@shared/models/edge.models'; | |
79 | 81 | |
80 | 82 | @Injectable({ |
81 | 83 | providedIn: 'root' |
... | ... | @@ -924,6 +926,51 @@ export class EntityService { |
924 | 926 | |
925 | 927 | public saveEntityParameters(entityType: EntityType, entityData: ImportEntityData, update: boolean, |
926 | 928 | config?: RequestConfig): Observable<ImportEntitiesResultInfo> { |
929 | + const saveEntityObservable: Observable<BaseData<EntityId>> = this.getSaveEntityObservable(entityType, entityData, config); | |
930 | + return saveEntityObservable.pipe( | |
931 | + mergeMap((entity) => { | |
932 | + return this.saveEntityData(entity.id, entityData, config).pipe( | |
933 | + map(() => { | |
934 | + return { create: { entity: 1 } } as ImportEntitiesResultInfo; | |
935 | + }), | |
936 | + catchError(err => of({ error: { entity: 1 } } as ImportEntitiesResultInfo)) | |
937 | + ); | |
938 | + }), | |
939 | + catchError(err => { | |
940 | + if (update) { | |
941 | + let findEntityObservable: Observable<BaseData<EntityId>>; | |
942 | + switch (entityType) { | |
943 | + case EntityType.DEVICE: | |
944 | + findEntityObservable = this.deviceService.findByName(entityData.name, config); | |
945 | + break; | |
946 | + case EntityType.ASSET: | |
947 | + findEntityObservable = this.assetService.findByName(entityData.name, config); | |
948 | + break; | |
949 | + case EntityType.EDGE: | |
950 | + findEntityObservable = this.edgeService.findByName(entityData.name, config); | |
951 | + break; | |
952 | + } | |
953 | + return findEntityObservable.pipe( | |
954 | + mergeMap((entity) => { | |
955 | + const updateEntityTasks: Observable<any>[] = this.getUpdateEntityTasks(entityType, entityData, entity, config); | |
956 | + return forkJoin(updateEntityTasks).pipe( | |
957 | + map(() => { | |
958 | + return { update: { entity: 1 } } as ImportEntitiesResultInfo; | |
959 | + }), | |
960 | + catchError(updateError => of({ error: { entity: 1 } } as ImportEntitiesResultInfo)) | |
961 | + ); | |
962 | + }), | |
963 | + catchError(findErr => of({ error: { entity: 1 } } as ImportEntitiesResultInfo)) | |
964 | + ); | |
965 | + } else { | |
966 | + return of({ error: { entity: 1 } } as ImportEntitiesResultInfo); | |
967 | + } | |
968 | + }) | |
969 | + ); | |
970 | + } | |
971 | + | |
972 | + private getSaveEntityObservable(entityType: EntityType, entityData: ImportEntityData, | |
973 | + config?: RequestConfig): Observable<BaseData<EntityId>> { | |
927 | 974 | let saveEntityObservable: Observable<BaseData<EntityId>>; |
928 | 975 | switch (entityType) { |
929 | 976 | case EntityType.DEVICE: |
... | ... | @@ -954,70 +1001,96 @@ export class EntityService { |
954 | 1001 | }; |
955 | 1002 | saveEntityObservable = this.assetService.saveAsset(asset, config); |
956 | 1003 | break; |
1004 | + case EntityType.EDGE: | |
1005 | + const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData; | |
1006 | + const edge: Edge = { | |
1007 | + name: edgeEntityData.name, | |
1008 | + type: edgeEntityData.type, | |
1009 | + label: edgeEntityData.label, | |
1010 | + additionalInfo: { | |
1011 | + description: edgeEntityData.description | |
1012 | + }, | |
1013 | + edgeLicenseKey: edgeEntityData.edgeLicenseKey, | |
1014 | + cloudEndpoint: edgeEntityData.cloudEndpoint, | |
1015 | + routingKey: edgeEntityData.routingKey, | |
1016 | + secret: edgeEntityData.secret | |
1017 | + }; | |
1018 | + saveEntityObservable = this.edgeService.saveEdge(edge, config); | |
1019 | + break; | |
957 | 1020 | } |
958 | - return saveEntityObservable.pipe( | |
959 | - mergeMap((entity) => { | |
960 | - return this.saveEntityData(entity.id, entityData, config).pipe( | |
961 | - map(() => { | |
962 | - return { create: { entity: 1 } } as ImportEntitiesResultInfo; | |
963 | - }), | |
964 | - catchError(err => of({ error: { entity: 1 } } as ImportEntitiesResultInfo)) | |
965 | - ); | |
966 | - }), | |
967 | - catchError(err => { | |
968 | - if (update) { | |
969 | - let findEntityObservable: Observable<BaseData<EntityId>>; | |
970 | - switch (entityType) { | |
1021 | + return saveEntityObservable; | |
1022 | + | |
1023 | + } | |
1024 | + | |
1025 | + private getUpdateEntityTasks(entityType: EntityType, entityData: ImportEntityData | EdgeImportEntityData, | |
1026 | + entity: BaseData<EntityId>, config?: RequestConfig): Observable<any>[] { | |
1027 | + const tasks: Observable<any>[] = []; | |
1028 | + let result; | |
1029 | + let additionalInfo; | |
1030 | + switch (entityType) { | |
1031 | + case EntityType.EDGE: | |
1032 | + result = entity as Edge; | |
1033 | + additionalInfo = result.additionalInfo || {}; | |
1034 | + const edgeEntityData: EdgeImportEntityData = entityData as EdgeImportEntityData; | |
1035 | + if (result.label !== edgeEntityData.label || | |
1036 | + result.type !== edgeEntityData.type || | |
1037 | + result.cloudEndpoint !== edgeEntityData.cloudEndpoint || | |
1038 | + result.edgeLicenseKey !== edgeEntityData.edgeLicenseKey || | |
1039 | + result.routingKey !== edgeEntityData.routingKey || | |
1040 | + result.secret !== edgeEntityData.secret || | |
1041 | + additionalInfo.description !== edgeEntityData.description) { | |
1042 | + result.type = edgeEntityData.type; | |
1043 | + if (edgeEntityData.label !== '') { | |
1044 | + result.label = edgeEntityData.label; | |
1045 | + } | |
1046 | + if (edgeEntityData.description !== '') { | |
1047 | + result.additionalInfo = additionalInfo; | |
1048 | + result.additionalInfo.description = edgeEntityData.description; | |
1049 | + } | |
1050 | + if (edgeEntityData.cloudEndpoint !== '') { | |
1051 | + result.cloudEndpoint = edgeEntityData.cloudEndpoint; | |
1052 | + } | |
1053 | + if (edgeEntityData.edgeLicenseKey !== '') { | |
1054 | + result.edgeLicenseKey = edgeEntityData.edgeLicenseKey; | |
1055 | + } | |
1056 | + if (edgeEntityData.routingKey !== '') { | |
1057 | + result.routingKey = edgeEntityData.routingKey; | |
1058 | + } | |
1059 | + if (edgeEntityData.cloudEndpoint !== '') { | |
1060 | + result.secret = edgeEntityData.secret; | |
1061 | + } | |
1062 | + tasks.push(this.edgeService.saveEdge(result, config)); | |
1063 | + } | |
1064 | + tasks.push(this.saveEntityData(entity.id, edgeEntityData, config)); | |
1065 | + break; | |
1066 | + case EntityType.ASSET: | |
1067 | + case EntityType.DEVICE: | |
1068 | + result = entity as (Device | Asset); | |
1069 | + additionalInfo = result.additionalInfo || {}; | |
1070 | + if (result.label !== entityData.label || | |
1071 | + result.type !== entityData.type || | |
1072 | + additionalInfo.description !== entityData.description || | |
1073 | + (result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) { | |
1074 | + result.label = entityData.label; | |
1075 | + result.type = entityData.type; | |
1076 | + result.additionalInfo = additionalInfo; | |
1077 | + result.additionalInfo.description = entityData.description; | |
1078 | + if (result.id.entityType === EntityType.DEVICE) { | |
1079 | + result.additionalInfo.gateway = entityData.gateway; | |
1080 | + } | |
1081 | + switch (result.id.entityType) { | |
971 | 1082 | case EntityType.DEVICE: |
972 | - findEntityObservable = this.deviceService.findByName(entityData.name, config); | |
1083 | + tasks.push(this.deviceService.saveDevice(result, config)); | |
973 | 1084 | break; |
974 | 1085 | case EntityType.ASSET: |
975 | - findEntityObservable = this.assetService.findByName(entityData.name, config); | |
1086 | + tasks.push(this.assetService.saveAsset(result, config)); | |
976 | 1087 | break; |
977 | 1088 | } |
978 | - return findEntityObservable.pipe( | |
979 | - mergeMap((entity) => { | |
980 | - const tasks: Observable<any>[] = []; | |
981 | - const result: Device & Asset = entity as (Device | Asset); | |
982 | - const additionalInfo = result.additionalInfo || {}; | |
983 | - if (result.label !== entityData.label || | |
984 | - result.type !== entityData.type || | |
985 | - additionalInfo.description !== entityData.description || | |
986 | - (result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) { | |
987 | - result.label = entityData.label; | |
988 | - result.type = entityData.type; | |
989 | - result.additionalInfo = additionalInfo; | |
990 | - result.additionalInfo.description = entityData.description; | |
991 | - if (result.id.entityType === EntityType.DEVICE) { | |
992 | - result.additionalInfo.gateway = entityData.gateway; | |
993 | - } | |
994 | - if (result.id.entityType === EntityType.DEVICE && result.deviceProfileId) { | |
995 | - delete result.deviceProfileId; | |
996 | - } | |
997 | - switch (result.id.entityType) { | |
998 | - case EntityType.DEVICE: | |
999 | - tasks.push(this.deviceService.saveDevice(result, config)); | |
1000 | - break; | |
1001 | - case EntityType.ASSET: | |
1002 | - tasks.push(this.assetService.saveAsset(result, config)); | |
1003 | - break; | |
1004 | - } | |
1005 | - } | |
1006 | - tasks.push(this.saveEntityData(entity.id, entityData, config)); | |
1007 | - return forkJoin(tasks).pipe( | |
1008 | - map(() => { | |
1009 | - return { update: { entity: 1 } } as ImportEntitiesResultInfo; | |
1010 | - }), | |
1011 | - catchError(updateError => of({ error: { entity: 1 } } as ImportEntitiesResultInfo)) | |
1012 | - ); | |
1013 | - }), | |
1014 | - catchError(findErr => of({ error: { entity: 1 } } as ImportEntitiesResultInfo)) | |
1015 | - ); | |
1016 | - } else { | |
1017 | - return of({ error: { entity: 1 } } as ImportEntitiesResultInfo); | |
1018 | 1089 | } |
1019 | - }) | |
1020 | - ); | |
1090 | + tasks.push(this.saveEntityData(entity.id, entityData, config)); | |
1091 | + break; | |
1092 | + } | |
1093 | + return tasks; | |
1021 | 1094 | } |
1022 | 1095 | |
1023 | 1096 | public saveEntityData(entityId: EntityId, entityData: ImportEntityData, config?: RequestConfig): Observable<any> { | ... | ... |