短信告警这个模块在很多项目中都用上了,比如之前做过的安防系统,温湿度报警系统等,主要的流程就是收到数据判断属于某种报警后,组织短信字符串内容,发送到指定的多个手机号码上面,使用的是短信猫硬件设备,其实就是个短信模块,没有使用网络的发送短信的api,毕竟大部分的软件应用场景都不能要求连通外网,安全考虑,所以必须采用本地的硬件来实现发送短信。这个短信猫设备在8年前开始用过,当时用的C#去写了个纯协议解析短信的收发,后面用Qt重新写了个。特意写了个类SendMsgThread封装了所有的操作,这样就通用任何开发环境了,掌握了原理,从底层原理上着手,好处还是很多的,我看很多初学者热衷于去调用第三方厂家封装好的SDK,一旦换了操作系统,完蛋了歇菜,没有嵌入式linux系统的SDK开发包咋搞。
皮肤开源:https://gitee.com/feiyangqingyun/QWidgetDemo https://github.com/feiyangqingyun/QWidgetDemo
文件名称:styledemo
体验地址:https://gitee.com/feiyangqingyun/QWidgetExe https://github.com/feiyangqingyun/QWidgetExe
文件名称:bin_sams.zip
#include "sendserver.h"
#include "quiwidget.h"
#include "dbhelper.h"
#include "devicehelper.h"
#include "sendmsgthread.h"
#include "sendemailthread.h"
QScopedPointer SendServer::self;
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 <= 1440 && listMsg.count() <= 30) {
listMsg << msg;
}
}
void SendServer::appendEmail(const QString &email)
{
if (App::EmailInterval <= 1440 && listEmail.count() <= 30) {
listEmail << email;
}
}
void SendServer::append(const QString &content)
{
appendMsg(content);
appendEmail(content);
}
void SendServer::clearMsg()
{
listMsg.clear();
}
void SendServer::clearEmail()
{
listEmail.clear();
}
void SendServer::checkMsg()
{
int count = listMsg.count();
if (count == 0) {
return;
}
//一条短信最多70个汉字160个全英文数字字符,同时保证是一条完成的报警信息
//将要发送的手机号码和短信内容追加到发送报警短信线程中
for (int i = 0; i < count; i++) {
QString msg = listMsg.takeFirst();
QStringList tels = App::MsgTel.split(";");
foreach (QString tel, tels) {
SendMsgThread::Instance()->append(tel, msg);
}
}
}
void SendServer::checkEmail()
{
int count = listEmail.count();
if (count == 0) {
return;
}
//邮件内容,无论多长都可以拼在一起发送,
用来换行显示
QStringList listContent, listFileName;
for (int i = 0; i < count; i++) {
QString str = listEmail.takeFirst();
if (str.contains("|")) {
QStringList list = str.split("|");
listContent.append(list.at(0));
listFileName.append(list.at(1));
} else {
listContent.append(str);
}
}
//将字符串转换为表格形式,先取出字段名,然后取出内容
//位号: AT-40002 控制器: 1#调度室控制器 探测器: 2#阀站CO 触发值: 78.7 %LEL 类型: 浓度上限报警 时间: 2019-02-24 21:01:30
//位号: AT-40003 控制器: 1#调度室控制器 探测器: 3#阀站CO 触发值: 57 %VOL 类型: 浓度下限报警 时间: 2019-02-24 21:01:30
//位号: AT-40004 控制器: 1#调度室控制器 探测器: 4#阀站CO 触发值: 88 PPM 类型: 浓度下限报警 时间: 2019-02-24 21:01:30
//先取出字段名
QString str = listContent.first();
QStringList list = str.split(" ");
QStringList columnNames;
foreach (QString str, list) {
columnNames << str.split(": ").first();
}
//字段宽度
QList columnWidth;
columnWidth << 150 << 200 << 180 << 130 << 160 << 200;
//再遍历取出所有数据
QList rowContents;
foreach (QString str, listContent) {
QStringList list = str.split(" ");
QStringList rowContent;
foreach (QString temp, list) {
rowContent << temp.split(": ").last();
}
rowContents << rowContent;
}
//构建html字符串
QStringList html;
//表格开始
html.append("");
//标题字段名称
QString title;
for (int i = 0; i < columnNames.count(); i++) {
title += QString("%2 ")
.arg(columnWidth.at(i)).arg(columnNames.at(i));
}
html.append(QString("%1 ").arg(title));
//内容
for (int j = 0; j < rowContents.count(); j++) {
QString row;
QStringList rowContent = rowContents.at(j);
for (int i = 0; i < rowContent.count(); i++) {
row += QString("%2 ")
.arg(columnWidth.at(i)).arg(rowContent.at(i));
}
html.append(QString("%1 ").arg(row));
}
//表格结束
html.append("
");
qDebug() << html;
//将邮件内容和附件链接追加到发送报警邮件线程中
SendEmailThread::Instance()->setSendEmailAddr(App::SendEmailAddr);
SendEmailThread::Instance()->setSendEmailPwd(App::SendEmailPwd);
SendEmailThread::Instance()->setReceiveEmailAddr(App::ReceiveEmailAddr);
SendEmailThread::Instance()->append(html.join(""), listFileName.join(";"));
//SendEmailThread::Instance()->append(listContent.join("
"), listFileName.join(";"));
}