SafeDispatch/MotoRepeaterCore/DataBaseSD.cs

353 lines
13 KiB
C#
Raw Permalink Normal View History

2024-02-22 16:43:59 +00:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SafeMobileLib;
namespace MotoRepeater
{
public class DataBaseSD : DataBaseInterface
{
private MessageBus mBus = null;
private DBvehiclesManager vehiclesManager;
/// <summary>
/// Default Constructor
/// </summary>
public DataBaseSD()
{
// create the Message Bus
mBus = new MessageBus(MotoRepeater_GW.cfg.MBUS_IP, MotoRepeater_GW.cfg.MBUS_PORT);
vehiclesManager = new DBvehiclesManager(MotoRepeater_GW.cfg.DB_SERVER, MotoRepeater_GW.cfg.DB_DATABASE,
MotoRepeater_GW.cfg.DB_USERNAME, MotoRepeater_GW.cfg.DB_PASSWORD, MotoRepeater_GW.cfg.DB_PORT);
// add event listener for when a Poll request is received
mBus.OnPollRequest += delegate(Int64 radioID)
{
PollRequestEventArgs e = new PollRequestEventArgs();
e.RadioID = radioID+"";
e.SeqID = 0;
// raise the event
OnPollRequestReceived(null, e);
};
// add event listener for when a radio needs to be enabled/disabled
mBus.OnRadioEnableDisableRequest += delegate(Int64 radioID, bool enable, Wireline.SlotNumber slot)
{
RadioEnableDisableRequestEventArgs e = new RadioEnableDisableRequestEventArgs();
e.Enabled = enable;
e.RadioID = radioID;
e.Slot = slot;
// raise the event
OnRadioEnableDisableRequest(null, e);
};
mBus.OnEmergencyAckRequest += delegate(Int64 radioID, Wireline.SlotNumber slot)
{
EmergencyAckEventArgs e = new EmergencyAckEventArgs();
e.RadioID = radioID;
e.Slot = slot;
// raise the event
OnEmergencyAckRequest(null, e);
};
mBus.OnChannelQueryRequest += delegate(Int64 radioGWID)
{
ChannelQueryEventArgs e = new ChannelQueryEventArgs();
e.RadioGwID = radioGWID;
// raise the event
OnChannelQuery(null, e);
};
mBus.OnSMSRequestReceived += delegate(String radioID, String message, Boolean isAckWanted, int seqID)
{
SMSRequestEventArgs e = new SMSRequestEventArgs();
e.RadioID = radioID;
e.message = message;
e.isACKWanted = isAckWanted;
e.seqID = seqID;
// raise the event
if (OnSMSRequestReceived != null)
OnSMSRequestReceived(null, e);
};
mBus.OnGroupSMSRequestReceived += delegate(String groupID, String message, Boolean isAckWanted, int seqID)
{
SMSRequestEventArgs e = new SMSRequestEventArgs();
e.GroupID = groupID;
e.message = message;
e.isACKWanted = isAckWanted;
e.seqID = seqID;
// raise the event
OnGroupSMSRequestReceived(null, e);
};
mBus.OnTelemetryRequestReceived += delegate(String radioID, String GPIO, String type, int seqID)
{
TelemetryRequestEventArgs e = new TelemetryRequestEventArgs();
e.RadioID = radioID;
e.GPIO = GPIO;
e.Type = type;
e.seqID = seqID;
// raise the event
OnTelemetryRequestReceived(null, e);
};
//mBus.OnS
#region PRIVATECALL
// intercept events for Private Call
mBus.OnInitPrivateCallRequest += delegate(Int64 radioID, string broadcastAddress, Wireline.SlotNumber slot)
{
this.OnInitPrivateCallRequest(radioID, broadcastAddress, slot);
};
mBus.OnEndPrivateCallRequest += delegate(Int64 radioID, string broadcastAddress, Wireline.SlotNumber slot)
{
this.OnEndPrivateCallRequest(radioID, broadcastAddress, slot);
};
#endregion
#region GROUPCALL
// intercept events for Group Call
mBus.OnInitGroupCallRequest += delegate(Int64 groupID, string broadcastAddress, Wireline.SlotNumber slot)
{
this.OnInitGroupCallRequest(groupID, broadcastAddress, slot);
};
mBus.OnEndGroupCallRequest += delegate(Int64 groupID, string broadcastAddress, Wireline.SlotNumber slot)
{
this.OnEndGroupCallRequest(groupID, broadcastAddress, slot);
};
#endregion
#region ALLCALL
// intercept events for All Call
mBus.OnInitAllCallRequest += delegate(string broadcastAddress, Wireline.SlotNumber slot)
{
this.OnInitAllCallRequest(broadcastAddress, slot);
};
mBus.OnEndAllCallRequest += delegate(string broadcastAddress, Wireline.SlotNumber slot)
{
this.OnEndAllCallRequest(broadcastAddress, slot);
};
#endregion
}
public void PollResponseReceived(long radioID, DateTime locationTime, int speed, double latitude, double longitude)
{
mBus.SendLocation(radioID, locationTime, speed, latitude, longitude, true);
}
public void PollResponseReceived(long radioID, DateTime locationTime, int speed, double latitude, double longitude, long seqID)
{
mBus.SendLocation(radioID, locationTime, speed, latitude, longitude, true);
}
public void LocationReceived(LocationEventArgs e)
{
mBus.SendLocation(e.RadioID, Utils.UnixTimeStampToDateTime(e.GPSTime), (int)e.Speed, e.Latitude, e.Longitude, false);
}
public void LocationResponseReceived(long radioID, DateTime locationTime, int speed, double latitude, double longitude)
{
mBus.SendLocation(radioID, locationTime, speed, latitude, longitude, false);
}
public void ARSStateReceived(long radioID, bool isON)
{
mBus.SendARS(radioID, isON ? ARSStatus.ON : ARSStatus.OFF);
if (isON)
mBus.SendAssignedGwForRadio(radioID);
}
public void SMSReceived(long radioID, byte[] received)
{
mBus.SendSMS(radioID, received);
}
public void SMSReceived(long radioID, string message)
{
mBus.SendSMS(radioID, message);
}
public void SMSAckReceived(byte[] received, string seq_no)
{
mBus.SendOnMsgBuss(received);
}
public void SMSAckReceived(byte[] received)
{
mBus.SendOnMsgBuss(received);
}
public void TelemetryReceived(long radioID, byte[] received)
{
mBus.SendTelemetry(radioID, received);
}
public void TelemetryReceived(TelemetryReceivedEventArgs e)
{
mBus.SendParsedTelemetry(e.RadioID, e.GPIO);
}
public void RadioEnableDisableStatusReceived(long radioID, Wireline.SlotNumber slot, bool isEnabled)
{
mBus.RadioEnableDisableStatusBroadcast(radioID, isEnabled);
}
public void EmergencyReceived(long radioID, Wireline.SlotNumber slot)
{
mBus.SendEmergencyReceived(radioID);
}
public void CallStatusBroadcastReceived(long sourceID, long targertID, CallType callType, CallStatus callStatus, Wireline.SlotNumber slot)
{
mBus.CallStatusBroadcast(sourceID, targertID, (int)callType, (int)callStatus, slot);
}
public void TallysmanEventReceived(TallysmanEventArgs tallysmanArgs)
{
// this has nothing to do with SafeDispatch, only with stupid SafeNet
}
public void SendChannelBroadcastMessage(long radioGWID)
{
mBus.sendChannelBroadcastMessage(radioGWID);
}
/// <summary>
/// Get the reporting interval from the Database for a particular unit
/// </summary>
/// <param name="radioID">Radio Id of the unit for which </param>
/// <returns></returns>
public int GetReportingIntervalForUnit(string radioID)
{
int reportInterval = 60;
reportInterval = vehiclesManager.getReportingInterval(radioID);
return reportInterval;
}
public bool UnitIsAssignedToGw(string radioID)
{
return true;
}
public void Stop()
{
}
public delegate void InitPrivateCallRequest(Int64 radioID, String broadcastAddress, Wireline.SlotNumber slot);
public event InitPrivateCallRequest OnInitPrivateCallRequest;
public delegate void EndPrivateCallRequest(Int64 radioID, String broadcastAddress, Wireline.SlotNumber slot);
public event EndPrivateCallRequest OnEndPrivateCallRequest;
public delegate void InitGroupCallRequest(Int64 groupID, String broadcastAddress, Wireline.SlotNumber slot);
public event InitGroupCallRequest OnInitGroupCallRequest;
public delegate void EndGroupCallRequest(Int64 groupID, String broadcastAddress, Wireline.SlotNumber slot);
public event EndGroupCallRequest OnEndGroupCallRequest;
public delegate void InitAllCallRequest(String broadcastAddress, Wireline.SlotNumber slot);
public event InitAllCallRequest OnInitAllCallRequest;
public delegate void EndAllCallRequest(String broadcastAddress, Wireline.SlotNumber slot);
public event EndAllCallRequest OnEndAllCallRequest;
#region POLL EVENT
private object _lockPoll = new object();
public event EventHandler<PollRequestEventArgs> OnPollRequestReceived;
event EventHandler<PollRequestEventArgs> DataBaseInterface.OnPollRequestReceived
{
add { lock (_lockPoll) { OnPollRequestReceived += value; } }
remove { lock (_lockPoll) { OnPollRequestReceived -= value; } }
}
#endregion
#region RADIO ENABLE DISABLE EVENT
private object _lockRadioED = new object();
public event EventHandler<RadioEnableDisableRequestEventArgs> OnRadioEnableDisableRequest;
event EventHandler<RadioEnableDisableRequestEventArgs> DataBaseInterface.OnRadioEnableDisableRequest
{
add { lock (_lockRadioED) { OnRadioEnableDisableRequest += value; } }
remove { lock (_lockRadioED) { OnRadioEnableDisableRequest -= value; } }
}
#endregion
#region EMERGENCY ACK EVENT
private object _lockEmergencyAck = new object();
public event EventHandler<EmergencyAckEventArgs> OnEmergencyAckRequest;
event EventHandler<EmergencyAckEventArgs> DataBaseInterface.OnEmergencyAckRequest
{
add { lock (_lockEmergencyAck) { OnEmergencyAckRequest += value; } }
remove { lock (_lockEmergencyAck) { OnEmergencyAckRequest -= value; } }
}
#endregion
#region CHANNEL QUERY EVENT
private object _lockChannelQuery = new object();
public event EventHandler<ChannelQueryEventArgs> OnChannelQuery;
event EventHandler<ChannelQueryEventArgs> DataBaseInterface.OnChannelQuery
{
add { lock (_lockChannelQuery) { OnChannelQuery += value; } }
remove { lock (_lockChannelQuery) { OnChannelQuery -= value; } }
}
#endregion
#region SMS REQUEST EVENT
private object _lockSMSRequest = new object();
public event EventHandler<SMSRequestEventArgs> OnSMSRequestReceived;
event EventHandler<SMSRequestEventArgs> DataBaseInterface.OnSMSRequestReceived
{
add { lock (_lockSMSRequest) { OnSMSRequestReceived += value; } }
remove { lock (_lockSMSRequest) { OnSMSRequestReceived -= value; } }
}
#endregion
#region GROUP SMS REQUEST EVENT
private object _lockGroupSMSRequest = new object();
public event EventHandler<SMSRequestEventArgs> OnGroupSMSRequestReceived;
event EventHandler<SMSRequestEventArgs> DataBaseInterface.OnGroupSMSRequestReceived
{
add { lock (_lockGroupSMSRequest) { OnGroupSMSRequestReceived += value; } }
remove { lock (_lockGroupSMSRequest) { OnGroupSMSRequestReceived -= value; } }
}
#endregion
#region TELEMETRY REQUEST EVENT
private object _lockTelemetryRequest = new object();
public event EventHandler<TelemetryRequestEventArgs> OnTelemetryRequestReceived;
event EventHandler<TelemetryRequestEventArgs> DataBaseInterface.OnTelemetryRequestReceived
{
add { lock (_lockTelemetryRequest) { OnTelemetryRequestReceived += value; } }
remove { lock (_lockTelemetryRequest) { OnTelemetryRequestReceived -= value; } }
}
#endregion
}
}