#include #include #include #include #include #include #include "main.hpp" #include "AutomForArduino.cpp" #include #include #include #include #include #include #include #include #include #include #undef timeout #include "mqtt/async_client.h" #include 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() != 0; if (j.contains("b1")) pompe2 = j["b1"].get() != 0; if (j.contains("b2")) pompe3 = j["b2"].get() != 0; if (j.contains("b3")) pompe4 = j["b3"].get() != 0; std::cout << "Pompes : " << pompe1 << " " << pompe2 << " " << pompe3 << " " << pompe4 << std::endl; } catch (const json::parse_error& e) { std::cerr << "Erreur JSON : " << e.what() << "\n"; } } }; // ************************************************************ int main() { 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; } while (1) { ProcessMQTT(&client); usleep(100000); } 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; return 0; } 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); }