92 lines
2.8 KiB
C#
92 lines
2.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.JScript;
|
|
using Microsoft.JScript.Vsa;
|
|
|
|
|
|
namespace ExecJavaScript
|
|
{
|
|
public class ExecScript
|
|
{
|
|
///// <summary>
|
|
/////
|
|
///// </summary>
|
|
///// <param name="dc_TagID_PlcValue"></param>
|
|
///// <param name="script"></param>
|
|
///// <returns></returns>
|
|
//public string RunScript(Dictionary<String, String> dc_TagID_PlcValue, string script)
|
|
//{
|
|
|
|
// using (JavascriptContext context = new JavascriptContext())
|
|
// {
|
|
// foreach (KeyValuePair<String, String> item in dc_TagID_PlcValue)
|
|
// {
|
|
// var tagID = item.Key;
|
|
// var tagValue = item.Value;
|
|
// context.SetParameter("tag_" + tagID, tagValue);
|
|
// //context.SetParameter("console", new SystemConsole());
|
|
// }
|
|
// context.Run("result = " + script);
|
|
// return context.GetParameter("result").ToString();
|
|
// }
|
|
//}
|
|
|
|
private static VsaEngine vsaEngine = VsaEngine.CreateEngine();
|
|
|
|
//public string RunScript(Dictionary<String, String> dc_TagID_PlcValue, string script)
|
|
//{
|
|
// List<String> listVarDef = new List<String>();
|
|
|
|
// foreach (KeyValuePair<String, String> item in dc_TagID_PlcValue)
|
|
// {
|
|
// var tagID = item.Key;
|
|
// var tagValue = item.Value;
|
|
// listVarDef.Add($"var tag_{tagID} = {tagValue}; ");
|
|
// }
|
|
// listVarDef.Add(script);
|
|
|
|
// var execScriptStr = string.Join(" ", listVarDef.ToArray());
|
|
|
|
// return EvalJScript(execScriptStr).ToString();
|
|
|
|
//}
|
|
|
|
public string RunScript_SymbolTable(Dictionary<String, String> dc_SymbolName_SymbolValue, string script)
|
|
{
|
|
var listVarDef = new List<String>();
|
|
foreach (KeyValuePair<String, String> item in dc_SymbolName_SymbolValue)
|
|
listVarDef.Add($"var {item.Key} = {item.Value}; ");
|
|
listVarDef.Add(script);
|
|
|
|
return EvalJScript(string.Join(" ", listVarDef.ToArray())).ToString();
|
|
}
|
|
|
|
private object EvalJScript(string JScript)
|
|
{
|
|
lock (this)
|
|
{
|
|
object Result = null;
|
|
try
|
|
{
|
|
Result = Microsoft.JScript.Eval.JScriptEvaluate(JScript, vsaEngine);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
LogNet.LogisTrac.WriteLog(typeof(ExecScript), ex);
|
|
|
|
return ex.Message;
|
|
}
|
|
return Result;
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
}
|