From bd8a104b9f4900b2d25003a1fe681d2a7617149d Mon Sep 17 00:00:00 2001 From: Igor Kulikov Date: Fri, 16 Aug 2019 20:09:56 +0300 Subject: [PATCH] UI: Asset and Entity View services --- ui-ngx/src/app/core/http/asset.service.ts | 84 +++++++++++++++++++ .../src/app/core/http/entity-view.service.ts | 83 ++++++++++++++++++ ui-ngx/src/app/core/http/entity.service.ts | 23 +++-- ...d-entities-to-customer-dialog.component.ts | 8 +- .../assign-to-customer-dialog.component.ts | 8 +- .../entity-subtype-autocomplete.component.ts | 8 +- .../entity/entity-subtype-select.component.ts | 8 +- 7 files changed, 206 insertions(+), 16 deletions(-) create mode 100644 ui-ngx/src/app/core/http/asset.service.ts create mode 100644 ui-ngx/src/app/core/http/entity-view.service.ts diff --git a/ui-ngx/src/app/core/http/asset.service.ts b/ui-ngx/src/app/core/http/asset.service.ts new file mode 100644 index 0000000000..e7cd9af761 --- /dev/null +++ b/ui-ngx/src/app/core/http/asset.service.ts @@ -0,0 +1,84 @@ +/// +/// Copyright © 2016-2019 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 {defaultHttpOptions} from './http-utils'; +import {Observable} from 'rxjs/index'; +import {HttpClient} from '@angular/common/http'; +import {PageLink} from '@shared/models/page/page-link'; +import {PageData} from '@shared/models/page/page-data'; +import {EntitySubtype} from '@app/shared/models/entity-type.models'; +import {Asset, AssetInfo} from '@app/shared/models/asset.models'; + +@Injectable({ + providedIn: 'root' +}) +export class AssetService { + + constructor( + private http: HttpClient + ) { } + + public getTenantAssetInfos(pageLink: PageLink, type: string = '', ignoreErrors: boolean = false, + ignoreLoading: boolean = false): Observable> { + return this.http.get>(`/api/tenant/assetInfos${pageLink.toQuery()}&type=${type}`, + defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public getCustomerAssetInfos(customerId: string, pageLink: PageLink, type: string = '', ignoreErrors: boolean = false, + ignoreLoading: boolean = false): Observable> { + return this.http.get>(`/api/customer/${customerId}/assetInfos${pageLink.toQuery()}&type=${type}`, + defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public getAsset(assetId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable { + return this.http.get(`/api/asset/${assetId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public getAssets(assetIds: Array, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable> { + return this.http.get>(`/api/assets?assetIds=${assetIds.join(',')}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public getAssetInfo(assetId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable { + return this.http.get(`/api/asset/info/${assetId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public saveAsset(asset: Asset, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable { + return this.http.post('/api/asset', asset, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public deleteAsset(assetId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false) { + return this.http.delete(`/api/asset/${assetId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public getAssetTypes(ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable> { + return this.http.get>('/api/asset/types', defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public makeAssetPublic(assetId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable { + return this.http.post(`/api/customer/public/asset/${assetId}`, null, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public assignAssetToCustomer(customerId: string, assetId: string, + ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable { + return this.http.post(`/api/customer/${customerId}/asset/${assetId}`, null, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public unassignAssetFromCustomer(assetId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false) { + return this.http.delete(`/api/customer/asset/${assetId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + +} diff --git a/ui-ngx/src/app/core/http/entity-view.service.ts b/ui-ngx/src/app/core/http/entity-view.service.ts new file mode 100644 index 0000000000..c22c2e0d74 --- /dev/null +++ b/ui-ngx/src/app/core/http/entity-view.service.ts @@ -0,0 +1,83 @@ +/// +/// Copyright © 2016-2019 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 {defaultHttpOptions} from './http-utils'; +import {Observable} from 'rxjs/index'; +import {HttpClient} from '@angular/common/http'; +import {PageLink} from '@shared/models/page/page-link'; +import {PageData} from '@shared/models/page/page-data'; +import {EntitySubtype} from '@app/shared/models/entity-type.models'; +import {EntityView, EntityViewInfo} from '@app/shared/models/entity-view.models'; + +@Injectable({ + providedIn: 'root' +}) +export class EntityViewService { + + constructor( + private http: HttpClient + ) { } + + public getTenantEntityViewInfos(pageLink: PageLink, type: string = '', ignoreErrors: boolean = false, + ignoreLoading: boolean = false): Observable> { + return this.http.get>(`/api/tenant/entityViewInfos${pageLink.toQuery()}&type=${type}`, + defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public getCustomerEntityViewInfos(customerId: string, pageLink: PageLink, type: string = '', ignoreErrors: boolean = false, + ignoreLoading: boolean = false): Observable> { + return this.http.get>(`/api/customer/${customerId}/entityViewInfos${pageLink.toQuery()}&type=${type}`, + defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public getEntityView(entityViewId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable { + return this.http.get(`/api/entityView/${entityViewId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public getEntityViewInfo(entityViewId: string, ignoreErrors: boolean = false, + ignoreLoading: boolean = false): Observable { + return this.http.get(`/api/entityView/info/${entityViewId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public saveEntityView(entityView: EntityView, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable { + return this.http.post('/api/entityView', entityView, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public deleteEntityView(entityViewId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false) { + return this.http.delete(`/api/entityView/${entityViewId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public getEntityViewTypes(ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable> { + return this.http.get>('/api/entityView/types', defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public makeEntityViewPublic(entityViewId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable { + return this.http.post(`/api/customer/public/entityView/${entityViewId}`, null, + defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public assignEntityViewToCustomer(customerId: string, entityViewId: string, + ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable { + return this.http.post(`/api/customer/${customerId}/entityView/${entityViewId}`, null, + defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + + public unassignEntityViewFromCustomer(entityViewId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false) { + return this.http.delete(`/api/customer/entityView/${entityViewId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); + } + +} diff --git a/ui-ngx/src/app/core/http/entity.service.ts b/ui-ngx/src/app/core/http/entity.service.ts index f0f22c50da..ef4b306710 100644 --- a/ui-ngx/src/app/core/http/entity.service.ts +++ b/ui-ngx/src/app/core/http/entity.service.ts @@ -35,6 +35,8 @@ import {Authority} from '@shared/models/authority.enum'; import {Tenant} from '@shared/models/tenant.model'; import {concatMap, expand, map, toArray} from 'rxjs/operators'; import {Customer} from '@app/shared/models/customer.model'; +import {AssetService} from '@core/http/asset.service'; +import {EntityViewService} from '@core/http/entity-view.service'; @Injectable({ providedIn: 'root' @@ -45,6 +47,8 @@ export class EntityService { private http: HttpClient, private store: Store, private deviceService: DeviceService, + private assetService: AssetService, + private entityViewService: EntityViewService, private tenantService: TenantService, private customerService: CustomerService, private userService: UserService, @@ -60,10 +64,10 @@ export class EntityService { observable = this.deviceService.getDevice(entityId, ignoreErrors, ignoreLoading); break; case EntityType.ASSET: - // TODO: + observable = this.assetService.getAsset(entityId, ignoreErrors, ignoreLoading); break; case EntityType.ENTITY_VIEW: - // TODO: + observable = this.entityViewService.getEntityView(entityId, ignoreErrors, ignoreLoading); break; case EntityType.TENANT: observable = this.tenantService.getTenant(entityId, ignoreErrors, ignoreLoading); @@ -127,10 +131,12 @@ export class EntityService { observable = this.deviceService.getDevices(entityIds, ignoreErrors, ignoreLoading); break; case EntityType.ASSET: - // TODO: + observable = this.assetService.getAssets(entityIds, ignoreErrors, ignoreLoading); break; case EntityType.ENTITY_VIEW: - // TODO: + observable = this.getEntitiesByIdsObservable( + (id) => this.entityViewService.getEntityView(id, ignoreErrors, ignoreLoading), + entityIds); break; case EntityType.TENANT: observable = this.getEntitiesByIdsObservable( @@ -233,17 +239,18 @@ export class EntityService { case EntityType.ASSET: pageLink.sortOrder.property = 'name'; if (authUser.authority === Authority.CUSTOMER_USER) { - // TODO: + entitiesObservable = this.assetService.getCustomerAssetInfos(customerId, pageLink, subType, ignoreErrors, ignoreLoading); } else { - // TODO: + entitiesObservable = this.assetService.getTenantAssetInfos(pageLink, subType, ignoreErrors, ignoreLoading); } break; case EntityType.ENTITY_VIEW: pageLink.sortOrder.property = 'name'; if (authUser.authority === Authority.CUSTOMER_USER) { - // TODO: + entitiesObservable = this.entityViewService.getCustomerEntityViewInfos(customerId, pageLink, + subType, ignoreErrors, ignoreLoading); } else { - // TODO: + entitiesObservable = this.entityViewService.getTenantEntityViewInfos(pageLink, subType, ignoreErrors, ignoreLoading); } break; case EntityType.TENANT: diff --git a/ui-ngx/src/app/modules/home/dialogs/add-entities-to-customer-dialog.component.ts b/ui-ngx/src/app/modules/home/dialogs/add-entities-to-customer-dialog.component.ts index 19a4ac3be7..7420ddaf7f 100644 --- a/ui-ngx/src/app/modules/home/dialogs/add-entities-to-customer-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/dialogs/add-entities-to-customer-dialog.component.ts @@ -24,6 +24,8 @@ import {DeviceService} from '@core/http/device.service'; import {EntityId} from '@shared/models/id/entity-id'; import {EntityType} from '@shared/models/entity-type.models'; import {forkJoin, Observable} from 'rxjs'; +import {AssetService} from '@core/http/asset.service'; +import {EntityViewService} from '@core/http/entity-view.service'; export interface AddEntitiesToCustomerDialogData { customerId: string; @@ -50,6 +52,8 @@ export class AddEntitiesToCustomerDialogComponent extends PageComponent implemen constructor(protected store: Store, @Inject(MAT_DIALOG_DATA) public data: AddEntitiesToCustomerDialogData, private deviceService: DeviceService, + private assetService: AssetService, + private entityViewService: EntityViewService, @SkipSelf() private errorStateMatcher: ErrorStateMatcher, public dialogRef: MatDialogRef, public fb: FormBuilder) { @@ -107,10 +111,10 @@ export class AddEntitiesToCustomerDialogComponent extends PageComponent implemen return this.deviceService.assignDeviceToCustomer(customerId, entityId); break; case EntityType.ASSET: - // TODO: + return this.assetService.assignAssetToCustomer(customerId, entityId); break; case EntityType.ENTITY_VIEW: - // TODO: + return this.entityViewService.assignEntityViewToCustomer(customerId, entityId); break; } } diff --git a/ui-ngx/src/app/modules/home/dialogs/assign-to-customer-dialog.component.ts b/ui-ngx/src/app/modules/home/dialogs/assign-to-customer-dialog.component.ts index ea40fea272..b7bc4dbc09 100644 --- a/ui-ngx/src/app/modules/home/dialogs/assign-to-customer-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/dialogs/assign-to-customer-dialog.component.ts @@ -24,6 +24,8 @@ import {DeviceService} from '@core/http/device.service'; import {EntityId} from '@shared/models/id/entity-id'; import {EntityType} from '@shared/models/entity-type.models'; import {forkJoin, Observable} from 'rxjs'; +import {AssetService} from '@core/http/asset.service'; +import {EntityViewService} from '@core/http/entity-view.service'; export interface AssignToCustomerDialogData { entityIds: Array; @@ -50,6 +52,8 @@ export class AssignToCustomerDialogComponent extends PageComponent implements On constructor(protected store: Store, @Inject(MAT_DIALOG_DATA) public data: AssignToCustomerDialogData, private deviceService: DeviceService, + private assetService: AssetService, + private entityViewService: EntityViewService, @SkipSelf() private errorStateMatcher: ErrorStateMatcher, public dialogRef: MatDialogRef, public fb: FormBuilder) { @@ -106,10 +110,10 @@ export class AssignToCustomerDialogComponent extends PageComponent implements On return this.deviceService.assignDeviceToCustomer(customerId, entityId); break; case EntityType.ASSET: - // TODO: + return this.assetService.assignAssetToCustomer(customerId, entityId); break; case EntityType.ENTITY_VIEW: - // TODO: + return this.entityViewService.assignEntityViewToCustomer(customerId, entityId); break; } } diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts index b2d2e3b185..3cc79ab416 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-autocomplete.component.ts @@ -33,6 +33,8 @@ import {DeviceService} from '@core/http/device.service'; import {EntitySubtype, EntityType} from '@app/shared/models/entity-type.models'; import {BroadcastService} from '@app/core/services/broadcast.service'; import {coerceBooleanProperty} from '@angular/cdk/coercion'; +import {AssetService} from '@core/http/asset.service'; +import {EntityViewService} from '@core/http/entity-view.service'; @Component({ selector: 'tb-entity-subtype-autocomplete', @@ -85,6 +87,8 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, private broadcast: BroadcastService, public translate: TranslateService, private deviceService: DeviceService, + private assetService: AssetService, + private entityViewService: EntityViewService, private fb: FormBuilder) { this.subTypeFormGroup = this.fb.group({ subType: [null] @@ -203,13 +207,13 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, if (!this.subTypes) { switch (this.entityType) { case EntityType.ASSET: - // TODO: + this.subTypes = this.assetService.getAssetTypes(false, true); break; case EntityType.DEVICE: this.subTypes = this.deviceService.getDeviceTypes(false, true); break; case EntityType.ENTITY_VIEW: - // TODO: + this.subTypes = this.entityViewService.getEntityViewTypes(false, true); break; } if (this.subTypes) { diff --git a/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts b/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts index db31c53f56..ceed407498 100644 --- a/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts +++ b/ui-ngx/src/app/shared/components/entity/entity-subtype-select.component.ts @@ -32,6 +32,8 @@ import {TranslateService} from '@ngx-translate/core'; import {DeviceService} from '@core/http/device.service'; import {EntitySubtype, EntityType} from '@app/shared/models/entity-type.models'; import {BroadcastService} from '@app/core/services/broadcast.service'; +import {AssetService} from '@core/http/asset.service'; +import {EntityViewService} from '@core/http/entity-view.service'; @Component({ selector: 'tb-entity-subtype-select', @@ -83,6 +85,8 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni private broadcast: BroadcastService, public translate: TranslateService, private deviceService: DeviceService, + private assetService: AssetService, + private entityViewService: EntityViewService, private fb: FormBuilder) { this.subTypeFormGroup = this.fb.group({ subType: [null] @@ -202,13 +206,13 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni if (!this.subTypes) { switch (this.entityType) { case EntityType.ASSET: - // TODO: + this.subTypes = this.assetService.getAssetTypes(false, true); break; case EntityType.DEVICE: this.subTypes = this.deviceService.getDeviceTypes(false, true); break; case EntityType.ENTITY_VIEW: - // TODO: + this.subTypes = this.entityViewService.getEntityViewTypes(false, true); break; } if (this.subTypes) {