#ifndef UK1104_h
#define UK1104_h

#include <qextserialport.h>
#include <QTimer>
#include <QStringList>
#include <QString>

// Class for controlling the UK1104 usb relay and digital I/O device.
// Very low level, just designed to send one command at a time and
// collect the response.
//
class UK1104 : public QextSerialPort {

   Q_OBJECT

public:

   enum UK1104Errors {
      UK1104_OK,        // all OK - no error
      UK1104_PENDING,   // command not yet run, waiting to execute.
      UK1104_TIMEOUT,   // command timed out
      UK1104_ERROR,     // UK1104 reported error.
      UK1104_CLOSED     // device closed (unplugged or something).
   };

      // Constructor arg is device name (/dev/ttyACM0 for instance).
      //
   UK1104(const QString & devname);

   virtual
   ~UK1104();

      // Send one command to the UK1104 and emit the commandDone()
      // signal when it has finished or timed out.
      //
      // Return true if command sent, false if another command is already
      // being processed (you can only send one at a time).
      //
   bool
   SendCommand(QString command,
               int timeout_milliseconds);

      // Return true if things seem OK, false otherwise.
      //
   bool
   ValidConnection();

signals:

      // When commands passed to StartCommands are complete, return the
      // results from running the command string. The unique_id is the same
      // as the one passed in by StartCommands to begin the command
      // sequence.
      //
   void
   commandDone(UK1104Errors status, QString result);

private slots:

      // Read all available data from file looking for the "::" prompt.
      //
   void
   doRead();

      // Called by watchdog timeout if response from command doesn't
      // show up and stop the timer first.
      //
   void
   watchdogTimeout();

private:

   void
   emitResult(UK1104Errors status);

   bool         valid_connection;

   QTimer       watchdog;

   QString      partial_line;

   bool         command_running;
};

#endif // UK1104_h
