Skip to content

Commit 6e7cac5

Browse files
authored
serial interface: respond with correctly formatted dummy data (#122)
Resolves #61 - [x] dependency must be merged first: Task-Tracker-Systems/Protocol-Serial-Interface#1 - [x] describe suggested changes ### Summary Use a JSON library to serialize objects to JSON formatted strings. Send dummy data. - `lib/3rd_party_adapters/nlohmann/JsonGenerator.cpp`: Add adapter for using a library which parses to/from JSON format. Although the operations currently used by the adapter are rather simple (to JSON) - one could argue that the use of the library is excessive. The library is used in anticipation that JSON strings will be parsed (from JSON). The `to_json()` definitions allows to use the library to parse arrays of the specific object. - `lib/application_business_rules/serial_interface/JsonGenerator.hpp` is a generic interface to the adapter above. - `lib/application_business_rules/serial_interface/Protocol.cpp`: - add command for "info" query - add dummy objects to be sent - `lib/enterprise_business_rules/serial_protocol/DeletedTaskObject.hpp`: defines structure for [deleted task JSON object](https://github.com/Task-Tracker-Systems/Protocol-Serial-Interface/blob/9315fd14875894c13f2616d422e197d37d798233/deleted-task-object.schema.json) - `lib/enterprise_business_rules/serial_protocol/ProtocolVersionObject.hpp`: Defines structure for [protocol version JSON object](https://github.com/Task-Tracker-Systems/Protocol-Serial-Interface/blob/9315fd14875894c13f2616d422e197d37d798233/protocol-version-object.schema.json) - `lib/enterprise_business_rules/serial_protocol/TaskObject.hpp`: Defines structure for [task JSON object](https://github.com/Task-Tracker-Systems/Protocol-Serial-Interface/blob/9315fd14875894c13f2616d422e197d37d798233/task-object.schema.json) - `lib/enterprise_business_rules/serial_protocol/TaskList.hpp`: Defines a container for [an array of JSON task objects](https://github.com/Task-Tracker-Systems/Protocol-Serial-Interface/blob/9315fd14875894c13f2616d422e197d37d798233/task-object.schema.json)
2 parents fc7e164 + 77082a0 commit 6e7cac5

8 files changed

Lines changed: 166 additions & 6 deletions

File tree

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
@@ -31,6 +31,7 @@ build_src_filter = +<*> -<.git/> -<.svn/>
3131
lib_deps =
3232
adafruit/Adafruit SSD1306@~2.5.3
3333
lvgl@^8.3
34+
johboh/nlohmann-json@^3.11.3
3435
; adding local library as dependency is necessary as it does not have any include files. This is because nothing should depend from this layer.
3536
3rd_party_adapters ; this is necessary as no source code dependency shall exist to that packet
3637
lib_ldf_mode = deep ; to automatically detect nested dependencies (for external libraries)
@@ -50,6 +51,7 @@ platform = native
5051
lib_deps =
5152
unity
5253
ArduinoFake
54+
enterprise_business_rules
5355
lib_ldf_mode = chain ; automatically detect dependencies but to not recurse into other components
5456
build_flags =
5557
-Wno-deprecated ; Workaround for https://github.com/FabioBatSilva/ArduinoFake/pull/41#issuecomment-1440550553

0 commit comments

Comments
 (0)