mirror of
https://github.com/modelec/tirette.git
synced 2026-01-18 16:57:24 +01:00
Tcp Client
This commit is contained in:
@@ -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
23
MyClient.cpp
Normal 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
16
MyClient.h
Normal 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;
|
||||
};
|
||||
10
main.cpp
10
main.cpp
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user