285 lines
11 KiB
C#
285 lines
11 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using SafeNetLib;
|
|
using System.Threading;
|
|
using HyteraWrapper;
|
|
using System.Collections;
|
|
|
|
namespace Hytera_SOC
|
|
{
|
|
class SMShandle
|
|
{
|
|
private GatewayRadio gw;
|
|
private Thread thread;
|
|
//private Thread t_manage_waitSMSack;
|
|
int sms_seq_id = 1;
|
|
public bool isAlive = false;
|
|
private DBhandle DB;
|
|
|
|
public SMShandle(GatewayRadio _gw)
|
|
{
|
|
gw = _gw;
|
|
thread = new Thread(new ThreadStart(HandleConnection));
|
|
thread.IsBackground = true;
|
|
|
|
//t_manage_waitSMSack = new Thread(new ThreadStart(HandleSMSacks));
|
|
//t_manage_waitSMSack.IsBackground = true;
|
|
if (gw != null)
|
|
{
|
|
DB = new DBhandle(Hytera_GW.MyConString, Hytera_GW.cfg.gatewayID);
|
|
isAlive = true;
|
|
thread.Start();
|
|
//t_manage_waitSMSack.Start();
|
|
|
|
gw.OnTM_CS += new GatewayRadio.OnTM_CS_del(gw_OnTM_CS);
|
|
}
|
|
}
|
|
|
|
public void Stop()
|
|
{
|
|
isAlive = false;
|
|
if (thread != null)
|
|
{
|
|
thread.Abort();
|
|
thread = null;
|
|
}
|
|
}
|
|
|
|
public void HandleConnection()
|
|
{
|
|
while (isAlive)
|
|
{
|
|
try
|
|
{
|
|
//MotoTRBOcmdMsg msg = MotoTRBOGW.locationQueue.GetItem(100);
|
|
SMSmsg msg = SN_Queues.sendSMSQueue.GetItem(-1);//block until message is in queue
|
|
bool ret = SendSMSmsg(msg);
|
|
if (msg.req_conf)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Requesting SMS Confirmation!!!!");
|
|
lock (SN_Queues.waitConfSMSList.SyncRoot)
|
|
{
|
|
msg.seq_no = this.sms_seq_id - 1;//-1 because was alraedy incremented for next id
|
|
msg.waitConfSMSList_time = DateTime.Now;
|
|
|
|
SN_Queues.waitConfSMSList.Add(msg);
|
|
}
|
|
}
|
|
|
|
Thread.Sleep(1200);//minimum interval between SMS as per MotoTRBO specs
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "Exception in SMShandle:HandleConnection(): \r\n\tData=" +
|
|
ex.Data + "\r\n\tSource=" +
|
|
ex.Source + "\r\n\tStackTrace=" +
|
|
ex.StackTrace + "\r\n\tMessage=" +
|
|
ex.Message + "\r\n\tInnerException=" +
|
|
ex.InnerException);
|
|
}
|
|
} // end while (true)
|
|
}
|
|
|
|
//if SMS acks are stuck in here delete them and let them be send again
|
|
public void HandleSMSacks()
|
|
{
|
|
while (isAlive)
|
|
{
|
|
try
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Managing SMS's ACKs!!!!");
|
|
LOGS.LOG("Managing SMS's ACKs!!!!");
|
|
|
|
lock (SN_Queues.waitConfSMSList.SyncRoot)
|
|
{
|
|
ArrayList delID_list = new ArrayList();
|
|
int count = 0;
|
|
foreach (SMSmsg msgTmp in SN_Queues.waitConfSMSList)
|
|
{
|
|
if (msgTmp.waitConfSMSList_time.AddMinutes(5) < DateTime.Now)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Found SMS waiting in ACK queue from:" + msgTmp.waitConfSMSList_time + " Current time:" + DateTime.Now);
|
|
LOGS.LOG("Found SMS waiting in ACK queue from:" + msgTmp.waitConfSMSList_time + " Current time:" + DateTime.Now);
|
|
delID_list.Add(count);
|
|
}
|
|
count++;
|
|
}
|
|
|
|
foreach (int i in delID_list)
|
|
{
|
|
SN_Queues.waitConfSMSList.RemoveAt(i);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, "Exception in SMShandle:HandleSMSacks(): \r\n\tData=" +
|
|
ex.Data + "\r\n\tSource=" +
|
|
ex.Source + "\r\n\tStackTrace=" +
|
|
ex.StackTrace + "\r\n\tMessage=" +
|
|
ex.Message + "\r\n\tInnerException=" +
|
|
ex.InnerException);
|
|
}
|
|
} // end while (true
|
|
}
|
|
|
|
public bool SendSMSmsg(SMSmsg p_msg)
|
|
{
|
|
try
|
|
{
|
|
uint u_suid = Convert.ToUInt32(p_msg.suid);
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "MSG sent to:" + p_msg.suid);
|
|
bool ret = gw.SendSMS(u_suid, p_msg.msg);
|
|
return ret;
|
|
}
|
|
catch (Exception exc)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, exc.ToString());
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
void gw_OnTM_CS(StringBuilder value)
|
|
{
|
|
try
|
|
{
|
|
|
|
if (value.Length == 0)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Empty message... Droping it!!!!");
|
|
return;
|
|
}
|
|
string temp = value.ToString();
|
|
string[] split_temp = temp.Split('#');
|
|
if (split_temp.Length == 0)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Empty message... Droping it!!!!");
|
|
return;
|
|
}
|
|
if ((split_temp[0] != "ACK") && (split_temp[0] != "ACKG"))
|
|
{
|
|
//received message... send it to messagebus
|
|
string rawMessage = value.ToString();
|
|
string message = rawMessage;
|
|
if (rawMessage.Contains("'"))
|
|
{
|
|
message = rawMessage.Replace("'", "`");
|
|
}
|
|
//bigu code remove # from text
|
|
Int32 x1 = message.IndexOf("#132#");
|
|
Int32 x2 = message.IndexOf("#hyt#");
|
|
Int32 x3 = message.IndexOf('#', x1 + 5, x2 - (x1 + 5) - 1);
|
|
Boolean finish = false;
|
|
while (!finish)
|
|
{
|
|
Int32 x4 = message.IndexOf('#', x3 + 1, x2 - (x3 + 1) - 1);
|
|
if (x4 == -1) finish = true;
|
|
else
|
|
{
|
|
message = message.Remove(x4, 1);
|
|
message = message.Insert(x4, " ");
|
|
}
|
|
}
|
|
//end of bigu code
|
|
|
|
//byte[] buf = System.Text.Encoding.ASCII.GetBytes(message);
|
|
//send to messagebus
|
|
//udpMulticast.Send(buf, buf.Length);
|
|
//SM.Debug("TX:" + value.ToString());
|
|
string[] msg_arr = message.Split('#');
|
|
|
|
string s = msg_arr[5];
|
|
//insert message into database
|
|
if (!DB.insertSMSinDB("" + msg_arr[4], msg_arr[5]))
|
|
{
|
|
SMSmsg sms = new SMSmsg();
|
|
sms.suid = msg_arr[4];
|
|
sms.msg = "ERROR sending message to server!";
|
|
sms.req_conf = false;
|
|
SN_Queues.sendSMSQueue.PostItem(sms);
|
|
}
|
|
else
|
|
{
|
|
LOGS.LOG(Hytera_GW.cfg.gatewayID + msg_arr[4] + " " + msg_arr[5]);
|
|
try
|
|
{
|
|
string email_addr = "";
|
|
string email_body = "";
|
|
if (s.Contains("@") && s.Contains(":") && s.Contains("."))
|
|
{ // this may be an email so analyze further
|
|
char[] sep = { ':' };
|
|
string[] all_msg = s.Split(sep);
|
|
email_addr = all_msg[0];
|
|
email_body = all_msg[1];
|
|
|
|
if (email_addr.Contains("@") && email_addr.Contains("."))
|
|
{
|
|
// this is email
|
|
|
|
EmailHandler.sendMail(email_addr, email_body, msg_arr[4]);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.always, ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
else if (split_temp[0] == "ACK")
|
|
{
|
|
try
|
|
{
|
|
//received ACK
|
|
int destID = int.Parse(split_temp[1]);
|
|
int result = int.Parse(split_temp[2]);
|
|
if (result == 0)
|
|
{
|
|
//send ACK on message bus
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "Got SMS ACK for suid :" + destID);
|
|
lock (SN_Queues.waitConfSMSList.SyncRoot)
|
|
{
|
|
int index = -1;
|
|
int count = 0;
|
|
SMSmsg temp_SMS = null;
|
|
foreach (SMSmsg msg in SN_Queues.waitConfSMSList)
|
|
{
|
|
if (msg.suid == destID.ToString())
|
|
{
|
|
index = count;
|
|
temp_SMS = msg;
|
|
//SN_Queues.confSMSQueue.PostItem(msg);
|
|
}
|
|
count++;
|
|
}
|
|
if (temp_SMS != null)
|
|
{
|
|
SN_Queues.confSMSQueue.PostItem(temp_SMS);
|
|
}
|
|
if (index > -1)//we've found the message
|
|
SN_Queues.waitConfSMSList.RemoveAt(index);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "ERROR parsing SMS ACK");
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, ex.ToString());
|
|
}
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, "ERROR in gw_OnTM_CS");
|
|
Utils.ConsWrite(DebugMSG_Type.SMS, ex.ToString());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
}
|
|
}
|