Skip to content
This repository has been archived by the owner on May 11, 2022. It is now read-only.

Commit

Permalink
错误提醒改进
Browse files Browse the repository at this point in the history
  • Loading branch information
skykeyjoker committed Dec 1, 2021
1 parent bf408ef commit a89246a
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 28 deletions.
98 changes: 81 additions & 17 deletions src/ASoulNotification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <QDebug>
#include <QDir>
#include <QFile>
#include <QTimer>
#include <spdlog/spdlog.h>
#include <spdlog/sinks/basic_file_sink.h>
#include <chrono>
Expand All @@ -19,7 +20,9 @@

using namespace WinToastLib;

const QString VERSION = "1.4.1";
const QString VERSION = "1.5.0";

static int errCount = 0;

class CustomHandler : public IWinToastHandler {
public:
Expand Down Expand Up @@ -114,7 +117,7 @@ QString checkForUpdate(const QString& version, std::shared_ptr<spdlog::logger> &
}

qDebug() << remoteUrl << remoteVersion<< QString("v" + version);
logger->info("Romote Version: {}, Remote Url: {}", version.toStdString(), remoteUrl.toStdString());
logger->info("Romote Version: {}, Remote Url: {}", remoteVersion.toStdString(), remoteUrl.toStdString());
if (QString("v" + version).compare(remoteVersion) != 0) // 本地版本与远程版本不同
{
retUrlStr = remoteUrl;
Expand All @@ -127,8 +130,6 @@ QString checkForUpdate(const QString& version, std::shared_ptr<spdlog::logger> &
return retUrlStr;
}



int main(int argc, char* argv[])
{
QCoreApplication app(argc, argv);
Expand Down Expand Up @@ -274,6 +275,7 @@ int main(int argc, char* argv[])

BiliBiliMessage bilibiliMessager(main_logger, uid_list);

// 直播消息提醒
QObject::connect(&bilibiliMessager, &BiliBiliMessage::newBilibiliLive, [&](int user, const QString title, const QString url)
{
main_logger->info("新直播消息信号,启动直播提醒Wintoast。成员:{},标题:{},网址:{}", user, title.toStdString(), url.toStdString());
Expand Down Expand Up @@ -301,6 +303,8 @@ int main(int argc, char* argv[])
main_logger->error("直播提醒WinToast启动失败!");
}
});

// 动态消息提醒
QObject::connect(&bilibiliMessager, &BiliBiliMessage::newBilibiliMessage, [&](int user, int type, const QString dynamic_id_str)
{
main_logger->info("新动态消息信号,启动动态提醒Wintoast。成员:{},类型:{},动态id:{}", user, type, dynamic_id_str.toStdString());
Expand Down Expand Up @@ -341,23 +345,83 @@ int main(int argc, char* argv[])
main_logger->error("动态提醒WinToast启动失败!");
}
});

// 错误提醒
QObject::connect(&bilibiliMessager, &BiliBiliMessage::errorOccurred, [&](const QString errorString)
{
qDebug() << "Error Occurred Signal...";
main_logger->info("新错误信号,启动错误提醒Wintoast。错误信息:{}", errorString.toStdString());
WinToastTemplate templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
QString imagePath = app.applicationDirPath() + "/avatar/error.png";
templ.setImagePath(imagePath.toStdWString());
templ.setTextField(L"插件发生错误", WinToastTemplate::FirstLine);
templ.setTextField(errorString.toStdWString(), WinToastTemplate::SecondLine);
templ.setExpiration(0);
templ.setAudioPath(WinToastTemplate::AudioSystemFile::DefaultSound);
templ.setAudioOption(WinToastTemplate::AudioOption::Default);
errCount++;
qDebug() << errCount;

if (WinToast::instance()->showToast(templ, new CustomHandler(0)) < 0) {
//QMessageBox::warning(this, "Error", "Could not launch your toast notification!");
qDebug() << "Could not launch your toast notification!";
main_logger->error("错误提醒WinToast启动失败!");
if (errCount < 3)
{
main_logger->error("新错误信号,启动错误提醒Wintoast。错误信息:{}", errorString.toStdString());
WinToastTemplate templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
QString imagePath = app.applicationDirPath() + "/avatar/error.png";
templ.setImagePath(imagePath.toStdWString());
templ.setTextField(L"插件发生错误", WinToastTemplate::FirstLine);
templ.setTextField(errorString.toStdWString(), WinToastTemplate::SecondLine);
templ.setExpiration(0);
templ.setAudioPath(WinToastTemplate::AudioSystemFile::DefaultSound);
templ.setAudioOption(WinToastTemplate::AudioOption::Default);

if (WinToast::instance()->showToast(templ, new CustomHandler(0)) < 0) {
//QMessageBox::warning(this, "Error", "Could not launch your toast notification!");
qDebug() << "Could not launch your toast notification!";
main_logger->error("错误提醒WinToast启动失败!");
}
}
else if (errCount == 3)
{
// 错误次数达到3次及以上,停止发送错误卡片
qDebug()<< "错误信号数已达到3次,停止发送错误提醒。";
main_logger->error("错误信号数已达到3次,停止发送错误提醒。");
WinToastTemplate templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
QString imagePath = app.applicationDirPath() + "/avatar/error.png";
templ.setImagePath(imagePath.toStdWString());
templ.setTextField(L"插件发生错误", WinToastTemplate::FirstLine);
templ.setTextField(L"插件错误达到3次,停止错误提醒。\n10分钟后错误提醒计数会自动归零,此后错误提醒会正常发送。", WinToastTemplate::SecondLine);
templ.setExpiration(0);
templ.setAudioPath(WinToastTemplate::AudioSystemFile::DefaultSound);
templ.setAudioOption(WinToastTemplate::AudioOption::Default);

if (WinToast::instance()->showToast(templ, new CustomHandler(0)) < 0) {
//QMessageBox::warning(this, "Error", "Could not launch your toast notification!");
qDebug() << "Could not launch your toast notification!";
main_logger->error("错误提醒WinToast启动失败!");
}

qDebug() << "错误信号数清零计时开始";
main_logger->info("错误信号数清零计时开始");
QTimer::singleShot(600000, [&]() {
qDebug() << "错误信号数清零计时达10分钟";
main_logger->info("错误信号数清零计时达10分钟");
errCount = 0;
});

}
else if (errCount >= 6)
{
// 错误次数达到6次及以上,退出程序
qDebug() << "错误信号数已达到6次及以上,发送错误提醒并退出程序。";
main_logger->error("错误信号数已达到6次及以上,发送错误提醒并退出程序。");
main_logger->flush();
WinToastTemplate templ = WinToastTemplate(WinToastTemplate::ImageAndText02);
QString imagePath = app.applicationDirPath() + "/avatar/exit.png";
templ.setImagePath(imagePath.toStdWString());
templ.setTextField(L"插件发生错误", WinToastTemplate::FirstLine);
templ.setTextField(L"插件错误达到6次,插件已退出。", WinToastTemplate::SecondLine);
templ.setExpiration(0);
templ.setAudioPath(WinToastTemplate::AudioSystemFile::DefaultSound);
templ.setAudioOption(WinToastTemplate::AudioOption::Default);

if (WinToast::instance()->showToast(templ, new CustomHandler(0)) < 0) {
//QMessageBox::warning(this, "Error", "Could not launch your toast notification!");
qDebug() << "Could not launch your toast notification!";
main_logger->error("错误提醒WinToast启动失败!");
}

exit(1);
}
});

Expand Down
26 changes: 15 additions & 11 deletions src/BilibiliMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,12 @@ void BiliBiliMessage::startQuery()
QThread::sleep(5);
}
QThread::sleep(5);
//emit errorOccurred("查询成员动态时发生错误,返回了空的消息卡片!");
//emit errorOccurred("查询成员直播状态时发生错误,返回了空的直播状态卡片!");
//errCnt += 2;
//if (errCnt < 6)
//{
// emit errorOccurred("查询成员动态时发生错误,返回了空的消息卡片!");
// emit errorOccurred("查询成员直播状态时发生错误,返回了空的直播状态卡片!");
//}
//emit errorOccurred("查询成员动态时发生错误,获取的JSON值为空!");
//emit errorOccurred("查询成员直播状态时发生错误,获取的JSON值为空!");
//emit errorOccurred("请求失败,错误类型:no network");
Expand All @@ -106,7 +110,7 @@ BilibiliMessageCard BiliBiliMessage::messageQuery(const QString& url)
qDebug() << "Message Query Error. Reason: The json is null";
QString errorString = "查询成员动态时发生错误,获取的JSON值为空!";
m_logger->error("查询成员动态时发生错误,获取的JSON值为空!");
emit errorOccurred(errorString);
//emit errorOccurred(errorString);
return ret;
}

