#ifndef DEVICE_CTRL_CASCADE_CLIENT_H
#define DEVICE_CTRL_CASCADE_CLIENT_H
#include <QTcpSocket>

#include "cascade_protocol.h"

namespace zsy
{
    class CascadeClient final : public QObject
    {
        Q_OBJECT

        std::atomic_uint8_t status;
        QString host;
        quint16 port;
        QByteArray buffer;
        QTcpSocket *socket;

        enum TcpClientStatus:uint8_t
        {
            UNAVAILABLE,
            STARTED,
            STOPPED,
        };

    public:
        explicit CascadeClient(QString host, quint16 port, QObject *parent = nullptr);

        ~CascadeClient() override;

        // 连接服务器
        void start();

        void stop();

        void restart();

        void restart(const QString &host, quint16 port);

        // 发送数据
        void sendData(const QByteArray &data);

    Q_SIGNALS:
        // 可扩展信号（如连接状态变化）
        void connected();

        void disconnected();

        void receive();

    private Q_SLOTS:
        void onConnected();

        void onReadyRead();

        void onDisconnected();

        void onError(QAbstractSocket::SocketError error);
    };
} // zsy

#endif // DEVICE_CTRL_CASCADE_CLIENT_H
