2
0
Files
lpsarii/main.cpp

128 lines
3.1 KiB
C++
Raw Normal View History

2026-01-09 09:25:37 +01:00
#include <iostream>
#include <iomanip>
#include <unistd.h>
#include <math.h>
#include <locale.h>
2026-01-09 07:46:52 +01:00
#include <array>
#include "main.hpp"
2026-01-09 09:25:37 +01:00
#include "AutomForArduino.cpp"
#include <curl/curl.h>
#include <string>
#include <thread>
#include <atomic>
#include <queue>
#include <mutex>
#include <condition_variable>
#include <csignal>
#include <chrono>
#include <cstring>
#undef timeout
#include "mqtt/async_client.h"
#include <nlohmann/json.hpp>
using json = nlohmann::json;
// Constantes de fonctionnement
#define LEVEL_MIN 2
#define FLOW_PER_PUMP 250
/* Configuration MQTT */
const std::string ADDRESS = "tcp://rabbitmq:1883";
const std::string CLIENTID = "CppClientTP";
const std::string TOPIC = "geii/in/#";
const int QOS = 1;
const int CYCLE_MS = 100;
int etape = 0; // Étape du grafcet : début Automatique
int bp_mode, bp_mode_fm;
unsigned short pompe1, pompe2, pompe3, pompe4; // bouton des pompes 0 (arrêt) / 1 (marche)
unsigned short pompe1_old, pompe2_old, pompe3_old, pompe4_old;
unsigned short sensor_max, sensor_high, sensor_low, sensor_min;
float TankInitialValue = 4.6;
int s0, s1, s2, s3, s4;
// Réception des messages MQTT
// ************************************************************
class callback : public virtual mqtt::callback {
public:
void message_arrived(mqtt::const_message_ptr msg) override {
std::string payload = msg->to_string();
try {
json j = json::parse(payload);
// Ne rien faire si l'objet JSON est vide
if (j.empty()) return;
if (j.contains("b0")) pompe1 = j["b0"].get<int>() != 0;
if (j.contains("b1")) pompe2 = j["b1"].get<int>() != 0;
if (j.contains("b2")) pompe3 = j["b2"].get<int>() != 0;
if (j.contains("b3")) pompe4 = j["b3"].get<int>() != 0;
std::cout << "Pompes : " << pompe1 << " " << pompe2 << " " << pompe3 << " " << pompe4 << std::endl;
}
catch (const json::parse_error& e) {
std::cerr << "Erreur JSON : " << e.what() << "\n";
}
}
};
// ************************************************************
2026-01-09 07:46:52 +01:00
int main()
{
2026-01-09 09:25:37 +01:00
mqtt::async_client client(ADDRESS, CLIENTID);
callback cb;
client.set_callback(cb);
mqtt::connect_options connOpts;
connOpts.set_clean_session(true);
connOpts.set_user_name("admin");
connOpts.set_password("geii2025");
try {
client.connect(connOpts)->wait();
client.start_consuming();
client.subscribe(TOPIC, QOS)->wait();
} catch (const mqtt::exception &exc) {
std::cerr << "Erreur MQTT: " << exc.what() << "\n";
return 1;
}
2026-01-09 07:46:52 +01:00
while (1)
{
2026-01-09 09:25:37 +01:00
ProcessMQTT(&client);
usleep(100000);
2026-01-09 07:46:52 +01:00
}
2026-01-09 09:25:37 +01:00
try {
client.unsubscribe(TOPIC)->wait();
client.stop_consuming();
client.disconnect()->wait();
} catch(const mqtt::exception &exc){
std::cerr << "Erreur déconnexion MQTT: " << exc.what() << std::endl;
}
std::cout << "Fin du programme" << std::endl;
2026-01-09 07:46:52 +01:00
return 0;
}
2026-01-09 09:25:37 +01:00
void ProcessMQTT(mqtt::async_client* client)
{
json obj = {
{"s0", s0 },
{"s1", s1 },
{"s2", s2 },
{"s3", s3 },
};
std::string payload = obj.dump();
auto msg = mqtt::make_message("geii/out", payload);
msg->set_qos(1);
client->publish(msg);
}