thingsboard/ui-ngx/src/app/shared/models/widget.models.ts

709 lines
18 KiB
TypeScript
Raw Normal View History

2019-09-03 19:31:16 +03:00
///
2022-01-17 14:07:46 +02:00
/// Copyright © 2016-2022 The Thingsboard Authors
2019-09-03 19:31:16 +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-06 20:17:45 +03:00
import { BaseData } from '@shared/models/base-data';
import { TenantId } from '@shared/models/id/tenant-id';
import { WidgetTypeId } from '@shared/models/id/widget-type-id';
2019-09-03 19:31:16 +03:00
import { Timewindow } from '@shared/models/time/time.models';
2019-09-10 15:12:10 +03:00
import { EntityType } from '@shared/models/entity-type.models';
2020-07-06 14:42:36 +03:00
import { AlarmSearchStatus, AlarmSeverity } from '@shared/models/alarm.models';
2019-09-10 15:12:10 +03:00
import { DataKeyType } from './telemetry/telemetry.models';
import { EntityId } from '@shared/models/id/entity-id';
2020-02-24 17:16:02 +02:00
import * as moment_ from 'moment';
import { EntityDataPageLink, EntityFilter, KeyFilter } from '@shared/models/query/query.models';
import { PopoverPlacement } from '@shared/components/popover.models';
import { PageComponent } from '@shared/components/page.component';
import { AfterViewInit, Directive, EventEmitter, Inject, OnInit } from '@angular/core';
import { Store } from '@ngrx/store';
import { AppState } from '@core/core.state';
import { AbstractControl, FormGroup } from '@angular/forms';
import {RuleChainType} from "@shared/models/rule-chain.models";
import {Observable} from "rxjs";
import {RuleNodeConfiguration} from "@shared/models/rule-node.models";
import { Dashboard } from '@shared/models/dashboard.models';
2019-09-03 19:31:16 +03:00
export enum widgetType {
timeseries = 'timeseries',
latest = 'latest',
rpc = 'rpc',
2019-09-10 15:12:10 +03:00
alarm = 'alarm',
static = 'static'
2019-09-03 19:31:16 +03:00
}
export interface WidgetTypeTemplate {
bundleAlias: string;
alias: string;
}
export interface WidgetTypeData {
name: string;
icon: string;
isMdiIcon?: boolean;
2019-09-25 19:37:29 +03:00
configHelpLinkId: string;
2019-09-03 19:31:16 +03:00
template: WidgetTypeTemplate;
}
export const widgetTypesData = new Map<widgetType, WidgetTypeData>(
[
[
widgetType.timeseries,
{
name: 'widget.timeseries',
icon: 'timeline',
2019-09-25 19:37:29 +03:00
configHelpLinkId: 'widgetsConfigTimeseries',
2019-09-03 19:31:16 +03:00
template: {
bundleAlias: 'charts',
alias: 'basic_timeseries'
}
}
],
[
widgetType.latest,
{
name: 'widget.latest',
icon: 'track_changes',
2019-09-25 19:37:29 +03:00
configHelpLinkId: 'widgetsConfigLatest',
2019-09-03 19:31:16 +03:00
template: {
bundleAlias: 'cards',
alias: 'attributes_card'
}
}
],
[
widgetType.rpc,
{
name: 'widget.rpc',
icon: 'mdi:developer-board',
2019-09-25 19:37:29 +03:00
configHelpLinkId: 'widgetsConfigRpc',
isMdiIcon: true,
2019-09-03 19:31:16 +03:00
template: {
bundleAlias: 'gpio_widgets',
alias: 'basic_gpio_control'
}
}
],
[
widgetType.alarm,
{
name: 'widget.alarm',
icon: 'error',
2019-09-25 19:37:29 +03:00
configHelpLinkId: 'widgetsConfigAlarm',
2019-09-03 19:31:16 +03:00
template: {
bundleAlias: 'alarm_widgets',
alias: 'alarms_table'
}
}
2019-09-10 15:12:10 +03:00
],
[
widgetType.static,
{
name: 'widget.static',
icon: 'font_download',
2019-09-25 19:37:29 +03:00
configHelpLinkId: 'widgetsConfigStatic',
2019-09-10 15:12:10 +03:00
template: {
bundleAlias: 'cards',
alias: 'html_card'
}
}
2019-09-03 19:31:16 +03:00
]
]
);
export interface WidgetResource {
url: string;
isModule?: boolean;
2019-09-03 19:31:16 +03:00
}
export interface WidgetActionSource {
name: string;
value: string;
multiple: boolean;
hasShowCondition?: boolean;
}
2019-10-24 19:52:19 +03:00
export const widgetActionSources: {[acionSourceId: string]: WidgetActionSource} = {
headerButton:
{
name: 'widget-action.header-button',
value: 'headerButton',
multiple: true,
hasShowCondition: true
}
};
2019-09-03 19:31:16 +03:00
export interface WidgetTypeDescriptor {
type: widgetType;
resources: Array<WidgetResource>;
templateHtml: string;
templateCss: string;
controllerScript: string;
2019-09-25 19:37:29 +03:00
settingsSchema?: string | any;
dataKeySettingsSchema?: string | any;
settingsDirective?: string;
dataKeySettingsDirective?: string;
2019-09-03 19:31:16 +03:00
defaultConfig: string;
sizeX: number;
sizeY: number;
}
export interface WidgetTypeParameters {
useCustomDatasources?: boolean;
maxDatasources?: number;
maxDataKeys?: number;
datasourcesOptional?: boolean;
dataKeysOptional?: boolean;
stateData?: boolean;
2020-06-22 18:55:15 +03:00
hasDataPageLink?: boolean;
singleEntity?: boolean;
warnOnPageDataOverflow?: boolean;
2021-03-02 13:21:53 +02:00
ignoreDataUpdateOnIntervalTick?: boolean;
}
export interface WidgetControllerDescriptor {
widgetTypeFunction?: any;
2019-09-25 19:37:29 +03:00
settingsSchema?: string | any;
dataKeySettingsSchema?: string | any;
typeParameters?: WidgetTypeParameters;
2019-10-24 19:52:19 +03:00
actionSources?: {[actionSourceId: string]: WidgetActionSource};
}
2021-03-05 17:08:46 +02:00
export interface BaseWidgetType extends BaseData<WidgetTypeId> {
2019-09-03 19:31:16 +03:00
tenantId: TenantId;
bundleAlias: string;
alias: string;
name: string;
2021-03-05 17:08:46 +02:00
}
export interface WidgetType extends BaseWidgetType {
descriptor: WidgetTypeDescriptor;
}
export interface WidgetTypeInfo extends BaseWidgetType {
image: string;
description: string;
widgetType: widgetType;
}
export interface WidgetTypeDetails extends WidgetType {
image: string;
description: string;
2019-09-03 19:31:16 +03:00
}
export enum LegendDirection {
column = 'column',
row = 'row'
}
export const legendDirectionTranslationMap = new Map<LegendDirection, string>(
[
[ LegendDirection.column, 'direction.column' ],
[ LegendDirection.row, 'direction.row' ]
]
);
export enum LegendPosition {
top = 'top',
bottom = 'bottom',
left = 'left',
right = 'right'
}
export const legendPositionTranslationMap = new Map<LegendPosition, string>(
[
[ LegendPosition.top, 'position.top' ],
[ LegendPosition.bottom, 'position.bottom' ],
[ LegendPosition.left, 'position.left' ],
[ LegendPosition.right, 'position.right' ]
]
);
export interface LegendConfig {
position: LegendPosition;
direction?: LegendDirection;
sortDataKeys: boolean;
showMin: boolean;
showMax: boolean;
showAvg: boolean;
showTotal: boolean;
}
2019-10-24 19:52:19 +03:00
export function defaultLegendConfig(wType: widgetType): LegendConfig {
return {
direction: LegendDirection.column,
position: LegendPosition.bottom,
sortDataKeys: false,
2019-10-24 19:52:19 +03:00
showMin: false,
showMax: false,
showAvg: wType === widgetType.timeseries,
showTotal: false
};
}
2019-09-10 15:12:10 +03:00
export interface KeyInfo {
name: string;
label?: string;
color?: string;
funcBody?: string;
postFuncBody?: string;
units?: string;
decimals?: number;
}
export interface DataKey extends KeyInfo {
type: DataKeyType;
pattern?: string;
settings?: any;
usePostProcessing?: boolean;
hidden?: boolean;
2020-02-25 12:30:38 +02:00
inLegend?: boolean;
2020-06-24 18:07:47 +03:00
isAdditional?: boolean;
origDataKeyIndex?: number;
2019-09-10 15:12:10 +03:00
_hash?: number;
}
export enum DatasourceType {
function = 'function',
2021-03-02 12:04:45 +02:00
entity = 'entity',
entityCount = 'entityCount'
2019-09-10 15:12:10 +03:00
}
export const datasourceTypeTranslationMap = new Map<DatasourceType, string>(
[
[ DatasourceType.function, 'function.function' ],
2021-03-02 12:04:45 +02:00
[ DatasourceType.entity, 'entity.entity' ],
[ DatasourceType.entityCount, 'entity.entities-count' ]
]
);
2019-09-10 15:12:10 +03:00
export interface Datasource {
type?: DatasourceType | any;
2019-09-10 15:12:10 +03:00
name?: string;
aliasName?: string;
2019-09-10 15:12:10 +03:00
dataKeys?: Array<DataKey>;
entityType?: EntityType;
entityId?: string;
entityName?: string;
entityAliasId?: string;
filterId?: string;
unresolvedStateEntity?: boolean;
dataReceived?: boolean;
entity?: BaseData<EntityId>;
entityLabel?: string;
entityDescription?: string;
generated?: boolean;
2020-02-24 17:16:02 +02:00
isAdditional?: boolean;
2020-06-24 18:07:47 +03:00
origDatasourceIndex?: number;
pageLink?: EntityDataPageLink;
keyFilters?: Array<KeyFilter>;
entityFilter?: EntityFilter;
dataKeyStartIndex?: number;
[key: string]: any;
}
2019-09-10 15:12:10 +03:00
export type DataSet = [number, any][];
export interface DataSetHolder {
data: DataSet;
}
export interface DatasourceData extends DataSetHolder {
2019-09-10 15:12:10 +03:00
datasource: Datasource;
dataKey: DataKey;
}
export interface LegendKey {
dataKey: DataKey;
dataIndex: number;
}
export interface LegendKeyData {
min: string;
max: string;
avg: string;
total: string;
2019-09-10 15:12:10 +03:00
hidden: boolean;
}
export interface LegendData {
keys: Array<LegendKey>;
data: Array<LegendKeyData>;
}
export enum WidgetActionType {
openDashboardState = 'openDashboardState',
updateDashboardState = 'updateDashboardState',
openDashboard = 'openDashboard',
custom = 'custom',
customPretty = 'customPretty',
mobileAction = 'mobileAction'
}
export enum WidgetMobileActionType {
takePictureFromGallery = 'takePictureFromGallery',
takePhoto = 'takePhoto',
mapDirection = 'mapDirection',
mapLocation = 'mapLocation',
scanQrCode = 'scanQrCode',
makePhoneCall = 'makePhoneCall',
getLocation = 'getLocation',
takeScreenshot = 'takeScreenshot'
}
export const widgetActionTypeTranslationMap = new Map<WidgetActionType, string>(
[
[ WidgetActionType.openDashboardState, 'widget-action.open-dashboard-state' ],
[ WidgetActionType.updateDashboardState, 'widget-action.update-dashboard-state' ],
[ WidgetActionType.openDashboard, 'widget-action.open-dashboard' ],
[ WidgetActionType.custom, 'widget-action.custom' ],
[ WidgetActionType.customPretty, 'widget-action.custom-pretty' ],
[ WidgetActionType.mobileAction, 'widget-action.mobile-action' ]
]
);
export const widgetMobileActionTypeTranslationMap = new Map<WidgetMobileActionType, string>(
[
[ WidgetMobileActionType.takePictureFromGallery, 'widget-action.mobile.take-picture-from-gallery' ],
[ WidgetMobileActionType.takePhoto, 'widget-action.mobile.take-photo' ],
[ WidgetMobileActionType.mapDirection, 'widget-action.mobile.map-direction' ],
[ WidgetMobileActionType.mapLocation, 'widget-action.mobile.map-location' ],
[ WidgetMobileActionType.scanQrCode, 'widget-action.mobile.scan-qr-code' ],
[ WidgetMobileActionType.makePhoneCall, 'widget-action.mobile.make-phone-call' ],
[ WidgetMobileActionType.getLocation, 'widget-action.mobile.get-location' ],
[ WidgetMobileActionType.takeScreenshot, 'widget-action.mobile.take-screenshot' ]
]
);
export interface MobileLaunchResult {
launched: boolean;
}
export interface MobileImageResult {
imageUrl: string;
}
export interface MobileQrCodeResult {
code: string;
format: string;
}
export interface MobileLocationResult {
latitude: number;
longitude: number;
}
export type MobileActionResult = MobileLaunchResult &
MobileImageResult &
MobileQrCodeResult &
MobileLocationResult;
export interface WidgetMobileActionResult<T extends MobileActionResult> {
result?: T;
hasResult: boolean;
error?: string;
hasError: boolean;
}
export interface ProcessImageDescriptor {
processImageFunction: string;
}
export interface ProcessLaunchResultDescriptor {
processLaunchResultFunction?: string;
}
export interface LaunchMapDescriptor extends ProcessLaunchResultDescriptor {
getLocationFunction: string;
}
export interface ScanQrCodeDescriptor {
processQrCodeFunction: string;
}
export interface MakePhoneCallDescriptor extends ProcessLaunchResultDescriptor {
getPhoneNumberFunction: string;
}
export interface GetLocationDescriptor {
processLocationFunction: string;
}
export type WidgetMobileActionDescriptors = ProcessImageDescriptor &
LaunchMapDescriptor &
ScanQrCodeDescriptor &
MakePhoneCallDescriptor &
GetLocationDescriptor;
export interface WidgetMobileActionDescriptor extends WidgetMobileActionDescriptors {
type: WidgetMobileActionType;
handleErrorFunction?: string;
handleEmptyResultFunction?: string;
}
2019-10-24 19:52:19 +03:00
export interface CustomActionDescriptor {
customFunction?: string;
customResources?: Array<WidgetResource>;
customHtml?: string;
customCss?: string;
}
export interface WidgetActionDescriptor extends CustomActionDescriptor {
id: string;
name: string;
icon: string;
displayName?: string;
type: WidgetActionType;
targetDashboardId?: string;
targetDashboardStateId?: string;
openRightLayout?: boolean;
openNewBrowserTab?: boolean;
openInPopover?: boolean;
popoverHideDashboardToolbar?: boolean;
popoverPreferredPlacement?: PopoverPlacement;
popoverHideOnClickOutside?: boolean;
popoverWidth?: string;
popoverHeight?: string;
popoverStyle?: { [klass: string]: any };
openInSeparateDialog?: boolean;
dialogTitle?: string;
dialogHideDashboardToolbar?: boolean;
dialogWidth?: number;
dialogHeight?: number;
setEntityId?: boolean;
stateEntityParamName?: string;
mobileAction?: WidgetMobileActionDescriptor;
useShowWidgetActionFunction?: boolean;
showWidgetActionFunction?: string;
}
2020-02-24 17:16:02 +02:00
export interface WidgetComparisonSettings {
comparisonEnabled?: boolean;
timeForComparison?: moment_.unitOfTime.DurationConstructor;
comparisonCustomIntervalValue?: number;
2020-02-24 17:16:02 +02:00
}
export interface WidgetSettings {
[key: string]: any;
}
2019-09-03 19:31:16 +03:00
export interface WidgetConfig {
title?: string;
titleIcon?: string;
showTitle?: boolean;
showTitleIcon?: boolean;
iconColor?: string;
2019-09-25 19:37:29 +03:00
iconSize?: string;
2020-02-25 19:11:25 +02:00
titleTooltip?: string;
2019-09-03 19:31:16 +03:00
dropShadow?: boolean;
enableFullscreen?: boolean;
useDashboardTimewindow?: boolean;
displayTimewindow?: boolean;
showLegend?: boolean;
legendConfig?: LegendConfig;
2019-09-03 19:31:16 +03:00
timewindow?: Timewindow;
mobileHide?: boolean;
2019-09-03 19:31:16 +03:00
mobileHeight?: number;
mobileOrder?: number;
color?: string;
backgroundColor?: string;
padding?: string;
margin?: string;
widgetStyle?: {[klass: string]: any};
widgetCss?: string;
2019-09-03 19:31:16 +03:00
titleStyle?: {[klass: string]: any};
units?: string;
decimals?: number;
noDataDisplayMessage?: string;
actions?: {[actionSourceId: string]: Array<WidgetActionDescriptor>};
settings?: WidgetSettings;
2019-09-10 15:12:10 +03:00
alarmSource?: Datasource;
2020-07-06 14:42:36 +03:00
alarmStatusList?: AlarmSearchStatus[];
alarmSeverityList?: AlarmSeverity[];
alarmTypeList?: string[];
searchPropagatedAlarms?: boolean;
2019-09-10 15:12:10 +03:00
datasources?: Array<Datasource>;
targetDeviceAliasIds?: Array<string>;
2019-09-03 19:31:16 +03:00
[key: string]: any;
}
2021-03-03 18:41:01 +02:00
export interface Widget extends WidgetInfo{
typeId?: WidgetTypeId;
2021-03-03 18:41:01 +02:00
sizeX: number;
sizeY: number;
row: number;
col: number;
config: WidgetConfig;
}
export interface WidgetInfo {
id?: string;
2019-09-03 19:31:16 +03:00
isSystemType: boolean;
bundleAlias: string;
typeAlias: string;
type: widgetType;
title: string;
2021-03-05 17:08:46 +02:00
image?: string;
description?: string;
2019-09-03 19:31:16 +03:00
}
2020-02-24 17:16:02 +02:00
export interface GroupInfo {
formIndex: number;
GroupTitle: string;
}
export interface JsonSchema {
type: string;
title?: string;
properties: {[key: string]: any};
required?: string[];
}
export interface JsonSettingsSchema {
2020-02-24 17:16:02 +02:00
schema?: JsonSchema;
form?: any[];
groupInfoes?: GroupInfo[];
}
export interface WidgetPosition {
row: number;
column: number;
}
export interface WidgetSize {
sizeX: number;
sizeY: number;
}
export interface IWidgetSettingsComponent {
dashboard: Dashboard;
widget: Widget;
settings: WidgetSettings;
settingsChanged: Observable<WidgetSettings>;
validate();
[key: string]: any;
}
@Directive()
// tslint:disable-next-line:directive-class-suffix
export abstract class WidgetSettingsComponent extends PageComponent implements
IWidgetSettingsComponent, OnInit, AfterViewInit {
dashboard: Dashboard;
widget: Widget;
settingsValue: WidgetSettings;
private settingsSet = false;
set settings(value: WidgetSettings) {
this.settingsValue = value || this.defaultSettings();
if (!this.settingsSet) {
this.settingsSet = true;
this.setupSettings(this.settingsValue);
} else {
this.updateSettings(this.settingsValue);
}
}
get settings(): WidgetSettings {
return this.settingsValue;
}
settingsChangedEmitter = new EventEmitter<WidgetSettings>();
settingsChanged = this.settingsChangedEmitter.asObservable();
protected constructor(@Inject(Store) protected store: Store<AppState>) {
super(store);
}
ngOnInit() {}
ngAfterViewInit(): void {
setTimeout(() => {
if (!this.validateSettings()) {
this.settingsChangedEmitter.emit(null);
}
}, 0);
}
validate() {
this.onValidate();
}
protected setupSettings(settings: WidgetSettings) {
this.onSettingsSet(this.prepareInputSettings(settings));
this.updateValidators(false);
for (const trigger of this.validatorTriggers()) {
const path = trigger.split('.');
let control: AbstractControl = this.settingsForm();
for (const part of path) {
control = control.get(part);
}
control.valueChanges.subscribe(() => {
this.updateValidators(true, trigger);
});
}
this.settingsForm().valueChanges.subscribe((updated: WidgetSettings) => {
this.onSettingsChanged(updated);
});
}
protected updateSettings(settings: WidgetSettings) {
this.settingsForm().reset(this.prepareInputSettings(settings), {emitEvent: false});
this.updateValidators(false);
}
protected updateValidators(emitEvent: boolean, trigger?: string) {
}
protected validatorTriggers(): string[] {
return [];
}
protected onSettingsChanged(updated: WidgetSettings) {
this.settingsValue = updated;
if (this.validateSettings()) {
this.settingsChangedEmitter.emit(this.prepareOutputSettings(updated));
} else {
this.settingsChangedEmitter.emit(null);
}
}
protected prepareInputSettings(settings: WidgetSettings): WidgetSettings {
return settings;
}
protected prepareOutputSettings(settings: WidgetSettings): WidgetSettings {
return settings;
}
protected validateSettings(): boolean {
return this.settingsForm().valid;
}
protected onValidate() {}
protected abstract settingsForm(): FormGroup;
protected abstract onSettingsSet(settings: WidgetSettings);
protected defaultSettings(): WidgetSettings {
return {};
}
}