using Application.Domain.Entities; using Application.Models; using Infrastructure.Data; using Infrastructure.Extensions; using IoT.Shared.Services; using Microsoft.AspNetCore.SignalR; using Microsoft.EntityFrameworkCore; using System; using System.Linq; namespace IoTCenter.Services { public class IoTCenterJob { private readonly IRepository _timerCommandRepo; private readonly IRepository _tiggerCommandRepo; private readonly IHubContext _hub; public IoTCenterJob(IRepository timerCommandRepo, IRepository tiggerCommandRepo, IHubContext hub) { this._timerCommandRepo = timerCommandRepo; this._tiggerCommandRepo = tiggerCommandRepo; this._hub = hub; } public void TimerHanle(Guid timerId) { var commands = this._timerCommandRepo.ReadOnlyTable() .Include(o => o.Command) .ThenInclude(o => o.Device) .ThenInclude(o => o.Node) .Where(o => o.IoTTimerId == timerId) .Select(o => o.Command) .ToList(); foreach (var command in commands) { try { this._hub.ServerToClient(Methods.ExecCommand, command.Id, command.Device.Node.Number, null); } catch (Exception ex) { ex.PrintStack(); } } } public void TiggerHandle(Guid tiggerId) { var commands = this._tiggerCommandRepo.ReadOnlyTable() .Include(o => o.Command) .ThenInclude(o => o.Device) .ThenInclude(o => o.Node) .Where(o => o.IoTTiggerId == tiggerId) .Select(o => o.Command) .ToList(); foreach (var command in commands) { try { this._hub.ServerToClient(Methods.ExecCommand, command.Id, command.Device.Node.Number, null); } catch (Exception ex) { ex.PrintStack(); } } } } }