Example #1
0
        public async Task <bool> Execute(long ruleId, RuleNode rule, RuleOutputAction outputAction,
                                         CancellationToken cancellationToken)
        {
            try
            {
                bool ruleCalculationResult = await VisitNode(rule, cancellationToken);

                if (ruleCalculationResult == false)
                {
                    _logger.LogInformation($"[RuleEngine] Rule with id: {ruleId} ignored");
                    return(false);
                }

                var  ruleOutputActionExecutor    = _outputActionExecutors.Single(x => x.OutputActionId == outputAction.Id);
                bool outputActionExecutionResult =
                    await ruleOutputActionExecutor.Execute(ruleId, outputAction, cancellationToken);

                if (outputActionExecutionResult)
                {
                    _logger.LogInformation($"[RuleEngine] Rule with id: {ruleId} executed");
                    await _eventBus.Publish(new RuleExecutionResultEvent
                    {
                        ExecutionStatus = RuleExecutionStatus.Success,
                        RuleId          = ruleId,
                        Timestamp       = _dateTimeProvider.GetUtcNow()
                    });
                }
                else
                {
                    _logger.LogInformation($"[RuleEngine] Rule with id: {ruleId} ignored");
                }

                return(outputActionExecutionResult);
            }
            catch (Exception e)
            {
                _logger.LogError(e, $"[RuleEngine] Error during executing rule with id: {ruleId}");
                await _eventBus.Publish(new RuleExecutionResultEvent
                {
                    ExecutionStatus = RuleExecutionStatus.Failed,
                    ErrorMessage    = e.Message,
                    RuleId          = ruleId,
                    Timestamp       = _dateTimeProvider.GetUtcNow()
                });

                return(false);
            }
        }
        public async Task <bool> Execute(long ruleId, RuleOutputAction outputAction, CancellationToken cancellationToken)
        {
            var currentTime = _dateTimeProvider.GetUtcNow();

            var lastRuleExecution = await _dbContext.RulesExecutionHistory
                                    .Where(x => x.Rule.Id == ruleId)
                                    .OrderByDescending(x => x.Timestamp)
                                    .Select(x => x.Timestamp)
                                    .FirstOrDefaultAsync(cancellationToken);

            if (lastRuleExecution != default && (currentTime - lastRuleExecution).TotalMinutes <= 35)
            {
                return(false);
            }

            var windowsStatusEvt = await _eventStoreClient.FindEventsByCriteriaAsync(new GetEventsQuery
            {
                Source     = DeviceIds.WindowsController,
                EventName  = nameof(WindowsControllerTelemetryEvent),
                PageNumber = 1,
                PageSize   = 1,
                From       = _dateTimeProvider.GetUtcNow().AddDays(-1)
            }, cancellationToken);

            if (windowsStatusEvt.ResultTotalCount == 0)
            {
                return(false);
            }

            var  lastEvent       = windowsStatusEvt.Result.First();
            var  evt             = lastEvent.EventData as WindowsControllerTelemetryEvent;
            bool isWindow1Opened = evt !.WindowsStatus[0];
            bool isWindow2Opened = evt.WindowsStatus[1];

            var commands         = outputAction.Commands.Select(x => JsonSerializerHelpers.DeserializeFromObject <CloseWindowCommand>(x)).ToList();
            int executedCommands = 0;

            if (isWindow1Opened && commands.Any(x => x is { WindowId: 0 }))
Example #3
0
        public async Task <bool> Execute(long ruleId, RuleOutputAction outputAction, CancellationToken cancellationToken)
        {
            var currentTime = _dateTimeProvider.GetUtcNow();

            var lastRuleExecution = await _dbContext.RulesExecutionHistory
                                    .Where(x => x.Rule.Id == ruleId)
                                    .OrderByDescending(x => x.Timestamp)
                                    .Select(x => x.Timestamp)
                                    .FirstOrDefaultAsync(cancellationToken);

            if (lastRuleExecution != default && (currentTime - lastRuleExecution).TotalHours < 3)
            {
                return(false);
            }

            foreach (var irrigateCommand in outputAction.Commands.Select(x => JsonSerializerHelpers.DeserializeFromObject <IrrigateCommand>(x))
                     .Where(x => x != null))
            {
                await _mediator.Send(irrigateCommand !, cancellationToken);
            }

            return(true);
        }