#ifndef BUZZERCONN_H #define BUZZERCONN_H #include #include #include #include #include #include #include #include #include #include #include "appsettings.h" //typedef struct strReturnData{ // int status_code; // QString text; //}ReturnData_t; class BuzzerConn : public QObject { Q_OBJECT 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) public: explicit BuzzerConn(QObject *parent = nullptr, QString ip = "http://192.168.4.1", int port = 80); // values for the time calculation double offset; QList latest_offsets; double latest_button_pressed; // double starttime; // bool connected; // values for the socket connection int connection_progress; QString ip; int port; int errors; int errors_until_disconnect = 4; // the current state QString state; // can be: // - 'disconnected' // - 'connecting' // - 'connected' private: QDateTime *date; //to get the current time QTcpSocket *socket; //socket for communication with the extention QStringList pending_commands; //commands to send to the extention //one command is being sent whenever refresh() is called signals: void triggered(); //is emitted when the device is triggered void stateChanged(); //is emitted, when the connection state changes void progressChanged(); //is emmited during the connection process when the progress changes void offsetChanged(); public slots: Q_INVOKABLE signed long sendCommand(QString command, int timeout); //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 Q_INVOKABLE QList 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 Q_INVOKABLE bool connect(); //function to connect to buzzer Q_INVOKABLE bool calcoffset(QList times); //function that calculates the average time offset between the buzzer and the device Q_INVOKABLE double get(QString key); //can return some things (offset, lastpressed, currtime, connection_progress, connected) Q_INVOKABLE bool refresh(); //- refreshes the connection to the buzzer //- checks if it was triggered, if so 'triggered()' is emitted //- sends one command from the pending commans list Q_INVOKABLE void appendCommand(QString command); //appends a command to the pending commnds list // functions for the qml adapter QString getIP() const; void setIP(const QString &ipAdress); QString getState() const; void setState(QString newState); int getProgress() const; double getOffset() const; double getLastTriggered() const; }; #endif // BUZZERCONN_H