ota-update-table-config.resolve.ts
5.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
///
/// Copyright © 2016-2021 The Thingsboard Authors
///
/// Licensed under the Apache License, Version 2.0 (the "License");
/// you may not use this file except in compliance with the License.
/// You may obtain a copy of the License at
///
/// http://www.apache.org/licenses/LICENSE-2.0
///
/// Unless required by applicable law or agreed to in writing, software
/// distributed under the License is distributed on an "AS IS" BASIS,
/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
/// See the License for the specific language governing permissions and
/// limitations under the License.
///
import { Injectable } from '@angular/core';
import { Resolve } from '@angular/router';
import {
DateEntityTableColumn,
EntityTableColumn,
EntityTableConfig
} from '@home/models/entity/entities-table-config.models';
import {
ChecksumAlgorithmTranslationMap,
OtaPackage,
OtaPackageInfo,
OtaUpdateTypeTranslationMap
} from '@shared/models/ota-package.models';
import { EntityType, entityTypeResources, entityTypeTranslations } from '@shared/models/entity-type.models';
import { TranslateService } from '@ngx-translate/core';
import { DatePipe } from '@angular/common';
import { OtaPackageService } from '@core/http/ota-package.service';
import { PageLink } from '@shared/models/page/page-link';
import { OtaUpdateComponent } from '@home/pages/ota-update/ota-update.component';
import { EntityAction } from '@home/models/entity/entity-component.models';
import { FileSizePipe } from '@shared/pipe/file-size.pipe';
@Injectable()
export class OtaUpdateTableConfigResolve implements Resolve<EntityTableConfig<OtaPackage, PageLink, OtaPackageInfo>> {
private readonly config: EntityTableConfig<OtaPackage, PageLink, OtaPackageInfo> =
new EntityTableConfig<OtaPackage, PageLink, OtaPackageInfo>();
constructor(private translate: TranslateService,
private datePipe: DatePipe,
private otaPackageService: OtaPackageService,
private fileSize: FileSizePipe) {
this.config.entityType = EntityType.OTA_PACKAGE;
this.config.entityComponent = OtaUpdateComponent;
this.config.entityTranslations = entityTypeTranslations.get(EntityType.OTA_PACKAGE);
this.config.entityResources = entityTypeResources.get(EntityType.OTA_PACKAGE);
this.config.entityTitle = (otaPackage) => otaPackage ? otaPackage.title : '';
this.config.columns.push(
new DateEntityTableColumn<OtaPackageInfo>('createdTime', 'common.created-time', this.datePipe, '150px'),
new EntityTableColumn<OtaPackageInfo>('title', 'ota-update.title', '25%'),
new EntityTableColumn<OtaPackageInfo>('version', 'ota-update.version', '25%'),
new EntityTableColumn<OtaPackageInfo>('type', 'ota-update.package-type', '25%', entity => {
return this.translate.instant(OtaUpdateTypeTranslationMap.get(entity.type));
}),
new EntityTableColumn<OtaPackageInfo>('fileName', 'ota-update.file-name', '25%'),
new EntityTableColumn<OtaPackageInfo>('dataSize', 'ota-update.file-size', '70px', entity => {
return this.fileSize.transform(entity.dataSize || 0);
}),
new EntityTableColumn<OtaPackageInfo>('checksum', 'ota-update.checksum', '540px', entity => {
return `${ChecksumAlgorithmTranslationMap.get(entity.checksumAlgorithm)}: ${entity.checksum}`;
}, () => ({}), false)
);
this.config.cellActionDescriptors.push(
{
name: this.translate.instant('ota-update.download'),
icon: 'file_download',
isEnabled: (otaPackage) => otaPackage.hasData,
onAction: ($event, entity) => this.exportPackage($event, entity)
}
);
this.config.deleteEntityTitle = otaPackage => this.translate.instant('ota-update.delete-ota-update-title',
{ title: otaPackage.title });
this.config.deleteEntityContent = () => this.translate.instant('ota-update.delete-ota-update-text');
this.config.deleteEntitiesTitle = count => this.translate.instant('ota-update.delete-ota-updates-title', {count});
this.config.deleteEntitiesContent = () => this.translate.instant('ota-update.delete-ota-updates-text');
this.config.entitiesFetchFunction = pageLink => this.otaPackageService.getOtaPackages(pageLink);
this.config.loadEntity = id => this.otaPackageService.getOtaPackageInfo(id.id);
this.config.saveEntity = otaPackage => this.otaPackageService.saveOtaPackage(otaPackage);
this.config.deleteEntity = id => this.otaPackageService.deleteOtaPackage(id.id);
this.config.onEntityAction = action => this.onPackageAction(action);
}
resolve(): EntityTableConfig<OtaPackage, PageLink, OtaPackageInfo> {
this.config.tableTitle = this.translate.instant('ota-update.packages-repository');
return this.config;
}
exportPackage($event: Event, otaPackageInfo: OtaPackageInfo) {
if ($event) {
$event.stopPropagation();
}
this.otaPackageService.downloadOtaPackage(otaPackageInfo.id.id).subscribe();
}
onPackageAction(action: EntityAction<OtaPackageInfo>): boolean {
switch (action.action) {
case 'uploadPackage':
this.exportPackage(action.event, action.entity);
return true;
}
return false;
}
}