326 lines
11 KiB
C#
326 lines
11 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.ComponentModel;
|
|||
|
using System.Data;
|
|||
|
using System.Text;
|
|||
|
using System.Text.RegularExpressions;
|
|||
|
using GsmComm.GsmCommunication;
|
|||
|
using GsmComm.Interfaces;
|
|||
|
using GsmComm.PduConverter;
|
|||
|
using GsmComm.Server;
|
|||
|
using System.IO;
|
|||
|
using System.Data.OleDb;
|
|||
|
using System.Management;
|
|||
|
|
|||
|
namespace Payroll.BO
|
|||
|
{
|
|||
|
[Serializable]
|
|||
|
public class SMSSender
|
|||
|
{
|
|||
|
private int[] ComPorts = { 1, 2, 3, 4, 5, 6, 7, 8 };
|
|||
|
private int[] BaudRates = { 9600, 19200, 38400, 57600, 115200 };
|
|||
|
private int[] TimeOuts = { 150, 300, 600, 900, 1200, 1500, 1800, 2000 };
|
|||
|
|
|||
|
private Int16 Comm_Port = 0;
|
|||
|
private Int32 Comm_BaudRate = 0;
|
|||
|
private Int32 Comm_TimeOut = 0;
|
|||
|
private GsmCommMain comm;
|
|||
|
private List<Port> ports;
|
|||
|
private Phone phone;
|
|||
|
private Port validPort;
|
|||
|
|
|||
|
public String CellNumber { get; set; }
|
|||
|
public String SMSMessage { get; set; }
|
|||
|
|
|||
|
#region private Functions
|
|||
|
|
|||
|
private bool PortFound()
|
|||
|
{
|
|||
|
// for Receiving maximum
|
|||
|
Array.Sort(TimeOuts);
|
|||
|
Array.Reverse(TimeOuts);
|
|||
|
|
|||
|
foreach (int comp in ComPorts)
|
|||
|
{
|
|||
|
foreach (var baud in BaudRates)
|
|||
|
{
|
|||
|
foreach (var tmOut in TimeOuts)
|
|||
|
{
|
|||
|
Port port = new Port();
|
|||
|
port.ComPort = comp;
|
|||
|
port.ConnDevice = "";
|
|||
|
port.MaxBaudRate = baud;
|
|||
|
port.Status = "";
|
|||
|
port.TimeOut = tmOut.ToString();
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
Comm_Port = (short)port.ComPort;
|
|||
|
Comm_BaudRate = (int)port.MaxBaudRate;
|
|||
|
Comm_TimeOut = Convert.ToInt32(port.TimeOut);
|
|||
|
}
|
|||
|
catch (Exception e1)
|
|||
|
{
|
|||
|
// Console.WriteLine("Error Converting COM Port Settings Values" + " Check COM Port Values");
|
|||
|
continue;
|
|||
|
}
|
|||
|
comm = new GsmCommMain(Comm_Port, Comm_BaudRate, Comm_TimeOut);
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
comm.Open();
|
|||
|
if (comm.IsConnected())
|
|||
|
{
|
|||
|
phone = new Phone();
|
|||
|
try
|
|||
|
{
|
|||
|
phone.Name = comm.IdentifyDevice().Manufacturer.ToUpper().ToString();
|
|||
|
phone.Model = comm.IdentifyDevice().Model.ToUpper().ToString();
|
|||
|
phone.RevisionNumber = comm.IdentifyDevice().Revision.ToUpper().ToString();
|
|||
|
phone.SerialNumber = comm.IdentifyDevice().SerialNumber.ToUpper().ToString();
|
|||
|
validPort = port;
|
|||
|
return true;
|
|||
|
}
|
|||
|
catch (Exception e2)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception e3)
|
|||
|
{
|
|||
|
continue;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
private bool ValidNumber()
|
|||
|
{
|
|||
|
Match matchNumber;
|
|||
|
|
|||
|
switch (CellNumber.Trim().Length)
|
|||
|
{
|
|||
|
case 11:
|
|||
|
if (checkStarting(0))
|
|||
|
{
|
|||
|
matchNumber = Regex.Match(CellNumber, "[0-9]+");
|
|||
|
if (matchNumber.Success)
|
|||
|
return true;
|
|||
|
}
|
|||
|
break;
|
|||
|
|
|||
|
case 14:
|
|||
|
if (checkStarting(3))
|
|||
|
{
|
|||
|
matchNumber = Regex.Match(CellNumber, "[+]88[0-9]+");
|
|||
|
if (matchNumber.Success)
|
|||
|
return true;
|
|||
|
}
|
|||
|
break;
|
|||
|
case 15:
|
|||
|
if (checkStarting(4))
|
|||
|
{
|
|||
|
matchNumber = Regex.Match(CellNumber, "0088[0-9]+");
|
|||
|
if (matchNumber.Success)
|
|||
|
return true;
|
|||
|
}
|
|||
|
break;
|
|||
|
default:
|
|||
|
return false;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
return false;
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
private bool checkStarting(int start)
|
|||
|
{
|
|||
|
String subString = CellNumber.Substring(start, 3);
|
|||
|
if (subString == "011" || subString == "015" || subString == "016" || subString == "017" || subString == "018" || subString == "019")
|
|||
|
return true;
|
|||
|
return false;
|
|||
|
}
|
|||
|
|
|||
|
//private bool ValidPortSelect()
|
|||
|
// {
|
|||
|
// foreach (Port port in ports)
|
|||
|
// {
|
|||
|
// try
|
|||
|
// {
|
|||
|
// Comm_Port = Convert.ToInt16(port.ComPort.ToString().Substring(3));
|
|||
|
// Comm_BaudRate = Convert.ToInt32(port.MaxBaudRate.ToString());
|
|||
|
// Comm_TimeOut = Convert.ToInt32(port.TimeOut);
|
|||
|
// }
|
|||
|
// catch (Exception e1)
|
|||
|
// {
|
|||
|
// // Console.WriteLine("Error Converting COM Port Settings Values" + " Check COM Port Values");
|
|||
|
// continue;
|
|||
|
// }
|
|||
|
// comm = new GsmCommMain(Comm_Port, Comm_BaudRate, Comm_TimeOut);
|
|||
|
|
|||
|
// try
|
|||
|
// {
|
|||
|
|
|||
|
// comm.Open();
|
|||
|
// if (comm.IsConnected())
|
|||
|
// {
|
|||
|
// phone = new Phone();
|
|||
|
// try
|
|||
|
// {
|
|||
|
// phone.Name = comm.IdentifyDevice().Manufacturer.ToUpper().ToString();
|
|||
|
// phone.Model = comm.IdentifyDevice().Model.ToUpper().ToString();
|
|||
|
// phone.RevisionNumber = comm.IdentifyDevice().Revision.ToUpper().ToString();
|
|||
|
// phone.SerialNumber = comm.IdentifyDevice().SerialNumber.ToUpper().ToString();
|
|||
|
// validPort = port;
|
|||
|
// return true;
|
|||
|
// }
|
|||
|
// catch (Exception e2)
|
|||
|
// {
|
|||
|
// continue;
|
|||
|
// }
|
|||
|
// }
|
|||
|
// }
|
|||
|
// catch (Exception e3)
|
|||
|
// {
|
|||
|
// continue;
|
|||
|
// }
|
|||
|
// }
|
|||
|
// return false;
|
|||
|
// }
|
|||
|
|
|||
|
//private void GeneratePortList()
|
|||
|
// {
|
|||
|
// ports = new List<Port>();
|
|||
|
// try
|
|||
|
// {
|
|||
|
// // Cursor.Current = Cursors.WaitCursor;
|
|||
|
|
|||
|
// //ManagementObjectSearcher searcher = new ManagementObjectSearcher("root\\CIMV2", "SELECT * FROM Win32_SerialPort");
|
|||
|
|
|||
|
// //foreach (ManagementObject queryObj in searcher.Get())
|
|||
|
// //{
|
|||
|
|
|||
|
// // if (queryObj != null)
|
|||
|
// // {
|
|||
|
// // Port port = new Port();
|
|||
|
// // port.ComPort = queryObj["DEVICEID"]; //0
|
|||
|
// // port.ConnDevice = queryObj["DESCRIPTION"]; //1
|
|||
|
// // port.MaxBaudRate = queryObj["MAXBAUDRATE"]; //2
|
|||
|
// // port.Status = queryObj["STATUS"]; //4
|
|||
|
// // port.TimeOut = "100"; //3
|
|||
|
|
|||
|
// // ports.Add(port);
|
|||
|
// // }
|
|||
|
// //}
|
|||
|
|
|||
|
// foreach (int comp in ComPorts)
|
|||
|
// {
|
|||
|
// foreach (var baud in BaudRates)
|
|||
|
// {
|
|||
|
// foreach (var tmOut in TimeOuts)
|
|||
|
// {
|
|||
|
// Port port = new Port();
|
|||
|
// port.ComPort = comp; //0
|
|||
|
// port.ConnDevice = ""; //1
|
|||
|
// port.MaxBaudRate = baud; //2
|
|||
|
// port.Status = ""; //4
|
|||
|
// port.TimeOut = tmOut.ToString(); //3
|
|||
|
|
|||
|
// ports.Add(port);
|
|||
|
// //dataGridView3.Rows.Add("COM" + comp, "", baud, tmOut, "Open");
|
|||
|
// }
|
|||
|
// }
|
|||
|
// }
|
|||
|
// }
|
|||
|
|
|||
|
// catch (Exception e15)
|
|||
|
// {
|
|||
|
// throw new Exception("An error occurred while querying for WMI data: " + e15.Message);
|
|||
|
// }
|
|||
|
|
|||
|
// }
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
public void SendSMS()
|
|||
|
{
|
|||
|
//GeneratePortList();
|
|||
|
|
|||
|
//if (!ValidPortSelect())
|
|||
|
//{
|
|||
|
// return;
|
|||
|
//}
|
|||
|
|
|||
|
if (!PortFound())
|
|||
|
{
|
|||
|
throw new Exception("No Port Found for sending SMS");
|
|||
|
}
|
|||
|
SmsSubmitPdu pdu1;
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
|
|||
|
if (comm.IsConnected())
|
|||
|
{
|
|||
|
if (!ValidNumber())
|
|||
|
{
|
|||
|
throw new Exception("Invalid Mobile No");
|
|||
|
}
|
|||
|
else if (SMSMessage.Trim() == "")
|
|||
|
{
|
|||
|
SMSMessage = "Blank SMS";
|
|||
|
}
|
|||
|
|
|||
|
try
|
|||
|
{
|
|||
|
pdu1 = new SmsSubmitPdu(SMSMessage, CellNumber, "");
|
|||
|
comm.SendMessage(pdu1);
|
|||
|
}
|
|||
|
catch (Exception e)
|
|||
|
{
|
|||
|
throw new Exception("Could Not Send SMS due To: \r\n" + e.Message);
|
|||
|
}
|
|||
|
finally
|
|||
|
{
|
|||
|
comm.Close();
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
throw new Exception("No GSM Phone / Modem Connected");
|
|||
|
}
|
|||
|
}
|
|||
|
catch (Exception E10)
|
|||
|
{
|
|||
|
if (comm.IsConnected()) comm.Close();
|
|||
|
throw new Exception(E10.Message);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
internal class Port
|
|||
|
{
|
|||
|
public object ComPort { get; set; }
|
|||
|
public object ConnDevice { get; set; }
|
|||
|
public object MaxBaudRate { get; set; }
|
|||
|
public string TimeOut { get; set; }
|
|||
|
public object Status { get; set; }
|
|||
|
}
|
|||
|
|
|||
|
internal class Phone
|
|||
|
{
|
|||
|
public String Name { get; set; }
|
|||
|
public String Model { get; set; }
|
|||
|
public String RevisionNumber { get; set; }
|
|||
|
public String SerialNumber { get; set; }
|
|||
|
|
|||
|
}
|
|||
|
}
|