//
// Created by Ht on 2021/1/9.
//

#ifndef LIBDEVCPP_SERIALIOGATE_H
#define LIBDEVCPP_SERIALIOGATE_H

#include <string>
#include <jebase/config.h>
#include <qlibc/QPObjectHolder.h>
#include <qlibc/StringUtils.hpp>
#include "SerialPort.h"

namespace jedge {

    class JE_API SerialIOGate {
    protected:
        qlibc::QSharedObjectHolder<SerialPort> mSerialPorts;

    public:
        static bool openSerial(jedge::RSerialIOHolderInterface& pHolder, const std::string &comPortName, const std::string &checkKey = "", unsigned int nBaud = 115200);
        static bool closeSerial(const std::string &comPortName);
        static void scanForTargetComPorts(QStringList &comNameList, const std::string &targetKey);
        static SerialIOGate* getGateInstance();

        bool hasSerialOpenned(const std::string &portName);
        bool openSerialByPort(const std::string & ComNum, const std::string &comKeyWord, unsigned int nBaud);
        bool startSerialReadByPort(const std::string & ComNum, const std::string &comKeyWord, unsigned int nBaud);
        void setPortHolder(const std::string &comPortName, RSerialIOHolderInterface& holder);
        void appendSerialPort(SerialPort *port);

        std::shared_ptr<SerialPort> getSerialPort(const std::string &portName);
        bool writeDataToSerialPort(const std::string &portName, uint8_t *buff, int32_t len);
        bool closeSerialPort(const std::string &portName);
    };
}

#endif //LIBDEVCPP_SERIALIOGATE_H
