2024-05-09 18:56:31 +03:00
|
|
|
///
|
|
|
|
|
/// Copyright © 2016-2024 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.
|
|
|
|
|
///
|
|
|
|
|
|
2024-05-20 18:08:25 +03:00
|
|
|
import {
|
|
|
|
|
AfterViewInit,
|
|
|
|
|
ChangeDetectorRef,
|
|
|
|
|
Component,
|
2024-05-21 19:26:48 +03:00
|
|
|
EventEmitter,
|
2024-05-20 18:08:25 +03:00
|
|
|
HostBinding,
|
|
|
|
|
OnDestroy,
|
|
|
|
|
OnInit,
|
|
|
|
|
ViewChild,
|
|
|
|
|
ViewEncapsulation
|
|
|
|
|
} from '@angular/core';
|
2024-05-09 18:56:31 +03:00
|
|
|
import { PageComponent } from '@shared/components/page.component';
|
|
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
|
|
|
|
import { ActivatedRoute, Router } from '@angular/router';
|
2024-05-21 19:26:48 +03:00
|
|
|
import { map, switchMap, takeUntil } from 'rxjs/operators';
|
2024-05-09 18:56:31 +03:00
|
|
|
import { Subject } from 'rxjs';
|
2024-05-23 12:20:37 +03:00
|
|
|
import { ScadaSymbolData, ScadaSymbolEditObjectCallbacks } from '@home/pages/scada-symbol/scada-symbol.models';
|
2024-05-21 19:26:48 +03:00
|
|
|
import {
|
|
|
|
|
IotSvgMetadata, IotSvgObjectSettings,
|
|
|
|
|
parseIotSvgMetadataFromContent,
|
|
|
|
|
updateIotSvgMetadataInContent
|
|
|
|
|
} from '@home/components/widget/lib/svg/iot-svg.models';
|
2024-05-20 18:08:25 +03:00
|
|
|
import { UntypedFormBuilder, UntypedFormGroup } from '@angular/forms';
|
2024-05-21 19:26:48 +03:00
|
|
|
import { createFileFromContent, deepClone } from '@core/utils';
|
2024-05-20 18:08:25 +03:00
|
|
|
import {
|
|
|
|
|
ScadaSymbolEditorComponent,
|
|
|
|
|
ScadaSymbolEditorData
|
|
|
|
|
} from '@home/pages/scada-symbol/scada-symbol-editor.component';
|
2024-05-21 19:26:48 +03:00
|
|
|
import { ImageService } from '@core/http/image.service';
|
|
|
|
|
import { imageResourceType, IMAGES_URL_PREFIX } from '@shared/models/resource.models';
|
|
|
|
|
import { HasDirtyFlag } from '@core/guards/confirm-on-exit.guard';
|
|
|
|
|
import { IAliasController, IStateController, StateParams } from '@core/api/widget-api.models';
|
|
|
|
|
import { EntityAliases } from '@shared/models/alias.models';
|
|
|
|
|
import { Filters } from '@shared/models/query/query.models';
|
|
|
|
|
import { AliasController } from '@core/api/alias-controller';
|
|
|
|
|
import { EntityService } from '@core/http/entity.service';
|
|
|
|
|
import { UtilsService } from '@core/services/utils.service';
|
|
|
|
|
import { TranslateService } from '@ngx-translate/core';
|
|
|
|
|
import { Widget, widgetType } from '@shared/models/widget.models';
|
|
|
|
|
import {
|
|
|
|
|
iotSvgWidgetDefaultSettings,
|
|
|
|
|
IotSvgWidgetSettings
|
|
|
|
|
} from '@home/components/widget/lib/svg/iot-svg-widget.models';
|
2024-05-22 19:40:49 +03:00
|
|
|
import { WidgetActionCallbacks } from '@home/components/widget/action/manage-widget-actions.component.models';
|
2024-05-23 12:20:37 +03:00
|
|
|
import {
|
|
|
|
|
ScadaSymbolMetadataComponent
|
|
|
|
|
} from '@home/pages/scada-symbol/metadata-components/scada-symbol-metadata.component';
|
2024-05-09 18:56:31 +03:00
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-scada-symbol',
|
|
|
|
|
templateUrl: './scada-symbol.component.html',
|
|
|
|
|
styleUrls: ['./scada-symbol.component.scss'],
|
|
|
|
|
encapsulation: ViewEncapsulation.None
|
|
|
|
|
})
|
2024-05-23 12:20:37 +03:00
|
|
|
export class ScadaSymbolComponent extends PageComponent
|
|
|
|
|
implements OnInit, OnDestroy, AfterViewInit, HasDirtyFlag, ScadaSymbolEditObjectCallbacks {
|
2024-05-09 18:56:31 +03:00
|
|
|
|
2024-05-22 19:40:49 +03:00
|
|
|
widgetType = widgetType;
|
|
|
|
|
|
2024-05-09 18:56:31 +03:00
|
|
|
@HostBinding('style.width') width = '100%';
|
|
|
|
|
@HostBinding('style.height') height = '100%';
|
|
|
|
|
|
2024-05-21 19:26:48 +03:00
|
|
|
@ViewChild('symbolEditor', {static: false})
|
2024-05-20 18:08:25 +03:00
|
|
|
symbolEditor: ScadaSymbolEditorComponent;
|
|
|
|
|
|
2024-05-23 12:20:37 +03:00
|
|
|
@ViewChild('symbolMetadata')
|
|
|
|
|
symbolMetadata: ScadaSymbolMetadataComponent;
|
|
|
|
|
|
2024-05-09 18:56:31 +03:00
|
|
|
symbolData: ScadaSymbolData;
|
2024-05-20 18:08:25 +03:00
|
|
|
symbolEditorData: ScadaSymbolEditorData;
|
2024-05-09 18:56:31 +03:00
|
|
|
|
2024-05-20 18:08:25 +03:00
|
|
|
previewMode = false;
|
|
|
|
|
|
|
|
|
|
scadaSymbolFormGroup: UntypedFormGroup;
|
|
|
|
|
|
2024-05-22 19:40:49 +03:00
|
|
|
scadaPreviewFormGroup: UntypedFormGroup;
|
|
|
|
|
|
2024-05-09 18:56:31 +03:00
|
|
|
private destroy$ = new Subject<void>();
|
|
|
|
|
|
2024-05-20 18:08:25 +03:00
|
|
|
private origSymbolData: ScadaSymbolData;
|
|
|
|
|
|
2024-05-21 19:26:48 +03:00
|
|
|
updateBreadcrumbs = new EventEmitter();
|
|
|
|
|
|
|
|
|
|
aliasController: IAliasController;
|
|
|
|
|
|
2024-05-22 19:40:49 +03:00
|
|
|
widgetActionCallbacks: WidgetActionCallbacks = {
|
|
|
|
|
fetchDashboardStates: () => [],
|
|
|
|
|
fetchCellClickColumns: () => []
|
|
|
|
|
};
|
|
|
|
|
|
2024-05-21 19:26:48 +03:00
|
|
|
previewWidgets: Array<Widget> = [];
|
|
|
|
|
|
2024-05-22 19:40:49 +03:00
|
|
|
tags: string[];
|
|
|
|
|
|
2024-05-23 12:20:37 +03:00
|
|
|
editObjectCallbacks: ScadaSymbolEditObjectCallbacks = this;
|
|
|
|
|
|
|
|
|
|
symbolEditorDirty = false;
|
|
|
|
|
|
2024-05-22 19:40:49 +03:00
|
|
|
private previewIotSvgObjectSettings: IotSvgObjectSettings;
|
|
|
|
|
|
2024-05-21 19:26:48 +03:00
|
|
|
private previewWidget: Widget;
|
|
|
|
|
|
|
|
|
|
private forcePristine = false;
|
|
|
|
|
|
|
|
|
|
get isDirty(): boolean {
|
2024-05-23 12:20:37 +03:00
|
|
|
return (this.scadaSymbolFormGroup.dirty || this.symbolEditorDirty) && !this.forcePristine;
|
2024-05-21 19:26:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
set isDirty(value: boolean) {
|
|
|
|
|
this.forcePristine = !value;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 18:56:31 +03:00
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
private router: Router,
|
2024-05-20 18:08:25 +03:00
|
|
|
private route: ActivatedRoute,
|
|
|
|
|
private fb: UntypedFormBuilder,
|
2024-05-21 19:26:48 +03:00
|
|
|
private cd: ChangeDetectorRef,
|
|
|
|
|
private entityService: EntityService,
|
|
|
|
|
private utils: UtilsService,
|
|
|
|
|
private translate: TranslateService,
|
|
|
|
|
private imageService: ImageService) {
|
2024-05-09 18:56:31 +03:00
|
|
|
super(store);
|
2024-05-20 18:08:25 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
|
|
|
|
this.scadaSymbolFormGroup = this.fb.group({
|
|
|
|
|
metadata: [null]
|
|
|
|
|
});
|
2024-05-22 19:40:49 +03:00
|
|
|
this.scadaPreviewFormGroup = this.fb.group({
|
|
|
|
|
iotSvgObject: [null]
|
|
|
|
|
});
|
2024-05-21 19:26:48 +03:00
|
|
|
|
|
|
|
|
const entitiAliases: EntityAliases = {};
|
|
|
|
|
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
const stateController: IStateController = {
|
|
|
|
|
getStateParams: (): StateParams => ({})
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const filters: Filters = {};
|
|
|
|
|
|
|
|
|
|
this.aliasController = new AliasController(this.utils,
|
|
|
|
|
this.entityService,
|
|
|
|
|
this.translate,
|
|
|
|
|
() => stateController, entitiAliases, filters);
|
|
|
|
|
|
2024-05-09 18:56:31 +03:00
|
|
|
this.route.data.pipe(
|
|
|
|
|
takeUntil(this.destroy$)
|
|
|
|
|
).subscribe(
|
|
|
|
|
() => {
|
|
|
|
|
this.reset();
|
2024-05-20 18:08:25 +03:00
|
|
|
this.init(this.route.snapshot.data.symbolData);
|
2024-05-09 18:56:31 +03:00
|
|
|
}
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit() {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnDestroy() {
|
|
|
|
|
super.ngOnDestroy();
|
|
|
|
|
this.destroy$.next();
|
|
|
|
|
this.destroy$.complete();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-20 18:08:25 +03:00
|
|
|
onApplyScadaSymbolConfig() {
|
|
|
|
|
if (this.scadaSymbolFormGroup.valid) {
|
2024-05-21 19:26:48 +03:00
|
|
|
const svgContent = this.prepareSvgContent();
|
|
|
|
|
const file = createFileFromContent(svgContent, this.symbolData.imageResource.fileName,
|
|
|
|
|
this.symbolData.imageResource.descriptor.mediaType);
|
|
|
|
|
const type = imageResourceType(this.symbolData.imageResource);
|
|
|
|
|
let imageInfoObservable =
|
|
|
|
|
this.imageService.updateImage(type, this.symbolData.imageResource.resourceKey, file);
|
|
|
|
|
const metadata: IotSvgMetadata = this.scadaSymbolFormGroup.get('metadata').value;
|
|
|
|
|
if (metadata.title !== this.symbolData.imageResource.title) {
|
|
|
|
|
imageInfoObservable = imageInfoObservable.pipe(
|
|
|
|
|
switchMap(imageInfo => {
|
|
|
|
|
imageInfo.title = metadata.title;
|
|
|
|
|
return this.imageService.updateImageInfo(imageInfo);
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
imageInfoObservable.pipe(
|
|
|
|
|
switchMap(imageInfo => this.imageService.getImageString(
|
|
|
|
|
`${IMAGES_URL_PREFIX}/${type}/${encodeURIComponent(imageInfo.resourceKey)}`).pipe(
|
|
|
|
|
map(content => ({
|
|
|
|
|
imageResource: imageInfo,
|
|
|
|
|
svgContent: content
|
|
|
|
|
}))
|
|
|
|
|
))
|
|
|
|
|
).subscribe(data => {
|
|
|
|
|
this.init(data);
|
|
|
|
|
this.updateBreadcrumbs.emit();
|
|
|
|
|
});
|
2024-05-20 18:08:25 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onRevertScadaSymbolConfig() {
|
|
|
|
|
this.init(this.origSymbolData);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 19:26:48 +03:00
|
|
|
enterPreviewMode() {
|
|
|
|
|
this.symbolData.svgContent = this.prepareSvgContent();
|
2024-05-23 19:08:40 +03:00
|
|
|
this.previewIotSvgObjectSettings = {
|
|
|
|
|
behavior: {},
|
|
|
|
|
properties: {}
|
|
|
|
|
};
|
2024-05-22 19:40:49 +03:00
|
|
|
this.scadaPreviewFormGroup.patchValue({
|
|
|
|
|
iotSvgObject: this.previewIotSvgObjectSettings
|
|
|
|
|
}, {emitEvent: false});
|
|
|
|
|
this.scadaPreviewFormGroup.markAsPristine();
|
2024-05-21 19:26:48 +03:00
|
|
|
const settings: IotSvgWidgetSettings = {...iotSvgWidgetDefaultSettings,
|
2024-05-23 19:08:40 +03:00
|
|
|
...{
|
|
|
|
|
simulated: true,
|
|
|
|
|
iotSvg: null,
|
|
|
|
|
iotSvgContent: this.symbolData.svgContent,
|
|
|
|
|
iotSvgObject: this.previewIotSvgObjectSettings
|
|
|
|
|
}
|
|
|
|
|
};
|
2024-05-21 19:26:48 +03:00
|
|
|
this.previewWidget = {
|
|
|
|
|
typeFullFqn: 'system.iot_svg',
|
|
|
|
|
type: widgetType.rpc,
|
|
|
|
|
sizeX: 24,
|
|
|
|
|
sizeY: 24,
|
|
|
|
|
row: 0,
|
|
|
|
|
col: 0,
|
|
|
|
|
config: {
|
|
|
|
|
settings,
|
2024-05-22 19:40:49 +03:00
|
|
|
showTitle: false,
|
2024-05-21 19:26:48 +03:00
|
|
|
dropShadow: false,
|
|
|
|
|
padding: '0',
|
|
|
|
|
margin: '0'
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
this.previewWidgets = [this.previewWidget];
|
|
|
|
|
this.previewMode = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exitPreviewMode() {
|
|
|
|
|
this.symbolEditorData = {
|
|
|
|
|
svgContent: this.symbolData.svgContent
|
|
|
|
|
};
|
|
|
|
|
this.previewMode = false;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-22 19:40:49 +03:00
|
|
|
onRevertPreviewSettings() {
|
|
|
|
|
this.scadaPreviewFormGroup.patchValue({
|
|
|
|
|
iotSvgObject: this.previewIotSvgObjectSettings
|
|
|
|
|
}, {emitEvent: false});
|
|
|
|
|
this.scadaPreviewFormGroup.markAsPristine();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onApplyPreviewSettings() {
|
|
|
|
|
this.scadaPreviewFormGroup.markAsPristine();
|
|
|
|
|
this.previewIotSvgObjectSettings = this.scadaPreviewFormGroup.get('iotSvgObject').value;
|
|
|
|
|
this.updatePreviewWidgetSettings();
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-23 12:20:37 +03:00
|
|
|
tagsUpdated(tags: string[]) {
|
|
|
|
|
this.tags = tags;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tagHasStateRenderFunction(tag: string): boolean {
|
|
|
|
|
const metadata: IotSvgMetadata = this.scadaSymbolFormGroup.get('metadata').value;
|
|
|
|
|
if (metadata.tags) {
|
|
|
|
|
const found = metadata.tags.find(t => t.tag === tag);
|
|
|
|
|
return !!found?.stateRenderFunction;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tagHasClickAction(tag: string): boolean {
|
|
|
|
|
const metadata: IotSvgMetadata = this.scadaSymbolFormGroup.get('metadata').value;
|
|
|
|
|
if (metadata.tags) {
|
|
|
|
|
const found = metadata.tags.find(t => t.tag === tag);
|
|
|
|
|
return !!found?.actions?.click?.actionFunction;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
editTagStateRenderFunction(tag: string) {
|
|
|
|
|
this.symbolMetadata.editTagStateRenderFunction(tag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
editTagClickAction(tag: string) {
|
|
|
|
|
this.symbolMetadata.editTagClickAction(tag);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
onSymbolEditObjectDirty(dirty: boolean) {
|
|
|
|
|
this.symbolEditorDirty = dirty;
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-21 19:26:48 +03:00
|
|
|
private updatePreviewWidgetSettings() {
|
|
|
|
|
this.previewWidget = deepClone(this.previewWidget);
|
2024-05-22 19:40:49 +03:00
|
|
|
this.previewWidget.config.settings.iotSvgObject = this.previewIotSvgObjectSettings;
|
2024-05-21 19:26:48 +03:00
|
|
|
this.previewWidgets = [this.previewWidget];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private prepareSvgContent(): string {
|
|
|
|
|
const svgContent = this.symbolEditor.getContent();
|
|
|
|
|
const metadata: IotSvgMetadata = this.scadaSymbolFormGroup.get('metadata').value;
|
|
|
|
|
return updateIotSvgMetadataInContent(svgContent, metadata);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-09 18:56:31 +03:00
|
|
|
private reset(): void {
|
2024-05-27 18:17:23 +03:00
|
|
|
if (this.symbolMetadata) {
|
|
|
|
|
this.symbolMetadata.selectedOption = 'general';
|
|
|
|
|
}
|
2024-05-20 18:08:25 +03:00
|
|
|
this.previewMode = false;
|
2024-05-09 18:56:31 +03:00
|
|
|
}
|
|
|
|
|
|
2024-05-20 18:08:25 +03:00
|
|
|
private init(data: ScadaSymbolData) {
|
|
|
|
|
this.origSymbolData = data;
|
|
|
|
|
this.symbolData = deepClone(data);
|
|
|
|
|
this.symbolEditorData = {
|
|
|
|
|
svgContent: this.symbolData.svgContent
|
|
|
|
|
};
|
2024-05-21 19:26:48 +03:00
|
|
|
const metadata = parseIotSvgMetadataFromContent(this.symbolData.svgContent);
|
2024-05-20 18:08:25 +03:00
|
|
|
this.scadaSymbolFormGroup.patchValue({
|
2024-05-21 19:26:48 +03:00
|
|
|
metadata
|
2024-05-20 18:08:25 +03:00
|
|
|
}, {emitEvent: false});
|
|
|
|
|
this.scadaSymbolFormGroup.markAsPristine();
|
2024-05-23 12:20:37 +03:00
|
|
|
this.symbolEditorDirty = false;
|
2024-05-20 18:08:25 +03:00
|
|
|
this.cd.markForCheck();
|
2024-05-09 18:56:31 +03:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|