C

Event Sender: Sending Messages to Applications

// Copyright (C) 2023 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial

// This file is part of the Qt Safe Renderer module
#ifndef TCPDATACONTROL_H
#define TCPDATACONTROL_H
#include "datacontrolinterface.h"
#include <QTcpSocket>
#include <QProcess>
#include <QString>
#include <QObject>

class TCPDataControl : public DataControlInterface
{
    Q_OBJECT
    Q_PROPERTY(QVariantList stringList READ getStringList NOTIFY stringListChanged)
    Q_PROPERTY(bool connectionBool READ connectionBool WRITE setConnectionBool NOTIFY myConnectionChanged)

public:
    explicit TCPDataControl(QObject *parent = nullptr);
    ~TCPDataControl();

    enum ConnectionState {
        Disconnected,
        Connecting,
        Connected
    };

    Q_SIGNAL void connectionStateChanged(ConnectionState state);
    Q_INVOKABLE void connectToServer(const QString &ip, int port);

    int carId() const;
    bool headLight() const;
    bool parkLight() const;
    bool rightTurnLight() const;
    bool leftTurnLight() const;
    int gear() const;
    bool lightFailure() const;
    bool frontLeftDoorOpen() const;
    bool frontRightDoorOpen() const;
    bool rearLeftDoorOpen() const;
    bool rearRightDoorOpen() const;
    bool hoodOpen() const;
    bool trunkOpen() const;
    bool flatTire() const;
    double direction() const;
    double longitude() const;
    double latitude() const;
    double vehicleSpeed() const;
    bool hazardSignal() const;
    bool brake() const;
    double oilTemp() const;
    int oilPressure() const;
    double batteryPotential() const;
    double gasLevel() const;
    int rpm() const;
    int engineTemp() const;
    QVariantList getStringList() const;
    bool crashArg() const;
    bool connectionBool() const { return m_connection; }
    void setConnectionBool(bool b) {
        m_connection = b;
        emit myConnectionChanged();
    }

public slots:
    void onConnectionStateChanged(QAbstractSocket::SocketState socketState);
    void handleTcpError(QAbstractSocket::SocketError socketError);
    void setAddress(QString ip, int port);
    void setVehicleSpeed(double vehicleSpeed);
    void setLatitude(double latitude);
    void setLongitude(double longitude);
    void setDirection(double direction);
    void setFlatTire(bool flatTire);
    void setFrontLeftDoorOpen(bool doorOpen);
    void setFrontRightDoorOpen(bool doorOpen);
    void setRearLeftDoorOpen(bool doorOpen);
    void setRearRightDoorOpen(bool doorOpen);
    void setHoodOpen(bool doorOpen);
    void setTrunkOpen(bool doorOpen);
    void setLightFailure(bool lightFailure);
    void setGear(int gear);
    void setLeftTurnLight(bool leftTurnLight);
    void setRightTurnLight(bool rightTurnLight);
    void setHeadLight(bool headLight);
    void setParkLight(bool parkLight);
    void setCarId(int carId);
    void setHazardSignal(bool hazardSignal);
    void setBrake(bool brake);
    void setOilTemp(double oilTemp);
    void setOilPressure(int oilPressure);
    void setBatteryPotential(double batteryPotential);
    void setGasLevel(double gasLevel);
    void setRpm(int rpm);
    void setEngineTemp(int engineTemp);
    void setCrash(bool crashArg);

signals:
    void stringListChanged();
    void myConnectionChanged();

private:
    void write(const char *, qint64 );
    void write(const char *);

private:
    QTcpSocket m_tcpSocket;
    QString m_address;
    int m_port;
    bool m_frontLeftDoorOpen;
    bool m_frontRightDoorOpen;
    bool m_rearLeftDoorOpen;
    bool m_rearRightDoorOpen;
    bool m_hoodOpen;
    bool m_trunkOpen;
    bool m_headLight;
    bool m_parkLight;
    bool m_rightTurnLight;
    bool m_leftTurnLight;
    bool m_hazardSignal;
    bool m_brake;
    bool m_crash;
    int m_carId;
    QVariantList m_stringList;
    bool m_connection;
};
#endif // TCPDATACONTROL_H