Skip to content

Commit

Permalink
Fix review findings
Browse files Browse the repository at this point in the history
  • Loading branch information
OleksandrChaika committed Oct 25, 2023
1 parent b93fbdc commit 0742ff0
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/Context.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ namespace sua {
std::string caFilepath = SUA_DEFAULT_CA_FILEPATH;
bool downloadMode = true;
bool fallbackMode = false;
int messageDelay = SUA_DEFAULT_MESSAGE_DELAY;
int feedbackInterval = SUA_DEFAULT_FEEDBACK_INTERVAL; // seconds

DesiredState desiredState;
CurrentState currentState;
Expand Down
18 changes: 9 additions & 9 deletions src/Defaults.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@

#include "Defaults.h"

const std::string SUA_DEFAULT_MQTT_PROTOCOL = "tcp";
const std::string SUA_DEFAULT_MQTT_HOST = "mosquitto";
const int SUA_DEFAULT_MQTT_PORT = 1883;
const std::string SUA_DEFAULT_MQTT_SERVER = "tcp://mosquitto:1883";
const std::string SUA_DEFAULT_MODE = "download";
const std::string SUA_DEFAULT_TEMP_DIRECTORY = "/data/selfupdates";
const std::string SUA_DEFAULT_CA_DIRECTORY = "/etc/ssl/certs";
const std::string SUA_DEFAULT_CA_FILEPATH = "";
const int SUA_DEFAULT_MESSAGE_DELAY = 5;
const std::string SUA_DEFAULT_MQTT_PROTOCOL = "tcp";
const std::string SUA_DEFAULT_MQTT_HOST = "mosquitto";
const int SUA_DEFAULT_MQTT_PORT = 1883;
const std::string SUA_DEFAULT_MQTT_SERVER = "tcp://mosquitto:1883";
const std::string SUA_DEFAULT_MODE = "download";
const std::string SUA_DEFAULT_TEMP_DIRECTORY = "/data/selfupdates";
const std::string SUA_DEFAULT_CA_DIRECTORY = "/etc/ssl/certs";
const std::string SUA_DEFAULT_CA_FILEPATH = "";
const int SUA_DEFAULT_FEEDBACK_INTERVAL = 5;
2 changes: 1 addition & 1 deletion src/Defaults.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ extern const std::string SUA_DEFAULT_MODE;
extern const std::string SUA_DEFAULT_TEMP_DIRECTORY;
extern const std::string SUA_DEFAULT_CA_DIRECTORY;
extern const std::string SUA_DEFAULT_CA_FILEPATH;
extern const int SUA_DEFAULT_MESSAGE_DELAY;
extern const int SUA_DEFAULT_FEEDBACK_INTERVAL; // seconds

#endif
4 changes: 2 additions & 2 deletions src/FSM/States/Downloading.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,9 @@ namespace sua {

Logger::info("Download progress: {}%", ctx.desiredState.downloadProgressPercentage);

if(now_in_seconds - _last_update > ctx.messageDelay) {
if((ctx.desiredState.downloadProgressPercentage == 100) || (now_in_seconds - _timeLastUpdate) >= ctx.feedbackInterval) {
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Downloading);
_last_update = now_in_seconds;
_timeLastUpdate = now_in_seconds;
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/FSM/States/Downloading.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace sua {
FotaEvent body(Context& ctx) override;

private:
long long _last_update = 0;
long long _timeLastUpdate = 0;
};

} // namespace sua
Expand Down
6 changes: 4 additions & 2 deletions src/FSM/States/Installing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include "Context.h"
#include "Logger.h"

#include <chrono>

namespace sua {

Installing::Installing()
Expand All @@ -41,9 +43,9 @@ namespace sua {
const auto now = std::chrono::system_clock::now().time_since_epoch();
const auto now_in_seconds = std::chrono::duration_cast<std::chrono::seconds>(now).count();

if(now_in_seconds - _last_update > ctx.messageDelay) {
if((ctx.desiredState.installProgressPercentage == 100) || (now_in_seconds - _timeLastUpdate) >= ctx.feedbackInterval) {
send(ctx, IMqttProcessor::TOPIC_FEEDBACK, MqttMessage::Installing);
_last_update = now_in_seconds;
_timeLastUpdate = now_in_seconds;
}
});

Expand Down
2 changes: 1 addition & 1 deletion src/FSM/States/Installing.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace sua {
FotaEvent body(Context& ctx) override;

private:
long long _last_update = 0;
long long _timeLastUpdate = 0;
};

} // namespace sua
Expand Down
10 changes: 5 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ int main(int argc, char* argv[])
std::string caDirectory = SUA_DEFAULT_CA_DIRECTORY;
std::string caFilepath = SUA_DEFAULT_CA_FILEPATH;
std::string help_string = fmt::format(help_string_template, server, installer, hostPathToSelfupdateDir, server, caDirectory, caFilepath);
int messageDelay = SUA_DEFAULT_MESSAGE_DELAY;
int feedbackInterval = SUA_DEFAULT_FEEDBACK_INTERVAL;

const char * env_server = std::getenv("SUA_SERVER");
if(env_server) {
server = env_server;
}

const char * env_delay = std::getenv("SUA_MESSAGE_DELAY");
if(env_delay) {
messageDelay = std::stoi(env_delay);
const char * env_interval = std::getenv("SUA_MESSAGE_DELAY");
if(env_interval) {
feedbackInterval = std::stoi(env_interval);
}

if(argc > 1) {
Expand Down Expand Up @@ -229,7 +229,7 @@ int main(int argc, char* argv[])
ctx.messagingProtocol = std::make_shared<sua::MqttMessagingProtocolJSON>();
ctx.bundleChecker = std::make_shared<sua::BundleChecker>();
ctx.mqttProcessor = std::make_shared<sua::MqttProcessor>(ctx);
ctx.messageDelay = messageDelay;
ctx.feedbackInterval = feedbackInterval;

sua::Logger::info("SUA build number : '{}'", SUA_BUILD_NUMBER );
sua::Logger::info("SUA commit hash : '{}'", SUA_COMMIT_HASH );
Expand Down

0 comments on commit 0742ff0

Please sign in to comment.