Ejemplo n.º 1
0
        private void GetPendingSMSes()
        {
            SharedClass.Logger.Info("Started");
            SqlConnection sqlConnection = new SqlConnection(SharedClass.ConnectionString);
            SqlCommand    sqlCommand    = new SqlCommand("GetPendingSMSes", sqlConnection);

            sqlCommand.CommandType = CommandType.StoredProcedure;
            SqlDataAdapter da     = null;
            DataSet        ds     = null;
            long           lastId = 0;

            while (!SharedClass.HasStopSignal)
            {
                try
                {
                    sqlCommand.Parameters.Clear();
                    sqlCommand.Parameters.Add(Label.LAST_ID, SqlDbType.BigInt).Value            = lastId;
                    sqlCommand.Parameters.Add(Label.SUCCESS, SqlDbType.Bit).Direction           = ParameterDirection.Output;
                    sqlCommand.Parameters.Add(Label.MESSAGE, SqlDbType.VarChar, 1000).Direction = ParameterDirection.Output;
                    da = new SqlDataAdapter();
                    da.SelectCommand = sqlCommand;
                    da.Fill(ds       = new DataSet());
                    if (ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow row in ds.Tables[0].Rows)
                        {
                            SMS sms = new SMS();
                            sms.Id = Convert.ToInt32(row["Id"].ToString());
                            if (!string.IsNullOrEmpty(row[Label.MobileNumber].ToString()))
                            {
                                sms.MobileNumber = row[Label.MobileNumber].ToString();
                            }
                            if (!string.IsNullOrEmpty(row[Label.SenderId].ToString()))
                            {
                                sms.SenderId = row[Label.SenderId].ToString();
                            }
                            if (!string.IsNullOrEmpty(row[Label.Message].ToString()))
                            {
                                sms.Message = row[Label.Message].ToString();
                            }
                            this.EnQueueSMS(sms);
                            lastId = sms.Id;
                        }
                    }
                    Thread.Sleep(5000);
                }
                catch (ThreadAbortException e)
                { }
                catch (ThreadInterruptedException e)
                { }
                catch (Exception e)
                {
                    SharedClass.Logger.Error(e.ToString());
                }
                finally
                {
                    da = null;
                    ds = null;
                }
            }
            SharedClass.Logger.Info("Exit");
        }
Ejemplo n.º 2
0
        private void Push()
        {
            SharedClass.Logger.Info("Started");
            string smsResponse = string.Empty;

            while (!SharedClass.HasStopSignal)
            {
                SMSResult smsResult = null;
                smsResult = new SMSResult();

                try
                {
                    if (this.SMSQueueCount() > 0)
                    {
                        SMS sms = this.DeQueueSMS();
                        smsResult.sms = sms;
                        JObject Jobj = new JObject();
                        if (sms != null)
                        {
                            JObject smsPostdata;
                            sms.MobileNumber = sms.MobileNumber.Replace(" ", "").Replace("\n", "").Replace("\r", "").Replace("+", "");
                            HttpWebRequest  webRequestObj   = null;
                            HttpWebResponse webResponseObj  = null;
                            StreamWriter    streamWriterObj = null;
                            StreamReader    streamReaderObj = null;
                            CredentialCache credentials     = new CredentialCache();

                            //if ((IsUnicode(sms.Message) == true)) {
                            //    sms.MessageType = "OL";
                            //}
                            //else {
                            //    sms.MessageType = "N";
                            //}

                            //if ((sms.MessageType == "OL")) {
                            //    sms.Message = convertHexa(sms.Message);
                            //}

                            smsPostdata = new JObject(
                                new JProperty("Text", sms.Message),
                                new JProperty("Number", sms.MobileNumber),
                                new JProperty("DRNotifyUrl", ""),
                                new JProperty("DRNotifyHttpMethod", "POST"),
                                new JProperty("Tool", "API"),
                                new JProperty("SenderId", sms.SenderId));
                            SharedClass.Logger.Info("PostData" + smsPostdata.ToString());
                            credentials.Add(new Uri(SharedClass.SMSProvider),
                                            "Basic", new NetworkCredential(SharedClass.SMSProviderAuthkey, SharedClass.SMSProviderSecretKey));
                            webRequestObj             = (HttpWebRequest)WebRequest.Create(SharedClass.SMSProvider);
                            webRequestObj.Method      = "Post";
                            webRequestObj.Credentials = credentials;
                            webRequestObj.ContentType = "application/json";
                            streamWriterObj           = new StreamWriter(webRequestObj.GetRequestStream());
                            streamWriterObj.Write(smsPostdata);
                            streamWriterObj.Flush();
                            streamWriterObj.Close();
                            webResponseObj  = (HttpWebResponse)webRequestObj.GetResponse();
                            streamReaderObj = new StreamReader(webResponseObj.GetResponseStream());
                            smsResponse     = streamReaderObj.ReadToEnd();
                            streamReaderObj.Close();

                            Jobj = JObject.Parse(smsResponse);

                            sms.JobNumber = Jobj.SelectToken("ApiId").ToString();
                            if (Convert.ToBoolean(Jobj.SelectToken("Success").ToString()))
                            {
                                smsResult.IsSuccess = true;
                                smsResult.StatusId  = 3;
                                smsResult.JobNumber = sms.JobNumber;
                            }
                            else
                            {
                                smsResult.IsSuccess = false;
                                smsResult.StatusId  = 5;
                                smsResult.JobNumber = sms.JobNumber;
                            }
                            this.EnQueueSMSStatus(smsResult);
                        }
                    }
                    else
                    {
                        try
                        {
                            Thread.Sleep(2000);
                        }
                        catch (ThreadInterruptedException e)
                        { }
                        catch (ThreadAbortException e)
                        { }
                    }
                }
                catch (Exception e)
                {
                    smsResult.StatusId  = 5;
                    smsResult.JobNumber = "";
                    this.EnQueueSMSStatus(smsResult);
                    SharedClass.Logger.Error(e.ToString());
                }
            }
            SharedClass.Logger.Info("Exit");
        }