Commit bd8a104b9f4900b2d25003a1fe681d2a7617149d
1 parent
7ff599f7
UI: Asset and Entity View services
Showing
7 changed files
with
206 additions
and
16 deletions
ui-ngx/src/app/core/http/asset.service.ts
0 → 100644
1 | +/// | |
2 | +/// Copyright © 2016-2019 The Thingsboard Authors | |
3 | +/// | |
4 | +/// Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | +/// you may not use this file except in compliance with the License. | |
6 | +/// You may obtain a copy of the License at | |
7 | +/// | |
8 | +/// http://www.apache.org/licenses/LICENSE-2.0 | |
9 | +/// | |
10 | +/// Unless required by applicable law or agreed to in writing, software | |
11 | +/// distributed under the License is distributed on an "AS IS" BASIS, | |
12 | +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | +/// See the License for the specific language governing permissions and | |
14 | +/// limitations under the License. | |
15 | +/// | |
16 | + | |
17 | +import {Injectable} from '@angular/core'; | |
18 | +import {defaultHttpOptions} from './http-utils'; | |
19 | +import {Observable} from 'rxjs/index'; | |
20 | +import {HttpClient} from '@angular/common/http'; | |
21 | +import {PageLink} from '@shared/models/page/page-link'; | |
22 | +import {PageData} from '@shared/models/page/page-data'; | |
23 | +import {EntitySubtype} from '@app/shared/models/entity-type.models'; | |
24 | +import {Asset, AssetInfo} from '@app/shared/models/asset.models'; | |
25 | + | |
26 | +@Injectable({ | |
27 | + providedIn: 'root' | |
28 | +}) | |
29 | +export class AssetService { | |
30 | + | |
31 | + constructor( | |
32 | + private http: HttpClient | |
33 | + ) { } | |
34 | + | |
35 | + public getTenantAssetInfos(pageLink: PageLink, type: string = '', ignoreErrors: boolean = false, | |
36 | + ignoreLoading: boolean = false): Observable<PageData<AssetInfo>> { | |
37 | + return this.http.get<PageData<AssetInfo>>(`/api/tenant/assetInfos${pageLink.toQuery()}&type=${type}`, | |
38 | + defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
39 | + } | |
40 | + | |
41 | + public getCustomerAssetInfos(customerId: string, pageLink: PageLink, type: string = '', ignoreErrors: boolean = false, | |
42 | + ignoreLoading: boolean = false): Observable<PageData<AssetInfo>> { | |
43 | + return this.http.get<PageData<AssetInfo>>(`/api/customer/${customerId}/assetInfos${pageLink.toQuery()}&type=${type}`, | |
44 | + defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
45 | + } | |
46 | + | |
47 | + public getAsset(assetId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<Asset> { | |
48 | + return this.http.get<Asset>(`/api/asset/${assetId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
49 | + } | |
50 | + | |
51 | + public getAssets(assetIds: Array<string>, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<Array<Asset>> { | |
52 | + return this.http.get<Array<Asset>>(`/api/assets?assetIds=${assetIds.join(',')}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
53 | + } | |
54 | + | |
55 | + public getAssetInfo(assetId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<AssetInfo> { | |
56 | + return this.http.get<AssetInfo>(`/api/asset/info/${assetId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
57 | + } | |
58 | + | |
59 | + public saveAsset(asset: Asset, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<Asset> { | |
60 | + return this.http.post<Asset>('/api/asset', asset, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
61 | + } | |
62 | + | |
63 | + public deleteAsset(assetId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false) { | |
64 | + return this.http.delete(`/api/asset/${assetId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
65 | + } | |
66 | + | |
67 | + public getAssetTypes(ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<Array<EntitySubtype>> { | |
68 | + return this.http.get<Array<EntitySubtype>>('/api/asset/types', defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
69 | + } | |
70 | + | |
71 | + public makeAssetPublic(assetId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<Asset> { | |
72 | + return this.http.post<Asset>(`/api/customer/public/asset/${assetId}`, null, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
73 | + } | |
74 | + | |
75 | + public assignAssetToCustomer(customerId: string, assetId: string, | |
76 | + ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<Asset> { | |
77 | + return this.http.post<Asset>(`/api/customer/${customerId}/asset/${assetId}`, null, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
78 | + } | |
79 | + | |
80 | + public unassignAssetFromCustomer(assetId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false) { | |
81 | + return this.http.delete(`/api/customer/asset/${assetId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
82 | + } | |
83 | + | |
84 | +} | ... | ... |
1 | +/// | |
2 | +/// Copyright © 2016-2019 The Thingsboard Authors | |
3 | +/// | |
4 | +/// Licensed under the Apache License, Version 2.0 (the "License"); | |
5 | +/// you may not use this file except in compliance with the License. | |
6 | +/// You may obtain a copy of the License at | |
7 | +/// | |
8 | +/// http://www.apache.org/licenses/LICENSE-2.0 | |
9 | +/// | |
10 | +/// Unless required by applicable law or agreed to in writing, software | |
11 | +/// distributed under the License is distributed on an "AS IS" BASIS, | |
12 | +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
13 | +/// See the License for the specific language governing permissions and | |
14 | +/// limitations under the License. | |
15 | +/// | |
16 | + | |
17 | +import {Injectable} from '@angular/core'; | |
18 | +import {defaultHttpOptions} from './http-utils'; | |
19 | +import {Observable} from 'rxjs/index'; | |
20 | +import {HttpClient} from '@angular/common/http'; | |
21 | +import {PageLink} from '@shared/models/page/page-link'; | |
22 | +import {PageData} from '@shared/models/page/page-data'; | |
23 | +import {EntitySubtype} from '@app/shared/models/entity-type.models'; | |
24 | +import {EntityView, EntityViewInfo} from '@app/shared/models/entity-view.models'; | |
25 | + | |
26 | +@Injectable({ | |
27 | + providedIn: 'root' | |
28 | +}) | |
29 | +export class EntityViewService { | |
30 | + | |
31 | + constructor( | |
32 | + private http: HttpClient | |
33 | + ) { } | |
34 | + | |
35 | + public getTenantEntityViewInfos(pageLink: PageLink, type: string = '', ignoreErrors: boolean = false, | |
36 | + ignoreLoading: boolean = false): Observable<PageData<EntityViewInfo>> { | |
37 | + return this.http.get<PageData<EntityViewInfo>>(`/api/tenant/entityViewInfos${pageLink.toQuery()}&type=${type}`, | |
38 | + defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
39 | + } | |
40 | + | |
41 | + public getCustomerEntityViewInfos(customerId: string, pageLink: PageLink, type: string = '', ignoreErrors: boolean = false, | |
42 | + ignoreLoading: boolean = false): Observable<PageData<EntityViewInfo>> { | |
43 | + return this.http.get<PageData<EntityViewInfo>>(`/api/customer/${customerId}/entityViewInfos${pageLink.toQuery()}&type=${type}`, | |
44 | + defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
45 | + } | |
46 | + | |
47 | + public getEntityView(entityViewId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<EntityView> { | |
48 | + return this.http.get<EntityView>(`/api/entityView/${entityViewId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
49 | + } | |
50 | + | |
51 | + public getEntityViewInfo(entityViewId: string, ignoreErrors: boolean = false, | |
52 | + ignoreLoading: boolean = false): Observable<EntityViewInfo> { | |
53 | + return this.http.get<EntityViewInfo>(`/api/entityView/info/${entityViewId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
54 | + } | |
55 | + | |
56 | + public saveEntityView(entityView: EntityView, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<EntityView> { | |
57 | + return this.http.post<EntityView>('/api/entityView', entityView, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
58 | + } | |
59 | + | |
60 | + public deleteEntityView(entityViewId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false) { | |
61 | + return this.http.delete(`/api/entityView/${entityViewId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
62 | + } | |
63 | + | |
64 | + public getEntityViewTypes(ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<Array<EntitySubtype>> { | |
65 | + return this.http.get<Array<EntitySubtype>>('/api/entityView/types', defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
66 | + } | |
67 | + | |
68 | + public makeEntityViewPublic(entityViewId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<EntityView> { | |
69 | + return this.http.post<EntityView>(`/api/customer/public/entityView/${entityViewId}`, null, | |
70 | + defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
71 | + } | |
72 | + | |
73 | + public assignEntityViewToCustomer(customerId: string, entityViewId: string, | |
74 | + ignoreErrors: boolean = false, ignoreLoading: boolean = false): Observable<EntityView> { | |
75 | + return this.http.post<EntityView>(`/api/customer/${customerId}/entityView/${entityViewId}`, null, | |
76 | + defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
77 | + } | |
78 | + | |
79 | + public unassignEntityViewFromCustomer(entityViewId: string, ignoreErrors: boolean = false, ignoreLoading: boolean = false) { | |
80 | + return this.http.delete(`/api/customer/entityView/${entityViewId}`, defaultHttpOptions(ignoreLoading, ignoreErrors)); | |
81 | + } | |
82 | + | |
83 | +} | ... | ... |
... | ... | @@ -35,6 +35,8 @@ import {Authority} from '@shared/models/authority.enum'; |
35 | 35 | import {Tenant} from '@shared/models/tenant.model'; |
36 | 36 | import {concatMap, expand, map, toArray} from 'rxjs/operators'; |
37 | 37 | import {Customer} from '@app/shared/models/customer.model'; |
38 | +import {AssetService} from '@core/http/asset.service'; | |
39 | +import {EntityViewService} from '@core/http/entity-view.service'; | |
38 | 40 | |
39 | 41 | @Injectable({ |
40 | 42 | providedIn: 'root' |
... | ... | @@ -45,6 +47,8 @@ export class EntityService { |
45 | 47 | private http: HttpClient, |
46 | 48 | private store: Store<AppState>, |
47 | 49 | private deviceService: DeviceService, |
50 | + private assetService: AssetService, | |
51 | + private entityViewService: EntityViewService, | |
48 | 52 | private tenantService: TenantService, |
49 | 53 | private customerService: CustomerService, |
50 | 54 | private userService: UserService, |
... | ... | @@ -60,10 +64,10 @@ export class EntityService { |
60 | 64 | observable = this.deviceService.getDevice(entityId, ignoreErrors, ignoreLoading); |
61 | 65 | break; |
62 | 66 | case EntityType.ASSET: |
63 | - // TODO: | |
67 | + observable = this.assetService.getAsset(entityId, ignoreErrors, ignoreLoading); | |
64 | 68 | break; |
65 | 69 | case EntityType.ENTITY_VIEW: |
66 | - // TODO: | |
70 | + observable = this.entityViewService.getEntityView(entityId, ignoreErrors, ignoreLoading); | |
67 | 71 | break; |
68 | 72 | case EntityType.TENANT: |
69 | 73 | observable = this.tenantService.getTenant(entityId, ignoreErrors, ignoreLoading); |
... | ... | @@ -127,10 +131,12 @@ export class EntityService { |
127 | 131 | observable = this.deviceService.getDevices(entityIds, ignoreErrors, ignoreLoading); |
128 | 132 | break; |
129 | 133 | case EntityType.ASSET: |
130 | - // TODO: | |
134 | + observable = this.assetService.getAssets(entityIds, ignoreErrors, ignoreLoading); | |
131 | 135 | break; |
132 | 136 | case EntityType.ENTITY_VIEW: |
133 | - // TODO: | |
137 | + observable = this.getEntitiesByIdsObservable( | |
138 | + (id) => this.entityViewService.getEntityView(id, ignoreErrors, ignoreLoading), | |
139 | + entityIds); | |
134 | 140 | break; |
135 | 141 | case EntityType.TENANT: |
136 | 142 | observable = this.getEntitiesByIdsObservable( |
... | ... | @@ -233,17 +239,18 @@ export class EntityService { |
233 | 239 | case EntityType.ASSET: |
234 | 240 | pageLink.sortOrder.property = 'name'; |
235 | 241 | if (authUser.authority === Authority.CUSTOMER_USER) { |
236 | - // TODO: | |
242 | + entitiesObservable = this.assetService.getCustomerAssetInfos(customerId, pageLink, subType, ignoreErrors, ignoreLoading); | |
237 | 243 | } else { |
238 | - // TODO: | |
244 | + entitiesObservable = this.assetService.getTenantAssetInfos(pageLink, subType, ignoreErrors, ignoreLoading); | |
239 | 245 | } |
240 | 246 | break; |
241 | 247 | case EntityType.ENTITY_VIEW: |
242 | 248 | pageLink.sortOrder.property = 'name'; |
243 | 249 | if (authUser.authority === Authority.CUSTOMER_USER) { |
244 | - // TODO: | |
250 | + entitiesObservable = this.entityViewService.getCustomerEntityViewInfos(customerId, pageLink, | |
251 | + subType, ignoreErrors, ignoreLoading); | |
245 | 252 | } else { |
246 | - // TODO: | |
253 | + entitiesObservable = this.entityViewService.getTenantEntityViewInfos(pageLink, subType, ignoreErrors, ignoreLoading); | |
247 | 254 | } |
248 | 255 | break; |
249 | 256 | case EntityType.TENANT: | ... | ... |
... | ... | @@ -24,6 +24,8 @@ import {DeviceService} from '@core/http/device.service'; |
24 | 24 | import {EntityId} from '@shared/models/id/entity-id'; |
25 | 25 | import {EntityType} from '@shared/models/entity-type.models'; |
26 | 26 | import {forkJoin, Observable} from 'rxjs'; |
27 | +import {AssetService} from '@core/http/asset.service'; | |
28 | +import {EntityViewService} from '@core/http/entity-view.service'; | |
27 | 29 | |
28 | 30 | export interface AddEntitiesToCustomerDialogData { |
29 | 31 | customerId: string; |
... | ... | @@ -50,6 +52,8 @@ export class AddEntitiesToCustomerDialogComponent extends PageComponent implemen |
50 | 52 | constructor(protected store: Store<AppState>, |
51 | 53 | @Inject(MAT_DIALOG_DATA) public data: AddEntitiesToCustomerDialogData, |
52 | 54 | private deviceService: DeviceService, |
55 | + private assetService: AssetService, | |
56 | + private entityViewService: EntityViewService, | |
53 | 57 | @SkipSelf() private errorStateMatcher: ErrorStateMatcher, |
54 | 58 | public dialogRef: MatDialogRef<AddEntitiesToCustomerDialogComponent, boolean>, |
55 | 59 | public fb: FormBuilder) { |
... | ... | @@ -107,10 +111,10 @@ export class AddEntitiesToCustomerDialogComponent extends PageComponent implemen |
107 | 111 | return this.deviceService.assignDeviceToCustomer(customerId, entityId); |
108 | 112 | break; |
109 | 113 | case EntityType.ASSET: |
110 | - // TODO: | |
114 | + return this.assetService.assignAssetToCustomer(customerId, entityId); | |
111 | 115 | break; |
112 | 116 | case EntityType.ENTITY_VIEW: |
113 | - // TODO: | |
117 | + return this.entityViewService.assignEntityViewToCustomer(customerId, entityId); | |
114 | 118 | break; |
115 | 119 | } |
116 | 120 | } | ... | ... |
... | ... | @@ -24,6 +24,8 @@ import {DeviceService} from '@core/http/device.service'; |
24 | 24 | import {EntityId} from '@shared/models/id/entity-id'; |
25 | 25 | import {EntityType} from '@shared/models/entity-type.models'; |
26 | 26 | import {forkJoin, Observable} from 'rxjs'; |
27 | +import {AssetService} from '@core/http/asset.service'; | |
28 | +import {EntityViewService} from '@core/http/entity-view.service'; | |
27 | 29 | |
28 | 30 | export interface AssignToCustomerDialogData { |
29 | 31 | entityIds: Array<EntityId>; |
... | ... | @@ -50,6 +52,8 @@ export class AssignToCustomerDialogComponent extends PageComponent implements On |
50 | 52 | constructor(protected store: Store<AppState>, |
51 | 53 | @Inject(MAT_DIALOG_DATA) public data: AssignToCustomerDialogData, |
52 | 54 | private deviceService: DeviceService, |
55 | + private assetService: AssetService, | |
56 | + private entityViewService: EntityViewService, | |
53 | 57 | @SkipSelf() private errorStateMatcher: ErrorStateMatcher, |
54 | 58 | public dialogRef: MatDialogRef<AssignToCustomerDialogComponent, boolean>, |
55 | 59 | public fb: FormBuilder) { |
... | ... | @@ -106,10 +110,10 @@ export class AssignToCustomerDialogComponent extends PageComponent implements On |
106 | 110 | return this.deviceService.assignDeviceToCustomer(customerId, entityId); |
107 | 111 | break; |
108 | 112 | case EntityType.ASSET: |
109 | - // TODO: | |
113 | + return this.assetService.assignAssetToCustomer(customerId, entityId); | |
110 | 114 | break; |
111 | 115 | case EntityType.ENTITY_VIEW: |
112 | - // TODO: | |
116 | + return this.entityViewService.assignEntityViewToCustomer(customerId, entityId); | |
113 | 117 | break; |
114 | 118 | } |
115 | 119 | } | ... | ... |
... | ... | @@ -33,6 +33,8 @@ import {DeviceService} from '@core/http/device.service'; |
33 | 33 | import {EntitySubtype, EntityType} from '@app/shared/models/entity-type.models'; |
34 | 34 | import {BroadcastService} from '@app/core/services/broadcast.service'; |
35 | 35 | import {coerceBooleanProperty} from '@angular/cdk/coercion'; |
36 | +import {AssetService} from '@core/http/asset.service'; | |
37 | +import {EntityViewService} from '@core/http/entity-view.service'; | |
36 | 38 | |
37 | 39 | @Component({ |
38 | 40 | selector: 'tb-entity-subtype-autocomplete', |
... | ... | @@ -85,6 +87,8 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, |
85 | 87 | private broadcast: BroadcastService, |
86 | 88 | public translate: TranslateService, |
87 | 89 | private deviceService: DeviceService, |
90 | + private assetService: AssetService, | |
91 | + private entityViewService: EntityViewService, | |
88 | 92 | private fb: FormBuilder) { |
89 | 93 | this.subTypeFormGroup = this.fb.group({ |
90 | 94 | subType: [null] |
... | ... | @@ -203,13 +207,13 @@ export class EntitySubTypeAutocompleteComponent implements ControlValueAccessor, |
203 | 207 | if (!this.subTypes) { |
204 | 208 | switch (this.entityType) { |
205 | 209 | case EntityType.ASSET: |
206 | - // TODO: | |
210 | + this.subTypes = this.assetService.getAssetTypes(false, true); | |
207 | 211 | break; |
208 | 212 | case EntityType.DEVICE: |
209 | 213 | this.subTypes = this.deviceService.getDeviceTypes(false, true); |
210 | 214 | break; |
211 | 215 | case EntityType.ENTITY_VIEW: |
212 | - // TODO: | |
216 | + this.subTypes = this.entityViewService.getEntityViewTypes(false, true); | |
213 | 217 | break; |
214 | 218 | } |
215 | 219 | if (this.subTypes) { | ... | ... |
... | ... | @@ -32,6 +32,8 @@ import {TranslateService} from '@ngx-translate/core'; |
32 | 32 | import {DeviceService} from '@core/http/device.service'; |
33 | 33 | import {EntitySubtype, EntityType} from '@app/shared/models/entity-type.models'; |
34 | 34 | import {BroadcastService} from '@app/core/services/broadcast.service'; |
35 | +import {AssetService} from '@core/http/asset.service'; | |
36 | +import {EntityViewService} from '@core/http/entity-view.service'; | |
35 | 37 | |
36 | 38 | @Component({ |
37 | 39 | selector: 'tb-entity-subtype-select', |
... | ... | @@ -83,6 +85,8 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni |
83 | 85 | private broadcast: BroadcastService, |
84 | 86 | public translate: TranslateService, |
85 | 87 | private deviceService: DeviceService, |
88 | + private assetService: AssetService, | |
89 | + private entityViewService: EntityViewService, | |
86 | 90 | private fb: FormBuilder) { |
87 | 91 | this.subTypeFormGroup = this.fb.group({ |
88 | 92 | subType: [null] |
... | ... | @@ -202,13 +206,13 @@ export class EntitySubTypeSelectComponent implements ControlValueAccessor, OnIni |
202 | 206 | if (!this.subTypes) { |
203 | 207 | switch (this.entityType) { |
204 | 208 | case EntityType.ASSET: |
205 | - // TODO: | |
209 | + this.subTypes = this.assetService.getAssetTypes(false, true); | |
206 | 210 | break; |
207 | 211 | case EntityType.DEVICE: |
208 | 212 | this.subTypes = this.deviceService.getDeviceTypes(false, true); |
209 | 213 | break; |
210 | 214 | case EntityType.ENTITY_VIEW: |
211 | - // TODO: | |
215 | + this.subTypes = this.entityViewService.getEntityViewTypes(false, true); | |
212 | 216 | break; |
213 | 217 | } |
214 | 218 | if (this.subTypes) { | ... | ... |