PowerMaster/include/measurementDataUtils.h

61 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#ifndef MEASUREMENTDATAUTILS_H
#define MEASUREMENTDATAUTILS_H
#include "networkCommon.h"
#include <QObject>
#include <QWebSocket>
/**
* @brief WebSocket数据客户端
*
* 负责与后台数据服务建立WebSocket连接接收实时数据
*/
class QTimer;
class WebSocketClient : public QObject
{
Q_OBJECT
public:
explicit WebSocketClient(QObject* parent = nullptr);
~WebSocketClient();
//连接管理
bool connectToServer(const QUrl& url);
void disconnectFromServer();
//bool isConnected();
//数据配置
void setReconnectInterval(int intervalMs);
void setMaxReconnectAttempts(int maxAttempts);
signals:
void dataReceived(const QString& dataMsg);
private slots:
void onConnected();
void onDisconnected();
void onError(QAbstractSocket::SocketError error);
void onTextMessageReceived(const QString& message);
void onReconnectTimeout();
void resetReconnect();
private:
void setupWebSocket();
void cleanupWebSocket();
void scheduleReconnect();
QWebSocket* m_webSocket;
QUrl m_serverUrl;
//重连相关
QTimer* m_reconnectTimer;
int m_reconnectInterval;
int m_reconnectAttempts;
int m_maxReconnectAttempts;
// 状态
bool m_connected;
ConnectionStatus m_connectionStatus;
};
#endif