Add API to create telemetry websocket updater in custom widgets.
This commit is contained in:
parent
1e2d7f9f20
commit
ecd2be9456
@ -43,6 +43,15 @@ import { WidgetInfo } from '@home/models/widget-component.models';
|
||||
import jsonSchemaDefaults from 'json-schema-defaults';
|
||||
import materialIconsCodepoints from '!raw-loader!./material-icons-codepoints.raw';
|
||||
import { Observable, of, ReplaySubject } from 'rxjs';
|
||||
import { publishReplay, refCount } from 'rxjs/operators';
|
||||
import { WidgetContext } from '@app/modules/home/models/widget-component.models';
|
||||
import {
|
||||
AttributeData,
|
||||
TelemetrySubscriber,
|
||||
TelemetryType,
|
||||
LatestTelemetry
|
||||
} from '@shared/models/telemetry/telemetry.models';
|
||||
import { EntityId } from '@shared/models/id/entity-id';
|
||||
|
||||
const i18nRegExp = new RegExp(`{${i18nPrefix}:[^{}]+}`, 'g');
|
||||
|
||||
@ -479,4 +488,30 @@ export class UtilsService {
|
||||
return defaultValue;
|
||||
}
|
||||
}
|
||||
|
||||
public getEntityIdFromDatasource(dataSource: Datasource) {
|
||||
return { id: dataSource.entityId, entityType: dataSource.entityType};
|
||||
}
|
||||
|
||||
public createTelemetrySubscriber(ctx: WidgetContext,
|
||||
entityId?: EntityId,
|
||||
type: TelemetryType = LatestTelemetry.LATEST_TELEMETRY,
|
||||
keys: string[] = null): TelemetrySubscriber {
|
||||
if (!entityId && ctx.datasources.length > 0) {
|
||||
entityId = this.getEntityIdFromDatasource(ctx.datasources[0]);
|
||||
}
|
||||
return TelemetrySubscriber.createEntityAttributesSubscription(ctx.telemetryWsService, entityId, type, ctx.ngZone, keys);
|
||||
}
|
||||
|
||||
public subscribeToEntityTelemetry(ctx: WidgetContext,
|
||||
entityId?: EntityId,
|
||||
type: TelemetryType = LatestTelemetry.LATEST_TELEMETRY,
|
||||
keys: string[] = null): Observable<Array<AttributeData>> {
|
||||
ctx.telemetrySubscriber = this.createTelemetrySubscriber(ctx, entityId, type, keys);
|
||||
ctx.telemetrySubscriber.subscribe();
|
||||
return ctx.telemetrySubscriber.attributeData$().pipe(
|
||||
publishReplay(1),
|
||||
refCount()
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@ -40,6 +40,7 @@ import { AuthService } from '@core/auth/auth.service';
|
||||
import { DialogService } from '@core/services/dialog.service';
|
||||
import { CustomDialogService } from '@home/components/widget/dialog/custom-dialog.service';
|
||||
import { ResourceService } from '@core/http/resource.service';
|
||||
import { TelemetryWebsocketService } from '@core/ws/telemetry-websocket.service';
|
||||
import { DatePipe } from '@angular/common';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { DomSanitizer } from '@angular/platform-browser';
|
||||
@ -80,6 +81,7 @@ export class DynamicWidgetComponent extends PageComponent implements IDynamicWid
|
||||
this.ctx.dialogs = $injector.get(DialogService);
|
||||
this.ctx.customDialog = $injector.get(CustomDialogService);
|
||||
this.ctx.resourceService = $injector.get(ResourceService);
|
||||
this.ctx.telemetryWsService = $injector.get(TelemetryWebsocketService);
|
||||
this.ctx.date = $injector.get(DatePipe);
|
||||
this.ctx.translate = $injector.get(TranslateService);
|
||||
this.ctx.http = $injector.get(HttpClient);
|
||||
|
||||
@ -39,6 +39,7 @@ import { OtaPackageService } from '@core/http/ota-package.service';
|
||||
import { AuthService } from '@core/auth/auth.service';
|
||||
import { ResourceService } from '@core/http/resource.service';
|
||||
import { TwoFactorAuthenticationService } from '@core/http/two-factor-authentication.service';
|
||||
import { TelemetryWebsocketService } from '@core/ws/telemetry-websocket.service';
|
||||
|
||||
export const ServicesMap = new Map<string, Type<any>>(
|
||||
[
|
||||
@ -65,6 +66,7 @@ export const ServicesMap = new Map<string, Type<any>>(
|
||||
['otaPackageService', OtaPackageService],
|
||||
['authService', AuthService],
|
||||
['resourceService', ResourceService],
|
||||
['twoFactorAuthenticationService', TwoFactorAuthenticationService]
|
||||
['twoFactorAuthenticationService', TwoFactorAuthenticationService],
|
||||
['telemetryWsService', TelemetryWebsocketService]
|
||||
]
|
||||
);
|
||||
|
||||
@ -73,6 +73,7 @@ import { DialogService } from '@core/services/dialog.service';
|
||||
import { CustomDialogService } from '@home/components/widget/dialog/custom-dialog.service';
|
||||
import { AuthService } from '@core/auth/auth.service';
|
||||
import { ResourceService } from '@core/http/resource.service';
|
||||
import { TelemetryWebsocketService } from '@core/ws/telemetry-websocket.service';
|
||||
import { DatePipe } from '@angular/common';
|
||||
import { TranslateService } from '@ngx-translate/core';
|
||||
import { PageLink } from '@shared/models/page/page-link';
|
||||
@ -84,6 +85,7 @@ import * as RxJS from 'rxjs';
|
||||
import * as RxJSOperators from 'rxjs/operators';
|
||||
import { TbPopoverComponent } from '@shared/components/popover.component';
|
||||
import { EntityId } from '@shared/models/id/entity-id';
|
||||
import { TelemetrySubscriber } from '@app/shared/public-api';
|
||||
|
||||
export interface IWidgetAction {
|
||||
name: string;
|
||||
@ -173,6 +175,8 @@ export class WidgetContext {
|
||||
dialogs: DialogService;
|
||||
customDialog: CustomDialogService;
|
||||
resourceService: ResourceService;
|
||||
telemetryWsService: TelemetryWebsocketService;
|
||||
telemetrySubscriber?: TelemetrySubscriber;
|
||||
date: DatePipe;
|
||||
translate: TranslateService;
|
||||
http: HttpClient;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user