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.
82 lines
3.0 KiB
82 lines
3.0 KiB
using Application.Domain.Entities;
|
|
using CSScriptLib;
|
|
using Hangfire;
|
|
using Infrastructure.Application.Entites.Settings;
|
|
using Infrastructure.Events;
|
|
using Infrastructure.Extensions;
|
|
using IoTNode.DeviceServices.Onvif;
|
|
using System;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace IoTNode.Services
|
|
{
|
|
public class IoTNodeEventHandler :
|
|
IEventHander<EntityUpdatedEvent<Setting>>,
|
|
IEventHander<EntityInsertedEvent<SceneTimer>>,
|
|
IEventHander<EntityUpdatedEvent<SceneTimer>>,
|
|
IEventHander<EntityDeletedEvent<SceneTimer>>,
|
|
IEventHander<EntityUpdatedEvent<Data>>
|
|
{
|
|
private readonly ISceneTiggerService _sceneTiggerService;
|
|
private readonly IoTNodeJob _job;
|
|
private readonly OnvifService _onvifService;
|
|
|
|
public IoTNodeEventHandler(ISceneTiggerService sceneTiggerService, IoTNodeJob job, OnvifService onvifService)
|
|
{
|
|
this._sceneTiggerService = sceneTiggerService;
|
|
this._job = job;
|
|
this._onvifService = onvifService;
|
|
}
|
|
|
|
public void Handle(EntityInsertedEvent<SceneTimer> message)
|
|
{
|
|
RecurringJob.AddOrUpdate<IoTNodeJob>(message.Data.Id.ToString(), o => o.TimerHanle(message.Data.Id), message.Data.Cron, TimeZoneInfo.Local);
|
|
}
|
|
|
|
public void Handle(EntityUpdatedEvent<SceneTimer> message)
|
|
{
|
|
RecurringJob.AddOrUpdate<IoTNodeJob>(message.Data.Id.ToString(), o => o.TimerHanle(message.Data.Id), message.Data.Cron, TimeZoneInfo.Local);
|
|
}
|
|
|
|
public void Handle(EntityDeletedEvent<SceneTimer> message)
|
|
{
|
|
RecurringJob.RemoveIfExists(message.Data.Id.ToString());
|
|
}
|
|
|
|
public void Handle(EntityUpdatedEvent<Data> message)
|
|
{
|
|
var data = message.Data;
|
|
foreach (var tigger in this._sceneTiggerService.GetSceneTiggers())
|
|
{
|
|
if (tigger.DataId == data.Id)
|
|
{
|
|
var methodText = $"bool Valid(string name,string key,{data.Type.ToString().ToLower()} value,string description){{ return {tigger.Condition};}}";
|
|
try
|
|
{
|
|
dynamic method = CSScript.Evaluator.LoadMethod(methodText);
|
|
dynamic value = data.GetValue();
|
|
var result = method.Valid(data.Name, data.Key, value, data.Description);
|
|
if (result)
|
|
{
|
|
_job.TiggerHandle(tigger.Id);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ex.PrintStack();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Handle(EntityUpdatedEvent<Setting> message)
|
|
{
|
|
if (message.Data.Name == "stream.rtmp" || message.Data.Name == "ffmpeg.args")
|
|
{
|
|
Task.Delay(1000);
|
|
this._onvifService.StopPushToServer();
|
|
this._onvifService.StartPushToServer();
|
|
}
|
|
}
|
|
}
|
|
} |