SafeDispatch/MotoTrbo_GW/MessageBusHandler.cs

120 lines
4.4 KiB
C#
Raw 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;
using System.Collections;
namespace MotoTrbo_GW
{
public class MessageBusHandler
{
string messageBusIP;
int messageBusPort;
private static UdpMulticast udp;
public static Hashtable MBMessageQueues_Hash;
public MessageBusHandler(string messageBusIP, string messageBusPort)
{
this.messageBusIP = messageBusIP;
this.messageBusPort =Convert.ToInt32(messageBusPort);
MBMessageQueues_Hash = new Hashtable();
DBgatewaysManager DBgateways = new DBgatewaysManager(Main.DBServer, Main.DBSchema, Main.DBUser, Main.DBPass, Main.DBPort);
List<RadioGateway> radioGwList = DBgateways.gelAllRadioGateways(Main.GWID, (int)GatewayType.Tier2Radio);
for (int i = 0; i < radioGwList.Count; i++)
{
//create message buss Queue for radios that have voice eneble
if (radioGwList[i].Gw_voice == 1)
{
InterthreadMessageQueue<MessageBusMessage> MBMessageQueue = new InterthreadMessageQueue<MessageBusMessage>();
MBMessageQueues_Hash.Add(radioGwList[i].Id, MBMessageQueue);
}
}
//start messageBus listner
udp = new UdpMulticast(messageBusIP,this.messageBusPort);
udp.OnNewDataRecv += new UdpMulticast.newData4Send(udp_OnNewDataRecv);
udp.StartListen(Main.LocalIP);
}
void udp_OnNewDataRecv(byte[] data, int dataLen)
{
//SafeMobileLib.Utils.WriteLine("got data on gw" + radioID);
string str = System.Text.Encoding.ASCII.GetString(data, 0, dataLen);
// do not display ping messages
if (!str.StartsWith("#12#"))
SafeMobileLib.Utils.WriteLine(str.Trim());
try
{
MessageBusMessage msm = new MessageBusMessage(str, dataLen);
if (msm.OK)
{
SafeMobileLib.Utils.WriteLine("GWId : " + Main.GWID + " | " + msm.GwAPPid, ConsoleColor.DarkYellow);
if (Main.GWID == msm.GwAPPid)
{
if (MessageBusHandler.MBMessageQueues_Hash.ContainsKey(msm.RadioGWid))
{
//add message to appropiate queue
((InterthreadMessageQueue<MessageBusMessage>)
MessageBusHandler.MBMessageQueues_Hash[msm.RadioGWid]).PostItem(msm);
}
}
#region LINX
#if LINXB
// Check if received command is 209 (Refresh database command)
if (msm.OPcode == (int)MessageBusCmds.RefreshDatabaseCommand)
{
// Fire event
RefreshDatabaseCommandReceived?.Invoke(this, new EventArgs());
}
#endif
#endregion
}
else
{
//SafeMobileLib.Utils.WriteLine("ERROR in MessageBusHandler.cs error:"+msm.Error);
}
}
catch (Exception ex)
{
SafeMobileLib.Utils.WriteLine(ex.ToString());
}
}
public static void SendOnMsgBuss(string seqID, string test, string comment)
{
if (udp != null)
{
String cmdok = "#" + seqID + test;
Int32 tmp = cmdok.Length + 1;
tmp += tmp.ToString().Length;
cmdok = "#" + tmp.ToString() + cmdok;
System.Text.Encoding enc = System.Text.Encoding.ASCII;
byte[] buf = enc.GetBytes(cmdok);
udp.Send(buf, buf.Length);
SafeMobileLib.Utils.WriteLine($"TX: {cmdok} {(comment.Length > 0 ? "//" + comment : "")}");
}
else
{
SafeMobileLib.Utils.WriteLine("SendOnMsgBuss Error!!! MessageBusHandler messagebus =null", ConsoleColor.Red);
}
}
public static void SendOnMsgBuss(string seqID, string test)
{
SendOnMsgBuss(seqID, test, "");
}
#region LINX
#if LINXB
public event EventHandler RefreshDatabaseCommandReceived;
#endif
#endregion
}
}