You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
iot/projects/IoTCenter/Services/IoTCenterJob.cs

64 lines
2.3 KiB

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<SceneTimer> _sceneTimerRepo;
private readonly IRepository<SceneTigger> _sceneTiggerRepo;
private readonly IHubContext<IoTCenterHub> _hub;
public IoTCenterJob(IRepository<SceneTimer> sceneTimerRepo, IRepository<SceneTigger> sceneTiggerRepo, IHubContext<IoTCenterHub> 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();
}
}
}
}
}