-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathsendmsgthread.cpp
More file actions
153 lines (133 loc) · 3.89 KB
/
sendmsgthread.cpp
File metadata and controls
153 lines (133 loc) · 3.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#include "sendmsgthread.h"
#include "myhelper.h"
#include "api.h"
#include "dbhelper.h"
SendMsgThread::SendMsgThread(QObject *parent) : QThread(parent)
{
gsm = 0;
tel = "18001797656";
msg = "hello";
headMsg = "端口:短信端口";
}
void SendMsgThread::SetPar(GSMAPI *gsm, QString tel, QString msg)
{
this->gsm = gsm;
this->tel = tel;
this->msg = msg;
}
void SendMsgThread::run()
{
QString str;
if (gsm->Islive()) {
str = "告警短信发送失败";
char ctrlz = 26;
QString msgContent = QString("%1%2%3%4%5")
.arg("0011000D91")
.arg(getTel(tel))
.arg("000801")
.arg(getMsgHex(msg))
.arg(ctrlz);
int len = getMsgLen(msg);
//首先进入发送短信状态
QString result = gsm->SendAT(QString("AT+CMGS=%1\r").arg(len), 300);
//返回结果最末一位为 > 后输入短信内容编码
if (result.right(1) == ">") {
//发送短信正文
result = gsm->SendAT(msgContent, 8000);
if (result.right(2) == "OK") {
str = "告警短信发送成功";
}
}
} else {
str = "短信猫通讯故障";
}
qDebug() << TIME << headMsg << "命令解析:" << str;
API::AddMessage(str);
DBHelper::AddEventInfoUser(str);
}
int SendMsgThread::getMsgLen(QString msg)
{
int len = msg.length() * 2;
len += 15;
return len;
}
QString SendMsgThread::getTel(QString tel)
{
QString temp = "";
//检查手机号码是否按照标准格式写,如果不是则补上
if (tel.mid(0, 2) != "86") {
temp = QString("86%1F").arg(tel);
}
QByteArray ba = temp.toLatin1();
QString str = "";
//按照内存编码格式,将每两位的顺序调换
for (int i = 0; i < 13; i = i + 2) {
str += QString("%1%2").arg(ba[i + 1]).arg(ba[i]);
}
return str;
}
QString SendMsgThread::getMsgHex(QString msg)
{
int len = msg.length();
wchar_t *buffer = new wchar_t[len];
msg.toWCharArray((wchar_t *)buffer);
QStringList data;
for (int i = 0; i < len; i++) {
QString temp = QString::number(buffer[i], 16);
if (temp.length() == 2) {
temp = "00" + temp;
}
data.append(temp);
}
//将长度及短信内容的unicode编码格式内容拼接
QString str = QString("%1%2")
.arg(QString::number(len * 2, 16).toUpper())
.arg(data.join("").toUpper());
delete [] buffer;
return str;
}
QString SendMsgThread::unicodeToTel(QString unicode)
{
QString tel = "";
//按照内存编码格式,将每两位的顺序调换
for (int i = 0; i < 12; i = i + 2) {
tel += QString("%1%2").arg(unicode[i + 1]).arg(unicode[i]);
}
//去除最后一位 F
tel = tel.remove("F");
return tel;
}
QString SendMsgThread::unicodeToTime(QString unicode)
{
QString time = "";
//按照内存编码格式,将每两位的顺序调换
for (int i = 0; i < 12; i = i + 2) {
time += QString("%1%2").arg(unicode[i + 1]).arg(unicode[i]);
}
time = QString("20%1-%2-%3 %4:%5:%6")
.arg(time.mid(0, 2))
.arg(time.mid(2, 2))
.arg(time.mid(4, 2))
.arg(time.mid(6, 2))
.arg(time.mid(8, 2))
.arg(time.mid(10, 2));
return time;
}
QString SendMsgThread::unicodeToMsg(QString unicode)
{
QString msg = "";
QTextCodec *codec = QTextCodec::codecForName("utf-8");
QStringList str;
for(int i = 0; i < unicode.length(); i = i + 4) {
str.append(unicode.mid(i, 4));
}
QString s;
foreach (const QString & unicode, str) {
s.append(unicode.toUShort(0, 16));
}
msg = codec->fromUnicode(s);
return msg;
}