diff --git a/.gitignore b/.gitignore index 2b022e3..d537d18 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ debug/* release/* .qmake.stash -HxUtils.pro.* +utils.pro.* Makefile* *.Debug *.Release diff --git a/HxBroadcast.cpp b/HxBroadcast.cpp index 9d2bfb0..5c89bd5 100644 --- a/HxBroadcast.cpp +++ b/HxBroadcast.cpp @@ -20,12 +20,9 @@ void HxBroadcast::initialization(int port) void HxBroadcast::publish(QString message) { emit broadcast->publish_event(message); } -void HxBroadcast::publish_json(int action_type, std::initializer_list> args) +void HxBroadcast::publish_json(int action_type, QJsonObject msginfo) { - QJsonObject root, msginfo; - - for (std::initializer_list>::const_iterator i = args.begin(); i != args.end(); ++i) - msginfo.insert(i->first, i->second); + QJsonObject root; root.insert("action_type", action_type); root.insert("msgInfo", msginfo); @@ -33,6 +30,16 @@ void HxBroadcast::publish_json(int action_type, std::initializer_list> args) +{ + QJsonObject msginfo; + + for (std::initializer_list>::const_iterator i = args.begin(); i != args.end(); ++i) + msginfo.insert(i->first, i->second); + + publish_json(action_type, msginfo); +} + void HxBroadcast::publish_achieve(QString message) { socket->writeDatagram(message.toUtf8(), QHostAddress::Broadcast, broadcast->port); } void HxBroadcast::receive_ready_read() diff --git a/HxBroadcast.h b/HxBroadcast.h index 8b9ea55..73a25c5 100644 --- a/HxBroadcast.h +++ b/HxBroadcast.h @@ -24,6 +24,14 @@ public: */ static void publish(QString message); + /** + * @brief 发布消息 (JSON格式) + * + * @param action_type 操作类型 + * @param args msgInfo 参数 + */ + static void publish_json(int action_type, QJsonObject msgInfo); + /** * @brief 发布消息 (JSON格式) * diff --git a/HxLog.cpp b/HxLog.cpp deleted file mode 100644 index f8b7cf8..0000000 --- a/HxLog.cpp +++ /dev/null @@ -1,26 +0,0 @@ -#include "HxLog.h" - -#include -#include - -QMutex HxLog::mutex; - -void HxLog::append(QString title, QString message) -{ - mutex.lock(); - - auto current_time = QDateTime::currentDateTime(); - - QFile file(QString("log/%1.log").arg(current_time.toString("yyyyMMdd"))); - - if (file.open(QIODevice::WriteOnly | QIODevice::Append)) - { - auto data = QString("[%1] | [%2] | %3\r\n").arg(current_time.toString("yyyy-MM-dd HH:mm:ss"), title, message); - - file.write(data.toLocal8Bit()); - - file.close(); - } - - mutex.unlock(); -} diff --git a/HxLog.h b/HxLog.h deleted file mode 100644 index c6d7f3f..0000000 --- a/HxLog.h +++ /dev/null @@ -1,15 +0,0 @@ -#ifndef HXLOG_H -#define HXLOG_H - -#include - -class HxLog -{ -public: - static void append(QString title, QString message); - -private: - static QMutex mutex; -}; - -#endif // HXLOG_H diff --git a/HxSql.cpp b/HxSql.cpp deleted file mode 100644 index 01ae018..0000000 --- a/HxSql.cpp +++ /dev/null @@ -1,38 +0,0 @@ -#include "HxSql.h" -#include "HxTrace.h" - -#include -#include -#include -#include - -QSqlDatabase HxSql::open(QString filepath, QString connectionName) -{ - QSqlDatabase database; - if (QSqlDatabase::contains(connectionName)) - database = QSqlDatabase::database(connectionName); - else - database = QSqlDatabase::addDatabase("QSQLITE", connectionName); - - database.setDatabaseName(filepath); - - if (!database.open()) - { - QString bk_filepath = filepath + QString(".[%1].bk").arg(QDateTime::currentDateTime().toString("yyyyMMddHHmmss")); - - QFile::copy(filepath, bk_filepath); - - QFile::remove(filepath); - - HxTrace::debug_write_line("database", QString("file backup success. %1 => %2").arg(filepath).arg(bk_filepath)); - - database.open(); - } - - return database; -} - -void HxSql::close(QString connectionName) -{ - QSqlDatabase::removeDatabase(connectionName); -} diff --git a/HxSql.h b/HxSql.h deleted file mode 100644 index be24331..0000000 --- a/HxSql.h +++ /dev/null @@ -1,24 +0,0 @@ -#ifndef HXSQL_H -#define HXSQL_H - -#include - -class HxSql -{ -public: - /** - * @brief 打开数据库 - * @param filepath 数据库文件路径 - * @param connectionName 连接名 - * @return QSqlDatabase 数据库对象 - */ - static QSqlDatabase open(QString filepath, QString connectionName); - - /** - * @brief 关闭数据库 - * @param connectionName 连接名 - */ - static void close(QString connectionName); -}; - -#endif // HXSQL_H diff --git a/HxTask.cpp b/HxTask.cpp index 9776178..ec41ab3 100644 --- a/HxTask.cpp +++ b/HxTask.cpp @@ -1,7 +1,7 @@ -#include "HxTask.h" +#include "HxTask.h" #include #include QMutex HxTask::mutex; -QMap HxTask::dispatchers; +QMap HxTask::dispatchers; diff --git a/HxTask.h b/HxTask.h index 5fe0e7f..a65b4c0 100644 --- a/HxTask.h +++ b/HxTask.h @@ -11,7 +11,7 @@ public: * @brief 停止 * @param uuid 任务唯一编码 */ - static void stop(QUuid uuid) + static void stop(QString uuid) { if (dispatchers.contains(uuid)) { @@ -63,14 +63,14 @@ public: * @param uuid 任务唯一编码 */ template - static void run(Functor functor, int millisecond, QUuid uuid) + static void run(Functor functor, int millisecond, QString uuid) { dispatchers.insert(uuid, true); QtConcurrent::run( - [=](Functor functor, int _millisecond, QUuid _uuid) + [=](Functor functor, int _millisecond, QString _uuid) { - HxTrace::debug_write_line("HxTask", QString("Thread: %1, start").arg(_uuid.toString())); + HxTrace::debug_write_line("HxTask", QString("Thread: %1, start").arg(_uuid)); while (dispatchers[_uuid]) { @@ -79,7 +79,7 @@ public: QThread::msleep(_millisecond); } - HxTrace::debug_write_line("HxTask", QString("Thread: %1, stop").arg(_uuid.toString())); + HxTrace::debug_write_line("HxTask", QString("Thread: %1, stop").arg(_uuid)); dispatchers.remove(_uuid); }, @@ -110,14 +110,14 @@ public: * @param uuid 任务唯一编码 */ template - static void run(Class *object, T (Class::*fn)(), int millisecond, QUuid uuid) + static void run(Class *object, T (Class::*fn)(), int millisecond, QString uuid) { dispatchers.insert(uuid, true); QtConcurrent::run( - [=](Class *_object, T (Class::*_fn)(), int _millisecond, QUuid _uuid) + [=](Class *_object, T (Class::*_fn)(), int _millisecond, QString _uuid) { - HxTrace::debug_write_line("HxTask", QString("Thread: %1, start").arg(_uuid.toString())); + HxTrace::debug_write_line("HxTask", QString("Thread: %1, start").arg(_uuid)); while (dispatchers[_uuid]) { @@ -126,7 +126,7 @@ public: QThread::msleep(_millisecond); } - HxTrace::debug_write_line("HxTask", QString("Thread: %1, stop").arg(_uuid.toString())); + HxTrace::debug_write_line("HxTask", QString("Thread: %1, stop").arg(_uuid)); dispatchers.remove(_uuid); }, @@ -222,7 +222,7 @@ private: /** * @brief dispatchers */ - static QMap dispatchers; + static QMap dispatchers; }; #endif // HXTASK_H diff --git a/HxTrace.cpp b/HxTrace.cpp index 20b6eda..8b3b480 100644 --- a/HxTrace.cpp +++ b/HxTrace.cpp @@ -1,20 +1,17 @@ -#include "HxTrace.h" +#include "HxTrace.h" #include void HxTrace::debug_write_line(QString title, QString message) { -#ifdef QT_DEBUG - qDebug("[%s] [%s] => %s", + qDebug("%s | %s | %s", qPrintable(QDateTime::currentDateTime().toString("yyyy/MM/dd HH:mm:ss")), qPrintable(title), qPrintable(message)); -#endif } void HxTrace::debug_write_line(QString title, const char *format, ...) { -#ifdef QT_DEBUG char output[1024]; va_list arg_list; @@ -27,9 +24,8 @@ void HxTrace::debug_write_line(QString title, const char *format, ...) va_end(arg_list); - qDebug("[%s] [%s] => %s", + qDebug("%s | %s | %s", qPrintable(QDateTime::currentDateTime().toString("yyyy/MM/dd HH:mm:ss")), qPrintable(title), output); -#endif } diff --git a/utils.cpp b/utils.cpp new file mode 100644 index 0000000..15285db --- /dev/null +++ b/utils.cpp @@ -0,0 +1,3 @@ +#include "utils.h" + +utils::utils() {} diff --git a/utils.h b/utils.h new file mode 100644 index 0000000..f9c95bf --- /dev/null +++ b/utils.h @@ -0,0 +1,10 @@ +#ifndef UTILS_H +#define UTILS_H + +class utils +{ +public: + utils(); +}; + +#endif // UTILS_H diff --git a/HxUtils.pro b/utils.pro similarity index 50% rename from HxUtils.pro rename to utils.pro index 8afd286..7ac0dec 100644 --- a/HxUtils.pro +++ b/utils.pro @@ -1,61 +1,41 @@ -QT -= gui -QT += concurrent -QT += sql -QT += network - -TEMPLATE = lib -CONFIG += staticlib -CONFIG += c++11 - -CONFIG += debug_and_release - -unix { - CONFIG(debug, debug|release){ - TARGET = debug/HxUtils - } else { - TARGET = release/HxUtils - } -} - -# The following define makes your compiler emit warnings if you use -# any Qt feature that has been marked deprecated (the exact warnings -# depend on your compiler). Please consult the documentation of the -# deprecated API in order to know how to port your code away from it. -DEFINES += QT_DEPRECATED_WARNINGS - -# You can also make your code fail to compile if it uses deprecated APIs. -# In order to do so, uncomment the following line. -# You can also select to disable deprecated APIs only up to a certain version of Qt. -#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 - -SOURCES += \ - HxBroadcast.cpp \ - HxDisk.cpp \ - HxJson.cpp \ - HxLog.cpp \ - HxProcess.cpp \ - HxSocket.cpp \ - HxSql.cpp \ - HxSystem.cpp \ - HxTask.cpp \ - HxThread.cpp \ - HxTrace.cpp - -HEADERS += \ - HxBroadcast.h \ - HxDisk.h \ - HxJson.h \ - HxLog.h \ - HxProcess.h \ - HxSocket.h \ - HxSql.h \ - HxSystem.h \ - HxThread.h \ - HxTask.h \ - HxTrace.h - -# Default rules for deployment. -unix { - target.path = $$[QT_INSTALL_PLUGINS]/generic -} -!isEmpty(target.path): INSTALLS += target +QT -= gui +QT += concurrent +QT += sql +QT += network + +TEMPLATE = lib +CONFIG += staticlib + +CONFIG += c++17 + +# You can make your code fail to compile if it uses deprecated APIs. +# In order to do so, uncomment the following line. +#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0 + +SOURCES += \ + HxBroadcast.cpp \ + HxDisk.cpp \ + HxJson.cpp \ + HxProcess.cpp \ + HxSocket.cpp \ + HxSystem.cpp \ + HxTask.cpp \ + HxThread.cpp \ + HxTrace.cpp + +HEADERS += \ + HxBroadcast.h \ + HxDisk.h \ + HxJson.h \ + HxProcess.h \ + HxSocket.h \ + HxSystem.h \ + HxTask.h \ + HxThread.h \ + HxTrace.h + +# Default rules for deployment. +unix { + target.path = $$[QT_INSTALL_PLUGINS]/generic +} +!isEmpty(target.path): INSTALLS += target