2019-12-24 13:54:00 +02:00
|
|
|
///
|
2020-02-20 10:26:43 +02:00
|
|
|
/// Copyright © 2016-2020 The Thingsboard Authors
|
2019-12-24 13:54:00 +02: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 {
|
|
|
|
|
AfterViewInit,
|
|
|
|
|
Component,
|
2020-02-11 11:01:18 +02:00
|
|
|
ElementRef,
|
|
|
|
|
HostBinding,
|
2019-12-24 13:54:00 +02:00
|
|
|
Inject,
|
|
|
|
|
OnInit,
|
|
|
|
|
QueryList,
|
2020-02-11 11:01:18 +02:00
|
|
|
SkipSelf,
|
|
|
|
|
ViewChild,
|
2019-12-24 16:27:09 +02:00
|
|
|
ViewChildren,
|
|
|
|
|
ViewEncapsulation
|
2019-12-24 13:54:00 +02:00
|
|
|
} from '@angular/core';
|
2020-02-10 13:15:29 +02:00
|
|
|
import { ErrorStateMatcher } from '@angular/material/core';
|
|
|
|
|
import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog';
|
2019-12-24 13:54:00 +02:00
|
|
|
import { Store } from '@ngrx/store';
|
|
|
|
|
import { AppState } from '@core/core.state';
|
2019-12-24 16:27:09 +02:00
|
|
|
import { FormBuilder, FormControl, FormGroup, FormGroupDirective, NgForm, Validators } from '@angular/forms';
|
2020-02-11 11:01:18 +02:00
|
|
|
import { NEVER, Observable, of } from 'rxjs';
|
2019-12-24 13:54:00 +02:00
|
|
|
import { Router } from '@angular/router';
|
2020-02-11 11:01:18 +02:00
|
|
|
import { DialogComponent } from '@shared/components/dialog.component';
|
2019-12-27 16:35:11 +02:00
|
|
|
import { ContentType } from '@shared/models/constants';
|
|
|
|
|
import { JsonContentComponent } from '@shared/components/json-content.component';
|
|
|
|
|
import { TestScriptInputParams } from '@shared/models/rule-node.models';
|
|
|
|
|
import { RuleChainService } from '@core/http/rule-chain.service';
|
2020-02-11 11:01:18 +02:00
|
|
|
import { mergeMap } from 'rxjs/operators';
|
2019-12-27 16:35:11 +02:00
|
|
|
import { ActionNotificationShow } from '@core/notification/notification.actions';
|
2019-12-24 13:54:00 +02:00
|
|
|
|
|
|
|
|
export interface NodeScriptTestDialogData {
|
|
|
|
|
script: string;
|
|
|
|
|
scriptType: string;
|
|
|
|
|
functionTitle: string;
|
|
|
|
|
functionName: string;
|
|
|
|
|
argNames: string[];
|
|
|
|
|
msg?: any;
|
2019-12-27 16:35:11 +02:00
|
|
|
metadata?: {[key: string]: string};
|
2019-12-24 13:54:00 +02:00
|
|
|
msgType?: string;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Component({
|
|
|
|
|
selector: 'tb-node-script-test-dialog',
|
|
|
|
|
templateUrl: './node-script-test-dialog.component.html',
|
|
|
|
|
providers: [{provide: ErrorStateMatcher, useExisting: NodeScriptTestDialogComponent}],
|
2019-12-24 16:27:09 +02:00
|
|
|
styleUrls: ['./node-script-test-dialog.component.scss'],
|
|
|
|
|
encapsulation: ViewEncapsulation.None
|
2019-12-24 13:54:00 +02:00
|
|
|
})
|
|
|
|
|
export class NodeScriptTestDialogComponent extends DialogComponent<NodeScriptTestDialogComponent,
|
|
|
|
|
string> implements OnInit, AfterViewInit, ErrorStateMatcher {
|
|
|
|
|
|
2019-12-24 16:27:09 +02:00
|
|
|
@HostBinding('style.width') width = '100%';
|
|
|
|
|
@HostBinding('style.height') height = '100%';
|
|
|
|
|
|
2019-12-24 13:54:00 +02:00
|
|
|
@ViewChildren('topPanel')
|
|
|
|
|
topPanelElmRef: QueryList<ElementRef<HTMLElement>>;
|
|
|
|
|
|
|
|
|
|
@ViewChildren('topLeftPanel')
|
|
|
|
|
topLeftPanelElmRef: QueryList<ElementRef<HTMLElement>>;
|
|
|
|
|
|
|
|
|
|
@ViewChildren('topRightPanel')
|
|
|
|
|
topRightPanelElmRef: QueryList<ElementRef<HTMLElement>>;
|
|
|
|
|
|
|
|
|
|
@ViewChildren('bottomPanel')
|
|
|
|
|
bottomPanelElmRef: QueryList<ElementRef<HTMLElement>>;
|
|
|
|
|
|
|
|
|
|
@ViewChildren('bottomLeftPanel')
|
|
|
|
|
bottomLeftPanelElmRef: QueryList<ElementRef<HTMLElement>>;
|
|
|
|
|
|
2019-12-24 16:27:09 +02:00
|
|
|
@ViewChildren('bottomRightPanel')
|
2019-12-24 13:54:00 +02:00
|
|
|
bottomRightPanelElmRef: QueryList<ElementRef<HTMLElement>>;
|
|
|
|
|
|
2019-12-27 16:35:11 +02:00
|
|
|
@ViewChild('payloadContent', {static: true}) payloadContent: JsonContentComponent;
|
|
|
|
|
|
2019-12-24 13:54:00 +02:00
|
|
|
nodeScriptTestFormGroup: FormGroup;
|
|
|
|
|
|
|
|
|
|
functionTitle: string;
|
|
|
|
|
|
|
|
|
|
submitted = false;
|
|
|
|
|
|
2019-12-27 16:35:11 +02:00
|
|
|
contentTypes = ContentType;
|
|
|
|
|
|
2019-12-24 13:54:00 +02:00
|
|
|
constructor(protected store: Store<AppState>,
|
|
|
|
|
protected router: Router,
|
|
|
|
|
@Inject(MAT_DIALOG_DATA) public data: NodeScriptTestDialogData,
|
|
|
|
|
@SkipSelf() private errorStateMatcher: ErrorStateMatcher,
|
|
|
|
|
public dialogRef: MatDialogRef<NodeScriptTestDialogComponent, string>,
|
2019-12-27 16:35:11 +02:00
|
|
|
public fb: FormBuilder,
|
|
|
|
|
private ruleChainService: RuleChainService) {
|
2019-12-24 13:54:00 +02:00
|
|
|
super(store, router, dialogRef);
|
|
|
|
|
this.functionTitle = this.data.functionTitle;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngOnInit(): void {
|
2019-12-24 16:27:09 +02:00
|
|
|
this.nodeScriptTestFormGroup = this.fb.group({
|
2019-12-27 16:35:11 +02:00
|
|
|
payload: this.fb.group({
|
|
|
|
|
msgType: [this.data.msgType, [Validators.required]],
|
|
|
|
|
msg: [js_beautify(JSON.stringify(this.data.msg), {indent_size: 4}), []],
|
|
|
|
|
}),
|
|
|
|
|
metadata: [this.data.metadata, [Validators.required]],
|
|
|
|
|
script: [this.data.script, []],
|
|
|
|
|
output: ['', []]
|
2019-12-24 16:27:09 +02:00
|
|
|
});
|
2019-12-24 13:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ngAfterViewInit(): void {
|
2019-12-27 16:35:11 +02:00
|
|
|
this.initSplitLayout(this.topPanelElmRef.first.nativeElement,
|
|
|
|
|
this.topLeftPanelElmRef.first.nativeElement,
|
|
|
|
|
this.topRightPanelElmRef.first.nativeElement,
|
|
|
|
|
this.bottomPanelElmRef.first.nativeElement,
|
|
|
|
|
this.bottomLeftPanelElmRef.first.nativeElement,
|
|
|
|
|
this.bottomRightPanelElmRef.first.nativeElement);
|
2019-12-24 13:54:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private initSplitLayout(topPanel: any,
|
|
|
|
|
topLeftPanel: any,
|
|
|
|
|
topRightPanel: any,
|
|
|
|
|
bottomPanel: any,
|
|
|
|
|
bottomLeftPanel: any,
|
|
|
|
|
bottomRightPanel: any) {
|
|
|
|
|
|
|
|
|
|
Split([topPanel, bottomPanel], {
|
|
|
|
|
sizes: [35, 65],
|
|
|
|
|
gutterSize: 8,
|
|
|
|
|
cursor: 'row-resize',
|
|
|
|
|
direction: 'vertical'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Split([topLeftPanel, topRightPanel], {
|
|
|
|
|
sizes: [50, 50],
|
|
|
|
|
gutterSize: 8,
|
|
|
|
|
cursor: 'col-resize'
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
Split([bottomLeftPanel, bottomRightPanel], {
|
|
|
|
|
sizes: [50, 50],
|
|
|
|
|
gutterSize: 8,
|
|
|
|
|
cursor: 'col-resize'
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {
|
|
|
|
|
const originalErrorState = this.errorStateMatcher.isErrorState(control, form);
|
|
|
|
|
const customErrorState = !!(control && control.invalid && this.submitted);
|
|
|
|
|
return originalErrorState || customErrorState;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
cancel(): void {
|
|
|
|
|
this.dialogRef.close(null);
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-27 16:35:11 +02:00
|
|
|
test(): void {
|
|
|
|
|
this.testNodeScript().subscribe((output) => {
|
|
|
|
|
this.nodeScriptTestFormGroup.get('output').setValue(js_beautify(output, {indent_size: 4}));
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private testNodeScript(): Observable<string> {
|
|
|
|
|
if (this.checkInputParamErrors()) {
|
|
|
|
|
const inputParams: TestScriptInputParams = {
|
|
|
|
|
argNames: this.data.argNames,
|
|
|
|
|
scriptType: this.data.scriptType,
|
|
|
|
|
msgType: this.nodeScriptTestFormGroup.get('payload').get('msgType').value,
|
|
|
|
|
msg: this.nodeScriptTestFormGroup.get('payload').get('msg').value,
|
|
|
|
|
metadata: this.nodeScriptTestFormGroup.get('metadata').value,
|
|
|
|
|
script: this.nodeScriptTestFormGroup.get('script').value
|
|
|
|
|
};
|
|
|
|
|
return this.ruleChainService.testScript(inputParams).pipe(
|
|
|
|
|
mergeMap((result) => {
|
|
|
|
|
if (result.error) {
|
|
|
|
|
this.store.dispatch(new ActionNotificationShow(
|
|
|
|
|
{
|
|
|
|
|
message: result.error,
|
|
|
|
|
type: 'error'
|
|
|
|
|
}));
|
|
|
|
|
return NEVER;
|
|
|
|
|
} else {
|
|
|
|
|
return of(result.output);
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return NEVER;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private checkInputParamErrors(): boolean {
|
|
|
|
|
this.payloadContent.validateOnSubmit();
|
|
|
|
|
if (!this.nodeScriptTestFormGroup.get('payload').valid) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-24 13:54:00 +02:00
|
|
|
save(): void {
|
|
|
|
|
this.submitted = true;
|
2019-12-27 16:35:11 +02:00
|
|
|
this.testNodeScript().subscribe(() => {
|
|
|
|
|
this.nodeScriptTestFormGroup.get('script').markAsPristine();
|
|
|
|
|
this.dialogRef.close(this.nodeScriptTestFormGroup.get('script').value);
|
|
|
|
|
});
|
2019-12-24 13:54:00 +02:00
|
|
|
}
|
|
|
|
|
}
|