2019-08-14 19:55:24 +03:00
|
|
|
///
|
2020-02-20 10:26:43 +02:00
|
|
|
/// Copyright © 2016-2020 The Thingsboard Authors
|
2019-08-14 19:55:24 +03:00
|
|
|
///
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
|
2019-09-10 15:12:10 +03:00
|
|
|
import { Injectable } from '@angular/core';
|
2020-02-04 15:14:17 +02:00
|
|
|
import { EMPTY, forkJoin, Observable, of, throwError } from 'rxjs';
|
2019-09-10 15:12:10 +03:00
|
|
|
import { HttpClient } from '@angular/common/http';
|
|
|
|
|
import { PageLink } from '@shared/models/page/page-link';
|
|
|
|
|
import { AliasEntityType, EntityType } from '@shared/models/entity-type.models';
|
|
|
|
|
import { BaseData } from '@shared/models/base-data';
|
|
|
|
|
import { EntityId } from '@shared/models/id/entity-id';
|
|
|
|
|
import { DeviceService } from '@core/http/device.service';
|
|
|
|
|
import { TenantService } from '@core/http/tenant.service';
|
|
|
|
|
import { CustomerService } from '@core/http/customer.service';
|
|
|
|
|
import { UserService } from './user.service';
|
|
|
|
|
import { DashboardService } from '@core/http/dashboard.service';
|
|
|
|
|
import { Direction } from '@shared/models/page/sort-order';
|
|
|
|
|
import { PageData } from '@shared/models/page/page-data';
|
2020-02-04 15:14:17 +02:00
|
|
|
import { getCurrentAuthUser } from '@core/auth/auth.selectors';
|
2019-09-10 15:12:10 +03:00
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { Authority } from '@shared/models/authority.enum';
|
|
|
|
|
import { Tenant } from '@shared/models/tenant.model';
|
2019-10-10 13:00:29 +03:00
|
|
|
import { catchError, concatMap, expand, map, mergeMap, toArray } from 'rxjs/operators';
|
2019-09-10 15:12:10 +03:00
|
|
|
import { Customer } from '@app/shared/models/customer.model';
|
|
|
|
|
import { AssetService } from '@core/http/asset.service';
|
|
|
|
|
import { EntityViewService } from '@core/http/entity-view.service';
|
2019-11-15 12:22:14 +02:00
|
|
|
import { AttributeScope, DataKeyType } from '@shared/models/telemetry/telemetry.models';
|
|
|
|
|
import { defaultHttpOptionsFromConfig, RequestConfig } from '@core/http/http-utils';
|
2019-09-10 15:12:10 +03:00
|
|
|
import { RuleChainService } from '@core/http/rule-chain.service';
|
2019-10-11 19:22:03 +03:00
|
|
|
import { AliasInfo, StateParams, SubscriptionInfo } from '@core/api/widget-api.models';
|
2019-09-10 15:12:10 +03:00
|
|
|
import { Datasource, DatasourceType, KeyInfo } from '@app/shared/models/widget.models';
|
|
|
|
|
import { UtilsService } from '@core/services/utils.service';
|
2019-10-11 19:22:03 +03:00
|
|
|
import { AliasFilterType, EntityAlias, EntityAliasFilter, EntityAliasFilterResult } from '@shared/models/alias.models';
|
2020-02-21 19:04:49 +02:00
|
|
|
import { entityFields, EntityInfo, ImportEntitiesResultInfo, ImportEntityData } from '@shared/models/entity.models';
|
2019-10-10 13:00:29 +03:00
|
|
|
import {
|
|
|
|
|
EntityRelationInfo,
|
|
|
|
|
EntityRelationsQuery,
|
|
|
|
|
EntitySearchDirection,
|
|
|
|
|
EntitySearchQuery
|
|
|
|
|
} from '@shared/models/relation.models';
|
|
|
|
|
import { EntityRelationService } from '@core/http/entity-relation.service';
|
2020-02-04 15:14:17 +02:00
|
|
|
import { isDefined } from '@core/utils';
|
2019-11-15 12:22:14 +02:00
|
|
|
import { Asset, AssetSearchQuery } from '@shared/models/asset.models';
|
|
|
|
|
import { Device, DeviceCredentialsType, DeviceSearchQuery } from '@shared/models/device.models';
|
2019-10-10 13:00:29 +03:00
|
|
|
import { EntityViewSearchQuery } from '@shared/models/entity-view.models';
|
2019-11-15 12:22:14 +02:00
|
|
|
import { AttributeService } from '@core/http/attribute.service';
|
2019-08-14 19:55:24 +03:00
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class EntityService {
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
private http: HttpClient,
|
|
|
|
|
private store: Store<AppState>,
|
|
|
|
|
private deviceService: DeviceService,
|
2019-08-16 20:09:56 +03:00
|
|
|
private assetService: AssetService,
|
|
|
|
|
private entityViewService: EntityViewService,
|
2019-08-14 19:55:24 +03:00
|
|
|
private tenantService: TenantService,
|
|
|
|
|
private customerService: CustomerService,
|
|
|
|
|
private userService: UserService,
|
2019-08-20 20:42:48 +03:00
|
|
|
private ruleChainService: RuleChainService,
|
2019-09-10 15:12:10 +03:00
|
|
|
private dashboardService: DashboardService,
|
2019-10-10 13:00:29 +03:00
|
|
|
private entityRelationService: EntityRelationService,
|
2019-11-15 12:22:14 +02:00
|
|
|
private attributeService: AttributeService,
|
2019-09-10 15:12:10 +03:00
|
|
|
private utils: UtilsService
|
2019-08-14 19:55:24 +03:00
|
|
|
) { }
|
|
|
|
|
|
|
|
|
|
private getEntityObservable(entityType: EntityType, entityId: string,
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<BaseData<EntityId>> {
|
2019-08-14 19:55:24 +03:00
|
|
|
|
|
|
|
|
let observable: Observable<BaseData<EntityId>>;
|
|
|
|
|
switch (entityType) {
|
|
|
|
|
case EntityType.DEVICE:
|
2019-11-15 12:22:14 +02:00
|
|
|
observable = this.deviceService.getDevice(entityId, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.ASSET:
|
2019-11-15 12:22:14 +02:00
|
|
|
observable = this.assetService.getAsset(entityId, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.ENTITY_VIEW:
|
2019-11-15 12:22:14 +02:00
|
|
|
observable = this.entityViewService.getEntityView(entityId, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.TENANT:
|
2019-11-15 12:22:14 +02:00
|
|
|
observable = this.tenantService.getTenant(entityId, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.CUSTOMER:
|
2019-11-15 12:22:14 +02:00
|
|
|
observable = this.customerService.getCustomer(entityId, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.DASHBOARD:
|
2019-11-15 12:22:14 +02:00
|
|
|
observable = this.dashboardService.getDashboardInfo(entityId, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.USER:
|
2019-11-15 12:22:14 +02:00
|
|
|
observable = this.userService.getUser(entityId, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.RULE_CHAIN:
|
2019-11-15 12:22:14 +02:00
|
|
|
observable = this.ruleChainService.getRuleChain(entityId, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.ALARM:
|
|
|
|
|
console.error('Get Alarm Entity is not implemented!');
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return observable;
|
|
|
|
|
}
|
|
|
|
|
public getEntity(entityType: EntityType, entityId: string,
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<BaseData<EntityId>> {
|
|
|
|
|
const entityObservable = this.getEntityObservable(entityType, entityId, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
if (entityObservable) {
|
|
|
|
|
return entityObservable;
|
|
|
|
|
} else {
|
|
|
|
|
return throwError(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 20:39:56 +03:00
|
|
|
private getEntitiesByIdsObservable(fetchEntityFunction: (entityId: string) => Observable<BaseData<EntityId>>,
|
|
|
|
|
entityIds: Array<string>): Observable<Array<BaseData<EntityId>>> {
|
|
|
|
|
const tasks: Observable<BaseData<EntityId>>[] = [];
|
|
|
|
|
entityIds.forEach((entityId) => {
|
|
|
|
|
tasks.push(fetchEntityFunction(entityId));
|
|
|
|
|
});
|
|
|
|
|
return forkJoin(tasks).pipe(
|
|
|
|
|
map((entities) => {
|
|
|
|
|
if (entities) {
|
|
|
|
|
entities.sort((entity1, entity2) => {
|
|
|
|
|
const index1 = entityIds.indexOf(entity1.id.id);
|
|
|
|
|
const index2 = entityIds.indexOf(entity2.id.id);
|
|
|
|
|
return index1 - index2;
|
|
|
|
|
});
|
|
|
|
|
return entities;
|
|
|
|
|
} else {
|
|
|
|
|
return [];
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private getEntitiesObservable(entityType: EntityType, entityIds: Array<string>,
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<Array<BaseData<EntityId>>> {
|
2019-08-15 20:39:56 +03:00
|
|
|
let observable: Observable<Array<BaseData<EntityId>>>;
|
|
|
|
|
switch (entityType) {
|
|
|
|
|
case EntityType.DEVICE:
|
2019-11-15 12:22:14 +02:00
|
|
|
observable = this.deviceService.getDevices(entityIds, config);
|
2019-08-15 20:39:56 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.ASSET:
|
2019-11-15 12:22:14 +02:00
|
|
|
observable = this.assetService.getAssets(entityIds, config);
|
2019-08-15 20:39:56 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.ENTITY_VIEW:
|
2019-08-16 20:09:56 +03:00
|
|
|
observable = this.getEntitiesByIdsObservable(
|
2019-11-15 12:22:14 +02:00
|
|
|
(id) => this.entityViewService.getEntityView(id, config),
|
2019-08-16 20:09:56 +03:00
|
|
|
entityIds);
|
2019-08-15 20:39:56 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.TENANT:
|
|
|
|
|
observable = this.getEntitiesByIdsObservable(
|
2019-11-15 12:22:14 +02:00
|
|
|
(id) => this.tenantService.getTenant(id, config),
|
2019-08-15 20:39:56 +03:00
|
|
|
entityIds);
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.CUSTOMER:
|
|
|
|
|
observable = this.getEntitiesByIdsObservable(
|
2019-11-15 12:22:14 +02:00
|
|
|
(id) => this.customerService.getCustomer(id, config),
|
2019-08-15 20:39:56 +03:00
|
|
|
entityIds);
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.DASHBOARD:
|
|
|
|
|
observable = this.getEntitiesByIdsObservable(
|
2019-11-15 12:22:14 +02:00
|
|
|
(id) => this.dashboardService.getDashboardInfo(id, config),
|
2019-08-15 20:39:56 +03:00
|
|
|
entityIds);
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.USER:
|
|
|
|
|
observable = this.getEntitiesByIdsObservable(
|
2019-11-15 12:22:14 +02:00
|
|
|
(id) => this.userService.getUser(id, config),
|
2019-08-15 20:39:56 +03:00
|
|
|
entityIds);
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.ALARM:
|
|
|
|
|
console.error('Get Alarm Entity is not implemented!');
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return observable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getEntities(entityType: EntityType, entityIds: Array<string>,
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<Array<BaseData<EntityId>>> {
|
|
|
|
|
const entitiesObservable = this.getEntitiesObservable(entityType, entityIds, config);
|
2019-08-15 20:39:56 +03:00
|
|
|
if (entitiesObservable) {
|
|
|
|
|
return entitiesObservable;
|
|
|
|
|
} else {
|
|
|
|
|
return throwError(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-14 19:55:24 +03:00
|
|
|
private getSingleTenantByPageLinkObservable(pageLink: PageLink,
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<PageData<Tenant>> {
|
2019-08-14 19:55:24 +03:00
|
|
|
const authUser = getCurrentAuthUser(this.store);
|
|
|
|
|
const tenantId = authUser.tenantId;
|
2019-11-15 12:22:14 +02:00
|
|
|
return this.tenantService.getTenant(tenantId, config).pipe(
|
2019-08-14 19:55:24 +03:00
|
|
|
map((tenant) => {
|
|
|
|
|
const result = {
|
|
|
|
|
data: [],
|
|
|
|
|
totalPages: 0,
|
|
|
|
|
totalElements: 0,
|
|
|
|
|
hasNext: false
|
|
|
|
|
} as PageData<Tenant>;
|
|
|
|
|
if (tenant.title.toLowerCase().startsWith(pageLink.textSearch.toLowerCase())) {
|
|
|
|
|
result.data.push(tenant);
|
|
|
|
|
result.totalPages = 1;
|
|
|
|
|
result.totalElements = 1;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getSingleCustomerByPageLinkObservable(pageLink: PageLink,
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<PageData<Customer>> {
|
2019-08-14 19:55:24 +03:00
|
|
|
const authUser = getCurrentAuthUser(this.store);
|
|
|
|
|
const customerId = authUser.customerId;
|
2019-11-15 12:22:14 +02:00
|
|
|
return this.customerService.getCustomer(customerId, config).pipe(
|
2019-08-14 19:55:24 +03:00
|
|
|
map((customer) => {
|
|
|
|
|
const result = {
|
|
|
|
|
data: [],
|
|
|
|
|
totalPages: 0,
|
|
|
|
|
totalElements: 0,
|
|
|
|
|
hasNext: false
|
|
|
|
|
} as PageData<Customer>;
|
|
|
|
|
if (customer.title.toLowerCase().startsWith(pageLink.textSearch.toLowerCase())) {
|
|
|
|
|
result.data.push(customer);
|
|
|
|
|
result.totalPages = 1;
|
|
|
|
|
result.totalElements = 1;
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getEntitiesByPageLinkObservable(entityType: EntityType, pageLink: PageLink, subType: string = '',
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<PageData<BaseData<EntityId>>> {
|
2019-08-14 19:55:24 +03:00
|
|
|
let entitiesObservable: Observable<PageData<BaseData<EntityId>>>;
|
|
|
|
|
const authUser = getCurrentAuthUser(this.store);
|
|
|
|
|
const customerId = authUser.customerId;
|
|
|
|
|
switch (entityType) {
|
|
|
|
|
case EntityType.DEVICE:
|
|
|
|
|
pageLink.sortOrder.property = 'name';
|
|
|
|
|
if (authUser.authority === Authority.CUSTOMER_USER) {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.deviceService.getCustomerDeviceInfos(customerId, pageLink, subType, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
} else {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.deviceService.getTenantDeviceInfos(pageLink, subType, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.ASSET:
|
|
|
|
|
pageLink.sortOrder.property = 'name';
|
|
|
|
|
if (authUser.authority === Authority.CUSTOMER_USER) {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.assetService.getCustomerAssetInfos(customerId, pageLink, subType, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
} else {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.assetService.getTenantAssetInfos(pageLink, subType, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.ENTITY_VIEW:
|
|
|
|
|
pageLink.sortOrder.property = 'name';
|
|
|
|
|
if (authUser.authority === Authority.CUSTOMER_USER) {
|
2019-08-16 20:09:56 +03:00
|
|
|
entitiesObservable = this.entityViewService.getCustomerEntityViewInfos(customerId, pageLink,
|
2019-11-15 12:22:14 +02:00
|
|
|
subType, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
} else {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.entityViewService.getTenantEntityViewInfos(pageLink, subType, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.TENANT:
|
|
|
|
|
pageLink.sortOrder.property = 'title';
|
|
|
|
|
if (authUser.authority === Authority.TENANT_ADMIN) {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.getSingleTenantByPageLinkObservable(pageLink, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
} else {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.tenantService.getTenants(pageLink, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.CUSTOMER:
|
|
|
|
|
pageLink.sortOrder.property = 'title';
|
|
|
|
|
if (authUser.authority === Authority.CUSTOMER_USER) {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.getSingleCustomerByPageLinkObservable(pageLink, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
} else {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.customerService.getCustomers(pageLink, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.RULE_CHAIN:
|
|
|
|
|
pageLink.sortOrder.property = 'name';
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.ruleChainService.getRuleChains(pageLink, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
break;
|
|
|
|
|
case EntityType.DASHBOARD:
|
|
|
|
|
pageLink.sortOrder.property = 'title';
|
|
|
|
|
if (authUser.authority === Authority.CUSTOMER_USER) {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.dashboardService.getCustomerDashboards(customerId, pageLink, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
} else {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.dashboardService.getTenantDashboards(pageLink, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.USER:
|
|
|
|
|
console.error('Get User Entities is not implemented!');
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.ALARM:
|
|
|
|
|
console.error('Get Alarm Entities is not implemented!');
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return entitiesObservable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private getEntitiesByPageLink(entityType: EntityType, pageLink: PageLink, subType: string = '',
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<Array<BaseData<EntityId>>> {
|
2019-08-14 19:55:24 +03:00
|
|
|
const entitiesObservable: Observable<PageData<BaseData<EntityId>>> =
|
2019-11-15 12:22:14 +02:00
|
|
|
this.getEntitiesByPageLinkObservable(entityType, pageLink, subType, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
if (entitiesObservable) {
|
|
|
|
|
return entitiesObservable.pipe(
|
|
|
|
|
expand((data) => {
|
|
|
|
|
if (data.hasNext) {
|
|
|
|
|
pageLink.page += 1;
|
2019-11-15 12:22:14 +02:00
|
|
|
return this.getEntitiesByPageLinkObservable(entityType, pageLink, subType, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
} else {
|
|
|
|
|
return EMPTY;
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
map((data) => data.data),
|
|
|
|
|
concatMap((data) => data),
|
|
|
|
|
toArray()
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return of(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public getEntitiesByNameFilter(entityType: EntityType, entityNameFilter: string,
|
|
|
|
|
pageSize: number, subType: string = '',
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<Array<BaseData<EntityId>>> {
|
2019-08-14 19:55:24 +03:00
|
|
|
const pageLink = new PageLink(pageSize, 0, entityNameFilter, {
|
|
|
|
|
property: 'name',
|
|
|
|
|
direction: Direction.ASC
|
|
|
|
|
});
|
|
|
|
|
if (pageSize === -1) { // all
|
|
|
|
|
pageLink.pageSize = 100;
|
2019-11-15 12:22:14 +02:00
|
|
|
return this.getEntitiesByPageLink(entityType, pageLink, subType, config).pipe(
|
2019-08-14 19:55:24 +03:00
|
|
|
map((data) => data && data.length ? data : null)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
const entitiesObservable: Observable<PageData<BaseData<EntityId>>> =
|
2019-11-15 12:22:14 +02:00
|
|
|
this.getEntitiesByPageLinkObservable(entityType, pageLink, subType, config);
|
2019-08-14 19:55:24 +03:00
|
|
|
if (entitiesObservable) {
|
|
|
|
|
return entitiesObservable.pipe(
|
|
|
|
|
map((data) => data && data.data.length ? data.data : null)
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return of(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-08-19 20:09:41 +03:00
|
|
|
|
2019-10-10 13:00:29 +03:00
|
|
|
public getAliasFilterTypesByEntityTypes(entityTypes: Array<EntityType | AliasEntityType>): Array<AliasFilterType> {
|
|
|
|
|
const allAliasFilterTypes: Array<AliasFilterType> = Object.keys(AliasFilterType).map((key) => AliasFilterType[key]);
|
|
|
|
|
if (!entityTypes || !entityTypes.length) {
|
|
|
|
|
return allAliasFilterTypes;
|
|
|
|
|
}
|
|
|
|
|
const result = [];
|
|
|
|
|
for (const aliasFilterType of allAliasFilterTypes) {
|
|
|
|
|
if (this.filterAliasFilterTypeByEntityTypes(aliasFilterType, entityTypes)) {
|
|
|
|
|
result.push(aliasFilterType);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 19:22:03 +03:00
|
|
|
public filterAliasByEntityTypes(entityAlias: EntityAlias, entityTypes: Array<EntityType | AliasEntityType>): boolean {
|
|
|
|
|
const filter = entityAlias.filter;
|
|
|
|
|
if (this.filterAliasFilterTypeByEntityTypes(filter.type, entityTypes)) {
|
|
|
|
|
switch (filter.type) {
|
|
|
|
|
case AliasFilterType.singleEntity:
|
|
|
|
|
return entityTypes.indexOf(filter.singleEntity.entityType) > -1 ? true : false;
|
|
|
|
|
case AliasFilterType.entityList:
|
|
|
|
|
return entityTypes.indexOf(filter.entityType) > -1 ? true : false;
|
|
|
|
|
case AliasFilterType.entityName:
|
|
|
|
|
return entityTypes.indexOf(filter.entityType) > -1 ? true : false;
|
|
|
|
|
case AliasFilterType.stateEntity:
|
|
|
|
|
return true;
|
|
|
|
|
case AliasFilterType.assetType:
|
|
|
|
|
return entityTypes.indexOf(EntityType.ASSET) > -1 ? true : false;
|
|
|
|
|
case AliasFilterType.deviceType:
|
|
|
|
|
return entityTypes.indexOf(EntityType.DEVICE) > -1 ? true : false;
|
|
|
|
|
case AliasFilterType.entityViewType:
|
|
|
|
|
return entityTypes.indexOf(EntityType.ENTITY_VIEW) > -1 ? true : false;
|
|
|
|
|
case AliasFilterType.relationsQuery:
|
|
|
|
|
if (filter.filters && filter.filters.length) {
|
|
|
|
|
let match = false;
|
|
|
|
|
for (const relationFilter of filter.filters) {
|
|
|
|
|
if (relationFilter.entityTypes && relationFilter.entityTypes.length) {
|
|
|
|
|
for (const relationFilterEntityType of relationFilter.entityTypes) {
|
|
|
|
|
if (entityTypes.indexOf(relationFilterEntityType) > -1) {
|
|
|
|
|
match = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
match = true;
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return match;
|
|
|
|
|
} else {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
case AliasFilterType.assetSearchQuery:
|
|
|
|
|
return entityTypes.indexOf(EntityType.ASSET) > -1 ? true : false;
|
|
|
|
|
case AliasFilterType.deviceSearchQuery:
|
|
|
|
|
return entityTypes.indexOf(EntityType.DEVICE) > -1 ? true : false;
|
|
|
|
|
case AliasFilterType.entityViewSearchQuery:
|
|
|
|
|
return entityTypes.indexOf(EntityType.ENTITY_VIEW) > -1 ? true : false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 13:00:29 +03:00
|
|
|
private filterAliasFilterTypeByEntityTypes(aliasFilterType: AliasFilterType,
|
|
|
|
|
entityTypes: Array<EntityType | AliasEntityType>): boolean {
|
|
|
|
|
if (!entityTypes || !entityTypes.length) {
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
let valid = false;
|
|
|
|
|
entityTypes.forEach((entityType) => {
|
|
|
|
|
valid = valid || this.filterAliasFilterTypeByEntityType(aliasFilterType, entityType);
|
|
|
|
|
});
|
|
|
|
|
return valid;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private filterAliasFilterTypeByEntityType(aliasFilterType: AliasFilterType, entityType: EntityType | AliasEntityType): boolean {
|
|
|
|
|
switch (aliasFilterType) {
|
|
|
|
|
case AliasFilterType.singleEntity:
|
|
|
|
|
return true;
|
|
|
|
|
case AliasFilterType.entityList:
|
|
|
|
|
return true;
|
|
|
|
|
case AliasFilterType.entityName:
|
|
|
|
|
return true;
|
|
|
|
|
case AliasFilterType.stateEntity:
|
|
|
|
|
return true;
|
|
|
|
|
case AliasFilterType.assetType:
|
|
|
|
|
return entityType === EntityType.ASSET;
|
|
|
|
|
case AliasFilterType.deviceType:
|
|
|
|
|
return entityType === EntityType.DEVICE;
|
|
|
|
|
case AliasFilterType.entityViewType:
|
|
|
|
|
return entityType === EntityType.ENTITY_VIEW;
|
|
|
|
|
case AliasFilterType.relationsQuery:
|
|
|
|
|
return true;
|
|
|
|
|
case AliasFilterType.assetSearchQuery:
|
|
|
|
|
return entityType === EntityType.ASSET;
|
|
|
|
|
case AliasFilterType.deviceSearchQuery:
|
|
|
|
|
return entityType === EntityType.DEVICE;
|
|
|
|
|
case AliasFilterType.entityViewSearchQuery:
|
|
|
|
|
return entityType === EntityType.ENTITY_VIEW;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-19 20:09:41 +03:00
|
|
|
public prepareAllowedEntityTypesList(allowedEntityTypes: Array<EntityType | AliasEntityType>,
|
2019-10-10 13:00:29 +03:00
|
|
|
useAliasEntityTypes?: boolean): Array<EntityType | AliasEntityType> {
|
2019-08-19 20:09:41 +03:00
|
|
|
const authUser = getCurrentAuthUser(this.store);
|
|
|
|
|
const entityTypes: Array<EntityType | AliasEntityType> = [];
|
|
|
|
|
switch (authUser.authority) {
|
|
|
|
|
case Authority.SYS_ADMIN:
|
|
|
|
|
entityTypes.push(EntityType.TENANT);
|
|
|
|
|
break;
|
|
|
|
|
case Authority.TENANT_ADMIN:
|
|
|
|
|
entityTypes.push(EntityType.DEVICE);
|
|
|
|
|
entityTypes.push(EntityType.ASSET);
|
|
|
|
|
entityTypes.push(EntityType.ENTITY_VIEW);
|
|
|
|
|
entityTypes.push(EntityType.TENANT);
|
|
|
|
|
entityTypes.push(EntityType.CUSTOMER);
|
|
|
|
|
entityTypes.push(EntityType.DASHBOARD);
|
|
|
|
|
if (useAliasEntityTypes) {
|
|
|
|
|
entityTypes.push(AliasEntityType.CURRENT_CUSTOMER);
|
2020-04-10 15:04:12 +03:00
|
|
|
entityTypes.push(AliasEntityType.CURRENT_TENANT);
|
2019-08-19 20:09:41 +03:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case Authority.CUSTOMER_USER:
|
|
|
|
|
entityTypes.push(EntityType.DEVICE);
|
|
|
|
|
entityTypes.push(EntityType.ASSET);
|
|
|
|
|
entityTypes.push(EntityType.ENTITY_VIEW);
|
|
|
|
|
entityTypes.push(EntityType.CUSTOMER);
|
|
|
|
|
entityTypes.push(EntityType.DASHBOARD);
|
|
|
|
|
if (useAliasEntityTypes) {
|
|
|
|
|
entityTypes.push(AliasEntityType.CURRENT_CUSTOMER);
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
if (allowedEntityTypes && allowedEntityTypes.length) {
|
|
|
|
|
for (let index = entityTypes.length - 1; index >= 0; index--) {
|
|
|
|
|
if (allowedEntityTypes.indexOf(entityTypes[index]) === -1) {
|
|
|
|
|
entityTypes.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return entityTypes;
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 19:04:49 +02:00
|
|
|
private getEntityFieldKeys (entityType: EntityType, searchText: string): Array<string> {
|
|
|
|
|
const entityFieldKeys: string[] = [];
|
|
|
|
|
const query = searchText.toLowerCase();
|
|
|
|
|
switch(entityType) {
|
|
|
|
|
case EntityType.USER:
|
|
|
|
|
entityFieldKeys.push(entityFields.name.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.email.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.firstName.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.lastName.keyName);
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.TENANT:
|
|
|
|
|
case EntityType.CUSTOMER:
|
|
|
|
|
entityFieldKeys.push(entityFields.title.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.email.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.country.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.state.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.city.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.address.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.address2.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.zip.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.phone.keyName);
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.ENTITY_VIEW:
|
|
|
|
|
entityFieldKeys.push(entityFields.name.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.type.keyName);
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.DEVICE:
|
|
|
|
|
case EntityType.ASSET:
|
|
|
|
|
entityFieldKeys.push(entityFields.name.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.type.keyName);
|
|
|
|
|
entityFieldKeys.push(entityFields.label.keyName);
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.DASHBOARD:
|
|
|
|
|
entityFieldKeys.push(entityFields.title.keyName);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return query ? entityFieldKeys.filter((entityField) => entityField.toLowerCase().indexOf(query) === 0) : entityFieldKeys;
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-19 20:09:41 +03:00
|
|
|
public getEntityKeys(entityId: EntityId, query: string, type: DataKeyType,
|
2019-11-15 12:22:14 +02:00
|
|
|
config?: RequestConfig): Observable<Array<string>> {
|
2020-02-21 19:04:49 +02:00
|
|
|
if (type === DataKeyType.entityField) {
|
|
|
|
|
return of(this.getEntityFieldKeys(entityId.entityType as EntityType, query));
|
|
|
|
|
}
|
2019-08-19 20:09:41 +03:00
|
|
|
let url = `/api/plugins/telemetry/${entityId.entityType}/${entityId.id}/keys/`;
|
|
|
|
|
if (type === DataKeyType.timeseries) {
|
|
|
|
|
url += 'timeseries';
|
|
|
|
|
} else if (type === DataKeyType.attribute) {
|
|
|
|
|
url += 'attributes';
|
|
|
|
|
}
|
|
|
|
|
return this.http.get<Array<string>>(url,
|
2019-11-15 12:22:14 +02:00
|
|
|
defaultHttpOptionsFromConfig(config))
|
2019-08-19 20:09:41 +03:00
|
|
|
.pipe(
|
|
|
|
|
map(
|
|
|
|
|
(dataKeys) => {
|
|
|
|
|
if (query) {
|
|
|
|
|
const lowercaseQuery = query.toLowerCase();
|
|
|
|
|
return dataKeys.filter((dataKey) => dataKey.toLowerCase().indexOf(lowercaseQuery) === 0);
|
|
|
|
|
} else {
|
|
|
|
|
return dataKeys;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
}
|
2019-09-10 15:12:10 +03:00
|
|
|
|
|
|
|
|
public createDatasourcesFromSubscriptionsInfo(subscriptionsInfo: Array<SubscriptionInfo>): Observable<Array<Datasource>> {
|
|
|
|
|
const observables = new Array<Observable<Array<Datasource>>>();
|
|
|
|
|
subscriptionsInfo.forEach((subscriptionInfo) => {
|
|
|
|
|
observables.push(this.createDatasourcesFromSubscriptionInfo(subscriptionInfo));
|
|
|
|
|
});
|
|
|
|
|
return forkJoin(observables).pipe(
|
|
|
|
|
map((arrayOfDatasources) => {
|
|
|
|
|
const result = new Array<Datasource>();
|
|
|
|
|
arrayOfDatasources.forEach((datasources) => {
|
|
|
|
|
result.push(...datasources);
|
|
|
|
|
});
|
|
|
|
|
this.utils.generateColors(result);
|
|
|
|
|
return result;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public createAlarmSourceFromSubscriptionInfo(subscriptionInfo: SubscriptionInfo): Observable<Datasource> {
|
|
|
|
|
if (subscriptionInfo.entityId && subscriptionInfo.entityType) {
|
|
|
|
|
return this.getEntity(subscriptionInfo.entityType, subscriptionInfo.entityId,
|
2019-11-15 12:22:14 +02:00
|
|
|
{ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-09-10 15:12:10 +03:00
|
|
|
map((entity) => {
|
|
|
|
|
const alarmSource = this.createDatasourceFromSubscription(subscriptionInfo, entity);
|
|
|
|
|
this.utils.generateColors([alarmSource]);
|
|
|
|
|
return alarmSource;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return throwError(null);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-11 19:22:03 +03:00
|
|
|
public resolveAlias(entityAlias: EntityAlias, stateParams: StateParams): Observable<AliasInfo> {
|
|
|
|
|
const filter = entityAlias.filter;
|
|
|
|
|
return this.resolveAliasFilter(filter, stateParams, -1, false).pipe(
|
|
|
|
|
map((result) => {
|
|
|
|
|
const aliasInfo: AliasInfo = {
|
|
|
|
|
alias: entityAlias.alias,
|
|
|
|
|
stateEntity: result.stateEntity,
|
|
|
|
|
entityParamName: result.entityParamName,
|
|
|
|
|
resolveMultiple: filter.resolveMultiple
|
|
|
|
|
};
|
|
|
|
|
aliasInfo.resolvedEntities = result.entities;
|
|
|
|
|
aliasInfo.currentEntity = null;
|
|
|
|
|
if (aliasInfo.resolvedEntities.length) {
|
|
|
|
|
aliasInfo.currentEntity = aliasInfo.resolvedEntities[0];
|
|
|
|
|
}
|
|
|
|
|
return aliasInfo;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 13:00:29 +03:00
|
|
|
public resolveAliasFilter(filter: EntityAliasFilter, stateParams: StateParams,
|
|
|
|
|
maxItems: number, failOnEmpty: boolean): Observable<EntityAliasFilterResult> {
|
|
|
|
|
const result: EntityAliasFilterResult = {
|
|
|
|
|
entities: [],
|
|
|
|
|
stateEntity: false
|
|
|
|
|
};
|
|
|
|
|
if (filter.stateEntityParamName && filter.stateEntityParamName.length) {
|
|
|
|
|
result.entityParamName = filter.stateEntityParamName;
|
|
|
|
|
}
|
2020-02-21 19:04:49 +02:00
|
|
|
const stateEntityInfo = this.getStateEntityInfo(filter, stateParams);
|
|
|
|
|
const stateEntityId = stateEntityInfo.entityId;
|
2019-10-10 13:00:29 +03:00
|
|
|
switch (filter.type) {
|
|
|
|
|
case AliasFilterType.singleEntity:
|
|
|
|
|
const aliasEntityId = this.resolveAliasEntityId(filter.singleEntity.entityType, filter.singleEntity.id);
|
2019-11-15 12:22:14 +02:00
|
|
|
return this.getEntity(aliasEntityId.entityType as EntityType, aliasEntityId.id, {ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-10-10 13:00:29 +03:00
|
|
|
map((entity) => {
|
|
|
|
|
result.entities = this.entitiesToEntitiesInfo([entity]);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
case AliasFilterType.entityList:
|
2019-11-15 12:22:14 +02:00
|
|
|
return this.getEntities(filter.entityType, filter.entityList, {ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-10-10 13:00:29 +03:00
|
|
|
map((entities) => {
|
|
|
|
|
if (entities && entities.length || !failOnEmpty) {
|
|
|
|
|
result.entities = this.entitiesToEntitiesInfo(entities);
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
case AliasFilterType.entityName:
|
|
|
|
|
return this.getEntitiesByNameFilter(filter.entityType, filter.entityNameFilter, maxItems,
|
2019-11-15 12:22:14 +02:00
|
|
|
'', {ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-10-10 13:00:29 +03:00
|
|
|
map((entities) => {
|
|
|
|
|
if (entities && entities.length || !failOnEmpty) {
|
|
|
|
|
result.entities = this.entitiesToEntitiesInfo(entities);
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
case AliasFilterType.stateEntity:
|
|
|
|
|
result.stateEntity = true;
|
|
|
|
|
if (stateEntityId) {
|
2019-11-15 12:22:14 +02:00
|
|
|
return this.getEntity(stateEntityId.entityType as EntityType, stateEntityId.id, {ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-10-10 13:00:29 +03:00
|
|
|
map((entity) => {
|
|
|
|
|
result.entities = this.entitiesToEntitiesInfo([entity]);
|
|
|
|
|
return result;
|
|
|
|
|
}
|
|
|
|
|
));
|
|
|
|
|
} else {
|
|
|
|
|
return of(result);
|
|
|
|
|
}
|
|
|
|
|
case AliasFilterType.assetType:
|
|
|
|
|
return this.getEntitiesByNameFilter(EntityType.ASSET, filter.assetNameFilter, maxItems,
|
2019-11-15 12:22:14 +02:00
|
|
|
filter.assetType, {ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-10-10 13:00:29 +03:00
|
|
|
map((entities) => {
|
|
|
|
|
if (entities && entities.length || !failOnEmpty) {
|
|
|
|
|
result.entities = this.entitiesToEntitiesInfo(entities);
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
case AliasFilterType.deviceType:
|
|
|
|
|
return this.getEntitiesByNameFilter(EntityType.DEVICE, filter.deviceNameFilter, maxItems,
|
2019-11-15 12:22:14 +02:00
|
|
|
filter.deviceType, {ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-10-10 13:00:29 +03:00
|
|
|
map((entities) => {
|
|
|
|
|
if (entities && entities.length || !failOnEmpty) {
|
|
|
|
|
result.entities = this.entitiesToEntitiesInfo(entities);
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
case AliasFilterType.entityViewType:
|
|
|
|
|
return this.getEntitiesByNameFilter(EntityType.ENTITY_VIEW, filter.entityViewNameFilter, maxItems,
|
2019-11-15 12:22:14 +02:00
|
|
|
filter.entityViewType, {ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-10-10 13:00:29 +03:00
|
|
|
map((entities) => {
|
|
|
|
|
if (entities && entities.length || !failOnEmpty) {
|
|
|
|
|
result.entities = this.entitiesToEntitiesInfo(entities);
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
case AliasFilterType.relationsQuery:
|
|
|
|
|
result.stateEntity = filter.rootStateEntity;
|
|
|
|
|
let rootEntityType;
|
|
|
|
|
let rootEntityId;
|
|
|
|
|
if (result.stateEntity && stateEntityId) {
|
|
|
|
|
rootEntityType = stateEntityId.entityType;
|
|
|
|
|
rootEntityId = stateEntityId.id;
|
|
|
|
|
} else if (!result.stateEntity) {
|
|
|
|
|
rootEntityType = filter.rootEntity.entityType;
|
|
|
|
|
rootEntityId = filter.rootEntity.id;
|
|
|
|
|
}
|
|
|
|
|
if (rootEntityType && rootEntityId) {
|
|
|
|
|
const relationQueryRootEntityId = this.resolveAliasEntityId(rootEntityType, rootEntityId);
|
|
|
|
|
const searchQuery: EntityRelationsQuery = {
|
|
|
|
|
parameters: {
|
|
|
|
|
rootId: relationQueryRootEntityId.id,
|
|
|
|
|
rootType: relationQueryRootEntityId.entityType as EntityType,
|
2020-02-21 19:04:49 +02:00
|
|
|
direction: filter.direction,
|
|
|
|
|
fetchLastLevelOnly: filter.fetchLastLevelOnly
|
2019-10-10 13:00:29 +03:00
|
|
|
},
|
|
|
|
|
filters: filter.filters
|
|
|
|
|
};
|
|
|
|
|
searchQuery.parameters.maxLevel = filter.maxLevel && filter.maxLevel > 0 ? filter.maxLevel : -1;
|
2019-11-15 12:22:14 +02:00
|
|
|
return this.entityRelationService.findInfoByQuery(searchQuery, {ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-10-10 13:00:29 +03:00
|
|
|
mergeMap((allRelations) => {
|
|
|
|
|
if (allRelations && allRelations.length || !failOnEmpty) {
|
|
|
|
|
if (isDefined(maxItems) && maxItems > 0 && allRelations) {
|
|
|
|
|
const limit = Math.min(allRelations.length, maxItems);
|
|
|
|
|
allRelations.length = limit;
|
|
|
|
|
}
|
|
|
|
|
return this.entityRelationInfosToEntitiesInfo(allRelations, filter.direction).pipe(
|
|
|
|
|
map((entities) => {
|
|
|
|
|
result.entities = entities;
|
|
|
|
|
return result;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return throwError(null);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return of(result);
|
|
|
|
|
}
|
|
|
|
|
case AliasFilterType.assetSearchQuery:
|
|
|
|
|
case AliasFilterType.deviceSearchQuery:
|
|
|
|
|
case AliasFilterType.entityViewSearchQuery:
|
|
|
|
|
result.stateEntity = filter.rootStateEntity;
|
|
|
|
|
if (result.stateEntity && stateEntityId) {
|
|
|
|
|
rootEntityType = stateEntityId.entityType;
|
|
|
|
|
rootEntityId = stateEntityId.id;
|
|
|
|
|
} else if (!result.stateEntity) {
|
|
|
|
|
rootEntityType = filter.rootEntity.entityType;
|
|
|
|
|
rootEntityId = filter.rootEntity.id;
|
|
|
|
|
}
|
|
|
|
|
if (rootEntityType && rootEntityId) {
|
|
|
|
|
const searchQueryRootEntityId = this.resolveAliasEntityId(rootEntityType, rootEntityId);
|
|
|
|
|
const searchQuery: EntitySearchQuery = {
|
|
|
|
|
parameters: {
|
|
|
|
|
rootId: searchQueryRootEntityId.id,
|
|
|
|
|
rootType: searchQueryRootEntityId.entityType as EntityType,
|
2020-02-21 19:04:49 +02:00
|
|
|
direction: filter.direction,
|
|
|
|
|
fetchLastLevelOnly: filter.fetchLastLevelOnly
|
2019-10-10 13:00:29 +03:00
|
|
|
},
|
|
|
|
|
relationType: filter.relationType
|
|
|
|
|
};
|
2020-02-21 19:04:49 +02:00
|
|
|
searchQuery.parameters.maxLevel = filter.maxLevel && filter.maxLevel > 0 ? filter.maxLevel : -1;
|
2019-10-10 13:00:29 +03:00
|
|
|
let findByQueryObservable: Observable<Array<BaseData<EntityId>>>;
|
|
|
|
|
if (filter.type === AliasFilterType.assetSearchQuery) {
|
|
|
|
|
const assetSearchQuery = searchQuery as AssetSearchQuery;
|
|
|
|
|
assetSearchQuery.assetTypes = filter.assetTypes;
|
2019-11-15 12:22:14 +02:00
|
|
|
findByQueryObservable = this.assetService.findByQuery(assetSearchQuery, {ignoreLoading: true, ignoreErrors: true});
|
2019-10-10 13:00:29 +03:00
|
|
|
} else if (filter.type === AliasFilterType.deviceSearchQuery) {
|
|
|
|
|
const deviceSearchQuery = searchQuery as DeviceSearchQuery;
|
|
|
|
|
deviceSearchQuery.deviceTypes = filter.deviceTypes;
|
2019-11-15 12:22:14 +02:00
|
|
|
findByQueryObservable = this.deviceService.findByQuery(deviceSearchQuery, {ignoreLoading: true, ignoreErrors: true});
|
2019-10-10 13:00:29 +03:00
|
|
|
} else if (filter.type === AliasFilterType.entityViewSearchQuery) {
|
|
|
|
|
const entityViewSearchQuery = searchQuery as EntityViewSearchQuery;
|
|
|
|
|
entityViewSearchQuery.entityViewTypes = filter.entityViewTypes;
|
2019-11-15 12:22:14 +02:00
|
|
|
findByQueryObservable = this.entityViewService.findByQuery(entityViewSearchQuery, {ignoreLoading: true, ignoreErrors: true});
|
2019-10-10 13:00:29 +03:00
|
|
|
}
|
|
|
|
|
return findByQueryObservable.pipe(
|
|
|
|
|
map((entities) => {
|
|
|
|
|
if (entities && entities.length || !failOnEmpty) {
|
|
|
|
|
if (isDefined(maxItems) && maxItems > 0 && entities) {
|
|
|
|
|
const limit = Math.min(entities.length, maxItems);
|
|
|
|
|
entities.length = limit;
|
|
|
|
|
}
|
|
|
|
|
result.entities = this.entitiesToEntitiesInfo(entities);
|
|
|
|
|
return result;
|
|
|
|
|
} else {
|
|
|
|
|
throw Error();
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return of(result);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-08 14:17:15 +02:00
|
|
|
public checkEntityAlias(entityAlias: EntityAlias): Observable<boolean> {
|
|
|
|
|
return this.resolveAliasFilter(entityAlias.filter, null, 1, true).pipe(
|
|
|
|
|
map((result) => {
|
|
|
|
|
if (result.stateEntity) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
const entities = result.entities;
|
|
|
|
|
if (entities && entities.length) {
|
|
|
|
|
return true;
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
catchError(err => of(false))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-11-15 12:22:14 +02:00
|
|
|
public saveEntityParameters(entityType: EntityType, entityData: ImportEntityData, update: boolean,
|
|
|
|
|
config?: RequestConfig): Observable<ImportEntitiesResultInfo> {
|
|
|
|
|
let saveEntityObservable: Observable<BaseData<EntityId>>;
|
|
|
|
|
switch (entityType) {
|
|
|
|
|
case EntityType.DEVICE:
|
|
|
|
|
const device: Device = {
|
|
|
|
|
name: entityData.name,
|
2020-02-20 16:35:11 +02:00
|
|
|
type: entityData.type,
|
|
|
|
|
label: entityData.label,
|
|
|
|
|
additionalInfo: {
|
|
|
|
|
description: entityData.description
|
|
|
|
|
}
|
2019-11-15 12:22:14 +02:00
|
|
|
};
|
2020-02-20 16:35:11 +02:00
|
|
|
if (entityData.gateway !== null) {
|
|
|
|
|
device.additionalInfo = {
|
|
|
|
|
...device.additionalInfo,
|
|
|
|
|
gateway: entityData.gateway
|
|
|
|
|
};
|
|
|
|
|
}
|
2019-11-15 12:22:14 +02:00
|
|
|
saveEntityObservable = this.deviceService.saveDevice(device, config);
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.ASSET:
|
|
|
|
|
const asset: Asset = {
|
|
|
|
|
name: entityData.name,
|
2020-02-20 16:35:11 +02:00
|
|
|
type: entityData.type,
|
|
|
|
|
label: entityData.label,
|
|
|
|
|
additionalInfo: {
|
|
|
|
|
description: entityData.description
|
|
|
|
|
}
|
2019-11-15 12:22:14 +02:00
|
|
|
};
|
|
|
|
|
saveEntityObservable = this.assetService.saveAsset(asset, config);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return saveEntityObservable.pipe(
|
|
|
|
|
mergeMap((entity) => {
|
|
|
|
|
return this.saveEntityData(entity.id, entityData, config).pipe(
|
|
|
|
|
map(() => {
|
|
|
|
|
return { create: { entity: 1 } } as ImportEntitiesResultInfo;
|
|
|
|
|
}),
|
|
|
|
|
catchError(err => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
catchError(err => {
|
|
|
|
|
if (update) {
|
|
|
|
|
let findEntityObservable: Observable<BaseData<EntityId>>;
|
|
|
|
|
switch (entityType) {
|
|
|
|
|
case EntityType.DEVICE:
|
|
|
|
|
findEntityObservable = this.deviceService.findByName(entityData.name, config);
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.ASSET:
|
|
|
|
|
findEntityObservable = this.assetService.findByName(entityData.name, config);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
return findEntityObservable.pipe(
|
|
|
|
|
mergeMap((entity) => {
|
2020-02-20 16:35:11 +02:00
|
|
|
const tasks: Observable<any>[] = [];
|
|
|
|
|
const result: Device | Asset = entity as (Device | Asset);
|
|
|
|
|
const additionalInfo = result.additionalInfo || {};
|
|
|
|
|
if(result.label !== entityData.label ||
|
|
|
|
|
result.type !== entityData.type ||
|
|
|
|
|
additionalInfo.description !== entityData.description ||
|
|
|
|
|
(result.id.entityType === EntityType.DEVICE && (additionalInfo.gateway !== entityData.gateway)) ) {
|
|
|
|
|
result.label = entityData.label;
|
|
|
|
|
result.type = entityData.type;
|
|
|
|
|
result.additionalInfo = additionalInfo;
|
|
|
|
|
result.additionalInfo.description = entityData.description;
|
|
|
|
|
if (result.id.entityType === EntityType.DEVICE) {
|
|
|
|
|
result.additionalInfo.gateway = entityData.gateway;
|
|
|
|
|
}
|
|
|
|
|
switch (result.id.entityType) {
|
|
|
|
|
case EntityType.DEVICE:
|
|
|
|
|
tasks.push(this.deviceService.saveDevice(result, config));
|
|
|
|
|
break;
|
|
|
|
|
case EntityType.ASSET:
|
|
|
|
|
tasks.push(this.assetService.saveAsset(result, config));
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
tasks.push(this.saveEntityData(entity.id, entityData, config));
|
|
|
|
|
return forkJoin(tasks).pipe(
|
2019-11-15 12:22:14 +02:00
|
|
|
map(() => {
|
|
|
|
|
return { update: { entity: 1 } } as ImportEntitiesResultInfo;
|
|
|
|
|
}),
|
|
|
|
|
catchError(updateError => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
catchError(findErr => of({ error: { entity: 1 } } as ImportEntitiesResultInfo))
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return of({ error: { entity: 1 } } as ImportEntitiesResultInfo);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public saveEntityData(entityId: EntityId, entityData: ImportEntityData, config?: RequestConfig): Observable<any> {
|
|
|
|
|
const observables: Observable<string>[] = [];
|
|
|
|
|
let observable: Observable<string>;
|
|
|
|
|
if (entityData.accessToken && entityData.accessToken !== '') {
|
|
|
|
|
observable = this.deviceService.getDeviceCredentials(entityId.id, false, config).pipe(
|
|
|
|
|
mergeMap((credentials) => {
|
|
|
|
|
credentials.credentialsId = entityData.accessToken;
|
|
|
|
|
credentials.credentialsType = DeviceCredentialsType.ACCESS_TOKEN;
|
|
|
|
|
credentials.credentialsValue = null;
|
|
|
|
|
return this.deviceService.saveDeviceCredentials(credentials, config).pipe(
|
|
|
|
|
map(() => 'ok'),
|
|
|
|
|
catchError(err => of('error'))
|
|
|
|
|
);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
observables.push(observable);
|
|
|
|
|
}
|
|
|
|
|
if (entityData.attributes.shared && entityData.attributes.shared.length) {
|
|
|
|
|
observable = this.attributeService.saveEntityAttributes(entityId, AttributeScope.SHARED_SCOPE,
|
|
|
|
|
entityData.attributes.shared, config).pipe(
|
|
|
|
|
map(() => 'ok'),
|
|
|
|
|
catchError(err => of('error'))
|
|
|
|
|
);
|
|
|
|
|
observables.push(observable);
|
|
|
|
|
}
|
|
|
|
|
if (entityData.attributes.server && entityData.attributes.server.length) {
|
|
|
|
|
observable = this.attributeService.saveEntityAttributes(entityId, AttributeScope.SERVER_SCOPE,
|
|
|
|
|
entityData.attributes.server, config).pipe(
|
|
|
|
|
map(() => 'ok'),
|
|
|
|
|
catchError(err => of('error'))
|
|
|
|
|
);
|
|
|
|
|
observables.push(observable);
|
|
|
|
|
}
|
|
|
|
|
if (entityData.timeseries && entityData.timeseries.length) {
|
|
|
|
|
observable = this.attributeService.saveEntityTimeseries(entityId, 'time', entityData.timeseries, config).pipe(
|
|
|
|
|
map(() => 'ok'),
|
|
|
|
|
catchError(err => of('error'))
|
|
|
|
|
);
|
|
|
|
|
observables.push(observable);
|
|
|
|
|
}
|
|
|
|
|
return forkJoin(observables).pipe(
|
|
|
|
|
map((response) => {
|
|
|
|
|
const hasError = response.filter((status) => status === 'error').length > 0;
|
|
|
|
|
if (hasError) {
|
|
|
|
|
throw Error();
|
|
|
|
|
} else {
|
|
|
|
|
return response;
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2019-10-10 13:00:29 +03:00
|
|
|
private entitiesToEntitiesInfo(entities: Array<BaseData<EntityId>>): Array<EntityInfo> {
|
|
|
|
|
const entitiesInfo = [];
|
|
|
|
|
if (entities) {
|
|
|
|
|
entities.forEach((entity) => {
|
|
|
|
|
entitiesInfo.push(this.entityToEntityInfo(entity));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return entitiesInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private entityToEntityInfo(entity: BaseData<EntityId>): EntityInfo {
|
|
|
|
|
return {
|
|
|
|
|
origEntity: entity,
|
|
|
|
|
name: entity.name,
|
|
|
|
|
label: (entity as any).label ? (entity as any).label : '',
|
|
|
|
|
entityType: entity.id.entityType as EntityType,
|
|
|
|
|
id: entity.id.id,
|
|
|
|
|
entityDescription: (entity as any).additionalInfo ? (entity as any).additionalInfo.description : ''
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private entityRelationInfosToEntitiesInfo(entityRelations: Array<EntityRelationInfo>,
|
|
|
|
|
direction: EntitySearchDirection): Observable<Array<EntityInfo>> {
|
|
|
|
|
if (entityRelations) {
|
|
|
|
|
const tasks: Observable<EntityInfo>[] = [];
|
|
|
|
|
entityRelations.forEach((entityRelation) => {
|
|
|
|
|
tasks.push(this.entityRelationInfoToEntityInfo(entityRelation, direction));
|
|
|
|
|
});
|
|
|
|
|
return forkJoin(tasks);
|
|
|
|
|
} else {
|
|
|
|
|
return of([]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private entityRelationInfoToEntityInfo(entityRelationInfo: EntityRelationInfo, direction: EntitySearchDirection): Observable<EntityInfo> {
|
|
|
|
|
const entityId = direction === EntitySearchDirection.FROM ? entityRelationInfo.to : entityRelationInfo.from;
|
2019-11-15 12:22:14 +02:00
|
|
|
return this.getEntity(entityId.entityType as EntityType, entityId.id, {ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-10-10 13:00:29 +03:00
|
|
|
map((entity) => {
|
|
|
|
|
return this.entityToEntityInfo(entity);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-21 19:04:49 +02:00
|
|
|
private getStateEntityInfo(filter: EntityAliasFilter, stateParams: StateParams): {entityId: EntityId} {
|
|
|
|
|
let entityId: EntityId = null;
|
2019-10-10 13:00:29 +03:00
|
|
|
if (stateParams) {
|
|
|
|
|
if (filter.stateEntityParamName && filter.stateEntityParamName.length) {
|
|
|
|
|
if (stateParams[filter.stateEntityParamName]) {
|
|
|
|
|
entityId = stateParams[filter.stateEntityParamName].entityId;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
entityId = stateParams.entityId;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!entityId) {
|
|
|
|
|
entityId = filter.defaultStateEntity;
|
|
|
|
|
}
|
|
|
|
|
if (entityId) {
|
|
|
|
|
entityId = this.resolveAliasEntityId(entityId.entityType, entityId.id);
|
|
|
|
|
}
|
2020-02-21 19:04:49 +02:00
|
|
|
return {entityId};
|
2019-10-10 13:00:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private resolveAliasEntityId(entityType: EntityType | AliasEntityType, id: string): EntityId {
|
|
|
|
|
const entityId: EntityId = {
|
|
|
|
|
entityType,
|
|
|
|
|
id
|
|
|
|
|
};
|
|
|
|
|
if (entityType === AliasEntityType.CURRENT_CUSTOMER) {
|
|
|
|
|
const authUser = getCurrentAuthUser(this.store);
|
|
|
|
|
entityId.entityType = EntityType.CUSTOMER;
|
|
|
|
|
if (authUser.authority === Authority.CUSTOMER_USER) {
|
|
|
|
|
entityId.id = authUser.customerId;
|
|
|
|
|
}
|
2020-04-10 15:04:12 +03:00
|
|
|
} else if (entityType === AliasEntityType.CURRENT_TENANT){
|
|
|
|
|
const authUser = getCurrentAuthUser(this.store);
|
|
|
|
|
entityId.entityType = EntityType.TENANT;
|
|
|
|
|
entityId.id = authUser.tenantId;
|
2019-10-10 13:00:29 +03:00
|
|
|
}
|
|
|
|
|
return entityId;
|
|
|
|
|
}
|
|
|
|
|
|
2019-09-10 15:12:10 +03:00
|
|
|
private createDatasourcesFromSubscriptionInfo(subscriptionInfo: SubscriptionInfo): Observable<Array<Datasource>> {
|
|
|
|
|
subscriptionInfo = this.validateSubscriptionInfo(subscriptionInfo);
|
|
|
|
|
if (subscriptionInfo.type === DatasourceType.entity) {
|
|
|
|
|
return this.resolveEntitiesFromSubscriptionInfo(subscriptionInfo).pipe(
|
|
|
|
|
map((entities) => {
|
|
|
|
|
const datasources = new Array<Datasource>();
|
|
|
|
|
entities.forEach((entity) => {
|
|
|
|
|
datasources.push(this.createDatasourceFromSubscription(subscriptionInfo, entity));
|
|
|
|
|
});
|
|
|
|
|
return datasources;
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
} else if (subscriptionInfo.type === DatasourceType.function) {
|
|
|
|
|
return of([this.createDatasourceFromSubscription(subscriptionInfo)]);
|
|
|
|
|
} else {
|
|
|
|
|
return of([]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private resolveEntitiesFromSubscriptionInfo(subscriptionInfo: SubscriptionInfo): Observable<Array<BaseData<EntityId>>> {
|
|
|
|
|
if (subscriptionInfo.entityId) {
|
|
|
|
|
if (subscriptionInfo.entityName) {
|
|
|
|
|
const entity: BaseData<EntityId> = {
|
|
|
|
|
id: {id: subscriptionInfo.entityId, entityType: subscriptionInfo.entityType},
|
|
|
|
|
name: subscriptionInfo.entityName
|
|
|
|
|
};
|
|
|
|
|
return of([entity]);
|
|
|
|
|
} else {
|
|
|
|
|
return this.getEntity(subscriptionInfo.entityType, subscriptionInfo.entityId,
|
2019-11-15 12:22:14 +02:00
|
|
|
{ignoreLoading: true, ignoreErrors: true}).pipe(
|
2019-09-10 15:12:10 +03:00
|
|
|
map((entity) => [entity]),
|
|
|
|
|
catchError(e => of([]))
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} else if (subscriptionInfo.entityName || subscriptionInfo.entityNamePrefix || subscriptionInfo.entityIds) {
|
|
|
|
|
let entitiesObservable: Observable<Array<BaseData<EntityId>>>;
|
|
|
|
|
if (subscriptionInfo.entityName) {
|
|
|
|
|
entitiesObservable = this.getEntitiesByNameFilter(subscriptionInfo.entityType, subscriptionInfo.entityName,
|
2019-11-15 12:22:14 +02:00
|
|
|
1, null, {ignoreLoading: true, ignoreErrors: true});
|
2019-09-10 15:12:10 +03:00
|
|
|
} else if (subscriptionInfo.entityNamePrefix) {
|
|
|
|
|
entitiesObservable = this.getEntitiesByNameFilter(subscriptionInfo.entityType, subscriptionInfo.entityNamePrefix,
|
2019-11-15 12:22:14 +02:00
|
|
|
100, null, {ignoreLoading: true, ignoreErrors: true});
|
2019-09-10 15:12:10 +03:00
|
|
|
} else if (subscriptionInfo.entityIds) {
|
2019-11-15 12:22:14 +02:00
|
|
|
entitiesObservable = this.getEntities(subscriptionInfo.entityType, subscriptionInfo.entityIds,
|
|
|
|
|
{ignoreLoading: true, ignoreErrors: true});
|
2019-09-10 15:12:10 +03:00
|
|
|
}
|
|
|
|
|
return entitiesObservable.pipe(
|
|
|
|
|
catchError(e => of([]))
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return of([]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private validateSubscriptionInfo(subscriptionInfo: SubscriptionInfo): SubscriptionInfo {
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
if (subscriptionInfo.type === 'device') {
|
|
|
|
|
subscriptionInfo.type = DatasourceType.entity;
|
|
|
|
|
subscriptionInfo.entityType = EntityType.DEVICE;
|
|
|
|
|
if (subscriptionInfo.deviceId) {
|
|
|
|
|
subscriptionInfo.entityId = subscriptionInfo.deviceId;
|
|
|
|
|
} else if (subscriptionInfo.deviceName) {
|
|
|
|
|
subscriptionInfo.entityName = subscriptionInfo.deviceName;
|
|
|
|
|
} else if (subscriptionInfo.deviceNamePrefix) {
|
|
|
|
|
subscriptionInfo.entityNamePrefix = subscriptionInfo.deviceNamePrefix;
|
|
|
|
|
} else if (subscriptionInfo.deviceIds) {
|
|
|
|
|
subscriptionInfo.entityIds = subscriptionInfo.deviceIds;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return subscriptionInfo;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private createDatasourceFromSubscription(subscriptionInfo: SubscriptionInfo, entity?: BaseData<EntityId>): Datasource {
|
|
|
|
|
let datasource: Datasource;
|
|
|
|
|
if (subscriptionInfo.type === DatasourceType.entity) {
|
|
|
|
|
datasource = {
|
|
|
|
|
type: subscriptionInfo.type,
|
|
|
|
|
entityName: entity.name,
|
|
|
|
|
name: entity.name,
|
|
|
|
|
entityType: subscriptionInfo.entityType,
|
|
|
|
|
entityId: entity.id.id,
|
|
|
|
|
dataKeys: []
|
|
|
|
|
};
|
|
|
|
|
} else if (subscriptionInfo.type === DatasourceType.function) {
|
|
|
|
|
datasource = {
|
|
|
|
|
type: subscriptionInfo.type,
|
|
|
|
|
name: subscriptionInfo.name || DatasourceType.function,
|
|
|
|
|
dataKeys: []
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (subscriptionInfo.timeseries) {
|
|
|
|
|
this.createDatasourceKeys(subscriptionInfo.timeseries, DataKeyType.timeseries, datasource);
|
|
|
|
|
}
|
|
|
|
|
if (subscriptionInfo.attributes) {
|
|
|
|
|
this.createDatasourceKeys(subscriptionInfo.attributes, DataKeyType.attribute, datasource);
|
|
|
|
|
}
|
|
|
|
|
if (subscriptionInfo.functions) {
|
|
|
|
|
this.createDatasourceKeys(subscriptionInfo.functions, DataKeyType.function, datasource);
|
|
|
|
|
}
|
|
|
|
|
if (subscriptionInfo.alarmFields) {
|
|
|
|
|
this.createDatasourceKeys(subscriptionInfo.alarmFields, DataKeyType.alarm, datasource);
|
|
|
|
|
}
|
|
|
|
|
return datasource;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private createDatasourceKeys(keyInfos: Array<KeyInfo>, type: DataKeyType, datasource: Datasource) {
|
|
|
|
|
keyInfos.forEach((keyInfo) => {
|
|
|
|
|
const dataKey = this.utils.createKey(keyInfo, type);
|
|
|
|
|
datasource.dataKeys.push(dataKey);
|
|
|
|
|
});
|
|
|
|
|
}
|
2019-08-14 19:55:24 +03:00
|
|
|
}
|