嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
对于硬件短信猫,可实现多端口管理,数据库及网络通讯,实现短信猫自动值守
ps:缺少数据库,可参考其功能
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Threading;
namespace SMS.Entity
{
/// <summary>
/// 管理卡的加载,数据分发
/// </summary>
public class CardManager : ThreadCore, IdataManager
{
/// <summary>
/// 加载界面管理类
/// </summary>
FormManager FM;
/// <summary>
/// 短信猫是否全部失效
/// </summary>
Boolean gsmFail;
Form1 F;
//当前单独调试端口号
public static string debuggingPort = "100";
//分发策略
static int cl;
public static int Cl
{
get { return CardManager.cl; }
set { CardManager.cl = value; }
}
public Boolean GsmFail
{
get { return gsmFail; }
set { gsmFail = value; }
}
/// <summary>
/// 平台是否全部失效
/// </summary>
Boolean pTFail;
public Boolean PTFail
{
get { return pTFail; }
set { pTFail = value; }
}
List<gsmSender> gsmGroup;
List<SMSplat> RdsG;
//可用端口列表
List<String> PortGP;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="f">引用界面</param>
public CardManager(Form1 f)
{
string[] tempSMSp = Properties.Settings.Default.SMSplat1.Split(',');
this.PortGP = this.getPortAva();
F = f;
FM = new FormManager();
FM.setForm(F, this.PortGP, tempSMSp);
gsmGroup = new List<gsmSender>();
RdsG = new List<SMSplat>();
this.creatGroup();
for (int i = 0; i < gsmGroup.Count; i )
{
gsmGroup[i].StarRun();
}
for (int i = 0; i < RdsG.Count; i )
{
RdsG[i].StarRun();
}
}
/// <summary>
/// 启动线程
/// </summary>
public void StarRunCM()
{
base.StarRun();
}
//端口检测方法
public List<String> getPortAva()
{
//尝试打开端口
List<String> Portgp = new List<string>();
for (int i = 4; i < 40; i )
{
GsmModem tempGM = new GsmModem();
tempGM.ComPort = "com" i.ToString();
try
{
tempGM.Open();
Portgp.Add(i.ToString());
tempGM.Close();
}
catch (Exception e)
{
log.Log(e.ToString(), "tempPort.open");
}
}
return Portgp;
}
//创建发送类组
public void creatGroup()
{
//启动所有短信猫端口
for (int i = 0; i < this.PortGP.Count; i )
{
gsmSender temp = new gsmSender(PortGP[i]);
//可用状态为真
temp.ReadCardSign = true;
//标明在集合中的位置
temp.FreshCardIndex = i;
temp.Working = false;
temp.ProgressEvent = new ProgressEventHandler(FM.gsmFlushMsg);
temp.UpdateTimeEvent = new UpdateTimeEventHandler(FM.updatetime);
temp.FeedBackEvent = new FeedBackEventHandler(FM.updateFeedback);
gsmGroup.Add(temp);
}
//短信猫组中有可用端口
if (gsmGroup.Count > 0) this.gsmFail = false; else this.gsmFail = true;
//加载所有短信平台类
string[] tempSMSp = Properties.Settings.Default.SMSplat1.Split(',');
for (int i = 0; i < tempSMSp.Length; i )
{
SMSplat ly = new SMSplat(((i 1) * 100).ToString());
ly.ReadCardSign = true;
ly.FreshCardIndex = this.PortGP.Count i;
ly.ProgressEvent = new ProgressEventHandler(FM.gsmFlushMsg);
ly.UpdateTimeEvent = new UpdateTimeEventHandler(FM.updatetime);
ly.FeedBackEvent = new FeedBackEventHandler(FM.updateFeedback);
RdsG.Add(ly);
}
if (tempSMSp.Length > 0) this.pTFail = false; else pTFail = true;
}
#region 过滤非法字符
public static string encoding(string src)
{
if (src == null)
return "";
StringBuilder result = new StringBuilder();
if (src != null)
{
src = src.Trim();
for (int pos = 0; pos < src.Length; pos )
{
switch (src[pos])
{
case '\"': result.Append(" "); break;
case '<': result.Append(" "); break;
case '>': result.Append(" "); break;
case '\'': result.Append(" "); break;
case '&': result.Append(" "); break;
case '%': result.Append(" "); break;
case '_': result.Append(" "); break;
case '#': result.Append(" "); break;
case '?': result.Append(" "); break;
default: result.Append(src[pos]); break;
}
}
}
return result.ToString();
}
#endregion
/// <summary>
/// 从数据库从取发送数据
/// </summary>
/// <returns></returns>
public string getData()
{
DateTime tempDateTime = base.smsObject.GetDBDateTime();//获取服务器时间
DataTable dataTable = smsObject.GetListByDateTime(this.syncDateTime);//获取服务器短信数据
this.syncDateTime = tempDateTime;//更新服务器时间
smsObject.UpdateSendDate(successlist, 3);//发送成功队列
smsObject.UpdateSendDate(faillist, 4);//发送失败队列
if (dataTable != null && dataTable.Rows.Count > 0)
{
for (int i = 0; i < dataTable.Rows.Count; i )
{//写入数据
DataRow dataRow = base.sendlist.NewRow();
dataRow["Id"] = dataTable.Rows[i]["SMSInfo_Id"];
dataRow["Type"] = dataTable.Rows[i]["SMSTemplate_Type"];
dataRow["Level"] = dataTable.Rows[i]["SMSTemplate_SendLevel"];
String PhoneNoSpace = null;
PhoneNoSpace = dataTable.Rows[i]["SMSInfo_SendMobilePhone"].ToString().Trim();
//判断是否为手机号
if ((IsNum(PhoneNoSpace) && PhoneNoSpace.Length == 11) || PhoneNoSpace == "10010" || PhoneNoSpace == "10086")
{
dataRow["MobilePhone"] = PhoneNoSpace;
dataRow["Description"] = CardManager.encoding(dataTable.Rows[i]["SMSInfo_Description"].ToString());
dataRow["SendTimes"] = "0";
dataRow["PortNumber"] = "0";
if (dataTable.Rows[i]["SMSTemplate_Type"].ToString() == "0")
{
if (base.sendlist.Rows.Count > 0)
{
base.sendlist.Rows.InsertAt(dataRow, 1);
}
else
{
base.sendlist.Rows.InsertAt(dataRow, 0);
}
}
else
{
base.sendlist.Rows.Add(dataRow);
}
}
else
{
dataRow["MobilePhone"] = PhoneNoSpace;
dataRow["Description"] = "短信内容:" dataTable.Rows[i]["SMSInfo_Description"].ToString() ". " "手机号" dataTable.Rows[i]["SMSInfo_SendMobilePhone"].ToString() "格式不正确";
//写入操作错误数据库
SMSsenderWrong(dataRow["MobilePhone"].ToString(), dataRow["Description"].ToString());
}
}
}
//返回取到的数量
return base.sendlist.Rows.Count.ToString();
}
/// <summary>
/// 数据分发
/// </summary>
/// <param name="DistributionStrategy">分发策略</param>
/// <returns>共分发条数</returns>
public int setData(int DistributionStrategy)
{
switch (DistributionStrategy)
{
//全部短信猫
case 0:
{
DataRow DR = sendlist.NewRow();
while (sendlist.Rows.Count != 0)
{
for (int i = 0; i < gsmGroup.Count; i )
{
if (sendlist.Rows.Count != 0 && gsmGroup[i].ReadCardSign == true)
{
DR = sendlist.Rows[0];
gsmGroup[i].addData(DR);
sendlist.Rows.Remove(DR);
}
else { break; }
}
}
} break;
//全部平台
case 1:
{
DataRow DR = sendlist.NewRow();
while (sendlist.Rows.Count != 0)
{
for (int i = 0; i < RdsG.Count; i )
{
if (sendlist.Rows.Count != 0)
{
DR = sendlist.Rows[0];
RdsG[i].addData(DR);
sendlist.Rows.Remove(DR);
}
else { break; }
}
}
} break;
//混合
case 2:
{
DataRow DR = sendlist.NewRow();
List<ThreadCore> hhgropu = new List<ThreadCore>();
for (int i = 0; i < gsmGroup.Count; i )
{
hhgropu.Add(gsmGroup[i]);
}
for (int i = 0; i < RdsG.Count; i )
{
hhgropu.Add(RdsG[i]);
}
while (sendlist.Rows.Count != 0)
{
for (int i = 0; i < hhgropu.Count; i )
{
if (sendlist.Rows.Count != 0)
{
DR = sendlist.Rows[0];
hhgropu[i].addData(DR);
sendlist.Rows.Remove(DR);
}
else { break; }
}
}
} break;
//单端口
case 3:
{
DataRow DR = sendlist.NewRow();
List<ThreadCore> hhgropu = new List<ThreadCore>();
for (int i = 0; i < gsmGroup.Count; i )
{
hhgropu.Add(gsmGroup[i]);
}
for (int i = 0; i < RdsG.Count; i )
{
hhgropu.Add(RdsG[i]);
}
while (sendlist.Rows.Count != 0)
{
for (int i = 0; i < hhgropu.Count; i )
{
if (sendlist.Rows.Count != 0)
{
DR = sendlist.Rows[0];
if (hhgropu[i].Name == CardManager.debuggingPort) {
hhgropu[i].addData(DR);
sendlist.Rows.Remove(DR);
}
}
else { break; }
}
}
}break;
} return 0;
}
public void closeThread()
{
}
protected override void R1()
{
Thread.Sleep(8000);
this.getData();
}
protected override void R2()
{
Thread.Sleep(1000);
this.setData(CardManager.Cl);
}
//测试全部短信猫是否失效
protected override void R3()
{
int failnumber = 0;
for (int i = 0; i < gsmGroup.Count; i )
{
if (gsmGroup[i].ReadCardSign == false)
{
failnumber ;
}
}
if (gsmGroup.Count == failnumber) this.gsmFail = true; else this.gsmFail = false;
}
}