From e827eb150a787bf4d056495ab1b346ca95e0bb50 Mon Sep 17 00:00:00 2001 From: hehaoyang <1109196436@qq.com> Date: Sun, 21 Jan 2024 22:06:55 +0800 Subject: [PATCH] =?UTF-8?q?1.=20=E6=B7=BB=E5=8A=A0JSON=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E6=89=A9=E5=B1=95=E5=BA=93;=202.=20=E5=B9=BF=E6=92=AD=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E7=89=B9=E5=AE=9A=E7=9A=84JSON=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E6=95=B0=E6=8D=AE=E5=8F=91=E9=80=81;=203.=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?HxTask=E6=93=8D=E4=BD=9C=E6=96=B9=E6=B3=95:=20wait,invoke?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HxBroadcast.cpp | 27 +++++++++----- HxBroadcast.h | 10 +++++- HxJson.cpp | 57 ++++++++++++++++++++++++++++++ HxJson.h | 15 ++++++++ HxSystem.cpp | 7 +++- HxTask.h | 93 ++++++++++++++++++++++++++++++++++--------------- HxUtils.pro | 2 ++ 7 files changed, 172 insertions(+), 39 deletions(-) create mode 100644 HxJson.cpp create mode 100644 HxJson.h diff --git a/HxBroadcast.cpp b/HxBroadcast.cpp index d9990f9..a35d256 100644 --- a/HxBroadcast.cpp +++ b/HxBroadcast.cpp @@ -1,10 +1,12 @@ #include "HxBroadcast.h" #include +#include +#include HxBroadcast *broadcast = nullptr; -HxBroadcast* HxBroadcast::context() { return broadcast; } +HxBroadcast *HxBroadcast::context() { return broadcast; } void HxBroadcast::initialization(int port) { @@ -16,15 +18,22 @@ void HxBroadcast::initialization(int port) connect(broadcast, &HxBroadcast::publish_event, broadcast, &HxBroadcast::publish_achieve); } -void HxBroadcast::publish(QString message) +void HxBroadcast::publish(QString message) { emit broadcast->publish_event(message); } + +void HxBroadcast::publish_json(int action_type, std::initializer_list> args) { - emit broadcast->publish_event(message); + QJsonObject root, msginfo; + + for (std::initializer_list>::const_iterator i = args.begin(); i != args.end(); ++i) + msginfo.insert(i->first, i->second); + + root.insert("action_type", action_type); + root.insert("msgInfo", msginfo); + + publish(QJsonDocument(root).toJson(QJsonDocument::Compact)); } -void HxBroadcast::publish_achieve(QString message) -{ - socket->writeDatagram(message.toUtf8(), QHostAddress::Broadcast, broadcast->port); -} +void HxBroadcast::publish_achieve(QString message) { socket->writeDatagram(message.toUtf8(), QHostAddress::Broadcast, broadcast->port); } void HxBroadcast::receive_ready_read() { @@ -36,9 +45,9 @@ void HxBroadcast::receive_ready_read() /* 让datagram的大小为等待处理的数据报的大小,这样才能接收到完整的数据 */ datagram.resize(socket->pendingDatagramSize()); - /* 接收数据报,将其存放到datagram中*/ + /* 接收数据报,将其存放到datagram中 */ socket->readDatagram(datagram.data(), datagram.size()); - emit broadcast->receive_event(QString::fromLocal8Bit(datagram)); + emit broadcast->receive_event(datagram); } } diff --git a/HxBroadcast.h b/HxBroadcast.h index 6724206..fda3396 100644 --- a/HxBroadcast.h +++ b/HxBroadcast.h @@ -24,12 +24,20 @@ public: */ static void publish(QString message); + /** + * @brief 发布消息 (JSON格式) + * + * @param action_type 操作类型 + * @param args msgInfo 参数 + */ + static void publish_json(int action_type, std::initializer_list> args = std::initializer_list>()); + private: void publish_achieve(QString message); signals: void publish_event(QString message); - void receive_event(QString data); + void receive_event(QByteArray data); private slots: void receive_ready_read(); diff --git a/HxJson.cpp b/HxJson.cpp new file mode 100644 index 0000000..6b22890 --- /dev/null +++ b/HxJson.cpp @@ -0,0 +1,57 @@ +#include "HxJson.h" + +#include + +int HxJson::to_int(QJsonObject object, QString key) +{ + auto value = object.value(key); + + switch(value.type()) + { + case QJsonValue::Double: + return qRound(value.toDouble()); + + case QJsonValue::String: + return value.toString().toInt(); + + default: + return 0; + } +} + +bool HxJson::to_boolean(QJsonObject object, QString key) +{ + auto value = object.value(key); + + switch(value.type()) + { + case QJsonValue::Bool: + return value.toBool(); + + case QJsonValue::String: + return QVariant(value.toString()).toBool(); + + default: + return false; + } +} + +QString HxJson::to_string(QJsonObject object, QString key) +{ + auto value = object.value(key); + + switch(value.type()) + { + case QJsonValue::Bool: + case QJsonValue::Double: + return QString::number(value.toDouble()); + + case QJsonValue::String: + return value.toString(); + + default: + return ""; + } +} + +QStringList HxJson::to_string_list(QJsonObject object, QString key, QString split){ return to_string(object, key).split(split);} diff --git a/HxJson.h b/HxJson.h new file mode 100644 index 0000000..74b7ab6 --- /dev/null +++ b/HxJson.h @@ -0,0 +1,15 @@ +#ifndef HXJSON_H +#define HXJSON_H + +#include + +class HxJson +{ +public: + static int to_int(QJsonObject object, QString key); + static bool to_boolean(QJsonObject object, QString key); + static QString to_string(QJsonObject object, QString key); + static QStringList to_string_list(QJsonObject object, QString key, QString split); +}; + +#endif // HXJSON_H diff --git a/HxSystem.cpp b/HxSystem.cpp index 9c60eaa..31b8d3a 100644 --- a/HxSystem.cpp +++ b/HxSystem.cpp @@ -2,7 +2,9 @@ #include "HxProcess.h" #include +#ifdef Q_OS_LINUX #include +#endif static double m_memory_total = 0; static double m_cpu_total = 0, m_cpu_use = 0; @@ -110,6 +112,7 @@ bool HxSystem::get_memory_status(double *memory_use, double *memory_total, doubl bool HxSystem::get_program_status(double *cpu_usage, double *virtual_memory, double *resident_memory) { +#ifdef Q_OS_LINUX auto result = HxProcess::start(QString("ps u %1").arg(getpid())); auto list = result.split("\n"); @@ -126,6 +129,7 @@ bool HxSystem::get_program_status(double *cpu_usage, double *virtual_memory, dou return true; } +#endif return false; } @@ -189,6 +193,7 @@ bool HxSystem::get_harddisk_temperature(QString file_system, QString *temperatur bool HxSystem::get_harddisk_smart(QString file_system, QString *smart) { +#ifdef Q_OS_LINUX auto result = HxProcess::start(QString("echo tvis | sudo -S smartctl -i %1 | grep 'SMART support is'").arg(file_system)); if(result.indexOf("Unavailable") != -1) { @@ -212,7 +217,7 @@ bool HxSystem::get_harddisk_smart(QString file_system, QString *smart) } else return false; - +#endif return true; } diff --git a/HxTask.h b/HxTask.h index c14b5e9..5ec92bf 100644 --- a/HxTask.h +++ b/HxTask.h @@ -2,8 +2,6 @@ #define HXTASK_H #include "HxTrace.h" -//#include "HxThread.h" - #include class HxTask @@ -22,14 +20,70 @@ public: } /** - * @brief 异步执行 - * @param object 操作对象类 - * @param fn 操作对象类中的成员函数 + * @brief 等待结果 + * @param functor 函数, 返回值为 bool + * @param millisecond 最大等待时长 */ template - static QFuture invoke(Functor functor) + static bool wait(Functor functor, int millisecond) { - return QtConcurrent::run(functor); + auto timestamp = QDateTime::currentDateTime(); + + while(!functor()) + { + if (QDateTime::currentDateTime() > timestamp.addMSecs(millisecond)) + return false; + + QThread::msleep(10); + } + + return true; + } + + template + static QFuture invoke(Functor functor, const Arg1 &arg1) { return QtConcurrent::run(functor, arg1); } + + template + static QFuture invoke(Functor functor, const Arg1 &arg1, const Arg2 &arg2) { return QtConcurrent::run(functor, arg1, arg2); } + + template + static QFuture invoke(Functor functor, const Arg1 &arg1, const Arg2 &arg2, const Arg3 &arg3) { return QtConcurrent::run(functor, arg1, arg2, arg3); } + + /** + * @brief 异步执行 + * @param functor 函数 + */ + template + static QFuture invoke(Functor functor) { return QtConcurrent::run(functor); } + + /** + * @brief 异步执行 + * @param functor 函数 + * @param millisecond 函数执行间隔频率,单位毫秒 + * @param uuid 任务唯一编码 + */ + template + static void run(Functor functor, int millisecond, QUuid uuid) + { + dispatchers.insert(uuid, true); + + QtConcurrent::run( + [=](Functor functor, int _millisecond, QUuid _uuid) + { + HxTrace::debug_write_line("HxTask", QString("Thread: %1, start").arg(_uuid.toString())); + + while (dispatchers[_uuid]) + { + functor(); + + QThread::msleep(_millisecond); + } + + HxTrace::debug_write_line("HxTask", QString("Thread: %1, stop").arg(_uuid.toString())); + + dispatchers.remove(_uuid); + }, + functor, millisecond, uuid); } /** @@ -50,18 +104,18 @@ public: /** * @brief 异步执行线程 - * @param uuid 任务唯一编码 * @param object 操作对象类 * @param fn 操作对象类中的成员函数 * @param millisecond 函数执行间隔频率,单位毫秒 + * @param uuid 任务唯一编码 */ template - static void run(QUuid uuid, Class *object, T (Class::*fn)(), int millisecond) + static void run(Class *object, T (Class::*fn)(), int millisecond, QUuid uuid) { dispatchers.insert(uuid, true); QtConcurrent::run( - [=](QUuid _uuid, Class *_object, T (Class::*_fn)(), int _millisecond) + [=](Class *_object, T (Class::*_fn)(), int _millisecond, QUuid _uuid) { HxTrace::debug_write_line("HxTask", QString("Thread: %1, start").arg(_uuid.toString())); @@ -76,7 +130,7 @@ public: dispatchers.remove(_uuid); }, - uuid, object, fn, millisecond); + object, fn, millisecond, uuid); } /** @@ -171,21 +225,4 @@ private: static QMap dispatchers; }; -class HxParalle -{ -public: - /** - * @brief 任务并行 - * @param object 操作对象类 - * @param fn1 操作对象类中的成员函数1 - * @param fn2 操作对象类中的成员函数2 - */ - template - static void invoke(Class *object, T (Class::*fn1)(), T (Class::*fn2)()) - { - HxTask::run(object, fn1); - HxTask::run(object, fn2); - } -}; - #endif // HXTASK_H diff --git a/HxUtils.pro b/HxUtils.pro index eaf7de9..8afd286 100644 --- a/HxUtils.pro +++ b/HxUtils.pro @@ -31,6 +31,7 @@ DEFINES += QT_DEPRECATED_WARNINGS SOURCES += \ HxBroadcast.cpp \ HxDisk.cpp \ + HxJson.cpp \ HxLog.cpp \ HxProcess.cpp \ HxSocket.cpp \ @@ -43,6 +44,7 @@ SOURCES += \ HEADERS += \ HxBroadcast.h \ HxDisk.h \ + HxJson.h \ HxLog.h \ HxProcess.h \ HxSocket.h \