From 1e8fc7086b1d977fbecc69873f266a8e396c8d47 Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Fri, 19 Mar 2021 10:07:26 +0200 Subject: [PATCH] Device profile import/export support --- .../import-export/import-export.service.ts | 51 +++++++++++++++++ .../profile/device-profile.component.html | 6 ++ .../device-profiles-table-config.resolver.ts | 56 +++++++++++++++++-- .../assets/locale/locale.constant-en_US.json | 8 ++- 4 files changed, 116 insertions(+), 5 deletions(-) diff --git a/ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts b/ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts index 48324dcbfe..2169f46d71 100644 --- a/ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts +++ b/ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts @@ -55,6 +55,8 @@ import { RequestConfig } from '@core/http/http-utils'; import { RuleChain, RuleChainImport, RuleChainMetaData } from '@shared/models/rule-chain.models'; import { RuleChainService } from '@core/http/rule-chain.service'; import { FiltersInfo } from '@shared/models/query/query.models'; +import { DeviceProfileService } from '@core/http/device-profile.service'; +import { DeviceProfile } from '@shared/models/device.models'; // @dynamic @Injectable() @@ -67,6 +69,7 @@ export class ImportExportService { private dashboardService: DashboardService, private dashboardUtils: DashboardUtilsService, private widgetService: WidgetService, + private deviceProfileService: DeviceProfileService, private entityService: EntityService, private ruleChainService: RuleChainService, private utils: UtilsService, @@ -420,6 +423,37 @@ export class ImportExportService { ); } + public exportDeviceProfile(deviceProfileId: string) { + this.deviceProfileService.getDeviceProfile(deviceProfileId).subscribe( + (deviceProfile) => { + let name = deviceProfile.name; + name = name.toLowerCase().replace(/\W/g, '_'); + this.exportToPc(this.prepareDeviceProfileExport(deviceProfile), name); + }, + (e) => { + this.handleExportError(e, 'device-profile.export-failed-error'); + } + ); + } + + public importDeviceProfile(): Observable { + return this.openImportDialog('device-profile.import', 'device-profile.device-profile-file').pipe( + mergeMap((deviceProfile: DeviceProfile) => { + if (!this.validateImportedDeviceProfile(deviceProfile)) { + this.store.dispatch(new ActionNotificationShow( + {message: this.translate.instant('device-profile.invalid-device-profile-file-error'), + type: 'error'})); + throw new Error('Invalid device profile file'); + } else { + return this.deviceProfileService.saveDeviceProfile(deviceProfile); + } + }), + catchError((err) => { + return of(null); + }) + ); + } + public exportJSZip(data: object, filename: string) { import('jszip').then((JSZip) => { const jsZip = new JSZip.default(); @@ -463,6 +497,17 @@ export class ImportExportService { return true; } + private validateImportedDeviceProfile(deviceProfile: DeviceProfile): boolean { + if (isUndefined(deviceProfile.name) + || isUndefined(deviceProfile.type) + || isUndefined(deviceProfile.transportType) + || isUndefined(deviceProfile.provisionType) + || isUndefined(deviceProfile.profileData)) { + return false; + } + return true; + } + private sumObject(obj1: any, obj2: any): any { Object.keys(obj2).map((key) => { if (isObject(obj2[key])) { @@ -740,6 +785,12 @@ export class ImportExportService { return dashboard; } + private prepareDeviceProfileExport(deviceProfile: DeviceProfile): DeviceProfile { + deviceProfile = this.prepareExport(deviceProfile); + deviceProfile.default = false; + return deviceProfile; + } + private prepareExport(data: any): any { const exportedData = deepClone(data); if (isDefined(exportedData.id)) { diff --git a/ui-ngx/src/app/modules/home/components/profile/device-profile.component.html b/ui-ngx/src/app/modules/home/components/profile/device-profile.component.html index 0d1efcbe04..9ee6201db6 100644 --- a/ui-ngx/src/app/modules/home/components/profile/device-profile.component.html +++ b/ui-ngx/src/app/modules/home/components/profile/device-profile.component.html @@ -16,6 +16,12 @@ -->
+