短信告警这个模块在很多项目中都用上了,比如之前做过的安防系统,温湿度报警系统等,主要的流程就是收到数据判断属于某种报警后,组织短信字符串内容,发送到指定的多个手机号码上面,使用的是短信猫硬件设备,其实就是个短信模块,没有使用网络的发送短信的api,毕竟大部分的软件应用场景都不能要求连通外网,安全考虑,所以必须采用本地的硬件来实现发送短信。这个短信猫设备在8年前开始用过,当时用的C#去写了个纯协议解析短信的收发,后面用Qt重新写了个。特意写了个类SendMsgThread封装了所有的操作,这样就通用任何开发环境了,掌握了原理,从底层原理上着手,好处还是很多的,我看很多初学者热衷于去调用第三方厂家封装好的SDK,一旦换了操作系统,完蛋了歇菜,没有嵌入式linux系统的SDK开发包咋搞。
#include "sendserver.h"
#include "quiwidget.h"
#include "dbhelper.h"
#include "devicehelper.h"
#include "sendmsgthread.h"
#include "sendemailthread.h"QScopedPointer
SendServer *SendServer::Instance()
{if (self.isNull()) {static QMutex mutex;QMutexLocker locker(&mutex);if (self.isNull()) {self.reset(new SendServer);}}return self.data();
}SendServer::SendServer(QObject *parent) : QObject(parent)
{//初始化发送报警短信定时器timerMsg = new QTimer(this);connect(timerMsg, SIGNAL(timeout()), this, SLOT(checkMsg()));changeMsgInterval();//初始化发送报警邮件定时器timerEmail = new QTimer(this);connect(timerEmail, SIGNAL(timeout()), this, SLOT(checkEmail()));changeEmailInterval();
}SendServer::~SendServer()
{this->stop();
}void SendServer::receiveMsgResult(const QString &result)
{if (App::MsgInterval != 10000) {//DeviceHelper::addMsg(result, 0);DBHelper::addUserLog("设备上报", result);if (result.contains("失败")) {QUIHelper::showMessageBoxError(result, 3);} else {QUIHelper::showMessageBoxInfo(result, 3);}}
}void SendServer::receiveEmailResult(const QString &result)
{if (App::EmailInterval != 10000) {//DeviceHelper::addMsg(result, 0);DBHelper::addUserLog("设备上报", result);if (result.contains("失败")) {QUIHelper::showMessageBoxError(result, 3);} else {QUIHelper::showMessageBoxInfo(result, 3);}}
}void SendServer::start()
{if (App::MsgInterval != 10000) {QStringList tels = App::MsgTel.split(";");foreach (QString tel, tels) {if (!QUIHelper::isTel(tel)) {//DeviceHelper::addMsg("转发手机号码错误", 0);break;}}}//启动发送报警短信线程发送短信,防止界面卡住if (SendMsgThread::Instance()->open(App::MsgPortName)) {connect(SendMsgThread::Instance(), SIGNAL(receiveResult(QString)), this, SLOT(receiveMsgResult(QString)));SendMsgThread::Instance()->init();SendMsgThread::Instance()->start();//DeviceHelper::addMsg("短信端口打开成功",0);DBHelper::addUserLog("设备上报", "短信端口打开成功");} else {if (App::MsgInterval != 10000) {//DeviceHelper::addMsg("短信端口打开失败",0);DBHelper::addUserLog("设备上报", "短信端口打开失败");}}if (App::MsgInterval != 10000) {if (!QUIHelper::isEmail(App::SendEmailAddr)) {//DeviceHelper::addMsg("发件人邮箱格式错误",0);DBHelper::addUserLog("设备上报", "发件人邮箱格式错误");}QStringList emails = App::ReceiveEmailAddr.split(";");foreach (QString email, emails) {if (!QUIHelper::isEmail(email)) {//DeviceHelper::addMsg("收件人邮箱格式错误",0);DBHelper::addUserLog("设备上报", "收件人邮箱格式错误");break;}}}//启动发送报警邮件线程发送邮件,防止界面卡住connect(SendEmailThread::Instance(), SIGNAL(receiveEmailResult(QString)), this, SLOT(receiveEmailResult(QString)));SendEmailThread::Instance()->setEmialTitle(App::LogoCn + "报警邮件");SendEmailThread::Instance()->setSendEmailAddr(App::SendEmailAddr);SendEmailThread::Instance()->setSendEmailPwd(App::SendEmailPwd);SendEmailThread::Instance()->setReceiveEmailAddr(App::ReceiveEmailAddr);SendEmailThread::Instance()->start();
}void SendServer::stop()
{timerMsg->stop();timerEmail->stop();
}void SendServer::changeMsgInterval()
{//如果间隔是0则按照10秒间隔发送,如果是10000则不启动if (App::MsgInterval == 0) {timerMsg->start(10000);} else if (App::MsgInterval == 10000) {timerMsg->stop();} else {timerMsg->start(App::MsgInterval * 60 * 1000);}
}void SendServer::changeEmailInterval()
{//如果间隔是0秒则按照10秒间隔发送,如果是10000则不启动if (App::EmailInterval == 0) {timerEmail->start(10000);} else if (App::EmailInterval == 10000) {timerEmail->stop();} else {timerEmail->start(App::EmailInterval * 60 * 1000);}
}void SendServer::appendMsg(const QString &msg)
{if (App::MsgInterval <&#61; 1440 && listMsg.count() <&#61; 30) {listMsg <
{if (App::EmailInterval <&#61; 1440 && listEmail.count() <&#61; 30) {listEmail <
{appendMsg(content);appendEmail(content);
}void SendServer::clearMsg()
{listMsg.clear();
}void SendServer::clearEmail()
{listEmail.clear();
}void SendServer::checkMsg()
{int count &#61; listMsg.count();if (count &#61;&#61; 0) {return;}//一条短信最多70个汉字160个全英文数字字符,同时保证是一条完成的报警信息//将要发送的手机号码和短信内容追加到发送报警短信线程中for (int i &#61; 0; i
}void SendServer::checkEmail()
{int count &#61; listEmail.count();if (count &#61;&#61; 0) {return;}//邮件内容,无论多长都可以拼在一起发送,
用来换行显示QStringList listContent, listFileName;for (int i &#61; 0; i
//位号: AT-40004 控制器: 1#调度室控制器 探测器: 4#阀站CO 触发值: 88 PPM 类型: 浓度下限报警 时间: 2019-02-24 21:01:30//先取出字段名QString str &#61; listContent.first();QStringList list &#61; str.split(" ");QStringList columnNames;foreach (QString str, list) {columnNames <%1 ").arg(title));//内容for (int j &#61; 0; j %1 ").arg(row));}//表格结束html.append("");qDebug()
"), listFileName.join(";"));
}