Skip to content

Commit

Permalink
fix: frontend script getTag value
Browse files Browse the repository at this point in the history
  • Loading branch information
unocelli committed Mar 13, 2024
1 parent 17466c1 commit 266841d
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 6 deletions.
7 changes: 6 additions & 1 deletion client/src/app/_services/project.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import { Injectable, Output, EventEmitter } from '@angular/core';
import { Observable, Subject } from 'rxjs';
import { Observable, Subject, firstValueFrom } from 'rxjs';

import { environment } from '../../environments/environment';
import { ProjectData, ProjectDataCmdType, UploadFile } from '../_models/project';
Expand Down Expand Up @@ -858,6 +858,11 @@ export class ProjectService {
}
//#endregion

async getTagsValues(tagsIds: string[]): Promise<any[]> {
let values = await firstValueFrom(this.storage.getTagsValues(tagsIds));
return values;
}

/**
* Set Project data and save resource to backend
* Used from open and upload JSON Project file
Expand Down
6 changes: 6 additions & 0 deletions client/src/app/_services/rcgi/resclient.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@ export class ResClientService implements ResourceStorageService {
});
}

getTagsValues(query: string[]): Observable<any> {
return new Observable((observer) => {
observer.error('Not supported!');
});
}

heartbeat(activity: boolean): Observable<any> {
return new Observable(observer => {
observer.error('Not supported!');
Expand Down
6 changes: 6 additions & 0 deletions client/src/app/_services/rcgi/resdemo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export class ResDemoService implements ResourceStorageService {
});
}

getTagsValues(query: string[]): Observable<any> {
return new Observable((observer) => {
observer.error('Not supported!');
});
}

heartbeat(activity: boolean): Observable<any> {
return new Observable(observer => {
observer.error('Not supported!');
Expand Down
2 changes: 2 additions & 0 deletions client/src/app/_services/rcgi/resource-storage.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ export abstract class ResourceStorageService {

public abstract downloadFile(fileName: string, type: CommanType): Observable<Blob>;

public abstract getTagsValues(query: string[]): Observable<any>;

public static defileProject(source: ProjectData): ProjectData {
if (!source) {return source;}
let destination = JSON.parse(JSON.stringify(source));
Expand Down
6 changes: 6 additions & 0 deletions client/src/app/_services/rcgi/reswebapi.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ export class ResWebApiService implements ResourceStorageService {
return this.http.get<any>(this.endPointConfig + '/api/daq', { headers: header, params });
}

getTagsValues(tagsIds: string[]): Observable<any> {
let header = new HttpHeaders({ 'Content-Type': 'application/json' });
let params = { ids: JSON.stringify(tagsIds) };
return this.http.get<any>(this.endPointConfig + '/api/getTagValue', { headers: header, params });
}

heartbeat(activity: boolean): Observable<any> {
let header = new HttpHeaders({ 'Content-Type': 'application/json' });
return this.http.post<any>(this.endPointConfig + '/api/heartbeat', { headers: header, params: activity });
Expand Down
18 changes: 13 additions & 5 deletions client/src/app/_services/script.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { environment } from '../../environments/environment';
import { Script, ScriptMode } from '../_models/script';
import { ProjectService } from './project.service';
import { HmiService, ScriptCommandEnum, ScriptCommandMessage } from './hmi.service';
import { Utils } from '../_helpers/utils';

@Injectable({
providedIn: 'root'
Expand Down Expand Up @@ -41,7 +42,8 @@ export class ScriptService {
console.warn('TODO: Script with mode CLIENT not work with parameters.');
}
try {
const result = eval(this.addSysFunctions(script.code));
const asyncScript = `(async () => { ${this.addSysFunctions(script.code)} })();`;
const result = eval(asyncScript);
observer.next(result);
} catch (err) {
console.error(err);
Expand All @@ -56,23 +58,29 @@ export class ScriptService {
console.warn('TODO: Script with mode CLIENT not work with parameters.');
}
try {
eval(this.addSysFunctions(script.code));
const asyncScript = `(async () => { ${this.addSysFunctions(script.code)} })();`;
eval(asyncScript);
} catch (err) {
console.error(err);
}
}

private addSysFunctions(scriptCode: string): string {
let code = scriptCode.replace(/\$getTag\(/g, 'this.$getTag(');
let code = scriptCode.replace(/\$getTag\(/g, 'await this.$getTag(');
code = code.replace(/\$setTag\(/g, 'this.$setTag(');
code = code.replace(/\$getTagId\(/g, 'this.$getTagId(');
code = code.replace(/\$setView\(/g, 'this.$setView(');
code = code.replace(/\$enableDevice\(/g, 'this.$enableDevice(');
return code;
}

public $getTag(id: string) {
return this.projectService.getTagFromId(id)?.value;
public async $getTag(id: string) {
let tag = this.projectService.getTagFromId(id);
if (!Utils.isNullOrUndefined(tag?.value)) {
return tag.value;
}
let values = await this.projectService.getTagsValues([id]);
return values[0]?.value;
}

public $setTag(id: string, value: any) {
Expand Down

0 comments on commit 266841d

Please sign in to comment.