Files

67 lines
1.7 KiB
C++

#pragma once
#include <qelapsedtimer.h>
#include <qqmlintegration.h>
#include "internal/circularbuffer.hpp"
#include "tickingservice.hpp"
namespace ZShell::services {
class NetworkUsage : public TickingService {
Q_OBJECT
QML_ELEMENT
QML_SINGLETON
Q_PROPERTY(qreal downloadSpeed READ downloadSpeed NOTIFY changed)
Q_PROPERTY(qreal uploadSpeed READ uploadSpeed NOTIFY changed)
Q_PROPERTY(qreal downloadTotal READ downloadTotal NOTIFY changed)
Q_PROPERTY(qreal uploadTotal READ uploadTotal NOTIFY changed)
Q_PROPERTY(int historyLength READ historyLength NOTIFY historyLengthChanged)
Q_PROPERTY(
ZShell::internal::CircularBuffer* downloadBuffer READ downloadBuffer
CONSTANT)
Q_PROPERTY(
ZShell::internal::CircularBuffer* uploadBuffer READ uploadBuffer
CONSTANT)
public:
explicit NetworkUsage(QObject* parent = nullptr);
[[nodiscard]] qreal downloadSpeed() const;
[[nodiscard]] qreal uploadSpeed() const;
[[nodiscard]] qreal downloadTotal() const;
[[nodiscard]] qreal uploadTotal() const;
[[nodiscard]] int historyLength() const;
[[nodiscard]] internal::CircularBuffer* downloadBuffer() const;
[[nodiscard]] internal::CircularBuffer* uploadBuffer() const;
signals:
void changed();
void historyLengthChanged();
protected:
void tick() override;
private:
void setHistoryLength(int length);
qreal m_downloadSpeed = 0.0;
qreal m_uploadSpeed = 0.0;
qreal m_downloadTotal = 0.0;
qreal m_uploadTotal = 0.0;
int m_historyLength = 30;
internal::CircularBuffer* m_downloadBuffer = nullptr;
internal::CircularBuffer* m_uploadBuffer = nullptr;
quint64 m_prevRx = 0;
quint64 m_prevTx = 0;
bool m_initialized = false;
QElapsedTimer m_timer;
};
} // namespace ZShell::services