aboutsummaryrefslogtreecommitdiffstats
path: root/src/Xpl2Client.h
blob: f05296290eb0fb4905fbe956bf55229d1645f469 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
 * @file   Xpl2Client.h
 * @brief  TCP client for the Alchemie XPL2 printhead protocol.
 */
#pragma once

#include <QObject>
#include <QQmlEngine>
#include <QTcpSocket>

class Xpl2Client : public QObject {
  Q_OBJECT
  QML_ELEMENT
  QML_SINGLETON

  Q_PROPERTY(QString host READ host WRITE setHost NOTIFY hostChanged)
  Q_PROPERTY(quint16 port READ port WRITE setPort NOTIFY portChanged)
  Q_PROPERTY(bool connected READ isConnected NOTIFY connectedChanged)

public:
  explicit Xpl2Client(QObject *parent = nullptr);

  QString host() const;
  void setHost(const QString &host);

  quint16 port() const;
  void setPort(quint16 port);

  bool isConnected() const;

  Q_INVOKABLE void connectToServer();
  Q_INVOKABLE void disconnectFromServer();
  Q_INVOKABLE void sendCommand(const QString &command);

signals:
  void hostChanged();
  void portChanged();
  void connectedChanged();
  void responseReceived(const QString &response);
  void errorOccurred(const QString &error);
  void statusMessage(const QString &message);

private slots:
  void onConnected();
  void onDisconnected();
  void onReadyRead();
  void onErrorOccurred(QAbstractSocket::SocketError error);

private:
  QTcpSocket m_socket;
  QString m_host = QStringLiteral("127.0.0.1");
  quint16 m_port = 5000;
};