Skip to content

Commit f13ee27

Browse files
Preserve internal state if disconnected from MQTT Broker
* Preserve internal state if disconnected from MQTT Broker * Fix review findings #1
1 parent 81d23b4 commit f13ee27

File tree

2 files changed

+17
-14
lines changed

2 files changed

+17
-14
lines changed

src/Mqtt/MqttProcessor.cpp

+17-7
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ namespace {
112112
const auto message = msg->get_payload_str();
113113
const auto topic = msg->get_topic();
114114

115-
sua::Logger::trace("Received request, topic={}", topic);
115+
sua::Logger::trace("Received message via topic '{}'", topic);
116116

117117
try {
118118
if(topic == sua::IMqttProcessor::TOPIC_IDENTIFY) {
@@ -126,20 +126,26 @@ namespace {
126126
ctx.desiredState.activityId = c.activityId;
127127
ctx.stateMachine->handleEvent(c.event);
128128
} else {
129-
sua::Logger::error("Invalid topic: {}", topic);
129+
sua::Logger::error("Invalid topic: '{}'", topic);
130130
}
131131
} catch(const nlohmann::json::parse_error & e) {
132-
sua::Logger::error("Invalid request for {}, unable to parse json: {}", topic, e.what());
132+
// catch here if request is not a valid json (yaml or xml for example)
133+
sua::Logger::error("Unable to parse desired state request (not a valid json?): '{}'", e.what());
133134
} catch(const nlohmann::json::exception& e) {
135+
// catch here if request is incomplete (missing field)
136+
// result is lost, extract activityId again and reply IdentificationFailed
134137
ctx.desiredState.activityId = nlohmann::json::parse(message).at("activityId");
135-
sua::Logger::error("Invalid request for {}: {}", topic, e.what());
138+
sua::Logger::error("Incomplete desired state request, unable to identify (incomplete json?): '{}'", e.what());
136139
mqtt->send(sua::IMqttProcessor::TOPIC_FEEDBACK, sua::MqttMessage::Identifying);
137140
mqtt->send(sua::IMqttProcessor::TOPIC_FEEDBACK, sua::MqttMessage::IdentificationFailed, e.what());
138141
} catch(const std::logic_error & e) {
139-
sua::Logger::error("Invalid request for {}: {}", topic, e.what());
142+
// catch here if request is valid json but activityId is empty
143+
sua::Logger::error("Invalid desired state request: '{}'", e.what());
140144
} catch(const std::runtime_error & e) {
145+
// catch here if request is incomplete (empty field)
146+
// result is lost, extract activityId again and reply IdentificationFailed
141147
ctx.desiredState.activityId = nlohmann::json::parse(message).at("activityId");
142-
sua::Logger::error("Invalid request for {}: {}", topic, e.what());
148+
sua::Logger::error("Malformed desired state request, unable to identify: '{}'", e.what());
143149
mqtt->send(sua::IMqttProcessor::TOPIC_FEEDBACK, sua::MqttMessage::Identifying);
144150
mqtt->send(sua::IMqttProcessor::TOPIC_FEEDBACK, sua::MqttMessage::IdentificationFailed, e.what());
145151
}
@@ -214,7 +220,11 @@ namespace sua {
214220
auto mqtt_message = mqtt::make_message(topic, content);
215221
mqtt_message->set_qos(QUALITY);
216222
mqtt_message->set_retained(retained);
217-
_client.publish(mqtt_message);
223+
try {
224+
_client.publish(mqtt_message);
225+
} catch(const mqtt::exception& e) {
226+
Logger::error("Publish to topic '{}' failed: '{}'", topic, e.what());
227+
}
218228

219229
if(!_client.is_connected() || !_client.get_pending_delivery_tokens().empty()) {
220230
_client.reconnect();

src/SelfUpdateAgent.cpp

-7
Original file line numberDiff line numberDiff line change
@@ -61,39 +61,32 @@ namespace sua {
6161
{ FotaEvent::Identify , "Connected" , "Connected" , FotaEvent::BundleVersionUnchanged },
6262
{ FotaEvent::GetCurrentState , "Connected" , "SendCurrentState"},
6363
// from "Downloading"
64-
{ FotaEvent::ConnectivityLost , "Downloading" , "Uninitialized" },
6564
{ FotaEvent::DownloadStart , "Downloading" , "Installing" , FotaEvent::DownloadSucceeded },
6665
{ FotaEvent::DownloadStart , "Downloading" , "Failed" , FotaEvent::DownloadFailed },
6766
{ FotaEvent::Rollback , "Downloading" , "Idle" },
6867
// from "Installing"
69-
{ FotaEvent::ConnectivityLost , "Installing" , "Uninitialized" },
7068
{ FotaEvent::InstallStart , "Installing" , "Installed" , FotaEvent::InstallCompleted },
7169
{ FotaEvent::InstallStart , "Installing" , "Failed" , FotaEvent::BundleVersionInconsistent },
7270
{ FotaEvent::InstallStart , "Installing" , "Failed" , FotaEvent::InstallFailed },
7371
{ FotaEvent::InstallStart , "Installing" , "Downloading" , FotaEvent::InstallFailedFallback },
7472
{ FotaEvent::Rollback , "Installing" , "Idle" },
7573
// from "Installed"
76-
{ FotaEvent::ConnectivityLost , "Installed" , "Uninitialized" },
7774
{ FotaEvent::Activate , "Installed" , "Activating" },
7875
{ FotaEvent::Rollback , "Installed" , "Idle" },
7976
// from "Activating"
80-
{ FotaEvent::ConnectivityLost , "Activating" , "Uninitialized" },
8177
{ FotaEvent::Cleanup , "Activating" , "Cleaning" },
8278
{ FotaEvent::Rollback , "Activating" , "Idle" },
8379
// from "Failed"
84-
{ FotaEvent::ConnectivityLost , "Failed" , "Uninitialized" },
8580
{ FotaEvent::Cleanup , "Failed" , "Cleaning" },
8681
{ FotaEvent::Rollback , "Failed" , "Idle" },
8782
// from "Cleaning"
88-
{ FotaEvent::ConnectivityLost , "Cleaning" , "Uninitialized" },
8983
{ FotaEvent::Waiting , "Cleaning" , "Idle" },
9084
// from "Idle"
9185
{ FotaEvent::ConnectivityLost , "Idle" , "Uninitialized" },
9286
{ FotaEvent::Identify , "Idle" , "Downloading" , FotaEvent::BundleVersionOK },
9387
{ FotaEvent::Identify , "Idle" , "Failed" , FotaEvent::BundleVersionUnchanged },
9488
{ FotaEvent::GetCurrentState , "Idle" , "SendCurrentState"},
9589
// from "SendCurrentState"
96-
{ FotaEvent::ConnectivityLost , "SendCurrentState", "Uninitialized" },
9790
{ FotaEvent::Waiting , "SendCurrentState", "Idle" },
9891
});
9992
_context.stateMachine->transitTo("Uninitialized");

0 commit comments

Comments
 (0)