Expand All @@ -129,7 +133,7 @@ BilibiliMessageCard BiliBiliMessage::messageQuery(const QString& url)
qDebug() << "Message Query Error. Reason: The json can not be parsed";
QString errorString = "查询成员动态时发生错误,未能解析获取的JSON!";
m_logger->error("查询成员动态时发生错误,未能解析获取的JSON!");
emit errorOccurred(errorString);
//emit errorOccurred(errorString);
return ret;
}

Expand All @@ -156,7 +160,7 @@ BilibiliLiveCard BiliBiliMessage::liveQuery(const QString& url)
qDebug() << "Live Query Error. Reason: The json is null";
QString errorString = "查询成员直播状态时发生错误,获取的JSON值为空!";
m_logger->error("查询成员直播状态时发生错误,获取的JSON值为空!");
emit errorOccurred(errorString);
//emit errorOccurred(errorString);
return ret;
}

Expand All @@ -181,7 +185,7 @@ BilibiliLiveCard BiliBiliMessage::liveQuery(const QString& url)
qDebug() << "Live Query Error. Reason: The json can not be parsed";
QString errorString = "查询成员直播状态时发生错误,未能解析获取的JSON!";
m_logger->error("查询成员直播状态时发生错误,未能解析获取的JSON!");
emit errorOccurred(errorString);
//emit errorOccurred(errorString);
return ret;
}

