2018-07-25 00:32:20 +02:00
|
|
|
#ifndef BUZZERCONN_H
|
|
|
|
#define BUZZERCONN_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QUrl>
|
|
|
|
#include <QtNetwork>
|
|
|
|
#include <QAuthenticator>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QDateTime>
|
2018-08-29 18:33:39 +02:00
|
|
|
#include <QtDebug>
|
2018-10-14 18:39:39 +02:00
|
|
|
#include <QtConcurrent/qtconcurrentthreadengine.h>
|
2018-07-25 00:32:20 +02:00
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
#include "appsettings.h"
|
|
|
|
|
2018-09-18 17:11:47 +02:00
|
|
|
//typedef struct strReturnData{
|
|
|
|
// int status_code;
|
|
|
|
// QString text;
|
|
|
|
//}ReturnData_t;
|
2018-07-25 00:32:20 +02:00
|
|
|
|
|
|
|
class BuzzerConn : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
2018-09-17 23:06:34 +02:00
|
|
|
Q_PROPERTY(double lastTriggered READ getLastTriggered NOTIFY triggered)
|
|
|
|
Q_PROPERTY(QString ipAdress WRITE setIP READ getIP)
|
|
|
|
Q_PROPERTY(QString state READ getState NOTIFY stateChanged)
|
|
|
|
Q_PROPERTY(int progress READ getProgress NOTIFY progressChanged)
|
|
|
|
Q_PROPERTY(double offset READ getOffset NOTIFY offsetChanged)
|
|
|
|
|
2018-07-25 00:32:20 +02:00
|
|
|
public:
|
2018-08-30 16:44:04 +02:00
|
|
|
explicit BuzzerConn(QObject *parent = nullptr, QString ip = "http://192.168.4.1", int port = 80);
|
2018-09-18 17:11:47 +02:00
|
|
|
|
|
|
|
// values for the time calculation
|
2018-07-26 20:51:46 +02:00
|
|
|
double offset;
|
2018-07-25 00:32:20 +02:00
|
|
|
QList<double> latest_offsets;
|
2018-07-26 14:54:11 +02:00
|
|
|
double latest_button_pressed;
|
2018-09-18 17:11:47 +02:00
|
|
|
|
|
|
|
// double starttime;
|
|
|
|
// bool connected;
|
|
|
|
|
|
|
|
// values for the socket connection
|
2018-07-26 20:51:46 +02:00
|
|
|
int connection_progress;
|
2018-08-30 16:44:04 +02:00
|
|
|
QString ip;
|
|
|
|
int port;
|
2018-08-30 17:49:24 +02:00
|
|
|
int errors;
|
|
|
|
int errors_until_disconnect = 4;
|
2018-09-18 17:11:47 +02:00
|
|
|
|
|
|
|
// the current state
|
2018-09-17 23:06:34 +02:00
|
|
|
QString state;
|
2018-09-18 17:11:47 +02:00
|
|
|
// can be:
|
|
|
|
// - 'disconnected'
|
|
|
|
// - 'connecting'
|
|
|
|
// - 'connected'
|
2018-08-30 16:44:04 +02:00
|
|
|
|
|
|
|
|
2018-07-26 14:54:11 +02:00
|
|
|
|
2018-07-25 00:32:20 +02:00
|
|
|
private:
|
2018-07-26 14:54:11 +02:00
|
|
|
QDateTime *date;
|
2018-09-18 17:11:47 +02:00
|
|
|
//to get the current time
|
|
|
|
|
2018-08-30 16:44:04 +02:00
|
|
|
QTcpSocket *socket;
|
2018-09-18 17:11:47 +02:00
|
|
|
//socket for communication with the extention
|
|
|
|
|
2018-09-09 20:18:00 +02:00
|
|
|
QStringList pending_commands;
|
2018-09-18 17:11:47 +02:00
|
|
|
//commands to send to the extention
|
|
|
|
//one command is being sent whenever refresh() is called
|
2018-07-25 00:32:20 +02:00
|
|
|
signals:
|
2018-09-17 23:06:34 +02:00
|
|
|
void triggered();
|
2018-09-18 17:11:47 +02:00
|
|
|
//is emitted when the device is triggered
|
2018-09-17 23:06:34 +02:00
|
|
|
|
|
|
|
void stateChanged();
|
2018-09-18 17:11:47 +02:00
|
|
|
//is emitted, when the connection state changes
|
2018-09-17 23:06:34 +02:00
|
|
|
|
|
|
|
void progressChanged();
|
2018-09-23 17:54:20 +02:00
|
|
|
//is emmited during the connection process when the progress changes
|
2018-09-17 23:06:34 +02:00
|
|
|
|
|
|
|
void offsetChanged();
|
2018-07-25 00:32:20 +02:00
|
|
|
|
2018-09-23 17:54:20 +02:00
|
|
|
|
2018-07-25 00:32:20 +02:00
|
|
|
public slots:
|
2018-09-18 17:11:47 +02:00
|
|
|
|
2018-09-09 20:18:00 +02:00
|
|
|
Q_INVOKABLE signed long sendCommand(QString command, int timeout);
|
2018-08-30 17:49:24 +02:00
|
|
|
//function to send commands to the sensor
|
|
|
|
//Can be:
|
|
|
|
//command - return
|
|
|
|
//GET_TIMESTAMP - timestamp of the sensor
|
|
|
|
//GET_LASTPRESSED - timestamp of the sensor when it was triggered the last time
|
|
|
|
//
|
|
|
|
//error codes:
|
|
|
|
//(-1) : timeout
|
|
|
|
//(-2) : invalid data was recieved
|
2018-09-18 17:11:47 +02:00
|
|
|
|
|
|
|
Q_INVOKABLE QList<double> gettimes(int timeout, bool bothTimes = true);
|
|
|
|
//function to get the times from the buzzer as a list
|
|
|
|
//if bothTimes is true the current and the last-pressed timestamp will be returned
|
|
|
|
//else only the current timestamp will be returned
|
|
|
|
|
2018-07-26 14:54:11 +02:00
|
|
|
Q_INVOKABLE bool connect();
|
2018-08-02 18:10:08 +02:00
|
|
|
//function to connect to buzzer
|
2018-09-18 17:11:47 +02:00
|
|
|
|
2018-08-02 18:10:08 +02:00
|
|
|
Q_INVOKABLE bool calcoffset(QList<double> times);
|
|
|
|
//function that calculates the average time offset between the buzzer and the device
|
2018-09-18 17:11:47 +02:00
|
|
|
|
2018-07-26 14:54:11 +02:00
|
|
|
Q_INVOKABLE double get(QString key);
|
2018-08-02 18:10:08 +02:00
|
|
|
//can return some things (offset, lastpressed, currtime, connection_progress, connected)
|
2018-09-18 17:11:47 +02:00
|
|
|
|
2018-08-02 18:10:08 +02:00
|
|
|
Q_INVOKABLE bool refresh();
|
2018-09-18 17:11:47 +02:00
|
|
|
//- refreshes the connection to the buzzer
|
2018-09-23 17:54:20 +02:00
|
|
|
//- checks if it was triggered, if so 'triggered()' is emitted
|
2018-09-18 17:11:47 +02:00
|
|
|
//- sends one command from the pending commans list
|
|
|
|
|
2018-09-09 20:18:00 +02:00
|
|
|
Q_INVOKABLE void appendCommand(QString command);
|
2018-09-18 17:11:47 +02:00
|
|
|
//appends a command to the pending commnds list
|
2018-08-30 16:44:04 +02:00
|
|
|
|
2018-09-18 17:11:47 +02:00
|
|
|
|
|
|
|
// functions for the qml adapter
|
2018-09-17 23:06:34 +02:00
|
|
|
QString getIP() const;
|
2018-09-18 17:11:47 +02:00
|
|
|
void setIP(const QString &ipAdress);
|
2018-09-17 23:06:34 +02:00
|
|
|
|
|
|
|
QString getState() const;
|
|
|
|
void setState(QString newState);
|
|
|
|
|
|
|
|
int getProgress() const;
|
|
|
|
|
|
|
|
double getOffset() const;
|
|
|
|
|
|
|
|
double getLastTriggered() const;
|
2018-07-25 00:32:20 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif // BUZZERCONN_H
|