using Application.Domain.Entities; using Application.Models; using Infrastructure.Data; using Infrastructure.Extensions; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using System; using System.Linq; namespace IoTCenter.Services { public class IoTCenterJob { private readonly IRepository _sceneTimerRepo; private readonly IRepository _sceneTiggerRepo; private readonly IHubContext _hub; public IoTCenterJob(IRepository sceneTimerRepo, IRepository sceneTiggerRepo, IHubContext hub) { this._sceneTimerRepo = sceneTimerRepo; this._sceneTiggerRepo = sceneTiggerRepo; this._hub = hub; } public void TimerHanle(Guid timerId) { Console.WriteLine($"global timer exec:{timerId}"); var timer = this._sceneTimerRepo.ReadOnlyTable() .Include(o => o.Scene).ThenInclude(o => o.SceneCommands).ThenInclude(o => o.Command).ThenInclude(o => o.Device).ThenInclude(o => o.Node) .FirstOrDefault(o => o.Id == timerId); foreach (var sceneCommand in timer.Scene.SceneCommands) { try { this._hub.ServerToClient(Methods.ExecCommand, sceneCommand.CommandId, sceneCommand.Command.Device.Node.Number, null); } catch (Exception ex) { ex.PrintStack(); } } } public void TiggerHandle(Guid tiggerId) { Console.WriteLine($"global tigger exec:{tiggerId}"); var tigger = this._sceneTiggerRepo.ReadOnlyTable() .Include(o => o.Scene).ThenInclude(o => o.SceneCommands).ThenInclude(o => o.Command).ThenInclude(o => o.Device).ThenInclude(o => o.Node) .FirstOrDefault(o => o.Id == tiggerId); foreach (var sceneCommand in tigger.Scene.SceneCommands) { try { this._hub.ServerToClient(Methods.ExecCommand, sceneCommand.CommandId, sceneCommand.Command.Device.Node.Number, null); } catch (Exception ex) { ex.PrintStack(); } } } } }