using Application.Domain.Entities; using Infrastructure.Data; using IoT.Shared.Services; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Logging; using System; using System.Linq; namespace IoTNode.Services { public class IoTNodeJob { private readonly ILogger _logger; private readonly IRepository _sceneTimerRepo; private readonly IRepository _sceneTiggerRepo; private readonly IoTNodeClient _client; public IoTNodeJob(ILogger logger, IRepository sceneTimerRepo, IRepository sceneTiggerRepo, IoTNodeClient client) { this._logger = logger; this._sceneTimerRepo = sceneTimerRepo; this._sceneTiggerRepo = sceneTiggerRepo; this._client = client; } public void TimerHanle(Guid timerId) { this._logger.LogDebug($"node timer exec:{timerId}"); var commands = this._sceneTimerRepo.ReadOnlyTable() .Include(o => o.Scene).ThenInclude(o => o.SceneCommands).ThenInclude(o => o.Command).ThenInclude(o => o.Api) .Include(o => o.Scene).ThenInclude(o => o.SceneCommands).ThenInclude(o => o.Command).ThenInclude(o => o.Device) .Where(o => o.Id == timerId) .SelectMany(o => o.Scene.SceneCommands) .Select(o => o.Command) .ToList(); this._client.ExecCommands(commands); } public void TiggerHandle(Guid tiggerId) { this._logger.LogDebug($"node tigger exec:{tiggerId}"); var commands = this._sceneTiggerRepo.ReadOnlyTable() .Include(o => o.Scene).ThenInclude(o => o.SceneCommands).ThenInclude(o => o.Command).ThenInclude(o => o.Api) .Include(o => o.Scene).ThenInclude(o => o.SceneCommands).ThenInclude(o => o.Command).ThenInclude(o => o.Device) .Where(o => o.Id == tiggerId) .SelectMany(o => o.Scene.SceneCommands) .Select(o => o.Command) .ToList(); this._client.ExecCommands(commands); } } }