2020-06-19 18:13:57 +03:00
|
|
|
///
|
2021-01-11 13:42:16 +02:00
|
|
|
/// Copyright © 2016-2021 The Thingsboard Authors
|
2020-06-19 18:13:57 +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.
|
|
|
|
|
///
|
|
|
|
|
|
|
|
|
|
import { DataSetHolder, Datasource, DatasourceType, widgetType } from '@shared/models/widget.models';
|
|
|
|
|
import { SubscriptionTimewindow } from '@shared/models/time/time.models';
|
2020-06-26 11:00:00 +03:00
|
|
|
import { EntityData, EntityDataPageLink, KeyFilter } from '@shared/models/query/query.models';
|
2020-08-03 12:41:36 +03:00
|
|
|
import { emptyPageData, PageData } from '@shared/models/page/page-data';
|
2020-06-19 18:13:57 +03:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
|
import { TelemetryWebsocketService } from '@core/ws/telemetry-websocket.service';
|
|
|
|
|
import { UtilsService } from '@core/services/utils.service';
|
2020-06-26 11:00:00 +03:00
|
|
|
import { deepClone } from '@core/utils';
|
|
|
|
|
import {
|
|
|
|
|
EntityDataSubscription,
|
|
|
|
|
EntityDataSubscriptionOptions,
|
|
|
|
|
SubscriptionDataKey
|
|
|
|
|
} from '@core/api/entity-data-subscription';
|
2020-06-22 18:55:15 +03:00
|
|
|
import { Observable, of } from 'rxjs';
|
2020-06-19 18:13:57 +03:00
|
|
|
|
|
|
|
|
export interface EntityDataListener {
|
|
|
|
|
subscriptionType: widgetType;
|
2020-06-22 18:55:15 +03:00
|
|
|
subscriptionTimewindow?: SubscriptionTimewindow;
|
2020-06-19 18:13:57 +03:00
|
|
|
configDatasource: Datasource;
|
|
|
|
|
configDatasourceIndex: number;
|
2020-06-26 17:02:41 +03:00
|
|
|
dataLoaded: (pageData: PageData<EntityData>,
|
|
|
|
|
data: Array<Array<DataSetHolder>>,
|
|
|
|
|
datasourceIndex: number, pageLink: EntityDataPageLink) => void;
|
2020-06-19 18:13:57 +03:00
|
|
|
dataUpdated: (data: DataSetHolder, datasourceIndex: number, dataIndex: number, dataKeyIndex: number, detectChanges: boolean) => void;
|
2020-06-24 11:31:52 +03:00
|
|
|
initialPageDataChanged?: (nextPageData: PageData<EntityData>) => void;
|
2020-06-22 18:55:15 +03:00
|
|
|
updateRealtimeSubscription?: () => SubscriptionTimewindow;
|
|
|
|
|
setRealtimeSubscription?: (subscriptionTimewindow: SubscriptionTimewindow) => void;
|
2020-08-03 12:41:36 +03:00
|
|
|
subscriptionOptions?: EntityDataSubscriptionOptions;
|
2020-06-22 18:55:15 +03:00
|
|
|
subscription?: EntityDataSubscription;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export interface EntityDataLoadResult {
|
|
|
|
|
pageData: PageData<EntityData>;
|
|
|
|
|
data: Array<Array<DataSetHolder>>;
|
|
|
|
|
datasourceIndex: number;
|
2020-06-26 17:02:41 +03:00
|
|
|
pageLink: EntityDataPageLink;
|
2020-06-19 18:13:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
|
providedIn: 'root'
|
|
|
|
|
})
|
|
|
|
|
export class EntityDataService {
|
|
|
|
|
|
|
|
|
|
constructor(private telemetryService: TelemetryWebsocketService,
|
|
|
|
|
private utils: UtilsService) {}
|
|
|
|
|
|
2021-02-15 18:41:44 +02:00
|
|
|
public prepareSubscription(listener: EntityDataListener,
|
2021-03-02 13:21:53 +02:00
|
|
|
ignoreDataUpdateOnIntervalTick = false): Observable<EntityDataLoadResult> {
|
2020-06-19 18:13:57 +03:00
|
|
|
const datasource = listener.configDatasource;
|
2020-08-03 12:41:36 +03:00
|
|
|
listener.subscriptionOptions = this.createSubscriptionOptions(
|
|
|
|
|
datasource,
|
|
|
|
|
listener.subscriptionType,
|
|
|
|
|
datasource.pageLink,
|
|
|
|
|
datasource.keyFilters,
|
|
|
|
|
null,
|
2021-02-15 18:41:44 +02:00
|
|
|
false,
|
2021-03-02 13:21:53 +02:00
|
|
|
ignoreDataUpdateOnIntervalTick);
|
2020-06-19 18:13:57 +03:00
|
|
|
if (datasource.type === DatasourceType.entity && (!datasource.entityFilter || !datasource.pageLink)) {
|
2020-06-22 18:55:15 +03:00
|
|
|
return of(null);
|
|
|
|
|
}
|
2020-08-03 12:41:36 +03:00
|
|
|
listener.subscription = new EntityDataSubscription(listener, this.telemetryService, this.utils);
|
2020-06-22 18:55:15 +03:00
|
|
|
return listener.subscription.subscribe();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public startSubscription(listener: EntityDataListener) {
|
2020-06-25 20:08:07 +03:00
|
|
|
if (listener.subscription) {
|
|
|
|
|
if (listener.subscriptionType === widgetType.timeseries) {
|
2020-08-03 12:41:36 +03:00
|
|
|
listener.subscriptionOptions.subscriptionTimewindow = deepClone(listener.subscriptionTimewindow);
|
2020-06-25 20:08:07 +03:00
|
|
|
}
|
|
|
|
|
listener.subscription.start();
|
2020-06-22 18:55:15 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-24 11:31:52 +03:00
|
|
|
public subscribeForPaginatedData(listener: EntityDataListener,
|
|
|
|
|
pageLink: EntityDataPageLink,
|
2021-02-15 18:41:44 +02:00
|
|
|
keyFilters: KeyFilter[],
|
2021-03-02 13:21:53 +02:00
|
|
|
ignoreDataUpdateOnIntervalTick = false): Observable<EntityDataLoadResult> {
|
2020-06-22 18:55:15 +03:00
|
|
|
const datasource = listener.configDatasource;
|
2020-08-03 12:41:36 +03:00
|
|
|
listener.subscriptionOptions = this.createSubscriptionOptions(
|
|
|
|
|
datasource,
|
|
|
|
|
listener.subscriptionType,
|
|
|
|
|
pageLink,
|
|
|
|
|
datasource.keyFilters,
|
|
|
|
|
keyFilters,
|
2021-02-15 18:41:44 +02:00
|
|
|
true,
|
2021-03-02 13:21:53 +02:00
|
|
|
ignoreDataUpdateOnIntervalTick);
|
2020-06-22 18:55:15 +03:00
|
|
|
if (datasource.type === DatasourceType.entity && (!datasource.entityFilter || !pageLink)) {
|
2020-08-03 12:41:36 +03:00
|
|
|
listener.dataLoaded(emptyPageData<EntityData>(), [],
|
|
|
|
|
listener.configDatasourceIndex, listener.subscriptionOptions.pageLink);
|
2020-06-25 20:08:07 +03:00
|
|
|
return of(null);
|
2020-06-19 18:13:57 +03:00
|
|
|
}
|
2020-08-03 12:41:36 +03:00
|
|
|
listener.subscription = new EntityDataSubscription(listener, this.telemetryService, this.utils);
|
2020-06-25 20:08:07 +03:00
|
|
|
if (listener.subscriptionType === widgetType.timeseries) {
|
2020-08-03 12:41:36 +03:00
|
|
|
listener.subscriptionOptions.subscriptionTimewindow = deepClone(listener.subscriptionTimewindow);
|
2020-06-25 20:08:07 +03:00
|
|
|
}
|
|
|
|
|
return listener.subscription.subscribe();
|
2020-06-22 18:55:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public stopSubscription(listener: EntityDataListener) {
|
2020-06-25 20:08:07 +03:00
|
|
|
if (listener.subscription) {
|
|
|
|
|
listener.subscription.unsubscribe();
|
|
|
|
|
}
|
2020-06-22 18:55:15 +03:00
|
|
|
}
|
|
|
|
|
|
2020-08-03 12:41:36 +03:00
|
|
|
private createSubscriptionOptions(datasource: Datasource,
|
|
|
|
|
subscriptionType: widgetType,
|
|
|
|
|
pageLink: EntityDataPageLink,
|
|
|
|
|
keyFilters: KeyFilter[],
|
|
|
|
|
additionalKeyFilters: KeyFilter[],
|
2021-02-15 18:41:44 +02:00
|
|
|
isPaginatedDataSubscription: boolean,
|
2021-03-02 13:21:53 +02:00
|
|
|
ignoreDataUpdateOnIntervalTick: boolean): EntityDataSubscriptionOptions {
|
2020-06-19 18:13:57 +03:00
|
|
|
const subscriptionDataKeys: Array<SubscriptionDataKey> = [];
|
|
|
|
|
datasource.dataKeys.forEach((dataKey) => {
|
|
|
|
|
const subscriptionDataKey: SubscriptionDataKey = {
|
|
|
|
|
name: dataKey.name,
|
|
|
|
|
type: dataKey.type,
|
|
|
|
|
funcBody: dataKey.funcBody,
|
|
|
|
|
postFuncBody: dataKey.postFuncBody
|
|
|
|
|
};
|
|
|
|
|
subscriptionDataKeys.push(subscriptionDataKey);
|
|
|
|
|
});
|
|
|
|
|
const entityDataSubscriptionOptions: EntityDataSubscriptionOptions = {
|
|
|
|
|
datasourceType: datasource.type,
|
|
|
|
|
dataKeys: subscriptionDataKeys,
|
2020-08-03 12:41:36 +03:00
|
|
|
type: subscriptionType
|
2020-06-19 18:13:57 +03:00
|
|
|
};
|
2021-03-02 12:04:45 +02:00
|
|
|
if (entityDataSubscriptionOptions.datasourceType === DatasourceType.entity ||
|
|
|
|
|
entityDataSubscriptionOptions.datasourceType === DatasourceType.entityCount) {
|
2020-06-19 18:13:57 +03:00
|
|
|
entityDataSubscriptionOptions.entityFilter = datasource.entityFilter;
|
2020-06-22 18:55:15 +03:00
|
|
|
entityDataSubscriptionOptions.keyFilters = keyFilters;
|
2020-07-01 20:09:25 +03:00
|
|
|
entityDataSubscriptionOptions.additionalKeyFilters = additionalKeyFilters;
|
2021-03-02 12:04:45 +02:00
|
|
|
if (entityDataSubscriptionOptions.datasourceType === DatasourceType.entity) {
|
|
|
|
|
entityDataSubscriptionOptions.pageLink = pageLink;
|
|
|
|
|
}
|
2020-06-19 18:13:57 +03:00
|
|
|
}
|
2020-06-24 11:31:52 +03:00
|
|
|
entityDataSubscriptionOptions.isPaginatedDataSubscription = isPaginatedDataSubscription;
|
2021-03-02 13:21:53 +02:00
|
|
|
entityDataSubscriptionOptions.ignoreDataUpdateOnIntervalTick = ignoreDataUpdateOnIntervalTick;
|
2020-08-03 12:41:36 +03:00
|
|
|
return entityDataSubscriptionOptions;
|
2020-06-19 18:13:57 +03:00
|
|
|
}
|
|
|
|
|
}
|