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 }))
Пример #2
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);
        }