Tcp Client

This commit is contained in:
ackimixs
2024-03-26 22:51:52 +01:00
parent 16ab75f7d1
commit 44b31a5a87
4 changed files with 62 additions and 2 deletions

View File

@@ -1,6 +1,17 @@
cmake_minimum_required(VERSION 3.28)
cmake_minimum_required(VERSION 3.25)
project(tirette)
set(CMAKE_CXX_STANDARD 17)
add_executable(tirette main.cpp)
find_package(PkgConfig REQUIRED)
pkg_check_modules(TCPSocket REQUIRED TCPSocket)
find_package(WiringPi REQUIRED)
add_executable(tirette main.cpp
MyClient.cpp
MyClient.h)
target_link_libraries(tirette TCPSocket)
target_link_libraries(tirette WiringPi)

23
MyClient.cpp Normal file
View File

@@ -0,0 +1,23 @@
#include "MyClient.h"
MyClient::MyClient(bool* tiretteState, const char* host, int port) : TCPClient(host, port), tiretteState(tiretteState)
{
}
void MyClient::handleMessage(const std::string& message)
{
// if get tirette => send tiretteState
if (split(message, ";")[1] == "tirette") {
if (split(message, ";")[2] == "get tirette_state") {
const std::string toSend = "tirette;start;set tirette_state;" + std::to_string(*tiretteState);
this->sendMessage(toSend.c_str());
}
}
}
void MyClient::setTiretteState(bool* tiretteState)
{
this->tiretteState = tiretteState;
}

16
MyClient.h Normal file
View File

@@ -0,0 +1,16 @@
#pragma once
#include <TCPSocket/TCPClient.hpp>
#include <TCPSocket/TCPUtils.hpp>
class MyClient : public TCPClient {
public:
MyClient(bool* tiretteState, const char* host, int port);
void handleMessage(const std::string& message) override;
void setTiretteState(bool* tiretteState);
private:
bool* tiretteState;
};

View File

@@ -1,6 +1,8 @@
#include <wiringPi.h>
#include <iostream>
#include "MyClient.h"
// Numéro du GPIO connecté à la tirette
#define TIRETTE_GPIO 17
@@ -14,6 +16,12 @@ int main() {
// Configuration du GPIO de la tirette en mode entrée
pinMode(TIRETTE_GPIO, INPUT);
bool* tiretteState = new bool(true);
MyClient client(tiretteState, "127.0.0.1", 8080);
client.start();
// Boucle principale
while (true) {
// Lecture de l'état du GPIO de la tirette
@@ -21,7 +29,9 @@ int main() {
// Si les aimants ne sont plus en contact, afficher un message
if (etat == LOW) {
tiretteState = new bool(false);
std::cout << "Les aimants ne sont plus en contact." << std::endl;
client.sendMessage("tirette;start;set tirette_state;1");
// Vous pouvez exécuter d'autres actions ici
}