Skip to content

Commit 4fb6576

Browse files
Include parse errors in MQTT response
1 parent 951efc1 commit 4fb6576

File tree

1 file changed

+33
-6
lines changed

1 file changed

+33
-6
lines changed

Diff for: src/Mqtt/MqttProcessor.cpp

+33-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
#include "FotaEvent.h"
2020
#include "Logger.h"
2121

22+
#include "nlohmann/json.hpp"
23+
2224
#include <string>
2325
#include <chrono>
2426

@@ -101,12 +103,37 @@ namespace {
101103

102104
void message_arrived(mqtt::const_message_ptr msg) override
103105
{
104-
if(msg->get_topic() == sua::IMqttProcessor::TOPIC_START) {
105-
_context.desiredState = _context.messagingProtocol->readDesiredState(msg->get_payload_str());
106-
_context.stateMachine->handleEvent(sua::FotaEvent::Start);
107-
} if(msg->get_topic() == sua::IMqttProcessor::TOPIC_STATE_GET) {
108-
_context.desiredState = _context.messagingProtocol->readCurrentStateRequest(msg->get_payload_str());
109-
_context.stateMachine->handleEvent(sua::FotaEvent::GetCurrentState);
106+
auto & ctx = _context;
107+
auto proto = ctx.messagingProtocol;
108+
auto mqtt = ctx.mqttProcessor;
109+
110+
const auto message = msg->get_payload_str();
111+
const auto topic = msg->get_topic();
112+
113+
sua::Logger::trace("Received fequest, topic={}", topic);
114+
115+
try {
116+
if(topic == sua::IMqttProcessor::TOPIC_START) {
117+
ctx.desiredState = proto->readDesiredState(message);
118+
ctx.stateMachine->handleEvent(sua::FotaEvent::Start);
119+
} if(topic == sua::IMqttProcessor::TOPIC_STATE_GET) {
120+
ctx.desiredState = proto->readCurrentStateRequest(message);
121+
ctx.stateMachine->handleEvent(sua::FotaEvent::GetCurrentState);
122+
}
123+
} catch(const nlohmann::json::parse_error & e) {
124+
sua::Logger::error("Invalid request for {}, unable to parse json: {}", topic, e.what());
125+
} catch(const nlohmann::json::exception& e) {
126+
ctx.desiredState.activityId = nlohmann::json::parse(message).at("activityId");
127+
sua::Logger::error("Invalid request for {}: {}", topic, e.what());
128+
mqtt->send(sua::IMqttProcessor::TOPIC_FEEDBACK, proto->createMessage(ctx, "identifying"));
129+
mqtt->send(sua::IMqttProcessor::TOPIC_FEEDBACK, proto->createMessage(ctx, "identificationFailed", e.what()));
130+
} catch(const std::logic_error & e) {
131+
sua::Logger::error("Invalid request for {}: {}", topic, e.what());
132+
} catch(const std::runtime_error & e) {
133+
ctx.desiredState.activityId = nlohmann::json::parse(message).at("activityId");
134+
sua::Logger::error("Invalid request for {}: {}", topic, e.what());
135+
mqtt->send(sua::IMqttProcessor::TOPIC_FEEDBACK, proto->createMessage(ctx, "identifying"));
136+
mqtt->send(sua::IMqttProcessor::TOPIC_FEEDBACK, proto->createMessage(ctx, "identificationFailed", e.what()));
110137
}
111138
}
112139

0 commit comments

Comments
 (0)