AntiClipSettings/DeviceConnection.h

148 lines
4.7 KiB
C
Raw Permalink Normal View History

2024-08-26 16:46:41 +08:00
#ifndef DEVICECONNECTION_H
2024-08-13 20:06:10 +08:00
#define DEVICECONNECTION_H
2024-08-16 16:24:15 +08:00
#include "DataStructure.h"
2024-08-22 10:48:28 +08:00
#include <QFuture>
#include <QFutureInterface>
2024-08-13 20:06:10 +08:00
#include <QObject>
2024-08-14 20:01:38 +08:00
#include <QQmlEngine>
2024-08-16 16:24:15 +08:00
#include <QTcpSocket>
2024-08-14 20:01:38 +08:00
#include <queue>
2024-08-13 20:06:10 +08:00
#include <string_view>
2024-08-16 16:24:15 +08:00
class NetworkInfomation;
2024-08-13 20:06:10 +08:00
class DeviceConnection : public QObject {
Q_OBJECT
2024-08-14 20:01:38 +08:00
QML_ELEMENT
2024-08-16 16:24:15 +08:00
QML_UNCREATABLE("Only created in C++...")
2024-08-14 20:01:38 +08:00
2024-08-13 20:06:10 +08:00
public:
2024-08-22 18:12:11 +08:00
constexpr static auto WirelessAddress = "192.168.10.2";
constexpr static auto HeartbeatInterval = std::chrono::milliseconds(2500);
2024-08-14 20:01:38 +08:00
enum Resolution {
Video_360P = 0,
Video_720P,
};
enum AreaWay {
Diabled = 0,
2024-08-16 16:24:15 +08:00
FullArea,
2024-08-14 20:01:38 +08:00
Quadrangle, // 四边形
};
2024-08-21 09:26:06 +08:00
class Infomation {
public:
QString deviceId;
QString softwareVersion;
QString firmwareVersion;
QString ip;
int rotation = 0;
bool flip = false;
2024-08-21 09:26:06 +08:00
QList<QPointF> openDoorArea;
AreaWay openDoorAreaWay;
QList<QPointF> shieldedArea;
bool shieldedAreaEnabled;
QList<QPointF> antiClipArea;
bool antiClipAreaEnabled;
};
Q_ENUM(AreaWay)
2024-08-21 09:26:06 +08:00
2024-08-13 20:06:10 +08:00
using H264FrameCallback = std::function<void(const char *data, uint32_t size)>;
2024-08-27 11:14:36 +08:00
DeviceConnection(QObject *parent = nullptr);
~DeviceConnection();
2024-08-21 09:26:06 +08:00
Infomation infomation() const;
bool isConnected() const;
2024-08-13 20:06:10 +08:00
void setH264FrameCallback(H264FrameCallback &&callback);
2024-08-21 09:26:06 +08:00
void connect(const Infomation &infomation);
void setLiveStreamEnabled(bool enabled);
NetworkInfomation networkInfomation() const;
2024-08-13 20:06:10 +08:00
void requestOpenDoorArea();
2024-08-22 10:48:28 +08:00
QFuture<bool> updateOpenDoorAreaPoints(AreaWay way, const QList<QPointF> &points);
2024-08-14 20:01:38 +08:00
void requestShieldedArea();
void updateShieldedAreaPoints(bool enabled, const QList<QPointF> &points);
void requestAntiClipArea();
void updateAntiClipAreaPoints(bool enabled, const QList<QPointF> &points);
void requestResolution(Resolution resolution);
2024-08-16 16:24:15 +08:00
void requestNetworkInfomation();
2024-08-22 10:48:28 +08:00
QFuture<bool> updateNetworkInfomation(bool dhcp, const QString &ip, const QString &netmask, const QString &gateway,
const QString &dns);
2024-08-21 16:03:49 +08:00
void requestVersion();
void requestVideoInformation();
void updateRotation(int rotation);
void updateFlip(bool flip);
2024-08-13 20:06:10 +08:00
2024-08-21 09:26:06 +08:00
/**
* @brief OTA,
* 1. OTA请求 1
2024-08-22 10:48:28 +08:00
* 2. 2-98
* 3. 99
* 4. 100
2024-08-21 09:26:06 +08:00
*
* @param file
*/
2024-08-22 15:32:58 +08:00
void requestOta(const QString &firmware, const QString &file);
2024-08-19 09:33:04 +08:00
2024-08-13 20:06:10 +08:00
signals:
2024-08-21 09:26:06 +08:00
void connected();
void disconnected();
void openDoorAreaChanged(AreaWay way, const QList<QPointF> &points);
void shieldedAreaChanged(bool enabled, const QList<QPointF> &points);
void antiClipAreaChanged(bool enabled, const QList<QPointF> &points);
void rotationChanged(int rotation);
void flipChanged(bool flip);
2024-08-21 09:26:06 +08:00
void networkInfomationChanged(const NetworkInfomation &info);
2024-08-21 16:03:49 +08:00
void firmwareChanged(const QString &firmware);
2024-08-21 09:26:06 +08:00
void otaProgressChanged(bool status, int progress, const QString &message);
2024-08-13 20:06:10 +08:00
protected:
2024-08-22 10:48:28 +08:00
class Task {
public:
QString command;
std::chrono::system_clock::time_point time = std::chrono::system_clock::now();
2024-08-22 10:48:28 +08:00
std::function<void()> task;
std::shared_ptr<QTimer> timeoutTimer = nullptr;
std::shared_ptr<QFutureInterface<bool>> future;
};
2024-08-27 11:14:36 +08:00
void close();
2024-08-13 20:06:10 +08:00
void onConnected();
2024-08-21 16:03:49 +08:00
void onDisconnected();
2024-08-13 20:06:10 +08:00
void onH264ReadyRead();
void onCommandReadyRead();
2024-08-22 10:48:28 +08:00
QString handleCommand(const std::string_view &replyText, const Task *task);
2024-08-16 16:24:15 +08:00
void onErrorOccurred(QAbstractSocket::SocketError socketError);
2024-08-21 09:26:06 +08:00
void timerEvent(QTimerEvent *event) final;
void transferBinContent();
2024-08-13 20:06:10 +08:00
private:
2024-08-21 09:26:06 +08:00
Infomation m_infomation;
2024-08-13 20:06:10 +08:00
QTcpSocket *m_commandSocket = nullptr;
QTcpSocket *m_h264Socket = nullptr;
2024-08-22 18:12:11 +08:00
bool m_videoEnabled = false;
2024-08-13 20:06:10 +08:00
bool m_receivedFirstJsonReply = false;
QByteArray m_commandBuffer;
QByteArray m_h264Buffer;
2024-08-21 09:26:06 +08:00
2024-08-22 10:48:28 +08:00
int m_otaProgress = -1;
2024-08-19 09:33:04 +08:00
std::vector<uint8_t> m_uploadBuffer;
2024-08-21 09:26:06 +08:00
int m_sendedSize = 0;
2024-08-22 10:48:28 +08:00
QTimer *m_otaTimer = nullptr; // 检测OTA超时
2024-08-21 09:26:06 +08:00
2024-08-13 20:06:10 +08:00
H264FrameCallback m_frameCallback;
2024-08-21 16:03:49 +08:00
std::queue<Task> m_requests;
int m_requestTimerId = -1;
int m_heartbeatTimerId = -1;
2024-08-21 16:03:49 +08:00
int heartbeats = 0;
2024-08-21 09:26:06 +08:00
NetworkInfomation m_networkInfomation;
2024-08-21 16:03:49 +08:00
QString m_firmware;
2024-08-13 20:06:10 +08:00
};
#endif // DEVICECONNECTION_H