Files
ThingsGateway/src/Gateway/ThingsGateway.Gateway.Application/Services/RulesEngine/Node/Actuator/ExecuteScriptNode.cs
2248356998 qq.com aa8aa36aef build: 10.8.19
fix: s7 复用地址对象导致读取异常
feat: 规则引擎node添加内部异常捕获
feat: 变量增加属性: 写入后再次读取检查值是否一致
2025-06-23 21:21:27 +08:00

142 lines
4.7 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using ThingsGateway.Blazor.Diagrams.Core.Geometry;
using ThingsGateway.NewLife;
using TouchSocket.Core;
namespace ThingsGateway.Gateway.Application;
[CategoryNode(Category = "Actuator", ImgUrl = "_content/ThingsGateway.Gateway.Razor/img/CSharpScript.svg", Desc = nameof(ExecuteScriptNode), LocalizerType = typeof(ThingsGateway.Gateway.Application.DefaultDiagram), WidgetType = "ThingsGateway.Gateway.Razor.CSharpScriptWidget,ThingsGateway.Gateway.Razor")]
public class ExecuteScriptNode : TextNode, IActuatorNode, IExexcuteExpressionsBase, IDisposable
{
public ExecuteScriptNode(string id, Point? position = null) : base(id, position)
{
Title = "ExecuteScriptNode"; Placeholder = "ExecuteScriptNode.Placeholder";
Text =
"""
using ThingsGateway.Foundation;
using TouchSocket.Core;
using System.Text;
using ThingsGateway.Gateway.Application;
using ThingsGateway.Gateway.Razor;
public class TestExexcuteExpressions : IExexcuteExpressions
{
public TouchSocket.Core.ILog Logger { get; set; }
public async System.Threading.Tasks.Task<NodeOutput> ExecuteAsync(NodeInput input, System.Threading.CancellationToken cancellationToken)
{
//想上传mqtt可以自己写mqtt上传代码或者通过mqtt插件的公开方法上传
//直接获取mqttclient插件类型的第一个设备
var mqttClient = GlobalData.ReadOnlyChannels.FirstOrDefault(a => a.Value.PluginName == "ThingsGateway.Plugin.Mqtt.MqttClient").Value?.ReadDeviceRuntimes?.FirstOrDefault().Value?.Driver as ThingsGateway.Plugin.Mqtt.MqttClient;
if (mqttClient == null)
throw new("mqttClient NOT FOUND");
TopicArray topicArray = new()
{
Topic = "test",
Json = Encoding.UTF8.GetBytes("test")
};
var result = await mqttClient.MqttUpAsync(topicArray, default).ConfigureAwait(false);// 主题 和 负载
if (!result.IsSuccess)
throw new(result.ErrorMessage);
return new NodeOutput() { Value = result };
//通过设备名称找出mqttClient插件
//var mqttClient = GlobalData.ReadOnlyDevices.FirstOrDefault(a => a.Value.Name == "mqttDevice1").Value?.Driver as ThingsGateway.Plugin.Mqtt.MqttClient;
//if (mqttClient == null)
// throw new("mqttClient NOT FOUND");
//TopicArray topicArray = new()
//{
// Topic = "test",
// Json = Encoding.UTF8.GetBytes("test")
//};
//var result = await mqttClient.MqttUpAsync(topicArray, default).ConfigureAwait(false);// 主题 和 负载
//if (!result.IsSuccess)
// throw new(result.ErrorMessage);
//return new NodeOutput() { Value = result };
}
}
""";
}
private string text;
[ModelValue]
public override string Text
{
get
{
return text;
}
set
{
if (text != value)
{
try
{
var exexcuteExpressions = CSharpScriptEngineExtension.Do<IExexcuteExpressions>(text);
exexcuteExpressions.TryDispose();
}
catch
{
}
CSharpScriptEngineExtension.Remove(text);
}
text = value;
}
}
Task<OperResult<NodeOutput>> IActuatorNode.ExecuteAsync(NodeInput input, CancellationToken cancellationToken)
{
try
{
Logger?.Trace($"Execute script");
var exexcuteExpressions = CSharpScriptEngineExtension.Do<IExexcuteExpressions>(Text);
exexcuteExpressions.Logger = Logger;
return exexcuteExpressions.ExecuteAsync(input, cancellationToken);
}
catch (Exception ex)
{
Logger?.LogWarning(ex);
return Task.FromResult(new OperResult<NodeOutput>(ex));
}
}
public void Dispose()
{
if (text.IsNullOrWhiteSpace())
return;
try
{
var exexcuteExpressions = CSharpScriptEngineExtension.Do<IExexcuteExpressions>(text);
exexcuteExpressions.TryDispose();
}
catch
{
}
CSharpScriptEngineExtension.Remove(text);
GC.SuppressFinalize(this);
}
}