Skip to content

Commit

Permalink
Add MQTT back #20
Browse files Browse the repository at this point in the history
  • Loading branch information
Lecrapouille committed Jun 29, 2024
1 parent 2244692 commit 84cefe3
Show file tree
Hide file tree
Showing 4 changed files with 114 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/Editor/DearImGui/Backends/Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
###################################################
# Set MQTT Library.
#
ifneq ($(OS),Emscripten)
INCLUDES += $(THIRDPART_DIR)/MQTT/include
DEFINES += -DMQTT_BROKER_ADDR=\"localhost\"
DEFINES += -DMQTT_BROKER_PORT=1883
PKG_LIBS += libmosquitto
GUI_FILES += $(THIRDPART_DIR)/MQTT/src/MQTT.cpp
endif

###################################################
# If compiling for HTML5 (Emscripten) then force
# using Raylib backend since GLFW3 is not compilable.
Expand Down
96 changes: 96 additions & 0 deletions src/Editor/DearImGui/Editor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
#include "Editor/DearImGui/KeyBindings.hpp"
#include "Utils/Utils.hpp"

#ifndef MQTT_BROKER_ADDR
# error "MQTT_BROKER_ADDR shall be defined"
#endif
#ifndef MQTT_BROKER_PORT
# error "MQTT_BROKER_PORT shall be defined"
#endif

namespace tpne {

//! \brief path of the file storing dear imgui widgets. Cannot be placed
Expand All @@ -37,6 +44,7 @@ static std::string g_ini_filename = "imgui.ini";
Editor::Editor(size_t const width, size_t const height,
std::string const& title)
: Application(width, height, title),
MQTT(MQTT_BROKER_ADDR, MQTT_BROKER_PORT),
m_path(GET_DATA_PATH),
m_simulation(m_net, m_messages),
m_view(*this)
Expand Down Expand Up @@ -65,6 +73,94 @@ Editor::Editor(size_t const width, size_t const height,
ImGui::StyleColorsDark();
}

//------------------------------------------------------------------------------
void Editor::onConnected(int /*rc*/)
{
std::cout << "Connected to MQTT broker" << std::endl;

// Load a Petri net using the formalism used for TPNE json files. For example
// mosquitto_pub -h localhost -t "tpne/load" -m '{ "revision": 3, "type":
// "Petri net", "nets": [ { "name": "hello world",
// "places": [ { "id": 0, "caption": "P0", "tokens": 1, "x": 244, "y": 153 },
// { "id": 1, "caption": "P1", "tokens": 0, "x": 356, "y": 260 } ],
// "transitions": [ { "id": 0, "caption": "T0", "x": 298, "y": 207, "angle": 0 } ],
// "arcs": [ { "from": "P0", "to": "T0" }, { "from": "T0", "to": "P1", "duration": 3 }
// ] } ] }'
subscribe("tpne/load", [&](const MQTT::Message& msg){
std::cout << "load\n";
if (m_simulation.running)
{
m_messages.setError("MQTT: cannot load new Petri net while the simulation is still in progress");
return ;
}
const char* message = static_cast<const char*>(msg.payload);

// To temporary file
std::string path("/tmp/petri.json");
std::ofstream file(path);
file << message;
file.close();

// Import the file
bool shall_springify;
std::string error = loadFromFile(m_net, path, shall_springify);
if (error.empty())
{
m_messages.setInfo("Loaded with success " + path);
}
else
{
m_messages.setError(error);
}
}, MQTT::QoS::QoS0);

// Start the simulation for Petri net and GRAFCET.
// mosquitto_pub -h localhost -t "tpne/start" -m ''
subscribe("tpne/start", [&](const MQTT::Message& msg){
std::cout << "start\n";
if ((m_net.type() == TypeOfNet::TimedEventGraph) || (m_net.type() == TypeOfNet::TimedPetriNet))
{
m_messages.setError("MQTT: Please convert first to non timed net before starting simulation");
return ;
}
m_simulation.running = true;
framerate(30);
}, MQTT::QoS::QoS0);

// Stop the simulation.
// mosquitto_pub -h localhost -t "tpne/stop" -m ''
subscribe("tpne/stop", [&](const MQTT::Message& msg){
std::cout << "stop\n";
m_simulation.running = false;
framerate(60);
}, MQTT::QoS::QoS0);

// Fire transitions.
// mosquitto_pub -h localhost -t "tpne/fire" -m '1'
subscribe("tpne/fire", [&](const MQTT::Message& msg){
std::cout << "fire\n";
if (!m_simulation.running)
{
m_messages.setError("MQTT: The simulation is not running");
return ;
}
const char* message = static_cast<const char*>(msg.payload);
Net::Transitions& transitions = m_net.transitions();
size_t i = size_t(msg.payloadlen);
if (i == transitions.size())
{
while (i--)
{
transitions[i].receptivity = (message[i] != '0');
}
}
else
{
m_messages.setError("MQTT: fire command length does not match number of transitions");
}
}, MQTT::QoS::QoS0);
}

//------------------------------------------------------------------------------
void Editor::showStyleSelector()
{
Expand Down
7 changes: 6 additions & 1 deletion src/Editor/DearImGui/Editor.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,15 @@
# include "Utils/ForceDirected.hpp"
# include "Utils/History.hpp"
# include "Utils/Path.hpp"
# include "MQTT/MQTT.hpp"
# include <vector>

namespace tpne {

// ****************************************************************************
//! \brief Graphical User interface for manipulating and simulating Petri net.
// ****************************************************************************
class Editor: public PetriNetEditor, public Application
class Editor: public PetriNetEditor, public Application, protected MQTT
{
public:

Expand All @@ -61,6 +62,10 @@ class Editor: public PetriNetEditor, public Application
virtual void onDraw() override;
void close();

private: // Inheritance from MQTT

virtual void onConnected(int rc) override;

private: // Widgets

void menu();
Expand Down
2 changes: 1 addition & 1 deletion src/julia/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ VPATH += $(P)/src/Net/Imports VPATH += $(P)/src/Net/Exports
###################################################
# Inform Makefile where to find header files
#
INCLUDES += $(P)/include $(P)/src $(P)/external
INCLUDES += $(P)/include $(P)/src

###################################################
# Internal libraries since we can call the GUI from Julia REPL
Expand Down

0 comments on commit 84cefe3

Please sign in to comment.