|
|
|
@ -1,5 +1,6 @@
|
|
|
|
|
using Application.Domain.Entities;
|
|
|
|
|
using Application.Models;
|
|
|
|
|
using Hangfire;
|
|
|
|
|
using Infrastructure.Application.Services.Settings;
|
|
|
|
|
using Infrastructure.Data;
|
|
|
|
|
using Infrastructure.Events;
|
|
|
|
@ -11,12 +12,10 @@ using Microsoft.AspNetCore.SignalR;
|
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Net.Http;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Vibrant.InfluxDB.Client;
|
|
|
|
@ -62,6 +61,7 @@ namespace IoTCenter.Services
|
|
|
|
|
private readonly IRepository<Organ> _organRepo;
|
|
|
|
|
private readonly IRepository<OrganNode> _organNodeRepo;
|
|
|
|
|
private readonly IRepository<Device> _deviceRepo;
|
|
|
|
|
private readonly IRepository<OrganSceneTimer> _organSceneTimerRepo;
|
|
|
|
|
private readonly IRepository<OrganSceneTigger> _sceneTiggerRepo;
|
|
|
|
|
private readonly ISceneTiggerService _sceneTiggerService;
|
|
|
|
|
private readonly IHubContext<IoTCenterHub> _hub;
|
|
|
|
@ -76,6 +76,7 @@ namespace IoTCenter.Services
|
|
|
|
|
IRepository<Organ> organRepo,
|
|
|
|
|
IRepository<OrganNode> organNodeRepo,
|
|
|
|
|
IRepository<Device> deviceRepo,
|
|
|
|
|
IRepository<OrganSceneTimer> organSceneTimerRepo,
|
|
|
|
|
IRepository<OrganSceneTigger> sceneTiggerRepo,
|
|
|
|
|
ISceneTiggerService sceneTiggerService,
|
|
|
|
|
IHubContext<IoTCenterHub> hub,
|
|
|
|
@ -90,6 +91,7 @@ namespace IoTCenter.Services
|
|
|
|
|
this._organRepo = organRepo;
|
|
|
|
|
this._organNodeRepo = organNodeRepo;
|
|
|
|
|
this._deviceRepo = deviceRepo;
|
|
|
|
|
this._organSceneTimerRepo = organSceneTimerRepo;
|
|
|
|
|
this._sceneTiggerRepo = sceneTiggerRepo;
|
|
|
|
|
this._sceneTiggerService = sceneTiggerService;
|
|
|
|
|
this._hub = hub;
|
|
|
|
@ -101,34 +103,19 @@ namespace IoTCenter.Services
|
|
|
|
|
public void Handle(EntityInsertedEvent<OrganSceneTimer> message)
|
|
|
|
|
{
|
|
|
|
|
var timer = message.Data;
|
|
|
|
|
var url = _cfg.GetConnectionString("JobServer") + "/RecurringJob/AddOrUpdate";
|
|
|
|
|
var id = timer.Id.ToString();
|
|
|
|
|
this.UpdateJobServer(url, new
|
|
|
|
|
{
|
|
|
|
|
id,
|
|
|
|
|
url = $"{_cfg.GetConnectionString("JobCallBack")}/{id}",
|
|
|
|
|
cron = timer.Cron
|
|
|
|
|
});
|
|
|
|
|
RecurringJob.AddOrUpdate(timer.Id.ToString(), () => Handle(timer.Id), timer.Cron, TimeZoneInfo.Local);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(EntityUpdatedEvent<OrganSceneTimer> message)
|
|
|
|
|
{
|
|
|
|
|
var timer = message.Data;
|
|
|
|
|
var url = _cfg.GetConnectionString("JobServer") + "/RecurringJob/AddOrUpdate";
|
|
|
|
|
var id = timer.Id.ToString();
|
|
|
|
|
this.UpdateJobServer(url, new
|
|
|
|
|
{
|
|
|
|
|
id,
|
|
|
|
|
url = $"{_cfg.GetConnectionString("JobCallBack")}/{id}",
|
|
|
|
|
cron = timer.Cron
|
|
|
|
|
});
|
|
|
|
|
RecurringJob.AddOrUpdate(timer.Id.ToString(), () => Handle(timer.Id), timer.Cron, TimeZoneInfo.Local);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(EntityDeletedEvent<OrganSceneTimer> message)
|
|
|
|
|
{
|
|
|
|
|
var timer = message.Data;
|
|
|
|
|
var url = _cfg.GetConnectionString("JobServer") + $"/RecurringJob/Remove/{timer.Id}";
|
|
|
|
|
this.UpdateJobServer(url);
|
|
|
|
|
RecurringJob.RemoveIfExists(timer.Id.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion timer
|
|
|
|
@ -290,23 +277,6 @@ namespace IoTCenter.Services
|
|
|
|
|
|
|
|
|
|
#endregion Scene
|
|
|
|
|
|
|
|
|
|
private void UpdateJobServer(string url, object data = null)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this._logger.LogDebug($"update job server");
|
|
|
|
|
var client = _httpClientFactory.CreateClient();
|
|
|
|
|
using var content = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
|
|
|
|
|
var result = client.PostAsync(url, content).Result;
|
|
|
|
|
this._logger.LogDebug($"{result.StatusCode}:{result.Content.ReadAsStringAsync().Result}");
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
ex.PrintStack();
|
|
|
|
|
this._logger.LogError(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void TiggerHandle(BaseEvent<Data> message)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
@ -366,7 +336,7 @@ namespace IoTCenter.Services
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
this._hub.ServerToClient(Methods.ExecCommand, sceneCommand.CommandId, sceneCommand.Command.Device.Node.Number, null);
|
|
|
|
|
Delay(sceneCommand.Command.Delay);
|
|
|
|
|
Delay(_settingService, sceneCommand.Command.Delay);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
@ -444,27 +414,6 @@ namespace IoTCenter.Services
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Delay(int commandDelay)
|
|
|
|
|
{
|
|
|
|
|
var delay = 0;
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
delay = Convert.ToInt32(this._settingService.GetSetting("delay").Value);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
this._logger.LogError(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
if (commandDelay > 0)
|
|
|
|
|
{
|
|
|
|
|
delay += commandDelay;
|
|
|
|
|
}
|
|
|
|
|
if (delay > 0)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(delay);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(NodeClientConnectedEvent message)
|
|
|
|
|
{
|
|
|
|
|
var organ = this._organRepo.Table().FirstOrDefault(o => o.Number == message.OrganNumber);
|
|
|
|
@ -489,5 +438,59 @@ namespace IoTCenter.Services
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Handle(Guid id)
|
|
|
|
|
{
|
|
|
|
|
GlobalTimerExec(_logger, _settingService, _organSceneTimerRepo, _hub, id);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void GlobalTimerExec(ILogger _logger, ISettingService _settingService, IRepository<OrganSceneTimer> _organSceneTimerRepo, IHubContext<IoTCenterHub> _hub, Guid id)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_logger.LogInformation($"global timer exec at {DateTime.Now.ToString("G")}:{id}");
|
|
|
|
|
var timer = _organSceneTimerRepo.ReadOnlyTable()
|
|
|
|
|
.Include(o => o.OrganScene).ThenInclude(o => o.OrganSceneCommands).ThenInclude(o => o.Command).ThenInclude(o => o.Device).ThenInclude(o => o.Node)
|
|
|
|
|
.FirstOrDefault(o => o.Id == id);
|
|
|
|
|
if (timer != null)
|
|
|
|
|
{
|
|
|
|
|
foreach (var sceneCommand in timer.OrganScene.OrganSceneCommands.OrderBy(o => o.Command.DisplayOrder))
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
_hub.ServerToClient(Methods.ExecCommand, sceneCommand.CommandId, sceneCommand.Command.Device.Node.Number, null);
|
|
|
|
|
Delay(_settingService, sceneCommand.Command.Delay);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError($"timer {id} does not exist");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
_logger.LogError(ex.ToString());
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Delay(ISettingService _settingService, int delay)
|
|
|
|
|
{
|
|
|
|
|
if (int.TryParse(_settingService.GetSetting("delay").Value, out int commandDelay))
|
|
|
|
|
{
|
|
|
|
|
if (commandDelay > 0)
|
|
|
|
|
{
|
|
|
|
|
delay += commandDelay;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (delay > 0)
|
|
|
|
|
{
|
|
|
|
|
Thread.Sleep(delay);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|