SafeDispatch/SocketIOComponent/SocketIOClass.cs

348 lines
11 KiB
C#
Raw Normal View History

2024-02-22 16:43:59 +00:00
using Newtonsoft.Json;
using Quobject.SocketIoClientDotNet.Client;
using System;
namespace SocketIOComponent
{
/// <summary>
/// Class responsible for sending and receiving socket.IO messages
/// </summary>
public class SocketIOClass
{
#region Fields
Socket _socket = null;
#endregion
#region Public properties
/// <summary>
/// Returns the socket.IO uri in the form: ws://IP:SocketIOport
/// </summary>
public string Uri { get; private set; }
#endregion
/// <summary>
/// Constructor for the SocketIOClass
/// </summary>
/// <param name="uri">Socket io uri in the format: ws://IP:SocketIOport</param>
public SocketIOClass(string uri)
{
_socket = IO.Socket(uri);
Uri = uri;
_socket.On(SocketIOEvent.PrivateCallRequest.ToString(), (data) =>
{
PrivateCallInfo callInfo = null;
try
{
callInfo = JsonConvert.DeserializeObject<PrivateCallInfo>(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive private call request:\n" + ex.ToString());
}
if (callInfo != null)
OnPrivateCallRequestReceived(new PrivateCallRequestReceivedEventArgs(callInfo));
});
_socket.On(SocketIOEvent.GroupCallRequest.ToString(), (data) =>
{
GroupCallInfo callInfo = null;
try
{
callInfo = JsonConvert.DeserializeObject<GroupCallInfo>(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive group call request:\n" + ex.ToString());
}
if (callInfo != null)
OnGroupCallRequestReceived(new GroupCallRequestReceivedEventArgs(callInfo));
});
_socket.On(SocketIOEvent.Sms.ToString(), (data) =>
{
SmsInfo smsInfo = null;
try
{
smsInfo = JsonConvert.DeserializeObject<SmsInfo>(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive sms:\n" + ex.ToString());
}
if (smsInfo != null)
OnSmsReceived(new SmsReceivedEventArgs(smsInfo));
});
_socket.On(SocketIOEvent.SmsAck.ToString(), (data) =>
{
SmsAckInfo smsAckInfo = null;
try
{
smsAckInfo = JsonConvert.DeserializeObject<SmsAckInfo>(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive sms ack:\n" + ex.ToString());
}
if (smsAckInfo != null)
OnSmsAckReceived(new SmsAckReceivedEventArgs(smsAckInfo));
});
_socket.On(SocketIOEvent.Ars.ToString(), (data) =>
{
ArsInfo ars = null;
try
{
ars = JsonConvert.DeserializeObject<ArsInfo>(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive ars:\n" + ex.ToString());
}
if (ars != null)
OnArsReceived(new ArsReceivedEventArgs(ars));
});
_socket.On(SocketIOEvent.EmergencyReq.ToString(), (data) =>
{
EmergencyInfo emerg = null;
try
{
emerg = JsonConvert.DeserializeObject<EmergencyInfo>(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive emergency request:\n" + ex.ToString());
}
if (emerg != null)
OnEmergencyReceived(new EmergencyReceivedEventArgs(emerg));
});
_socket.On(SocketIOEvent.Gps.ToString(), (data) =>
{
GpsInfo gps = null;
try
{
gps = JsonConvert.DeserializeObject<GpsInfo>(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive gps:\n" + ex.ToString());
}
if (gps != null)
OnGpsReceived(new GpsReceivedEventArgs(gps));
});
_socket.On(SocketIOEvent.PollResponse.ToString(), (data) =>
{
GpsInfo gps = null;
try
{
gps = JsonConvert.DeserializeObject<GpsInfo>(data.ToString());
}
catch (Exception ex)
{
Console.WriteLine("SocketIOClass Error on receive poll response:\n" + ex.ToString());
}
if (gps != null)
OnPollResponseReceived(new GpsReceivedEventArgs(gps));
});
}
#region Public Methods
/// <summary>
/// Sends private call request
/// </summary>
/// <param name="callInfo"></param>
public void SendPrivateCallRequest(PrivateCallInfo callInfo)
{
_socket?.Emit(SocketIOEvent.PrivateCallRequest.ToString(), JsonConvert.SerializeObject(callInfo));
//Console.WriteLine("----> " + callInfo.ToString());
}
/// <summary>
/// Sends group call request
/// </summary>
/// <param name="callInfo">Call info for Socket.IO</param>
public void SendGroupCallRequest(GroupCallInfo callInfo)
{
_socket?.Emit(SocketIOEvent.GroupCallRequest.ToString(), JsonConvert.SerializeObject(callInfo));
//Console.WriteLine("----> " + callInfo.ToString());
}
/// <summary>
/// Sends an sms request on Socket.IO
/// </summary>
/// <param name="sms">Sms data for Socket.IO</param>
public void SendSms(SmsInfo sms)
{
_socket?.Emit(SocketIOEvent.Sms.ToString(), JsonConvert.SerializeObject(sms));
}
/// <summary>
/// Sends an sms ack on Socket.IO
/// </summary>
/// <param name="smsAck"></param>
public void SendSmsAck(SmsAckInfo smsAck)
{
_socket?.Emit(SocketIOEvent.SmsAck.ToString(), JsonConvert.SerializeObject(smsAck));
}
/// <summary>
/// Sends ARS on Socket.IO
/// </summary>
/// <param name="arsInfo">ARS data for SocketIO</param>
public void SendArs(ArsInfo arsInfo)
{
_socket?.Emit(SocketIOEvent.Ars.ToString(), JsonConvert.SerializeObject(arsInfo));
}
/// <summary>
/// Sends emergency ack request
/// </summary>
/// <param name="emergencyAckInfo">Object containing emergency ack parameters</param>
public void SendEmergencyAck(EmergencyAckInfo emergencyAckInfo)
{
_socket?.Emit(SocketIOEvent.EmergencyAck.ToString(), JsonConvert.SerializeObject(emergencyAckInfo));
}
/// <summary>
/// Sends poll request
/// </summary>
/// <param name="pollRequestInfo">Object containing poll request parameters</param>
public void SendPollRequest(PollRequestInfo pollRequestInfo)
{
_socket?.Emit(SocketIOEvent.PollReq.ToString(), JsonConvert.SerializeObject(pollRequestInfo));
}
/// <summary>
/// Disconnects the soket.IO client
/// </summary>
public void Disconect()
{
_socket?.Disconnect();
}
#endregion
#region Events
/// <summary>
/// Occurs when a private call request is received on socket.IO
/// </summary>
public event EventHandler<PrivateCallRequestReceivedEventArgs> PrivateCallRequestReceived;
/// <summary>
/// Occurs when a group call request is received on socket.IO
/// </summary>
public event EventHandler<GroupCallRequestReceivedEventArgs> GroupCallRequestReceived;
/// <summary>
/// Occurs when a sms is received on socket.IO
/// </summary>
public event EventHandler<SmsReceivedEventArgs> SmsReceived;
/// <summary>
/// Occurs when a sms ack is received on socket.IO
/// </summary>
public event EventHandler<SmsAckReceivedEventArgs> SmsAckReceived;
/// <summary>
/// Occurs when an Ars is received on socket.IO
/// </summary>
public event EventHandler<ArsReceivedEventArgs> ArsReceived;
/// <summary>
/// Occurs when a Emergency is received on socket.IO
/// </summary>
public event EventHandler<EmergencyReceivedEventArgs> EmergencyReceived;
/// <summary>
/// Occurs when a GPS Position is received on socket.IO
/// </summary>
public event EventHandler<GpsReceivedEventArgs> GpsReceived;
/// <summary>
/// Occurs when a Poll response is received on socket.IO
/// </summary>
public event EventHandler<GpsReceivedEventArgs> PollReceived;
private void OnPrivateCallRequestReceived(PrivateCallRequestReceivedEventArgs e)
{
EventHandler<PrivateCallRequestReceivedEventArgs> handler = PrivateCallRequestReceived;
if (handler != null)
handler(this, e);
}
private void OnGroupCallRequestReceived(GroupCallRequestReceivedEventArgs e)
{
EventHandler<GroupCallRequestReceivedEventArgs> handler = GroupCallRequestReceived;
if (handler != null)
handler(this, e);
}
private void OnSmsReceived(SmsReceivedEventArgs e)
{
EventHandler<SmsReceivedEventArgs> handler = SmsReceived;
if (handler != null)
handler(this, e);
}
private void OnSmsAckReceived(SmsAckReceivedEventArgs e)
{
EventHandler<SmsAckReceivedEventArgs> handler = SmsAckReceived;
if (handler != null)
handler(this, e);
}
private void OnArsReceived(ArsReceivedEventArgs e)
{
EventHandler<ArsReceivedEventArgs> handler = ArsReceived;
if (handler != null)
handler(this, e);
}
private void OnEmergencyReceived(EmergencyReceivedEventArgs e)
{
EventHandler<EmergencyReceivedEventArgs> handler = EmergencyReceived;
if (handler != null)
handler(this, e);
}
private void OnGpsReceived(GpsReceivedEventArgs e)
{
EventHandler<GpsReceivedEventArgs> handler = GpsReceived;
if (handler != null)
handler(this, e);
}
private void OnPollResponseReceived(GpsReceivedEventArgs e)
{
EventHandler<GpsReceivedEventArgs> handler = PollReceived;
if (handler != null)
handler(this, e);
}
#endregion
}
}