Commit 1e5be4d8cad80f29ea78887deeea6c592ded8eb6

Authored by Vladyslav_Prykhodko
Committed by Andrew Shvayka
1 parent 615af6bc

UI: Refactoring OTA Update service

... ... @@ -18,17 +18,14 @@ import { Injectable } from '@angular/core';
18 18 import { HttpClient } from '@angular/common/http';
19 19 import { PageLink } from '@shared/models/page/page-link';
20 20 import { defaultHttpOptionsFromConfig, RequestConfig } from './http-utils';
21   -import { forkJoin, Observable, of, throwError } from 'rxjs';
  21 +import { Observable, throwError } from 'rxjs';
22 22 import { PageData } from '@shared/models/page/page-data';
23 23 import { DeviceProfile, DeviceProfileInfo, DeviceTransportType } from '@shared/models/device.models';
24 24 import { isDefinedAndNotNull, isEmptyStr } from '@core/utils';
25 25 import { ObjectLwM2M, ServerSecurityConfig } from '@home/components/profile/device/lwm2m/lwm2m-profile-config.models';
26 26 import { SortOrder } from '@shared/models/page/sort-order';
27 27 import { OtaPackageService } from '@core/http/ota-package.service';
28   -import { OtaUpdateType } from '@shared/models/ota-package.models';
29 28 import { mergeMap } from 'rxjs/operators';
30   -import { DialogService } from '@core/services/dialog.service';
31   -import { TranslateService } from '@ngx-translate/core';
32 29
33 30 @Injectable({
34 31 providedIn: 'root'
... ... @@ -37,9 +34,7 @@ export class DeviceProfileService {
37 34
38 35 constructor(
39 36 private http: HttpClient,
40   - private otaPackageService: OtaPackageService,
41   - private dialogService: DialogService,
42   - private translate: TranslateService
  37 + private otaPackageService: OtaPackageService
43 38 ) {
44 39 }
45 40
... ... @@ -80,30 +75,9 @@ export class DeviceProfileService {
80 75
81 76 public saveDeviceProfileAndConfirmOtaChange(originDeviceProfile: DeviceProfile, deviceProfile: DeviceProfile,
82 77 config?: RequestConfig): Observable<DeviceProfile> {
83   - const tasks: Observable<number>[] = [];
84   - if (originDeviceProfile?.id?.id && originDeviceProfile.firmwareId?.id !== deviceProfile.firmwareId?.id) {
85   - tasks.push(this.otaPackageService.countUpdateDeviceAfterChangePackage(OtaUpdateType.FIRMWARE, deviceProfile.id.id));
86   - } else {
87   - tasks.push(of(0));
88   - }
89   - if (originDeviceProfile?.id?.id && originDeviceProfile.softwareId?.id !== deviceProfile.softwareId?.id) {
90   - tasks.push(this.otaPackageService.countUpdateDeviceAfterChangePackage(OtaUpdateType.SOFTWARE, deviceProfile.id.id));
91   - } else {
92   - tasks.push(of(0));
93   - }
94   - return forkJoin(tasks).pipe(
95   - mergeMap(([deviceFirmwareUpdate, deviceSoftwareUpdate]) => {
96   - let text = '';
97   - if (deviceFirmwareUpdate > 0) {
98   - text += this.translate.instant('ota-update.change-firmware', {count: deviceFirmwareUpdate});
99   - }
100   - if (deviceSoftwareUpdate > 0) {
101   - text += text.length ? ' ' : '';
102   - text += this.translate.instant('ota-update.change-software', {count: deviceSoftwareUpdate});
103   - }
104   - return text !== '' ? this.dialogService.confirm('', text, null, this.translate.instant('common.proceed')) : of(true);
105   - }),
106   - mergeMap((update) => update ? this.saveDeviceProfile(deviceProfile, config) : throwError('Canceled saving device profiles')));
  78 + return this.otaPackageService.confirmDialogUpdatePackage(deviceProfile, originDeviceProfile).pipe(
  79 + mergeMap((update) => update ? this.saveDeviceProfile(deviceProfile, config) : throwError('Canceled saving device profiles'))
  80 + );
107 81 }
108 82
109 83 public saveDeviceProfile(deviceProfile: DeviceProfile, config?: RequestConfig): Observable<DeviceProfile> {
... ...
... ... @@ -18,18 +18,30 @@ import { Injectable } from '@angular/core';
18 18 import { HttpClient } from '@angular/common/http';
19 19 import { PageLink } from '@shared/models/page/page-link';
20 20 import { defaultHttpOptionsFromConfig, defaultHttpUploadOptions, RequestConfig } from '@core/http/http-utils';
21   -import { Observable } from 'rxjs';
  21 +import { forkJoin, Observable, of } from 'rxjs';
22 22 import { PageData } from '@shared/models/page/page-data';
23   -import { ChecksumAlgorithm, OtaPackage, OtaPackageInfo, OtaUpdateType } from '@shared/models/ota-package.models';
  23 +import {
  24 + ChecksumAlgorithm,
  25 + OtaPackage,
  26 + OtaPackageInfo,
  27 + OtaPagesIds,
  28 + OtaUpdateType
  29 +} from '@shared/models/ota-package.models';
24 30 import { catchError, map, mergeMap } from 'rxjs/operators';
25 31 import { deepClone } from '@core/utils';
  32 +import { BaseData } from '@shared/models/base-data';
  33 +import { EntityId } from '@shared/models/id/entity-id';
  34 +import { TranslateService } from '@ngx-translate/core';
  35 +import { DialogService } from '@core/services/dialog.service';
26 36
27 37 @Injectable({
28 38 providedIn: 'root'
29 39 })
30 40 export class OtaPackageService {
31 41 constructor(
32   - private http: HttpClient
  42 + private http: HttpClient,
  43 + private translate: TranslateService,
  44 + private dialogService: DialogService
33 45 ) {
34 46
35 47 }
... ... @@ -120,8 +132,36 @@ export class OtaPackageService {
120 132 return this.http.delete(`/api/otaPackage/${otaPackageId}`, defaultHttpOptionsFromConfig(config));
121 133 }
122 134
123   - public countUpdateDeviceAfterChangePackage(type: OtaUpdateType, deviceProfileId: string, config?: RequestConfig): Observable<number> {
124   - return this.http.get<number>(`/api/devices/count/${type}?deviceProfileId=${deviceProfileId}`, defaultHttpOptionsFromConfig(config));
  135 + public countUpdateDeviceAfterChangePackage(type: OtaUpdateType, entityId: EntityId, config?: RequestConfig): Observable<number> {
  136 + return this.http.get<number>(`/api/devices/count/${type}?deviceProfileId=${entityId.id}`, defaultHttpOptionsFromConfig(config));
  137 + }
  138 +
  139 + public confirmDialogUpdatePackage(entity: BaseData<EntityId>&OtaPagesIds,
  140 + originEntity: BaseData<EntityId>&OtaPagesIds): Observable<boolean> {
  141 + const tasks: Observable<number>[] = [];
  142 + if (originEntity?.id?.id && originEntity.firmwareId?.id !== entity.firmwareId?.id) {
  143 + tasks.push(this.countUpdateDeviceAfterChangePackage(OtaUpdateType.FIRMWARE, entity.id));
  144 + } else {
  145 + tasks.push(of(0));
  146 + }
  147 + if (originEntity?.id?.id && originEntity.softwareId?.id !== entity.softwareId?.id) {
  148 + tasks.push(this.countUpdateDeviceAfterChangePackage(OtaUpdateType.SOFTWARE, entity.id));
  149 + } else {
  150 + tasks.push(of(0));
  151 + }
  152 + return forkJoin(tasks).pipe(
  153 + mergeMap(([deviceFirmwareUpdate, deviceSoftwareUpdate]) => {
  154 + let text = '';
  155 + if (deviceFirmwareUpdate > 0) {
  156 + text += this.translate.instant('ota-update.change-firmware', {count: deviceFirmwareUpdate});
  157 + }
  158 + if (deviceSoftwareUpdate > 0) {
  159 + text += text.length ? ' ' : '';
  160 + text += this.translate.instant('ota-update.change-software', {count: deviceSoftwareUpdate});
  161 + }
  162 + return text !== '' ? this.dialogService.confirm('', text, null, this.translate.instant('common.proceed')) : of(true);
  163 + })
  164 + );
125 165 }
126 166
127 167 }
... ...
... ... @@ -80,6 +80,11 @@ export const OtaUpdateTranslation = new Map<OtaUpdateType, OtaUpdateTranslation>
80 80 ]
81 81 );
82 82
  83 +export interface OtaPagesIds {
  84 + firmwareId?: OtaPackageId;
  85 + softwareId?: OtaPackageId;
  86 +}
  87 +
83 88 export interface OtaPackageInfo extends BaseData<OtaPackageId> {
84 89 tenantId?: TenantId;
85 90 type: OtaUpdateType;
... ...