2023-05-19 17:54:27 +03:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2023 The Thingsboard Authors
|
|
|
|
|
///
|
|
|
|
|
/// 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.
|
|
|
|
|
///
|
|
|
|
|
|
2023-05-30 12:37:47 +03:00
|
|
|
import { Component, forwardRef, Input, OnChanges, OnInit, SimpleChanges } from '@angular/core';
|
2023-05-19 17:54:27 +03:00
|
|
|
import {
|
|
|
|
|
AbstractControl,
|
2023-05-30 12:37:47 +03:00
|
|
|
ControlValueAccessor,
|
|
|
|
|
FormControl,
|
2023-05-19 17:54:27 +03:00
|
|
|
NG_VALIDATORS,
|
2023-05-30 12:37:47 +03:00
|
|
|
NG_VALUE_ACCESSOR,
|
|
|
|
|
UntypedFormArray,
|
|
|
|
|
UntypedFormBuilder,
|
|
|
|
|
UntypedFormControl,
|
2023-05-19 17:54:27 +03:00
|
|
|
UntypedFormGroup,
|
|
|
|
|
Validator
|
|
|
|
|
} from '@angular/forms';
|
|
|
|
|
import { WidgetConfigComponent } from '@home/components/widget/widget-config.component';
|
2023-05-30 12:37:47 +03:00
|
|
|
import {
|
|
|
|
|
Datasource,
|
|
|
|
|
DatasourceType,
|
|
|
|
|
JsonSettingsSchema,
|
|
|
|
|
WidgetConfigMode,
|
|
|
|
|
widgetType
|
|
|
|
|
} from '@shared/models/widget.models';
|
2023-05-19 17:54:27 +03:00
|
|
|
import { CdkDragDrop } from '@angular/cdk/drag-drop';
|
|
|
|
|
import { deepClone } from '@core/utils';
|
|
|
|
|
import { DataKeyType } from '@shared/models/telemetry/telemetry.models';
|
|
|
|
|
import { UtilsService } from '@core/services/utils.service';
|
2023-06-02 19:43:13 +03:00
|
|
|
import { DataKeysCallbacks } from '@home/components/widget/config/data-keys.component.models';
|
2023-05-30 12:37:47 +03:00
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
2023-06-02 19:37:16 +03:00
|
|
|
import { coerceBoolean } from '@shared/decorators/coercion';
|
2023-05-19 17:54:27 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-datasources',
|
|
|
|
|
templateUrl: './datasources.component.html',
|
2023-06-06 17:20:35 +03:00
|
|
|
styleUrls: ['./datasources.component.scss'],
|
2023-05-19 17:54:27 +03:00
|
|
|
providers: [
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALUE_ACCESSOR,
|
|
|
|
|
useExisting: forwardRef(() => DatasourcesComponent),
|
|
|
|
|
multi: true
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
provide: NG_VALIDATORS,
|
|
|
|
|
useExisting: forwardRef(() => DatasourcesComponent),
|
|
|
|
|
multi: true,
|
|
|
|
|
}
|
|
|
|
|
]
|
|
|
|
|
})
|
2023-05-30 12:37:47 +03:00
|
|
|
export class DatasourcesComponent implements ControlValueAccessor, OnInit, Validator, OnChanges {
|
|
|
|
|
|
|
|
|
|
datasourceType = DatasourceType;
|
|
|
|
|
|
2023-07-04 19:58:55 +03:00
|
|
|
|
|
|
|
|
public get isAlarmSource(): boolean {
|
|
|
|
|
return this.widgetConfigComponent.widgetType === widgetType.alarm;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 12:37:47 +03:00
|
|
|
public get basicMode(): boolean {
|
|
|
|
|
return !this.widgetConfigComponent.widgetEditMode && this.configMode === WidgetConfigMode.basic;
|
|
|
|
|
}
|
2023-05-19 17:54:27 +03:00
|
|
|
|
|
|
|
|
public get maxDatasources(): number {
|
2023-07-04 19:58:55 +03:00
|
|
|
return (this.forceSingleDatasource || this.isAlarmSource) ? 1 : this.widgetConfigComponent.modelValue?.typeParameters?.maxDatasources;
|
2023-05-19 17:54:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get singleDatasource(): boolean {
|
|
|
|
|
return this.maxDatasources === 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get showAddDatasource(): boolean {
|
|
|
|
|
return this.widgetConfigComponent.modelValue?.typeParameters &&
|
|
|
|
|
(this.maxDatasources === -1 || this.datasourcesFormArray.length < this.maxDatasources);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public get dragDisabled(): boolean {
|
|
|
|
|
return this.disabled || this.singleDatasource || this.datasourcesFormArray.length < 2;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
disabled: boolean;
|
|
|
|
|
|
2023-06-02 19:37:16 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
hideDataKeyLabel = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
hideDataKeyColor = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
hideDataKeyUnits = false;
|
|
|
|
|
|
|
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
hideDataKeyDecimals = false;
|
|
|
|
|
|
2023-06-05 19:52:34 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
hideDataKeys = false;
|
|
|
|
|
|
2023-08-15 16:52:35 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
hideLatestDataKeys = false;
|
|
|
|
|
|
2023-06-30 19:59:13 +03:00
|
|
|
@Input()
|
|
|
|
|
@coerceBoolean()
|
|
|
|
|
forceSingleDatasource = false;
|
|
|
|
|
|
2023-05-30 12:37:47 +03:00
|
|
|
@Input()
|
|
|
|
|
configMode: WidgetConfigMode;
|
|
|
|
|
|
2023-05-19 17:54:27 +03:00
|
|
|
datasourcesFormGroup: UntypedFormGroup;
|
|
|
|
|
|
|
|
|
|
timeseriesKeyError = false;
|
|
|
|
|
|
|
|
|
|
datasourceError: string[] = [];
|
|
|
|
|
|
2023-05-30 12:37:47 +03:00
|
|
|
datasourcesMode: DatasourceType;
|
|
|
|
|
|
2023-05-19 17:54:27 +03:00
|
|
|
private propagateChange = (_val: any) => {};
|
|
|
|
|
|
|
|
|
|
constructor(private fb: UntypedFormBuilder,
|
|
|
|
|
private utils: UtilsService,
|
2023-05-30 12:37:47 +03:00
|
|
|
public translate: TranslateService,
|
2023-05-19 17:54:27 +03:00
|
|
|
private widgetConfigComponent: WidgetConfigComponent) {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnChange(fn: any): void {
|
|
|
|
|
this.propagateChange = fn;
|
|
|
|
|
if (this.validate(null)) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.datasourcesUpdated(this.datasourcesFormGroup.get('datasources').value);
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
registerOnTouched(fn: any): void {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setDisabledState(isDisabled: boolean): void {
|
|
|
|
|
this.disabled = isDisabled;
|
|
|
|
|
if (this.disabled) {
|
|
|
|
|
this.datasourcesFormGroup.disable({emitEvent: false});
|
|
|
|
|
} else {
|
|
|
|
|
this.datasourcesFormGroup.enable({emitEvent: false});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
|
this.datasourcesFormGroup = this.fb.group({
|
|
|
|
|
datasources: this.fb.array([])
|
|
|
|
|
});
|
|
|
|
|
this.datasourcesFormGroup.valueChanges.subscribe(
|
|
|
|
|
() => {
|
|
|
|
|
this.datasourcesUpdated(this.datasourcesFormGroup.get('datasources').value);
|
|
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 12:37:47 +03:00
|
|
|
ngOnChanges(changes: SimpleChanges): void {
|
|
|
|
|
for (const propName of Object.keys(changes)) {
|
|
|
|
|
const change = changes[propName];
|
|
|
|
|
if (!change.firstChange && change.currentValue !== change.previousValue) {
|
|
|
|
|
if (propName === 'configMode') {
|
|
|
|
|
this.configModeChanged();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 17:54:27 +03:00
|
|
|
writeValue(datasources?: Datasource[]): void {
|
|
|
|
|
this.datasourcesFormArray.clear({emitEvent: false});
|
2023-05-30 12:37:47 +03:00
|
|
|
this.datasourcesMode = this.detectDatasourcesMode(datasources);
|
|
|
|
|
let changed = false;
|
2023-05-19 17:54:27 +03:00
|
|
|
if (datasources) {
|
2023-06-30 19:59:13 +03:00
|
|
|
let length;
|
|
|
|
|
if (this.maxDatasources === -1) {
|
|
|
|
|
length = datasources.length;
|
|
|
|
|
} else {
|
|
|
|
|
length = Math.min(this.maxDatasources, datasources.length);
|
|
|
|
|
}
|
|
|
|
|
for (let i = 0; i < length; i++) {
|
|
|
|
|
const datasource = datasources[i];
|
2023-05-30 12:37:47 +03:00
|
|
|
if (this.basicMode && datasource.type !== this.datasourcesMode) {
|
|
|
|
|
datasource.type = this.datasourcesMode;
|
|
|
|
|
changed = true;
|
|
|
|
|
}
|
2023-05-19 17:54:27 +03:00
|
|
|
this.datasourcesFormArray.push(this.fb.control(datasource, []), {emitEvent: false});
|
2023-06-30 19:59:13 +03:00
|
|
|
}
|
2023-05-19 17:54:27 +03:00
|
|
|
}
|
|
|
|
|
if (this.singleDatasource && !this.datasourcesFormArray.length) {
|
|
|
|
|
this.addDatasource(false);
|
|
|
|
|
}
|
2023-05-30 12:37:47 +03:00
|
|
|
if (changed) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
this.datasourcesUpdated(this.datasourcesFormGroup.get('datasources').value);
|
|
|
|
|
}, 0);
|
|
|
|
|
}
|
2023-05-19 17:54:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
validate(c: UntypedFormControl) {
|
|
|
|
|
this.timeseriesKeyError = false;
|
|
|
|
|
this.datasourceError = [];
|
|
|
|
|
if (!this.datasourcesFormGroup.valid) {
|
|
|
|
|
return {
|
|
|
|
|
datasources: {
|
|
|
|
|
valid: false,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
const datasources: Datasource[] = this.datasourcesFormGroup.get('datasources').value;
|
|
|
|
|
if (!this.datasourcesOptional && (!datasources || !datasources.length)) {
|
|
|
|
|
return {
|
|
|
|
|
datasources: {
|
|
|
|
|
valid: false
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (this.hasAdditionalLatestDataKeys) {
|
|
|
|
|
let valid = datasources.filter(datasource => datasource?.dataKeys?.length).length > 0;
|
|
|
|
|
if (!valid) {
|
|
|
|
|
this.timeseriesKeyError = true;
|
|
|
|
|
return {
|
|
|
|
|
timeseriesDataKeys: {
|
|
|
|
|
valid: false
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
const emptyDatasources = datasources.filter(datasource => !datasource?.dataKeys?.length &&
|
|
|
|
|
!datasource?.latestDataKeys?.length);
|
|
|
|
|
valid = emptyDatasources.length === 0;
|
|
|
|
|
if (!valid) {
|
|
|
|
|
for (const emptyDatasource of emptyDatasources) {
|
|
|
|
|
const i = datasources.indexOf(emptyDatasource);
|
|
|
|
|
this.datasourceError[i] = 'At least one data key should be specified';
|
|
|
|
|
}
|
|
|
|
|
return {
|
|
|
|
|
dataKeys: {
|
|
|
|
|
valid: false
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 12:37:47 +03:00
|
|
|
datasourcesModeChange(datasourcesMode: DatasourceType) {
|
|
|
|
|
this.datasourcesMode = datasourcesMode;
|
|
|
|
|
if (this.basicMode) {
|
|
|
|
|
for (const datasourceControl of this.datasourcesControls) {
|
|
|
|
|
const datasource: Datasource = datasourceControl.value;
|
|
|
|
|
if (datasource.type !== datasourcesMode) {
|
|
|
|
|
datasource.type = datasourcesMode;
|
|
|
|
|
datasourceControl.patchValue(datasource);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private configModeChanged() {
|
|
|
|
|
if (this.basicMode) {
|
2023-07-04 19:58:55 +03:00
|
|
|
const datasourcesMode = this.detectDatasourcesMode(this.datasourcesFormGroup.get('datasources').value);
|
2023-05-30 12:37:47 +03:00
|
|
|
this.datasourcesModeChange(datasourcesMode);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private detectDatasourcesMode(datasources?: Datasource[]) {
|
|
|
|
|
let datasourcesMode = DatasourceType.device;
|
|
|
|
|
if (datasources && datasources.length) {
|
|
|
|
|
datasourcesMode = datasources[0].type;
|
|
|
|
|
}
|
|
|
|
|
if (datasourcesMode !== DatasourceType.device && datasourcesMode !== DatasourceType.entity) {
|
|
|
|
|
datasourcesMode = DatasourceType.device;
|
|
|
|
|
}
|
|
|
|
|
return datasourcesMode;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-19 17:54:27 +03:00
|
|
|
get datasourcesFormArray(): UntypedFormArray {
|
|
|
|
|
return this.datasourcesFormGroup.get('datasources') as UntypedFormArray;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get datasourcesControls(): FormControl[] {
|
|
|
|
|
return this.datasourcesFormArray.controls as FormControl[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public trackByDatasource(index: number, datasourceControl: AbstractControl): any {
|
|
|
|
|
return datasourceControl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private datasourcesUpdated(datasources: Datasource[]) {
|
|
|
|
|
this.propagateChange(datasources);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public onDatasourceDrop(event: CdkDragDrop<string[]>) {
|
|
|
|
|
const datasourceForm = this.datasourcesFormArray.at(event.previousIndex);
|
|
|
|
|
this.datasourcesFormArray.removeAt(event.previousIndex);
|
|
|
|
|
this.datasourcesFormArray.insert(event.currentIndex, datasourceForm);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public removeDatasource(index: number) {
|
|
|
|
|
this.datasourcesFormArray.removeAt(index);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public addDatasource(emitEvent = true) {
|
|
|
|
|
let newDatasource: Datasource;
|
|
|
|
|
if (this.widgetConfigComponent.functionsOnly) {
|
|
|
|
|
newDatasource = deepClone(this.utils.getDefaultDatasource(this.dataKeySettingsSchema.schema));
|
|
|
|
|
newDatasource.dataKeys = [this.dataKeysCallbacks.generateDataKey('Sin', DataKeyType.function, this.dataKeySettingsSchema)];
|
|
|
|
|
} else {
|
2023-05-30 12:37:47 +03:00
|
|
|
const type = this.basicMode ? this.datasourcesMode : DatasourceType.entity;
|
|
|
|
|
newDatasource = { type,
|
2023-05-19 17:54:27 +03:00
|
|
|
dataKeys: []
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
if (this.hasAdditionalLatestDataKeys) {
|
|
|
|
|
newDatasource.latestDataKeys = [];
|
|
|
|
|
}
|
|
|
|
|
this.datasourcesFormArray.push(this.fb.control(newDatasource, []), {emitEvent});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private get dataKeySettingsSchema(): JsonSettingsSchema {
|
|
|
|
|
return this.widgetConfigComponent.modelValue?.dataKeySettingsSchema;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private get dataKeysCallbacks(): DataKeysCallbacks {
|
|
|
|
|
return this.widgetConfigComponent.widgetConfigCallbacks;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private get hasAdditionalLatestDataKeys(): boolean {
|
|
|
|
|
return this.widgetConfigComponent.widgetType === widgetType.timeseries &&
|
|
|
|
|
this.widgetConfigComponent.modelValue?.typeParameters?.hasAdditionalLatestDataKeys;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private get datasourcesOptional(): boolean {
|
|
|
|
|
return this.widgetConfigComponent.modelValue?.typeParameters?.datasourcesOptional;
|
|
|
|
|
}
|
|
|
|
|
}
|