1 //======================================================================
2 //
3 // copyright (C) 2012 All rights reserved
4 // framework : 4.0
5 // filename : SmsEngine
6 // description :
7 // author : marvin(马非马)
8 // company : Sysu.im.06imis
9 // create time : 2012/9/5 14:19:24
10 //
11 //======================================================================
12 using System;
13 using System.IO.Ports;
14 using System.Linq;
15 using System.Text;
16 using System.Threading;
17 using System.Threading.Tasks;
18 using Loncomip.Utility.IO;
19
20 namespace Loncomip.Utility.SmsLib
21 {
22 public class SmsEngine
23 {
24 #region Event
25 /// <summary>
26 /// 接收到短信时处理的回调函数
27 /// </summary>
28 public delegate void MsgReceivedHandler(object sender, MsgReceivedEventArgs e);
29 /// <summary>
30 /// 从串口缓冲区中读到任何内容时的回调函数
31 /// </summary>
32 public delegate void ReadBufferHandler(object sender, ReadBufferEventArgs e);
33 /// <summary>
34 /// 短信引擎发生异常时的回调函数
35 /// </summary>
36 public delegate void EngineExceptionHandler(object sender, EngineExceptionEventArgs e);
37
38 /// <summary>
39 ///
40 /// </summary>
41 public class MsgReceivedEventArgs : EventArgs
42 {
43 /// <summary>
44 /// 发送人的号码
45 /// </summary>
46 /// <value>The sender no.</value>
47 public string SenderNo { get; set; }
48 /// <summary>
49 /// 服务中心发送短信的时间
50 /// </summary>
51 /// <value>The service center send time.</value>
52 public DateTime ServiceCenterSendTime { get; set; }
53 /// <summary>
54 /// 短信的内容
55 /// </summary>
56 /// <value>The content.</value>
57 public string Content { get; set; }
58 public string RawBuffer { get; set; }
59
60 public MsgReceivedEventArgs(DateTime time, string no, string content)
61 {
62 ServiceCenterSendTime = time;
63 SenderNo = no;
64 Content = content;
65 }
66 }
67 /// <summary>
68 ///
69 /// </summary>
70 public class ReadBufferEventArgs : EventArgs
71 {
72 public string Buffer { get; set; }
73 public ReadBufferEventArgs(string buffer)
74 {
75 Buffer = buffer;
76 }
77 }
78 /// <summary>
79 ///
80 /// </summary>
81 public class EngineExceptionEventArgs : EventArgs
82 {
83 public Exception Source { get; set; }
84 public string Message { get; set; }
85
86 public EngineExceptionEventArgs(Exception e, string msg)
87 {
88 Source = e;
89 Message = msg;
90 }
91 }
92
93 public event MsgReceivedHandler OnReceivedMessage;
94 public event ReadBufferHandler OnBufferRead;
95 public event EngineExceptionHandler OnEngineException;
96 #endregion
97
98 #region Fields
99 private SerialPort _serialPort;
100 private CancellationTokenSource _cts;
101 private readonly string _comName;
102 #endregion
103
104 #region Construct
105 public SmsEngine(string comName)
106 {
107 _comName = comName;
108 QueryInterval = 5;
109 }
110
111 public SmsEngine(string comName, bool initCancellationTokenSource)
112 {
113 _comName = comName;
114 QueryInterval = 5;
115
116 if (initCancellationTokenSource) _cts = new CancellationTokenSource();
117 }
118 #endregion
119
120 public int QueryInterval
121 {
122 get;
123 set;
124 }
125
126 /// <summary>
127 /// Starts this instance.
128 /// </summary>
129 public void Start()
130 {
131 _cts = new CancellationTokenSource();
132 var task = new Task(Run, _cts.Token);
133 task.Start();
134 }
135
136 /// <summary>
137 /// Stops this instance.
138 /// </summary>
139 public void Stop()
140 {
141 _cts.Cancel();
142 }
143
144 /// <summary>
145 /// 线程主函数
146 /// </summary>
147 private void Run()
148 {
149 while (!_cts.IsCancellationRequested)
150 {
151 try
152 {
153 GetAllMsg();
154 }
155 catch (Exception e)
156 {
157 HandelEnginException(e, "线程异常:{0}", e.Message);
158 _cts.Token.WaitHandle.WaitOne(2 * 1000);
159 }
160 finally
161 {
162 _cts.Token.WaitHandle.WaitOne(QueryInterval * 1000);
163 }
164 }
165 }
166
167 /// <summary>
168 /// 获取所有的短信
169 /// </summary>
170 private void GetAllMsg()
171 {
172 OpenAndTestModem();
173 WriteCmd(SMSUtil.GenCmdGetList(MessageListType.All));//获取所有未阅读的消息
174
175 var result = GetResult();
176 if (string.IsNullOrEmpty(result)) return;
177
178 var list = SMSUtil.DecodeSMSGetResult(result);
179 if (list == null || !list.Any()) return;
180
181 foreach (var item in list)
182 {
183 try
184 {
185 WriteCmd(SMSUtil.GenCmdDeleteMsg(item.MsgIndex));
186 if (SMSUtil.IsSendCmdSuccess(GetResult()))
187 {//成功删除之后,才添加到数据库中
188 NLogHelper.Trace("已成功删除短信{0}#{1},将短信添加到数据库中...", item.MsgIndex,item.SenderNo);
189 var handler = OnReceivedMessage;
190 if (handler != null) handler(this, new MsgReceivedEventArgs(item.ServiceCenterTimeStamp, item.SenderNo, item.Message) { RawBuffer = result });
191 }
192 else
193 {
194 NLogHelper.Warn("删除短信失败{0}#{1}#{2}#{3}失败", item.MsgIndex, item.SenderNo, item.ServiceCenterTimeStamp, item.Message);
195 }
196 }
197 catch (Exception e)
198 {
199 HandelEnginException(e, "处理单条短信:{0}异常:{1}", item.Message, e.Message);
200 continue;
201 }
202 }
203 }
204
205 /// <summary>
206 /// 写入命令,并返回命令是否写入成功
207 /// </summary>
208 /// <param name="cmd">AT命令</param>
209 /// <param name="waitTime">每次写入命令的间隔时间</param>
210 /// <returns>返回命令的执行情况</returns>
211 private void WriteCmd(string cmd, int waitTime = 500)
212 {
213 try
214 {
215 var buffer = Encoding.ASCII.GetBytes(cmd);
216 _serialPort.Write(buffer, 0, buffer.Length);
217 _cts.Token.WaitHandle.WaitOne(waitTime);
218 }
219 catch (Exception e)
220 {
221 HandelEnginException(e, "往串口写入命令异常:{0}", e.Message);
222 }
223 }
224
225 /// <summary>
226 /// 获取串口中的返回值
227 /// </summary>
228 /// <param name="waitTime">The wait time.</param>
229 /// <returns></returns>
230 private string GetResult(int waitTime = 500)
231 {
232 var len = _serialPort.BytesToRead;
233 var result = new StringBuilder();
234 while (len > 0)
235 {
236 var buffer = new byte[len];
237 _serialPort.Read(buffer, 0, len);
238 result.Append(Encoding.ASCII.GetString(buffer));
239
240 _cts.Token.WaitHandle.WaitOne(waitTime);
241 len = _serialPort.BytesToRead;
242 }
243 if (result.Length > 0)
244 {
245 var handler = OnBufferRead;
246 if (handler != null) OnBufferRead(this, new ReadBufferEventArgs(result.ToString()));
247 }
248 return result.ToString();
249 }
250
251 /// <summary>
252 /// 打开串口并测试串口是否正常工作
253 /// </summary>
254 private void OpenAndTestModem()
255 {
256 while (!_cts.IsCancellationRequested && _serialPort == null)
257 {
258 OpenModem();
259 }
260 while (!_cts.IsCancellationRequested && !TestModem())
261 {
262
263 }
264 }
265
266 /// <summary>
267 /// 测试Modem是否在工作
268 /// </summary>
269 /// <returns></returns>
270 private bool TestModem()
271 {
272 if (_serialPort == null)
273 {
274 _cts.Token.WaitHandle.WaitOne(500);
275 return false;
276 }
277
278 try
279 {
280 WriteCmd("AT\r");//确认串口是否在工作
281 if (!SMSUtil.IsSendCmdSuccess(GetResult())) return false;
282
283 WriteCmd(SMSUtil.GenCmdMsgFormat());
284 if (!SMSUtil.IsSendCmdSuccess(GetResult())) return false;//设置接收格式为PDU
285
286 return true;
287 }
288 catch (Exception e)
289 {
290 HandelEnginException(e, "测试Modem异常:{0}", e.Message);
291 _cts.Token.WaitHandle.WaitOne(5 * 1000);
292 return false;
293 }
294 }
295
296 /// <summary>
297 /// 打开串口
298 /// </summary>
299 private void OpenModem()
300 {
301 try
302 {
303 _serialPort = new SerialPort
304 {
305 PortName = _comName,
306 BaudRate = 115200,
307 Parity = Parity.None,
308 DataBits = 8,
309 StopBits = StopBits.One,
310 Handshake = Handshake.None,
311 RtsEnable = true,
312 DtrEnable = true
313 };
314 _serialPort.Open();
315 }
316 catch (Exception e)
317 {
318 _serialPort = null;
319
320 HandelEnginException(e, "打开串口失败:{0}", e.Message);
321 _cts.Token.WaitHandle.WaitOne(5 * 1000);
322 }
323 }
324
325 /// <summary>
326 /// 处理异常
327 /// </summary>
328 /// <param name="e">The e.</param>
329 /// <param name="msg">The MSG.</param>
330 /// <param name="args">The args.</param>
331 private void HandelEnginException(Exception e, string msg, params object[] args)
332 {
333 var handler = OnEngineException;
334 if (handler != null)
335 {
336 try
337 {
338 if (args.Length > 0) msg = string.Format(msg, args);
339 handler(this, new EngineExceptionEventArgs(e, msg));
340 }
341 catch (Exception ex)
342 {
343 NLogHelper.Warn("外部异常处理异常:{0}", ex);
344 }
345
346 }
347 NLogHelper.Warn(e.ToString());
348 }
349 }
350 }