2025-12-12 17:46:37 +08:00
|
|
|
|
// uiCommunicationBus.h
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
|
|
#include "export.hpp"
|
|
|
|
|
|
#include <QObject>
|
|
|
|
|
|
#include <QMap>
|
|
|
|
|
|
#include <QVariant>
|
|
|
|
|
|
#include <QMutex>
|
|
|
|
|
|
|
|
|
|
|
|
// UI通信总线
|
|
|
|
|
|
class DIAGRAM_DESIGNER_PUBLIC UiCommunicationBus : public QObject
|
|
|
|
|
|
{
|
|
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
static UiCommunicationBus* instance();
|
|
|
|
|
|
|
|
|
|
|
|
// 发送HTTP请求
|
2025-12-19 18:28:13 +08:00
|
|
|
|
void sendHttpRequest(const QString& endpoint, const QVariant& data = QVariant(),const QString& method = "GET",const QVariantMap& = QVariantMap());
|
2025-12-12 17:46:37 +08:00
|
|
|
|
|
|
|
|
|
|
// 发送HTTP请求(无回复)
|
|
|
|
|
|
void sendHttpRequestNoReply(const QString& endpoint, const QVariant& data = QVariant());
|
|
|
|
|
|
|
|
|
|
|
|
// 向UI发送数据
|
|
|
|
|
|
void sendToUi(const QString& uiId, const QString& action, const QVariant& data);
|
|
|
|
|
|
|
|
|
|
|
|
// 广播到所有UI
|
|
|
|
|
|
void broadcastToUis(const QString& action, const QVariant& data);
|
|
|
|
|
|
|
|
|
|
|
|
// 注册/注销UI
|
|
|
|
|
|
void registerUi(const QString& uiId, QObject* uiObject);
|
|
|
|
|
|
void unregisterUi(const QString& uiId);
|
2025-12-19 18:28:13 +08:00
|
|
|
|
|
|
|
|
|
|
QMap<QString,QList<QPair<QString,QString>>>& getTempRequestMap() {return _tempRequest;}
|
|
|
|
|
|
void insertTempRequest(QString page,QList<QPair<QString,QString>> lst){
|
|
|
|
|
|
if(!_tempRequest.contains(page)){
|
|
|
|
|
|
_tempRequest.insert(page,lst);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
QMap<QString,QPair<QString,QList<QPair<QString,QString>>>>& getSesstionMap() {return _session;}
|
|
|
|
|
|
void insertSesstionMap(QString id,QMap<QString,QString> targetMap){ //从临时列表移除,插入到会话列表
|
|
|
|
|
|
|
|
|
|
|
|
QStringList sortedTargetList; //排序后的target列表
|
|
|
|
|
|
for(auto it = targetMap.begin();it != targetMap.end();++it){
|
|
|
|
|
|
sortedTargetList.append(it.key());
|
|
|
|
|
|
}
|
|
|
|
|
|
sortedTargetList.sort();
|
|
|
|
|
|
|
|
|
|
|
|
auto it = _tempRequest.begin();
|
|
|
|
|
|
while (it != _tempRequest.end())
|
|
|
|
|
|
{
|
|
|
|
|
|
const QString& page = it.key();
|
|
|
|
|
|
QList<QPair<QString, QString>>& tempList = it.value();
|
|
|
|
|
|
|
|
|
|
|
|
// 提取当前 tempList 中所有 first 元素组成的列表
|
|
|
|
|
|
QStringList firstElements;
|
|
|
|
|
|
firstElements.reserve(tempList.size());
|
|
|
|
|
|
for (const auto& pair : tempList)
|
|
|
|
|
|
{
|
|
|
|
|
|
firstElements.append(pair.first);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 先进行简单检查:如果元素数量不同,肯定不匹配
|
|
|
|
|
|
if (firstElements.size() != sortedTargetList.size())
|
|
|
|
|
|
{
|
|
|
|
|
|
++it;
|
|
|
|
|
|
continue;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 排序 firstElements
|
|
|
|
|
|
QStringList sortedFirstElements = firstElements;
|
|
|
|
|
|
sortedFirstElements.sort();
|
|
|
|
|
|
|
|
|
|
|
|
// 比较排序后的列表
|
|
|
|
|
|
if (sortedFirstElements == sortedTargetList)
|
|
|
|
|
|
{
|
|
|
|
|
|
// 匹配成功
|
|
|
|
|
|
// 检查 _session 中是否已有此 page
|
|
|
|
|
|
if (!_session.contains(page))
|
|
|
|
|
|
{
|
|
|
|
|
|
QList<QPair<QString, QString>> newList;
|
|
|
|
|
|
for (const auto& item : tempList) { //如果订阅不成功,从候选列表中移除 todo:记录错误target
|
|
|
|
|
|
auto it = targetMap.find(item.first);
|
|
|
|
|
|
if (it != targetMap.end()) {
|
2025-12-25 09:03:35 +08:00
|
|
|
|
if (it.value() != "1001") {
|
2025-12-19 18:28:13 +08:00
|
|
|
|
continue; // 跳过
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
newList.append(item);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
tempList = newList;
|
|
|
|
|
|
// _session 中没有此 page,可以插入
|
|
|
|
|
|
_session[page] = qMakePair(id, tempList);
|
|
|
|
|
|
}
|
|
|
|
|
|
// 无论 _session 中是否有此 page,都从 _tempRequest 删除 *********同一个page中只存在一个会话******
|
|
|
|
|
|
it = _tempRequest.erase(it);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
{
|
|
|
|
|
|
++it;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-12-12 17:46:37 +08:00
|
|
|
|
signals:
|
|
|
|
|
|
void httpDataProcessed(const QString& type,const QVariant& data); //发送分拣过的数据给外部
|
|
|
|
|
|
void websocketDataProcessed(const QVariant& data);
|
|
|
|
|
|
private:
|
|
|
|
|
|
UiCommunicationBus(QObject* parent = nullptr);
|
|
|
|
|
|
|
|
|
|
|
|
// 处理HTTP响应
|
|
|
|
|
|
void onHttpDataReceived(const QByteArray& data);
|
|
|
|
|
|
|
|
|
|
|
|
// 处理WebSocket数据
|
|
|
|
|
|
void onWebSocketDataReceived(const QByteArray& data);
|
|
|
|
|
|
|
|
|
|
|
|
// UI注册表
|
|
|
|
|
|
QMap<QString, QObject*> m_uiObjects;
|
|
|
|
|
|
mutable QMutex m_mutex;
|
2025-12-19 18:28:13 +08:00
|
|
|
|
QMap<QString,QList<QPair<QString,QString>>> _tempRequest; //临时请求队列,一个QList为一次会话 <图名<节点名,状态>>
|
|
|
|
|
|
QMap<QString,QPair<QString,QList<QPair<QString,QString>>>> _session; //会话队列 <图名<会话id<节点名,状态>>>
|
2025-12-12 17:46:37 +08:00
|
|
|
|
};
|