using Microsoft.Extensions.Hosting; using Quobject.SocketIoClientDotNet.Client; using Sentry; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks; using System; namespace BMA.EHR.Insignia.Service.Services; public class InsigniaRequestProcessService : BackgroundService { private readonly IBackgroundTaskQueue _queue; private Socket _socket; private bool _isConnected = false; public InsigniaRequestProcessService(IBackgroundTaskQueue queue) { _queue = queue; } public override Task StartAsync(CancellationToken cancellationToken) { try { _socket = IO.Socket("https://bma-ehr.frappet.synology.me", new IO.Options { Path = "/api/v1/org-socket", //Query = new Dictionary //{ // { "EIO", "4" }, // { "transport", "polling" }, // { "t", "tkitfptn" } //} }); _socket.On(Socket.EVENT_CONNECT, () => { _isConnected = true; Console.WriteLine("Connected to WebSocket server at: https://bma-ehr.frappet.synology.me/api/v1/org-socket"); }); _socket.On(Socket.EVENT_DISCONNECT, () => { _isConnected = false; Console.WriteLine("Disconnected from WebSocket server"); }); _socket.On(Socket.EVENT_ERROR, (data) => { _isConnected = false; Console.WriteLine($"WebSocket error: {data}"); }); _socket.Connect(); } catch (Exception ex) { _isConnected = false; Console.WriteLine($"Failed to initialize WebSocket connection: {ex.Message}"); } return base.StartAsync(cancellationToken); } protected override async Task ExecuteAsync(CancellationToken stoppingToken) { var userId = "4064c2b2-0414-464a-97c6-4a47c325b9a3"; while (!stoppingToken.IsCancellationRequested) { try { var workItem = await _queue.DequeueAsync(stoppingToken); if (workItem != null) { Console.WriteLine($"Starting background task at: {DateTime.Now}"); await workItem(stoppingToken); Console.WriteLine($"Finished background task at: {DateTime.Now}"); // Send notification to WebSocket after task completion if (_socket != null && _isConnected) { _socket.Emit("send-command-notification", new { success = true, message = "Background Task Completed Successfully", payload = new { completedAt = DateTime.Now, taskType = "background_processing" } }, new { userId = userId }); Console.WriteLine("WebSocket notification sent successfully"); } else { Console.WriteLine("WebSocket is not connected. Unable to send notification."); } } } catch (Exception ex) { Console.WriteLine($"Error processing background task: {ex.Message}"); // Send error notification to WebSocket if (_socket != null && _isConnected) { _socket.Emit("send-command-notification", new { success = false, message = "Background Task Failed", payload = new { error = ex.Message, failedAt = DateTime.Now, taskType = "background_processing" } }, new { userId = userId }); } } } } public override Task StopAsync(CancellationToken cancellationToken) { try { if (_socket != null) { Console.WriteLine("Disconnecting from WebSocket server..."); _socket.Disconnect(); _isConnected = false; _socket = null; } } catch (Exception ex) { Console.WriteLine($"Error disconnecting WebSocket: {ex.Message}"); } return base.StopAsync(cancellationToken); } }