Expand Down Expand Up @@ -215,7 +219,7 @@ Json BiliBiliMessage::getJson(const QString& url)
qDebug() << "Query Error! Reason: request error" << reply->errorString() << url;
m_logger->error("请求失败,错误类型:{}", reply->errorString().toStdString());
QString errorString = "请求失败,错误类型:" + reply->errorString();
emit errorOccurred(errorString);
//emit errorOccurred(errorString);
return j;
}

Expand All @@ -225,7 +229,7 @@ Json BiliBiliMessage::getJson(const QString& url)
qDebug() << "No Data return";
m_logger->error("请求失败,获取了空的返回数据!");
QString errorString = "请求失败,获取了空的返回数据!";
emit errorOccurred(errorString);
//emit errorOccurred(errorString);
return j;
}

Expand All @@ -235,7 +239,7 @@ Json BiliBiliMessage::getJson(const QString& url)
qDebug() << "Parse Json Error!"<< url;
m_logger->error("请求失败,未能成功解析JSON!");
QString errorString = "请求失败,未能成功解析JSON!";
emit errorOccurred(errorString);
//emit errorOccurred(errorString);
return j;
}

Expand All @@ -250,7 +254,7 @@ Json BiliBiliMessage::getJson(const QString& url)
qDebug() << "Parse Json Error! Can not get the [code]";
m_logger->error("请求失败,解析JSON[code]时失败!");
QString errorString = "请求失败,解析JSON[code]时失败!";
emit errorOccurred(errorString);
//emit errorOccurred(errorString);
return Json(); // 返回空的JSON
}

Expand All @@ -259,7 +263,7 @@ Json BiliBiliMessage::getJson(const QString& url)
qDebug() << "Return Code is not null";
m_logger->error("请求失败,返回状态码:{}", retCode);
QString errorString = "请求失败,返回状态码:" + QString::number(retCode);
emit errorOccurred(errorString);
//emit errorOccurred(errorString);
return Json(); // 返回空的JSON
}

Expand Down
1 change: 1 addition & 0 deletions src/BilibiliMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class BiliBiliMessage: public QObject
int currentLive;
std::shared_ptr<spdlog::logger>& m_logger;
QStringList m_uidList;
//int errCnt = 0;
};


Expand Down
Binary file added src/avatar/exit.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions version.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
{
"versions": [
{
"version": "v1.5.0",
"url": "https://github.com/skykeyjoker/A-Soul-Notification/releases/tag/v1.5.0"
},
{
"version": "v1.4.1",
"url": "https://github.com/skykeyjoker/A-Soul-Notification/releases/tag/v1.4.1"
Expand Down

0 comments on commit a89246a

Please sign in to comment.