// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | bytran@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Object to carry out serial port communication with an external device
#ifndef SERIALPORTDATA_H
#define SERIALPORTDATA_H

#include <QObject>
#ifndef Q_OS_WINRT
    #ifndef Q_OS_IOS
        #include <QSerialPort>
        #include <QSerialPortInfo>
    #endif
#endif

class SerialPort : public QObject
{
    Q_OBJECT

public:

    explicit SerialPort(QObject *parent = nullptr);

signals:

    // Signal is emitted when a command is received over the websockets protocol
    void commandReceived(const QString &message);

public slots:

public:

    // Method to indicate if a remote device is connected
    bool getRemoteDeviceIsConnected();

    // Methods to connect and disconnect the serial port
    void connectSerialPort();
    void disconnectSerialPort();

    // Method to get a list of available port names
    QStringList getPortNames();

    // Methods to get and set the serial port communication configuration
    quint8 getPortIndex();
    void setPortIndex(quint8 portIndex);
    int getBaudRate();
    void setBaudRate(int baudRate);
    quint8 getParity();
    void setParity(quint8 parity);
    quint8 getDataBits();
    void setDataBits(quint8 dataBits);
    quint8 getStopBits();
    void setStopBits(quint8 stopBits);

    // Method to send a message to a remote device
    void sendMessage(const QString &message);
    void sendMessage(const QByteArray &message);

private:

    // Method to create connections for the serial port
    void createConnections();

    // Method is used to read data from the serial port
    void readData();

    #ifndef Q_OS_WINRT
        #ifndef Q_OS_IOS
            QSerialPort *serial;  // Serial port object for serial port communication
            QList<QSerialPortInfo> ports;  // List of serial ports available on the device
        #endif
    #endif

};

#endif // SERIALPORTDATA_H
