From aad4bd8e18b134b8685942acb9dea17423ea270d Mon Sep 17 00:00:00 2001 From: wq1234wq Date: Mon, 25 Oct 2021 15:37:59 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E7=BC=96=E8=AF=91=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- IoTSharp/ClientApp/angular.json | 4 +- IoTSharp/ClientApp/package.json | 1 + .../src/app/layout/basic/basic.component.ts | 2 +- .../devicecertificate.component.html | 1 - .../flow/diagram/diagram.component.html | 11 +- IoTSharp/Controllers/RulesController.cs | 8 +- .../FlowRuleEngine/FlowRuleProcessorV2.cs | 197 ++++++++++++++---- IoTSharp/Handlers/EventBusHandler.cs | 4 +- IoTSharp/Handlers/MQTTServerHandler.cs | 4 +- IoTSharp/IoTSharp.xml | 2 +- IoTSharp/Startup.cs | 2 +- 11 files changed, 181 insertions(+), 55 deletions(-) diff --git a/IoTSharp/ClientApp/angular.json b/IoTSharp/ClientApp/angular.json index ffb8f9be..da549b56 100644 --- a/IoTSharp/ClientApp/angular.json +++ b/IoTSharp/ClientApp/angular.json @@ -62,8 +62,8 @@ "budgets": [ { "type": "initial", - "maximumWarning": "4mb", - "maximumError": "7mb" + "maximumWarning": "7mb", + "maximumError": "8mb" }, { "type": "anyComponentStyle", diff --git a/IoTSharp/ClientApp/package.json b/IoTSharp/ClientApp/package.json index 9d3d920e..a29a1c10 100644 --- a/IoTSharp/ClientApp/package.json +++ b/IoTSharp/ClientApp/package.json @@ -68,6 +68,7 @@ "ajv": "^8.1.0", "angular-baidu-maps": "^11.1.0", "angular-draggable-droppable": "^4.6.0", + "babel-loader": "^7.1.5", "bpmn-js": "^8.1.0", "file-saver": "^2.0.5", "grapesjs": "^0.17.26", diff --git a/IoTSharp/ClientApp/src/app/layout/basic/basic.component.ts b/IoTSharp/ClientApp/src/app/layout/basic/basic.component.ts index 9bd99a18..3ca4e814 100644 --- a/IoTSharp/ClientApp/src/app/layout/basic/basic.component.ts +++ b/IoTSharp/ClientApp/src/app/layout/basic/basic.component.ts @@ -6,7 +6,7 @@ import { environment } from '@env/environment'; @Component({ selector: 'layout-basic', template: ` - + diff --git a/IoTSharp/ClientApp/src/app/routes/device/devicecertificate/devicecertificate.component.html b/IoTSharp/ClientApp/src/app/routes/device/devicecertificate/devicecertificate.component.html index f6de427c..e69de29b 100644 --- a/IoTSharp/ClientApp/src/app/routes/device/devicecertificate/devicecertificate.component.html +++ b/IoTSharp/ClientApp/src/app/routes/device/devicecertificate/devicecertificate.component.html @@ -1 +0,0 @@ -

devicecertificate works!

diff --git a/IoTSharp/ClientApp/src/app/routes/flow/diagram/diagram.component.html b/IoTSharp/ClientApp/src/app/routes/flow/diagram/diagram.component.html index 7a74d5b6..3685f3cc 100644 --- a/IoTSharp/ClientApp/src/app/routes/flow/diagram/diagram.component.html +++ b/IoTSharp/ClientApp/src/app/routes/flow/diagram/diagram.component.html @@ -62,14 +62,15 @@ [ngModelOptions]="{ standalone: true }" (ngModelChange)="flowscripttypeChange($event)" > - - - - - + + + + + + diff --git a/IoTSharp/Controllers/RulesController.cs b/IoTSharp/Controllers/RulesController.cs index 75652efa..5294d9e4 100644 --- a/IoTSharp/Controllers/RulesController.cs +++ b/IoTSharp/Controllers/RulesController.cs @@ -33,10 +33,10 @@ namespace IoTSharp.Controllers public class RulesController : ControllerBase { private ApplicationDbContext _context; - private readonly FlowRuleProcessor _flowRuleProcessor; + private readonly FlowRuleProcessorV2 _flowRuleProcessor; private UserManager _userManager; - public RulesController(ApplicationDbContext context, UserManager userManager, FlowRuleProcessor flowRuleProcessor) + public RulesController(ApplicationDbContext context, UserManager userManager, FlowRuleProcessorV2 flowRuleProcessor) { this._userManager = userManager; this._context = context; @@ -874,8 +874,8 @@ namespace IoTSharp.Controllers var testabizId = Guid.NewGuid().ToString(); //根据业务保存起来,用来查询执行事件和步骤 // await _flowRuleProcessor.RunFlowRules(ruleid, d, profile.Id, EventType.TestPurpose, testabizId); - FlowRuleProcessorV2 ruleProcessorV2 = new FlowRuleProcessorV2(this._context); - var result= await ruleProcessorV2.RunFlowRules(ruleid, d, profile.Id, EventType.TestPurpose, testabizId); + // FlowRuleProcessorV2 ruleProcessorV2 = new FlowRuleProcessorV2(this._context); + var result= await _flowRuleProcessor.RunFlowRules(ruleid, d, profile.Id, EventType.TestPurpose, testabizId); //应该由事件总线去通知 return new ApiResult(ApiCode.Success, "test complete", result.OrderBy(c => c.Step). diff --git a/IoTSharp/FlowRuleEngine/FlowRuleProcessorV2.cs b/IoTSharp/FlowRuleEngine/FlowRuleProcessorV2.cs index 9caf53e1..7e7ed291 100644 --- a/IoTSharp/FlowRuleEngine/FlowRuleProcessorV2.cs +++ b/IoTSharp/FlowRuleEngine/FlowRuleProcessorV2.cs @@ -17,20 +17,21 @@ namespace IoTSharp.FlowRuleEngine { private readonly IServiceProvider _sp; private readonly ApplicationDbContext _context; - private readonly ILogger _logger; + private readonly ILogger _logger; private readonly AppSettings _setting; private List _allFlows = new List(); private List _allflowoperation = new List(); public FlowRuleProcessorV2( - ApplicationDbContext context) + ApplicationDbContext context, ILogger logger, IServiceScopeFactory scopeFactor, IOptions options) { - + _sp = scopeFactor.CreateScope().ServiceProvider; _context = context; - + _logger = logger; + _setting = options.Value; } - + /// /// @@ -40,7 +41,7 @@ namespace IoTSharp.FlowRuleEngine /// 创建者(可以是模拟器(测试),可以是设备,在EventType中区分一下) /// 类型 /// 业务Id(第三方唯一Id,用于取回事件以及记录的标识) - /// + /// 返回所有节点的记录信息,需要保存则保存 public async Task> RunFlowRules(Guid ruleid, object data, Guid creator, EventType type, string BizId) { @@ -56,7 +57,7 @@ namespace IoTSharp.FlowRuleEngine MataData = JsonConvert.SerializeObject(data), FlowRule = rule, Bizid = BizId, - Type = EventType.TestPurpose, + Type = type, EventStaus = 1 }; _context.BaseEvents.Add(_event); @@ -67,7 +68,7 @@ namespace IoTSharp.FlowRuleEngine var startoperation = new FlowOperation() { - OperationId = Guid.NewGuid(), + OperationId = Guid.NewGuid(), bpmnid = start.bpmnid, AddDate = DateTime.Now, FlowRule = start.FlowRule, @@ -86,7 +87,8 @@ namespace IoTSharp.FlowRuleEngine foreach (var item in nextflows) { var flowOperation = new FlowOperation() - { OperationId = Guid.NewGuid(), + { + OperationId = Guid.NewGuid(), AddDate = DateTime.Now, FlowRule = item.FlowRule, Flow = item, @@ -161,7 +163,7 @@ namespace IoTSharp.FlowRuleEngine BaseEvent = peroperation.BaseEvent }; _allflowoperation.Add(taskoperation); - + //脚本处理 if (!string.IsNullOrEmpty(flow.NodeProcessScriptType) && !string.IsNullOrEmpty(flow.NodeProcessScript)) @@ -175,11 +177,111 @@ namespace IoTSharp.FlowRuleEngine break; case "python": { + + dynamic obj = null; using (var pse = _sp.GetRequiredService()) { obj = pse.Do(scriptsrc, taskoperation.Data); } + + if (obj != null) + { + var next = await ProcessCondition(taskoperation.Flow.FlowId, obj); + foreach (var item in next) + { + var flowOperation = new FlowOperation() + { + OperationId = Guid.NewGuid(), + AddDate = DateTime.Now, + FlowRule = item.FlowRule, + Flow = item, + Data = JsonConvert.SerializeObject(data), + NodeStatus = 1, + OperationDesc = "执行条件(" + (string.IsNullOrEmpty(item.Conditionexpression) + ? "空条件" + : item.Conditionexpression) + ")", + Step = taskoperation.Step++, + bpmnid = item.bpmnid, + BaseEvent = taskoperation.BaseEvent + }; + _allflowoperation.Add(flowOperation); + await Process(flowOperation.OperationId, obj); + } + + } + else + { + taskoperation.OperationDesc = "脚本执行异常"; + taskoperation.NodeStatus = 2; + } + } + break; + case "sql": + { + dynamic obj = null; + using (var pse = _sp.GetRequiredService()) + { + obj = pse.Do(scriptsrc, taskoperation.Data); + } + + var next = await ProcessCondition(taskoperation.Flow.FlowId, obj); + + + if (obj!=null) + { + foreach (var item in next) + { + + var flowOperation = new FlowOperation() + { + OperationId = Guid.NewGuid(), + AddDate = DateTime.Now, + FlowRule = item.FlowRule, + Flow = item, + Data = JsonConvert.SerializeObject(data), + NodeStatus = 1, + OperationDesc = "执行条件(" + (string.IsNullOrEmpty(item.Conditionexpression) + ? "空条件" + : item.Conditionexpression) + ")", + Step = taskoperation.Step++, + bpmnid = item.bpmnid, + BaseEvent = taskoperation.BaseEvent + }; + _allflowoperation.Add(flowOperation); + + await Process(flowOperation.OperationId, obj); + } + + + } + else + { + taskoperation.OperationDesc = "脚本执行异常"; + taskoperation.NodeStatus = 2; + } + + + + } + + break; + case "lua": + { + dynamic obj = null; + + using (var lua = _sp.GetRequiredService()) + { + obj = lua.Do(scriptsrc, taskoperation.Data); + } + + + if (obj != null) + { + + + + var next = await ProcessCondition(taskoperation.Flow.FlowId, obj); foreach (var item in next) { @@ -203,42 +305,65 @@ namespace IoTSharp.FlowRuleEngine await Process(flowOperation.OperationId, obj); } + } + else + { + + taskoperation.OperationDesc = "脚本执行异常"; + taskoperation.NodeStatus = 2; + } + } + + break; - case "sql": - break; + + + case "javascript": { ExpandoObject obj = null; using (var js = _sp.GetRequiredService()) { - string result = js.Do(@"var output=input.Temperature;output = output + 100; return { Temperature:output};", taskoperation.Data); + string result = js.Do(scriptsrc, taskoperation.Data); obj = JsonConvert.DeserializeObject(result); } - var next = await ProcessCondition(taskoperation.Flow.FlowId, obj); - - foreach (var item in next) + if (obj != null) { - var flowOperation = new FlowOperation() + + var next = await ProcessCondition(taskoperation.Flow.FlowId, obj); + + foreach (var item in next) { - OperationId = Guid.NewGuid(), - AddDate = DateTime.Now, - FlowRule = item.FlowRule, - Flow = item, - Data = JsonConvert.SerializeObject(obj), - NodeStatus = 1, - OperationDesc = "执行条件(" + (string.IsNullOrEmpty(item.Conditionexpression) - ? "空条件" - : item.Conditionexpression) + ")", - Step = ++taskoperation.Step, - bpmnid = item.bpmnid, - BaseEvent = taskoperation.BaseEvent - }; - _allflowoperation.Add(flowOperation); - await Process(flowOperation.OperationId, obj); + var flowOperation = new FlowOperation() + { + OperationId = Guid.NewGuid(), + AddDate = DateTime.Now, + FlowRule = item.FlowRule, + Flow = item, + Data = JsonConvert.SerializeObject(obj), + NodeStatus = 1, + OperationDesc = "执行条件(" + + (string.IsNullOrEmpty(item.Conditionexpression) + ? "空条件" + : item.Conditionexpression) + ")", + Step = ++taskoperation.Step, + bpmnid = item.bpmnid, + BaseEvent = taskoperation.BaseEvent + }; + _allflowoperation.Add(flowOperation); + await Process(flowOperation.OperationId, obj); + } + } + else + { + + taskoperation.OperationDesc = "脚本执行异常"; + taskoperation.NodeStatus = 2; + } } break; @@ -266,7 +391,7 @@ namespace IoTSharp.FlowRuleEngine BaseEvent = taskoperation.BaseEvent }; _allflowoperation.Add(flowOperation); - + @@ -362,7 +487,7 @@ namespace IoTSharp.FlowRuleEngine end.BaseEvent = peroperation.BaseEvent; _allflowoperation.Add(end); } - + break; //case "bpmn:StartEvent": @@ -434,7 +559,7 @@ namespace IoTSharp.FlowRuleEngine // break; // // - //没有终结点的节点必须 + //没有终结点的节点必须留个空标签 case "label": break; @@ -457,7 +582,7 @@ namespace IoTSharp.FlowRuleEngine default: { - + break; } diff --git a/IoTSharp/Handlers/EventBusHandler.cs b/IoTSharp/Handlers/EventBusHandler.cs index 177965dd..9aced18e 100644 --- a/IoTSharp/Handlers/EventBusHandler.cs +++ b/IoTSharp/Handlers/EventBusHandler.cs @@ -37,11 +37,11 @@ namespace IoTSharp.Handlers readonly ILogger _logger; private readonly IServiceScopeFactory _scopeFactor; private readonly IStorage _storage; - private readonly FlowRuleProcessor _flowRuleProcessor; + private readonly FlowRuleProcessorV2 _flowRuleProcessor; private readonly IEasyCachingProvider _caching; public EventBusHandler(ILogger logger, IServiceScopeFactory scopeFactor - , IOptions options, IStorage storage, FlowRuleProcessor flowRuleProcessor, IEasyCachingProviderFactory factory + , IOptions options, IStorage storage, FlowRuleProcessorV2 flowRuleProcessor, IEasyCachingProviderFactory factory ) { _appSettings = options.Value; diff --git a/IoTSharp/Handlers/MQTTServerHandler.cs b/IoTSharp/Handlers/MQTTServerHandler.cs index a420c80d..82af12dd 100644 --- a/IoTSharp/Handlers/MQTTServerHandler.cs +++ b/IoTSharp/Handlers/MQTTServerHandler.cs @@ -31,11 +31,11 @@ namespace IoTSharp.Handlers private readonly IEasyCachingProviderFactory _factory; readonly IMqttServerEx _serverEx; private readonly ICapPublisher _queue; - private readonly FlowRuleProcessor _flowRuleProcessor; + private readonly FlowRuleProcessorV2 _flowRuleProcessor; private readonly IEasyCachingProvider _caching; readonly MqttClientSetting _mcsetting; public MQTTServerHandler(ILogger logger, IServiceScopeFactory scopeFactor, IMqttServerEx serverEx - , IOptions options, ICapPublisher queue, IEasyCachingProviderFactory factory, FlowRuleProcessor flowRuleProcessor + , IOptions options, ICapPublisher queue, IEasyCachingProviderFactory factory, FlowRuleProcessorV2 flowRuleProcessor ) { _mcsetting = options.Value.MqttClient; diff --git a/IoTSharp/IoTSharp.xml b/IoTSharp/IoTSharp.xml index 8d6e6248..459ca21f 100644 --- a/IoTSharp/IoTSharp.xml +++ b/IoTSharp/IoTSharp.xml @@ -595,7 +595,7 @@ 创建者(可以是模拟器(测试),可以是设备,在EventType中区分一下) 类型 业务Id(第三方唯一Id,用于取回事件以及记录的标识) - + 返回所有节点的记录信息,需要保存则保存 diff --git a/IoTSharp/Startup.cs b/IoTSharp/Startup.cs index db09c932..77ec5e26 100644 --- a/IoTSharp/Startup.cs +++ b/IoTSharp/Startup.cs @@ -378,7 +378,7 @@ namespace IoTSharp configuration.RootPath = "ClientApp/dist"; }); services.AddScriptEngines(Configuration.GetSection("EngineSetting")); - services.AddTransient(); + services.AddTransient(); } -- GitLab