Skip to content

Commit 9f4bb13

Browse files
committed
Merge remote-tracking branch 'github/develop' into feature/83-restructure-test-environments
2 parents 3bf5131 + 6e7cac5 commit 9f4bb13

File tree

8 files changed

+166
-6
lines changed

8 files changed

+166
-6
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#include <nlohmann/json.hpp>
2+
#include <serial_interface/JsonGenerator.hpp>
3+
#include <serial_protocol/DeletedTaskObject.hpp>
4+
#include <serial_protocol/ProtocolVersionObject.hpp>
5+
#include <serial_protocol/TaskList.hpp>
6+
#include <serial_protocol/TaskObject.hpp>
7+
8+
/**
9+
* Indentation used by default for formating JSON.
10+
*/
11+
static constexpr int defaultJsonIndent = 4;
12+
13+
template <>
14+
std::string toJsonString<task_tracker_systems::ProtocolVersionObject>(const task_tracker_systems::ProtocolVersionObject &object)
15+
{
16+
auto jsonObject = nlohmann::json::object();
17+
jsonObject["major"] = object.major;
18+
jsonObject["minor"] = object.minor;
19+
jsonObject["patch"] = object.patch;
20+
return jsonObject.dump(defaultJsonIndent);
21+
}
22+
23+
namespace task_tracker_systems
24+
{
25+
static void to_json(nlohmann::json &jsonObject, const task_tracker_systems::TaskObject &object)
26+
{
27+
jsonObject["id"] = object.id;
28+
jsonObject["label"] = object.label;
29+
jsonObject["duration"] = object.duration;
30+
}
31+
} // namespace task_tracker_systems
32+
33+
template <>
34+
std::string toJsonString<task_tracker_systems::TaskObject>(const task_tracker_systems::TaskObject &object)
35+
{
36+
auto jsonObject = nlohmann::json::object();
37+
to_json(jsonObject, object);
38+
return jsonObject.dump(defaultJsonIndent);
39+
}
40+
41+
template <>
42+
std::string toJsonString<task_tracker_systems::TaskList>(const task_tracker_systems::TaskList &object)
43+
{
44+
nlohmann::json jsonObject(object);
45+
return jsonObject.dump(defaultJsonIndent);
46+
}
47+
48+
template <>
49+
std::string toJsonString<task_tracker_systems::DeletedTaskObject>(const task_tracker_systems::DeletedTaskObject &object)
50+
{
51+
auto jsonObject = nlohmann::json::object();
52+
jsonObject["id"] = object.id;
53+
return jsonObject.dump(defaultJsonIndent);
54+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/**
2+
* \file .
3+
* \brief Interface to JSON serializer.
4+
*/
5+
#pragma once
6+
#include <string>
7+
8+
/**
9+
* Serializes an object to a JSON formatted string.
10+
*
11+
* @tparam T a serializable structure or container
12+
* @param object data to be serialized
13+
* @returns string JSON formatted
14+
*/
15+
template <class T>
16+
std::string toJsonString(const T &object);

lib/application_business_rules/serial_interface/Protocol.cpp

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,41 @@ namespace cli = command_line_interpreter;
88
// --------------------------
99
// --- define commands ------
1010
// --------------------------
11+
#include "JsonGenerator.hpp"
12+
#include <serial_protocol/ProtocolVersionObject.hpp>
13+
#include <serial_protocol/TaskList.hpp>
14+
#include <serial_protocol/TaskObject.hpp>
1115
#include <string>
1216

17+
using namespace task_tracker_systems;
18+
19+
// command for info
20+
static const auto info = []() {
21+
constexpr ProtocolVersionObject version = {.major = 0, .minor = 1, .patch = 0};
22+
serial_port::cout << toJsonString(version) << std::endl;
23+
};
24+
static const auto infoCmd = cli::makeCommand("info", std::function(info));
25+
1326
// command for list
14-
static const auto list = []() { serial_port::cout << "this is a list: a, b, c, ..." << std::endl; };
27+
static const auto list = []() {
28+
const TaskList dummyList = {
29+
{.id = 1, .label = "first", .duration = 100U},
30+
{.id = 2, .label = "second", .duration = 200U},
31+
};
32+
serial_port::cout << toJsonString(dummyList) << std::endl; };
1533
static const auto listCmd = cli::makeCommand("list", std::function(list));
1634

1735
// command for edit
18-
static const auto edit = [](const int id, const std::basic_string<ProtocolHandler::CharType> label, const int duration) {
19-
serial_port::cout << "Edit id(" << id << ") label('" << label << "') duration(" << duration << ")" << std::endl;
36+
static const auto edit = [](const unsigned int id, const std::basic_string<ProtocolHandler::CharType> label, const std::chrono::seconds::rep duration) {
37+
const TaskObject task = {.id = id, .label = label, .duration = duration};
38+
serial_port::cout << toJsonString(task) << std::endl;
2039
};
21-
static const cli::Option<int> id = {.labels = {"--id"}, .defaultValue = 0};
40+
static const cli::Option<unsigned int> id = {.labels = {"--id"}, .defaultValue = 0};
2241
static const cli::Option<std::basic_string<ProtocolHandler::CharType>> label = {.labels = {"--name"}, .defaultValue = "foo"};
23-
static const cli::Option<int> duration = {.labels = {"--duration"}, .defaultValue = 0};
42+
static const cli::Option<std::chrono::seconds::rep> duration = {.labels = {"--duration"}, .defaultValue = 0};
2443
static const auto editCmd = cli::makeCommand("edit", std::function(edit), std::make_tuple(&id, &label, &duration));
2544

26-
static const std::array<const cli::BaseCommand<char> *, 2> commands = {&listCmd, &editCmd};
45+
static const std::array<const cli::BaseCommand<char> *, 3> commands = {&listCmd, &editCmd, &infoCmd};
2746

2847
bool ProtocolHandler::execute(const CharType *const commandLine)
2948
{
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
namespace task_tracker_systems
4+
{
5+
6+
/**
7+
* deleted task object
8+
*/
9+
struct DeletedTaskObject
10+
{
11+
/**
12+
* unique identifier
13+
*/
14+
unsigned int id;
15+
};
16+
} // namespace task_tracker_systems
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#pragma once
2+
#include <cstdint>
3+
4+
namespace task_tracker_systems
5+
{
6+
/**
7+
* Protocol Version Object
8+
*/
9+
struct ProtocolVersionObject
10+
{
11+
std::uint8_t major;
12+
std::uint8_t minor;
13+
std::uint16_t patch;
14+
};
15+
} // namespace task_tracker_systems
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#pragma once
2+
#include "TaskObject.hpp"
3+
#include <vector>
4+
5+
namespace task_tracker_systems
6+
{
7+
8+
/**
9+
* list of tasks
10+
*/
11+
typedef std::vector<TaskObject> TaskList;
12+
} // namespace task_tracker_systems
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#pragma once
2+
#include <chrono>
3+
#include <string>
4+
5+
namespace task_tracker_systems
6+
{
7+
8+
/**
9+
* task object
10+
*/
11+
struct TaskObject
12+
{
13+
/**
14+
* unique identifier
15+
*/
16+
unsigned int id;
17+
/**
18+
* name or summary; ASCII only, no line breaks
19+
*/
20+
std::string label;
21+
/**
22+
* duration in seconds
23+
*/
24+
std::chrono::seconds::rep duration;
25+
};
26+
} // namespace task_tracker_systems

platformio.ini

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ extends = env
1313
lib_deps =
1414
adafruit/Adafruit SSD1306@~2.5.3
1515
lvgl@^8.3
16+
johboh/nlohmann-json@^3.11.3
1617
3rd_party_adapters ; this is necessary as no source code dependency shall exist to that packet
1718
lib_ldf_mode = deep ; to automatically detect nested dependencies (for external libraries)
1819
build_flags =
@@ -26,6 +27,7 @@ platform = native
2627
lib_deps =
2728
unity
2829
ArduinoFake@^0.4.0
30+
enterprise_business_rules
2931
utilities
3032
lib_ldf_mode = chain ; to simplify mocking, do not use deep mode
3133
build_flags =

0 commit comments

Comments
 (0)