|
19 | 19 | #include "FotaEvent.h"
|
20 | 20 | #include "Logger.h"
|
21 | 21 |
|
| 22 | +#include "nlohmann/json.hpp" |
| 23 | + |
22 | 24 | #include <string>
|
23 | 25 | #include <chrono>
|
24 | 26 |
|
@@ -101,12 +103,37 @@ namespace {
|
101 | 103 |
|
102 | 104 | void message_arrived(mqtt::const_message_ptr msg) override
|
103 | 105 | {
|
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())); |
110 | 137 | }
|
111 | 138 | }
|
112 | 139 |
|
|
0 commit comments