public class sms_receive : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
try
{
ReceiveSMSOperator receive = new ReceiveSMSOperator();
receive.ReceiveSMS(context);
}
catch (Exception ex)
{
PUBUtility.AppLog(PUBConstant.LogType.INFO, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Get httpcontext error!");
try
{
ExceptionPolicy.HandleException(ex, "UI Policy");
}
catch (Exception ex2) { }
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
/
ReceiveSMSOperator //
public class ReceiveSMSOperator
{
string result = "";
public void ReceiveSMS(HttpContext ctx)
{
HttpRequest request = ctx.Request;
string xsdPath = request.PhysicalApplicationPath + "common\\SMSXml\\RcxSMSOnbehalfReq.xsd";
PUBUtility.AppLog(PUBConstant.LogType.INFO, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":Receive a message!");
XmlSchemaSet xsSet = new XmlSchemaSet();
XmlDocument ReqxmlDoc = new XmlDocument();
try
{
xsSet.Add(null, xsdPath);
}
catch (Exception ex)
{
try
{
ExceptionPolicy.HandleException(ex, "UI Policy");
}
catch (Exception ex2) { }
}
ReqxmlDoc.Schemas = xsSet;
try
{
ReqxmlDoc.Load(request.InputStream);
}
catch (Exception e)
{
//Invalid Message Format, return RESCODE 10001
this.ResponseSMS(ctx, "10001",null);
return;
}
//Message Format is right, return RESCODE 10000
PUBUtility.AppLog(PUBConstant.LogType.INFO, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "Loading XML:" + ReqxmlDoc.InnerXml.ToString());
this.ResponseSMS(ctx, "10000", ReqxmlDoc);
SMSCommonOperator SMSOperator = new SMSCommonOperator();
//Save SMS and Check income number and station Code
if (!saveInComeSMS(ReqxmlDoc))
{
return;
}
// Check SMS Text
//if SMS Text=UN, UnSubscribe
if (ReqxmlDoc.GetElementsByTagName("MSG")[0].InnerText.Trim().ToLower().Equals("UN".ToLower()))
{
try
{
// unsubscribe operation
PUBUtility.AppLog(PUBConstant.LogType.INFO, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-----------Unsubscribe");
SMSOperator.UnSubscribe(ReqxmlDoc);
}
catch (Exception ex)
{
try
{
ExceptionPolicy.HandleException(ex, "UI Policy");
}
catch (Exception ex2) { }
}
}
else
{
PUBUtility.AppLog(PUBConstant.LogType.INFO, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "-----------Begin to Check Alert Rule");
//else if SMS Text != UN, Check Alert Rule
SMSOperator.CheckAlertRule(ReqxmlDoc);
}
}
private void ResponseSMS(HttpContext ctx, string ResCode, XmlDocument ReqxmlDoc)
{
XmlDocument ResxmlDoc = new XmlDocument();
ResxmlDoc.Load(ctx.Request.PhysicalApplicationPath + "common\\SMSxml\\RcxSMSOnbehalfRes.xml");
if (ReqxmlDoc != null)
{
ResxmlDoc.GetElementsByTagName("MERCHREF")[0].InnerText = ReqxmlDoc.GetElementsByTagName("TELCOREF")[0].InnerXml;
}
ResxmlDoc.GetElementsByTagName("DATE")[0].InnerText = DateTime.Now.ToString("yyyyMMdd");
ResxmlDoc.GetElementsByTagName("TIME")[0].InnerText = DateTime.Now.ToString("HHmmss");
ResxmlDoc.GetElementsByTagName("RESCODE")[0].InnerText = ResCode;
PUBUtility.AppLog(PUBConstant.LogType.INFO, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "Response XML:\r\n" + ResxmlDoc.InnerXml.ToString());
byte[] bytes = Encoding.UTF8.GetBytes(ResxmlDoc.InnerXml.ToString());
ctx.Response.ContentType = "xml";
ctx.Response.Clear();
ctx.Response.BufferOutput = true;
using (Stream ResponseStream = ctx.Response.OutputStream)
{
ResponseStream.Write(bytes, 0, bytes.Length);
}
ctx.Response.Flush();
}
/// <summary>
/// Save SMS in DB
/// Check in DB, if water_direction=fall and between interval time have no rise. just store in and return
///Check in DB, between interval time, if the same alert have been triggered. just store in DB and return
/// </summary>
/// <param name="ReqxmlDoc"></param>
private bool saveInComeSMS(XmlDocument ReqxmlDoc)
{
InMessageProxy InMsgProxy = new InMessageProxy();
InMessageDataSet InMsg = new InMessageDataSet();
InMessageDataSet.T_PFAS_In_MsgRow In_MsgRow = InMsg.T_PFAS_In_Msg.NewT_PFAS_In_MsgRow();
In_MsgRow.Date = DateTime.ParseExact(ReqxmlDoc.GetElementsByTagName("DATE")[0].InnerXml.Trim(), "yyyyMMdd", System.Globalization.CultureInfo.InvariantCulture);
In_MsgRow.Time = ReqxmlDoc.GetElementsByTagName("TIME")[0].InnerXml.Trim(); ;
In_MsgRow.Dcs = ReqxmlDoc.GetElementsByTagName("DCS")[0].InnerXml.Trim();
In_MsgRow.Mcode = ReqxmlDoc.GetElementsByTagName("MCODE")[0].InnerXml.Trim();
In_MsgRow.Msg = ReqxmlDoc.GetElementsByTagName("MSG")[0].InnerXml.Trim();
In_MsgRow.Msg_Type = ReqxmlDoc.GetElementsByTagName("MSGTYPE")[0].InnerXml.Trim();
In_MsgRow.Sid = ReqxmlDoc.GetElementsByTagName("SID")[0].InnerXml.Trim();
In_MsgRow.Src = ReqxmlDoc.GetElementsByTagName("SRC")[0].InnerXml.Trim();
In_MsgRow.Telco_Ref = ReqxmlDoc.GetElementsByTagName("TELCOREF")[0].InnerXml.Trim();
PUBUtility.UpdateCommonFields(In_MsgRow);
In_MsgRow.Last_Updated_By = "PublicUser";
In_MsgRow.Created_By = "PublicUser";
InMsg.T_PFAS_In_Msg.AddT_PFAS_In_MsgRow(In_MsgRow);
//Check the current message is not a Unsubscribe Message
//and save the current alert message
if (!ReqxmlDoc.GetElementsByTagName("MSG")[0].InnerXml.Trim().ToLower().Equals("UN".ToLower()))
{
InMessageDataSet.T_PFAS_Alert_MsgRow alert_MsgRow = InMsg.T_PFAS_Alert_Msg.NewT_PFAS_Alert_MsgRow();
string Msg = ReqxmlDoc.GetElementsByTagName("MSG")[0].InnerXml.Trim();
Msg = Msg.Replace("\r\n", "@").Trim();
string[] message = Msg.Substring(0, (Msg.Length - 1)).Trim().Split('@');
if (message.Length <= 1)
{
Msg = Msg.Replace("\n", "@").Trim();
message = Msg.Substring(0, (Msg.Length - 1)).Trim().Split('@');
}
if (message.Length <= 1)
{
Msg = Msg.Replace("\r", "@").Trim();
message = Msg.Substring(0, (Msg.Length - 1)).Trim().Split('@');
}
alert_MsgRow.StationCode =message[0].Trim();
alert_MsgRow.Trigger_Point = message[1].Trim().Substring(0, message[1].Trim().Length - 1);
alert_MsgRow.Water_Direction = message[2].Trim();
string[] dt = message[3].Trim().Split('/');
string date_time = dt[1] + "/" + dt[0] + "/" + dt[2];
alert_MsgRow.Date_Time = DateTime.Parse(date_time);
//****************************************************************
//2011-6-16 cao you gang
//****************************************************************
//int startIndex = message[4].Trim().IndexOf("(");
//int length = message[4].Trim().IndexOf(")") - message[4].Trim().IndexOf("(");
//string water_level = message[4].Trim().Substring(startIndex + 1, length - 2);
//alert_MsgRow.Water_Level = float.Parse(water_level);
//alert_MsgRow.Model_Operation = message[5].Trim();
//string Cope = message[6].Split(':')[1].Trim();
//alert_MsgRow.Cope = Decimal.Parse(Cope.Substring(0, Cope.Length - 1));
alert_MsgRow.Water_Level = 0;
alert_MsgRow.Model_Operation = "";
alert_MsgRow.Cope = 0;
PUBUtility.UpdateCommonFields(alert_MsgRow);
alert_MsgRow.InMsgID = InMsg.T_PFAS_In_Msg[0].ID;
alert_MsgRow.Created_By = "PublicUser";
alert_MsgRow.Last_Updated_By = "PublicUser";
InMsg.T_PFAS_Alert_Msg.AddT_PFAS_Alert_MsgRow(alert_MsgRow);
}
//Check in DB, if water_direction=fall and between interval time have no rise. just store in and return
//Check in DB, between interval time, if the same alert have been triggered. just store in DB and return
bool checkInterval = true;
try
{
if (!ReqxmlDoc.GetElementsByTagName("MSG")[0].InnerXml.Trim().ToLower().Equals("UN".ToLower()))
{
int receiveInterval = 24;
try
{
receiveInterval = int.Parse(WebConfigurationManager.AppSettings["ReceiveInterval"]);
}
catch (Exception ex)
{
receiveInterval = 24;
try
{
ExceptionPolicy.HandleException(ex, "UI Policy");
}
catch (Exception ex2) { }
}
DateTime lastTime = InMsg.T_PFAS_Alert_Msg[0].Date_Time.AddHours(-double.Parse(receiveInterval.ToString()));
bool outResult = false;
checkInterval = InMsgProxy.checkINMsg(lastTime, InMsg, out outResult);
//Check SRC
//if SRC number is not in the company mobile list, return function and don't do next steps
//First get SRC number
string SRC_number = ReqxmlDoc.GetElementsByTagName("SRC")[0].InnerText.Trim();
//Get station code from SMS body
string stationCode = InMsg.T_PFAS_Alert_Msg[0].StationCode;
//Check SRC and station code, if SRC is not belong to this company ,return FALSE.
CompanyProxy com = new CompanyProxy();
string ret = "";
string str = com.CompanyByCheckMobile(stationCode, decimal.Parse(SRC_number), out ret);
//check failed, return function
if (ret != null || str == null || str == "0" || !checkInterval)
{
checkInterval = false;
}
InMsg.T_PFAS_Alert_Msg[0].Trigger_Status = checkInterval;
}
//save SMS
InMsgProxy.AddInMessage(InMsg, out result);
}
catch (Exception ex)
{
try
{
ExceptionPolicy.HandleException(ex, "UI Policy");
}
catch (Exception ex2) { }
}
return checkInterval;
}
}