using Application.Domain.Entities; using Infrastructure.Application.Entites.Settings; using Infrastructure.Events; using Infrastructure.Extensions; using Infrastructure.Web.SignalR; using IoT.Shared.Application.Models; using IoT.Shared.Services; using IoTNode.DeviceServices.Onvif; using Microsoft.AspNetCore.SignalR; using Microsoft.Extensions.Logging; using System; using System.Threading.Tasks; namespace IoTNode.Services { public class IoTNodeEventHandler : IEventHander>, IEventHander>, IEventHander>, IEventHander>, IEventHander>, IEventHander>, IEventHander> { private readonly ILogger _logger; private readonly IHubContext _hub; private readonly OnvifService _onvifService; private readonly IoTNodeClient _ioTNodeClient; public IoTNodeEventHandler(ILogger logger, IHubContext hub, OnvifService onvifService, IoTNodeClient ioTNodeClient ) { this._logger = logger; this._hub = hub; this._onvifService = onvifService; this._ioTNodeClient = ioTNodeClient; } public void Handle(EntityUpdatedEvent message) { if (message.Data.Name == "stream.rtmp" || message.Data.Name == "ffmpeg.args") { Task.Delay(1000); this._onvifService.StopPushToServer(); this._onvifService.StartPushToServer(); } else if (message.Data.Name == "notify:host") { this._ioTNodeClient.Close(); this._ioTNodeClient.Connect(); } else if (message.Data.Name == "code") { this._ioTNodeClient.Close(); this._ioTNodeClient.Connect(); } } public void Handle(EntityInsertedEvent message) { this.Notify(message); } public void Handle(EntityUpdatedEvent message) { this.Notify(message); } public void Handle(EntityDeletedEvent message) { this.Notify(message); } public void Handle(EntityInsertedEvent message) { this.Upload(message); } public void Handle(EntityUpdatedEvent message) { this.Upload(message); } public void Handle(EntityDeletedEvent message) { this.Upload(message); } private void Notify(BaseEvent message) { Task.Run(() => { try { _hub.Clients.Group("page").SendAsync(Methods.ServerToClient, $"{typeof(T).Name}{message.Arg}", message.Data.ToJson(true), null,null); } catch (Exception ex) { ex.PrintStack(); } }); } private void Upload(BaseEvent message) { Task.Run(() => { try { this._ioTNodeClient.ClientToServer($"{typeof(T).Name}{message.Arg}",message.Data,null); } catch (Exception ex) { ex.PrintStack(); } }); } } }