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 #include <QHostAddress> #include <QSettings> #include <QTimer> #include <QProcess> #include "tcpdatacontrol.h" TCPDataControl::TCPDataControl(QObject *parent) : DataControlInterface(parent), m_frontLeftDoorOpen(false), m_frontRightDoorOpen(false), m_rearLeftDoorOpen(false), m_rearRightDoorOpen(false), m_hoodOpen(false), m_trunkOpen(false), m_headLight(false), m_rightTurnLight(false), m_leftTurnLight(false), m_hazardSignal(false), m_brake(false), m_crash(false), m_connection(false) { const QString items[] = { "turnleft", "turnright", "iconCoolant", "iconFuel", "iconParkingBrake", "iconTyre", "iconBattery", "iconSeatbelt", "iconLamp", "iconLights" }; for (const QString& item : items) { m_stringList.append(item); } } TCPDataControl::~TCPDataControl() { m_tcpSocket.close(); } static bool printIP = true; void TCPDataControl::connectToServer(const QString& ip, int port) { // Check if a connection is already in progress if (m_tcpSocket.state() == QAbstractSocket::ConnectedState || m_tcpSocket.state() == QAbstractSocket::ConnectingState) { // Close the existing connection before starting a new one m_tcpSocket.abort(); } connect(&m_tcpSocket, &QAbstractSocket::stateChanged, this, &TCPDataControl::onConnectionStateChanged); // Emit the Connecting state emit connectionStateChanged(Connecting); // Set the new address setAddress(ip, port); if (printIP) { printIP = false; qDebug() << "Connecting to" << ip << ":" << port; } m_tcpSocket.connectToHost(ip, port); } void TCPDataControl::setAddress(QString ip, int port) { if (ip.isEmpty()) { return; } if (ip != m_address) { m_address = ip; } if (port != m_port) { m_port = port; } } void TCPDataControl::onConnectionStateChanged(QAbstractSocket::SocketState socketState) { ConnectionState connectionState = Disconnected; if (socketState == QAbstractSocket::UnconnectedState) { connectionState = Disconnected; setConnectionBool(false); } else if (socketState == QAbstractSocket::ConnectedState) { connectionState = Connected; setConnectionBool(true); emit dataReset(); } else if (socketState == QAbstractSocket::ConnectingState) { connectionState = Connecting; } emit connectionStateChanged(connectionState); } void TCPDataControl::handleTcpError(QAbstractSocket::SocketError socketError) { Q_UNUSED(socketError); qWarning() << "TCP Error:" << m_tcpSocket.errorString(); } int TCPDataControl::carId() const { return m_carId; } bool TCPDataControl::headLight() const { return m_headLight; } bool TCPDataControl::parkLight() const { return m_parkLight; } bool TCPDataControl::rightTurnLight() const { return m_rightTurnLight; } bool TCPDataControl::leftTurnLight() const { return m_leftTurnLight; } int TCPDataControl::gear() const { return m_gear; } bool TCPDataControl::lightFailure() const { return m_lightFailure; } bool TCPDataControl::frontLeftDoorOpen() const { return m_frontLeftDoorOpen; } bool TCPDataControl::frontRightDoorOpen() const { return m_frontRightDoorOpen; } bool TCPDataControl::rearLeftDoorOpen() const { return m_rearLeftDoorOpen; } bool TCPDataControl::rearRightDoorOpen() const { return m_rearRightDoorOpen; } bool TCPDataControl::hoodOpen() const { return m_hoodOpen; } bool TCPDataControl::trunkOpen() const { return m_trunkOpen; } bool TCPDataControl::flatTire() const { return m_flatTire; } double TCPDataControl::direction() const { return m_direction; } double TCPDataControl::longitude() const { return m_longitude; } double TCPDataControl::latitude() const { return m_latitude; } double TCPDataControl::vehicleSpeed() const { return m_vehicleSpeed; } bool TCPDataControl::hazardSignal() const { return m_hazardSignal; } bool TCPDataControl::brake() const { return m_brake; } double TCPDataControl::oilTemp() const { return m_oilTemp; } int TCPDataControl::oilPressure() const { return m_oilPressure; } double TCPDataControl::batteryPotential() const { return m_batteryPotential; } double TCPDataControl::gasLevel() const { return m_gasLevel; } int TCPDataControl::rpm() const { return m_rpm; } int TCPDataControl::engineTemp() const { return m_engineTemp; } bool TCPDataControl::crashArg() const { return m_crash; } void TCPDataControl::setVehicleSpeed(double vehicleSpeed) { if (m_vehicleSpeed == vehicleSpeed) { return; } QString msg("vehicleSpeed:" + QString::number(vehicleSpeed) + " "); write(msg.toLatin1()); m_vehicleSpeed = vehicleSpeed; emit vehicleSpeedChanged(vehicleSpeed); } void TCPDataControl::setLatitude(double latitude) { if (m_latitude == latitude) { return; } QString msg("latitude:" + QString::number(latitude) + " "); write(msg.toLatin1(), msg.length()); m_latitude = latitude; emit latitudeChanged(latitude); } void TCPDataControl::setLongitude(double longitude) { if (m_longitude == longitude) { return; } QString msg("longitude:" + QString::number(longitude) + " "); write(msg.toLatin1()); m_longitude = longitude; emit longitudeChanged(longitude); } void TCPDataControl::setDirection(double direction) { if (m_direction == direction) { return; } QString msg("direction:" + QString::number(direction) + " "); write(msg.toLatin1()); m_direction = direction; emit directionChanged(direction); } void TCPDataControl::setFlatTire(bool flatTire) { if (m_flatTire == flatTire) { return; } QString msg("flatTire:" + QString::number(flatTire) + " "); write(msg.toLatin1()); m_flatTire = flatTire; emit flatTireChanged(flatTire); } void TCPDataControl::setFrontLeftDoorOpen(bool doorOpen) { if (m_frontLeftDoorOpen == doorOpen) { return; } QString msg("frontLeftDoorOpen:" + QString::number(doorOpen) + " "); write(msg.toLatin1()); m_frontLeftDoorOpen = doorOpen; emit frontLeftDoorOpenChanged(doorOpen); } void TCPDataControl::setFrontRightDoorOpen(bool doorOpen) { if (m_frontRightDoorOpen == doorOpen) { return; } QString msg("frontRightDoorOpen:" + QString::number(doorOpen) + " "); write(msg.toLatin1()); m_frontRightDoorOpen = doorOpen; emit frontRightDoorOpenChanged(doorOpen); } void TCPDataControl::setRearLeftDoorOpen(bool doorOpen) { if (m_rearLeftDoorOpen == doorOpen) { return; } QString msg("rearLeftDoorOpen:" + QString::number(doorOpen) + " "); write(msg.toLatin1()); m_rearLeftDoorOpen = doorOpen; emit rearLeftDoorOpenChanged(doorOpen); } void TCPDataControl::setHoodOpen(bool doorOpen) { if (m_hoodOpen == doorOpen) { return; } QString msg("hoodOpen:" + QString::number(doorOpen) + " "); write(msg.toLatin1()); m_hoodOpen = doorOpen; emit hoodOpenChanged(doorOpen); } void TCPDataControl::setTrunkOpen(bool doorOpen) { if (m_trunkOpen == doorOpen) { return; } QString msg("trunkOpen:" + QString::number(doorOpen) + " "); write(msg.toLatin1()); m_trunkOpen = doorOpen; emit rearRightDoorOpenChanged(doorOpen); } void TCPDataControl::setRearRightDoorOpen(bool doorOpen) { if (m_rearRightDoorOpen == doorOpen) { return; } QString msg("rearRightDoorOpen:" + QString::number(doorOpen) + " "); write(msg.toLatin1()); m_rearRightDoorOpen = doorOpen; emit rearRightDoorOpenChanged(doorOpen); } void TCPDataControl::setLightFailure(bool lightFailure) { if (m_lightFailure == lightFailure) { return; } QString msg("lightFailure:" + QString::number(lightFailure) + " "); write(msg.toLatin1()); m_lightFailure = lightFailure; emit lightFailureChanged(lightFailure); } void TCPDataControl::setGear(int gear) { if (m_gear == gear) { return; } QString msg("gear:" + QString::number(gear) + " "); write(msg.toLatin1()); m_gear = gear; emit gearChanged(gear); } void TCPDataControl::setLeftTurnLight(bool leftTurnLight) { if (m_leftTurnLight == leftTurnLight) { return; } QString msg("leftTurnLight:" + QString::number(leftTurnLight) + " "); write(msg.toLatin1()); m_leftTurnLight = leftTurnLight; emit leftTurnLightChanged(leftTurnLight); } void TCPDataControl::setRightTurnLight(bool rightTurnLight) { if (m_rightTurnLight == rightTurnLight) { return; } QString msg("rightTurnLight:" + QString::number(rightTurnLight) + " "); write(msg.toLatin1()); m_rightTurnLight = rightTurnLight; emit rightTurnLightChanged(rightTurnLight); } void TCPDataControl::setHeadLight(bool headLight) { if (m_headLight == headLight) { return; } QString msg("headLight:" + QString::number(headLight) + " "); write(msg.toLatin1()); m_headLight = headLight; emit headLightChanged(headLight); } void TCPDataControl::setParkLight(bool parkLight) { if (m_parkLight == parkLight) { return; } QString msg("parkLight:" + QString::number(parkLight) + " "); write(msg.toLatin1()); m_parkLight = parkLight; emit parkLightChanged(parkLight); } void TCPDataControl::setCarId(int carId) { if (m_carId == carId) { return; } QString msg("carId:" + QString::number(carId) + " "); write(msg.toLatin1()); m_carId = carId; emit carIdChanged(carId); } void TCPDataControl::setHazardSignal(bool hazardSignal) { if (m_hazardSignal == hazardSignal) { return; } QString msg("hazardSignal:" + QString::number(hazardSignal) + " "); write(msg.toLatin1()); m_hazardSignal = hazardSignal; emit hazardSignalChanged(hazardSignal); } void TCPDataControl::setBrake(bool brake) { if (m_brake == brake) { return; } QString msg("brake:" + QString::number(brake) + " "); write(msg.toLatin1()); m_brake = brake; emit brakeChanged(brake); } void TCPDataControl::setOilTemp(double oilTemp) { if (m_oilTemp == oilTemp) { return; } QString msg("oilTemp:" + QString::number(oilTemp) + " "); write(msg.toLatin1()); m_oilTemp = oilTemp; emit oilTempChanged(oilTemp); } void TCPDataControl::setOilPressure(int oilPressure) { if (m_oilPressure == oilPressure) { return; } QString msg("oilPressure:" + QString::number(oilPressure) + " "); write(msg.toLatin1()); m_oilPressure = oilPressure; emit oilPressureChanged(oilPressure); } void TCPDataControl::setBatteryPotential(double batteryPotential) { if (m_batteryPotential == batteryPotential) { return; } QString msg("batteryPotential:" + QString::number(batteryPotential) + " "); write(msg.toLatin1()); m_batteryPotential = batteryPotential; emit batteryPotentialChanged(batteryPotential); } void TCPDataControl::setGasLevel(double gasLevel) { if (m_gasLevel == gasLevel) { return; } QString msg("gasLevel:" + QString::number(gasLevel) + " "); write(msg.toLatin1()); m_gasLevel = gasLevel; emit gasLevelChanged(gasLevel); } void TCPDataControl::setRpm(int rpm) { if (m_rpm == rpm) { return; } QString msg("rpm:" + QString::number(rpm) + " "); write(msg.toLatin1()); m_rpm = rpm; emit rpmChanged(rpm); } void TCPDataControl::setEngineTemp(int engineTemp) { if (m_engineTemp == engineTemp) { return; } QString msg("engineTemp:" + QString::number(engineTemp) + " "); write(msg.toLatin1()); m_engineTemp = engineTemp; emit engineTempChanged(engineTemp); } void TCPDataControl::setCrash(bool crashArg) { if (m_crash == crashArg) { return; } QString msg("crash:" + QString::number(crashArg) + " "); write(msg.toLatin1()); m_crash = crashArg; emit crashChanged(crashArg); m_crash = false; } void TCPDataControl::write(const char *data, qint64 length) { if ((m_tcpSocket.state() == QTcpSocket::ConnectedState)) { m_tcpSocket.write(data, length); } } void TCPDataControl::write(const char *data) { if ((m_tcpSocket.state() == QTcpSocket::ConnectedState)) { m_tcpSocket.write(data); } } QVariantList TCPDataControl::getStringList() const { return m_stringList; }