Skip to content

Commit

Permalink
serial interface: connect protocol with actual data (#130)
Browse files Browse the repository at this point in the history
Resolves #119
  • Loading branch information
dhebbeker authored Sep 21, 2024
2 parents 4bf500a + 7105d07 commit 3d35ee2
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 16 deletions.
12 changes: 10 additions & 2 deletions lib/3rd_party_adapters/nlohmann/JsonGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <serial_protocol/ProtocolVersionObject.hpp>
#include <serial_protocol/TaskList.hpp>
#include <serial_protocol/TaskObject.hpp>
#include <tasks/Task.hpp>

/**
* Indentation used by default for formating JSON.
Expand Down Expand Up @@ -38,10 +39,17 @@ std::string toJsonString<task_tracker_systems::TaskObject>(const task_tracker_sy
return jsonObject.dump(defaultJsonIndent);
}

void to_json(nlohmann::json &jsonObject, const device::TaskCollection::value_type &object)
{
jsonObject["id"] = object.first;
jsonObject["label"] = object.second.getLabel();
jsonObject["duration"] = object.second.getLastRecordedDuration().count();
}

template <>
std::string toJsonString<task_tracker_systems::TaskList>(const task_tracker_systems::TaskList &object)
std::string toJsonString<device::TaskCollection>(const device::TaskCollection &container)
{
nlohmann::json jsonObject(object);
nlohmann::json jsonObject(container);
return jsonObject.dump(defaultJsonIndent);
}

Expand Down
63 changes: 51 additions & 12 deletions lib/application_business_rules/serial_interface/Protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ namespace cli = command_line_interpreter;
// --- define commands ------
// --------------------------
#include "JsonGenerator.hpp"
#include <serial_protocol/DeletedTaskObject.hpp>
#include <serial_protocol/ProtocolVersionObject.hpp>
#include <serial_protocol/TaskList.hpp>
#include <serial_protocol/TaskObject.hpp>
#include <string>
#include <tasks/Task.hpp>

using namespace task_tracker_systems;

Expand All @@ -24,25 +26,62 @@ static const auto info = []() {
static const auto infoCmd = cli::makeCommand("info", std::function(info));

// command for list
static const auto list = []() {
const TaskList dummyList = {
{.id = 1, .label = "first", .duration = 100U},
{.id = 2, .label = "second", .duration = 200U},
};
serial_port::cout << toJsonString(dummyList) << std::endl; };
static const auto list = []() { serial_port::cout << toJsonString(device::tasks) << std::endl; };
static const auto listCmd = cli::makeCommand("list", std::function(list));

// command for edit
static const auto edit = [](const unsigned int id, const std::basic_string<ProtocolHandler::CharType> label, const std::chrono::seconds::rep duration) {
const TaskObject task = {.id = id, .label = label, .duration = duration};
serial_port::cout << toJsonString(task) << std::endl;
static const auto edit = [](const TaskId id, const std::basic_string<ProtocolHandler::CharType> label, const Task::Duration::rep duration) {
try
{
auto &task = device::tasks.at(id);
task.setLabel(label);
task.setRecordedDuration(std::chrono::seconds(duration));
const TaskObject taskObject = {.id = id, .label = task.getLabel(), .duration = task.getLastRecordedDuration().count()};
serial_port::cout << toJsonString(taskObject) << std::endl;
}
catch (std::out_of_range &e)
{
serial_port::cout << "ERROR: Task not found." << std::endl;
}
};
static const cli::Option<unsigned int> id = {.labels = {"--id"}, .defaultValue = 0};
static const cli::Option<TaskId> id = {.labels = {"--id"}, .defaultValue = 0};
static const cli::Option<std::basic_string<ProtocolHandler::CharType>> label = {.labels = {"--name"}, .defaultValue = "foo"};
static const cli::Option<std::chrono::seconds::rep> duration = {.labels = {"--duration"}, .defaultValue = 0};
static const cli::Option<Task::Duration::rep> duration = {.labels = {"--duration"}, .defaultValue = 0};
static const auto editCmd = cli::makeCommand("edit", std::function(edit), std::make_tuple(&id, &label, &duration));

static const std::array<const cli::BaseCommand<char> *, 3> commands = {&listCmd, &editCmd, &infoCmd};
// command for create/add
static const auto add = [](const TaskId id, const std::basic_string<ProtocolHandler::CharType> label, const Task::Duration::rep duration) {
try
{
const auto &[element, created] = device::tasks.try_emplace(id, label, std::chrono::seconds(duration));
const auto &task = element->second;
const TaskObject taskObject = {.id = element->first, .label = task.getLabel(), .duration = task.getLastRecordedDuration().count()};
serial_port::cout << toJsonString(taskObject) << std::endl;
if (!created)
{
serial_port::cout << "ERROR: Task with the specified ID already exists." << std::endl;
}
}
catch (std::out_of_range &e)
{
serial_port::cout << "ERROR: Task not found." << std::endl;
}
};
static const auto addCmd = cli::makeCommand("add", std::function(add), std::make_tuple(&id, &label, &duration));

// command for delete/remove
static const auto del = [](const TaskId id) {
const bool deleted = device::tasks.erase(id) > 0;
const DeletedTaskObject taskObject{.id = id};
serial_port::cout << toJsonString(taskObject) << std::endl;
if (!deleted)
{
serial_port::cout << "ERROR: No task deleted." << std::endl;
}
};
static const auto delCmd = cli::makeCommand("delete", std::function(del), std::make_tuple(&id));

static const std::array<const cli::BaseCommand<char> *, 5> commands = {&listCmd, &editCmd, &infoCmd, &addCmd, &delCmd};

bool ProtocolHandler::execute(const CharType *const commandLine)
{
Expand Down
12 changes: 11 additions & 1 deletion lib/application_business_rules/tasks/Task.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,14 @@ Task::Duration Task::getRecordedDuration()
return std::chrono::round<Duration>(recordedDuration);
}

std::map<TaskId, Task> device::tasks;
device::TaskCollection device::tasks;

void Task::setRecordedDuration(Duration newDuration)
{
recordedDuration = newDuration;
}

Task::Duration Task::getLastRecordedDuration() const
{
return std::chrono::round<Duration>(recordedDuration);
}
14 changes: 13 additions & 1 deletion lib/application_business_rules/tasks/Task.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,16 @@ class Task
* \returns the accumulated duration
*/
Duration getRecordedDuration();

Duration getLastRecordedDuration() const;

/**
* Sets the recorded duration.
*
* Useful for example if the task has been recorded outside the device.
* @param newDuration new duration to apply
*/
void setRecordedDuration(Duration newDuration);
bool isRunning() const;

private:
Expand All @@ -87,8 +97,10 @@ class Task

namespace device
{
typedef std::map<TaskId, Task> TaskCollection;

/**
* *The* collection of tasks to be used by the device application.
*/
extern std::map<TaskId, Task> tasks;
extern TaskCollection tasks;
} // namespace device

0 comments on commit 3d35ee2

Please sign in to comment.