Browse Source

Device profile import/export support

pull/4275/head
Igor Kulikov 6 years ago
parent
commit
1e8fc7086b
  1. 51
      ui-ngx/src/app/modules/home/components/import-export/import-export.service.ts
  2. 6
      ui-ngx/src/app/modules/home/components/profile/device-profile.component.html
  3. 56
      ui-ngx/src/app/modules/home/pages/device-profile/device-profiles-table-config.resolver.ts
  4. 8
      ui-ngx/src/assets/locale/locale.constant-en_US.json

51
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 { RuleChain, RuleChainImport, RuleChainMetaData } from '@shared/models/rule-chain.models';
import { RuleChainService } from '@core/http/rule-chain.service'; import { RuleChainService } from '@core/http/rule-chain.service';
import { FiltersInfo } from '@shared/models/query/query.models'; import { FiltersInfo } from '@shared/models/query/query.models';
import { DeviceProfileService } from '@core/http/device-profile.service';
import { DeviceProfile } from '@shared/models/device.models';
// @dynamic // @dynamic
@Injectable() @Injectable()
@ -67,6 +69,7 @@ export class ImportExportService {
private dashboardService: DashboardService, private dashboardService: DashboardService,
private dashboardUtils: DashboardUtilsService, private dashboardUtils: DashboardUtilsService,
private widgetService: WidgetService, private widgetService: WidgetService,
private deviceProfileService: DeviceProfileService,
private entityService: EntityService, private entityService: EntityService,
private ruleChainService: RuleChainService, private ruleChainService: RuleChainService,
private utils: UtilsService, 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<DeviceProfile> {
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) { public exportJSZip(data: object, filename: string) {
import('jszip').then((JSZip) => { import('jszip').then((JSZip) => {
const jsZip = new JSZip.default(); const jsZip = new JSZip.default();
@ -463,6 +497,17 @@ export class ImportExportService {
return true; 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 { private sumObject(obj1: any, obj2: any): any {
Object.keys(obj2).map((key) => { Object.keys(obj2).map((key) => {
if (isObject(obj2[key])) { if (isObject(obj2[key])) {
@ -740,6 +785,12 @@ export class ImportExportService {
return dashboard; return dashboard;
} }
private prepareDeviceProfileExport(deviceProfile: DeviceProfile): DeviceProfile {
deviceProfile = this.prepareExport(deviceProfile);
deviceProfile.default = false;
return deviceProfile;
}
private prepareExport(data: any): any { private prepareExport(data: any): any {
const exportedData = deepClone(data); const exportedData = deepClone(data);
if (isDefined(exportedData.id)) { if (isDefined(exportedData.id)) {

6
ui-ngx/src/app/modules/home/components/profile/device-profile.component.html

@ -16,6 +16,12 @@
--> -->
<div class="tb-details-buttons" fxLayout.xs="column" *ngIf="!standalone"> <div class="tb-details-buttons" fxLayout.xs="column" *ngIf="!standalone">
<button mat-raised-button color="primary"
[disabled]="(isLoading$ | async)"
(click)="onEntityAction($event, 'export')"
[fxShow]="!isEdit">
{{'device-profile.export' | translate }}
</button>
<button mat-raised-button color="primary" <button mat-raised-button color="primary"
[disabled]="(isLoading$ | async)" [disabled]="(isLoading$ | async)"
(click)="onEntityAction($event, 'setDefault')" (click)="onEntityAction($event, 'setDefault')"

56
ui-ngx/src/app/modules/home/pages/device-profile/device-profiles-table-config.resolver.ts

@ -20,7 +20,8 @@ import {
checkBoxCell, checkBoxCell,
DateEntityTableColumn, DateEntityTableColumn,
EntityTableColumn, EntityTableColumn,
EntityTableConfig EntityTableConfig,
HeaderActionDescriptor
} from '@home/models/entity/entities-table-config.models'; } from '@home/models/entity/entities-table-config.models';
import { TranslateService } from '@ngx-translate/core'; import { TranslateService } from '@ngx-translate/core';
import { DatePipe } from '@angular/common'; import { DatePipe } from '@angular/common';
@ -33,14 +34,15 @@ import {
deviceTransportTypeTranslationMap deviceTransportTypeTranslationMap
} from '@shared/models/device.models'; } from '@shared/models/device.models';
import { DeviceProfileService } from '@core/http/device-profile.service'; import { DeviceProfileService } from '@core/http/device-profile.service';
import { DeviceProfileComponent } from '../../components/profile/device-profile.component'; import { DeviceProfileComponent } from '@home/components/profile/device-profile.component';
import { DeviceProfileTabsComponent } from './device-profile-tabs.component'; import { DeviceProfileTabsComponent } from './device-profile-tabs.component';
import { Observable } from 'rxjs'; import { Observable } from 'rxjs';
import { MatDialog } from '@angular/material/dialog'; import { MatDialog } from '@angular/material/dialog';
import { import {
AddDeviceProfileDialogComponent, AddDeviceProfileDialogComponent,
AddDeviceProfileDialogData AddDeviceProfileDialogData
} from '../../components/profile/add-device-profile-dialog.component'; } from '@home/components/profile/add-device-profile-dialog.component';
import { ImportExportService } from '@home/components/import-export/import-export.service';
@Injectable() @Injectable()
export class DeviceProfilesTableConfigResolver implements Resolve<EntityTableConfig<DeviceProfile>> { export class DeviceProfilesTableConfigResolver implements Resolve<EntityTableConfig<DeviceProfile>> {
@ -48,6 +50,7 @@ export class DeviceProfilesTableConfigResolver implements Resolve<EntityTableCon
private readonly config: EntityTableConfig<DeviceProfile> = new EntityTableConfig<DeviceProfile>(); private readonly config: EntityTableConfig<DeviceProfile> = new EntityTableConfig<DeviceProfile>();
constructor(private deviceProfileService: DeviceProfileService, constructor(private deviceProfileService: DeviceProfileService,
private importExport: ImportExportService,
private translate: TranslateService, private translate: TranslateService,
private datePipe: DatePipe, private datePipe: DatePipe,
private dialogService: DialogService, private dialogService: DialogService,
@ -80,6 +83,12 @@ export class DeviceProfilesTableConfigResolver implements Resolve<EntityTableCon
); );
this.config.cellActionDescriptors.push( this.config.cellActionDescriptors.push(
{
name: this.translate.instant('device-profile.export'),
icon: 'file_download',
isEnabled: () => true,
onAction: ($event, entity) => this.exportDeviceProfile($event, entity)
},
{ {
name: this.translate.instant('device-profile.set-default'), name: this.translate.instant('device-profile.set-default'),
icon: 'flag', icon: 'flag',
@ -101,7 +110,7 @@ export class DeviceProfilesTableConfigResolver implements Resolve<EntityTableCon
this.config.onEntityAction = action => this.onDeviceProfileAction(action); this.config.onEntityAction = action => this.onDeviceProfileAction(action);
this.config.deleteEnabled = (deviceProfile) => deviceProfile && !deviceProfile.default; this.config.deleteEnabled = (deviceProfile) => deviceProfile && !deviceProfile.default;
this.config.entitySelectionEnabled = (deviceProfile) => deviceProfile && !deviceProfile.default; this.config.entitySelectionEnabled = (deviceProfile) => deviceProfile && !deviceProfile.default;
this.config.addEntity = () => this.addDeviceProfile(); this.config.addActionDescriptors = this.configureAddActions();
} }
resolve(): EntityTableConfig<DeviceProfile> { resolve(): EntityTableConfig<DeviceProfile> {
@ -110,6 +119,25 @@ export class DeviceProfilesTableConfigResolver implements Resolve<EntityTableCon
return this.config; return this.config;
} }
configureAddActions(): Array<HeaderActionDescriptor> {
const actions: Array<HeaderActionDescriptor> = [];
actions.push(
{
name: this.translate.instant('device-profile.create-device-profile'),
icon: 'insert_drive_file',
isEnabled: () => true,
onAction: () => this.addDeviceProfile()
},
{
name: this.translate.instant('device-profile.import'),
icon: 'file_upload',
isEnabled: () => true,
onAction: ($event) => this.importDeviceProfile($event)
}
);
return actions;
}
addDeviceProfile(): Observable<DeviceProfile> { addDeviceProfile(): Observable<DeviceProfile> {
return this.dialog.open<AddDeviceProfileDialogComponent, AddDeviceProfileDialogData, return this.dialog.open<AddDeviceProfileDialogComponent, AddDeviceProfileDialogData,
DeviceProfile>(AddDeviceProfileDialogComponent, { DeviceProfile>(AddDeviceProfileDialogComponent, {
@ -144,11 +172,31 @@ export class DeviceProfilesTableConfigResolver implements Resolve<EntityTableCon
); );
} }
importDeviceProfile($event: Event) {
this.importExport.importDeviceProfile().subscribe(
(deviceProfile) => {
if (deviceProfile) {
this.config.table.updateData();
}
}
);
}
exportDeviceProfile($event: Event, deviceProfile: DeviceProfile) {
if ($event) {
$event.stopPropagation();
}
this.importExport.exportDeviceProfile(deviceProfile.id.id);
}
onDeviceProfileAction(action: EntityAction<DeviceProfile>): boolean { onDeviceProfileAction(action: EntityAction<DeviceProfile>): boolean {
switch (action.action) { switch (action.action) {
case 'setDefault': case 'setDefault':
this.setDefaultDeviceProfile(action.event, action.entity); this.setDefaultDeviceProfile(action.event, action.entity);
return true; return true;
case 'export':
this.exportDeviceProfile(action.event, action.entity);
return true;
} }
return false; return false;
} }

8
ui-ngx/src/assets/locale/locale.constant-en_US.json

@ -1091,7 +1091,13 @@
"schedule-time": "Time", "schedule-time": "Time",
"schedule-time-from": "From", "schedule-time-from": "From",
"schedule-time-to": "To", "schedule-time-to": "To",
"schedule-days-of-week-required": "At least one day of week should be selected." "schedule-days-of-week-required": "At least one day of week should be selected.",
"create-device-profile": "Create new device profile",
"import": "Import device profile",
"export": "Export device profile",
"export-failed-error": "Unable to export device profile: {{error}}",
"device-profile-file": "Device profile file",
"invalid-device-profile-file-error": "Unable to import device profile: Invalid device profile data structure."
}, },
"dialog": { "dialog": {
"close": "Close dialog" "close": "Close dialog"

Loading…
Cancel
Save