Skip to content

Commit 78e4f6f

Browse files
Merge 07c6724 into 613ccb9
2 parents 613ccb9 + 07c6724 commit 78e4f6f

File tree

3 files changed

+101
-32
lines changed

3 files changed

+101
-32
lines changed

Diff for: src/Mqtt/MqttMessagingProtocolJSON.cpp

+58-22
Original file line numberDiff line numberDiff line change
@@ -81,28 +81,11 @@ namespace sua {
8181
std::string MqttMessagingProtocolJSON::createMessage(const class Context& ctx, const std::string& name)
8282
{
8383
if(name == "systemVersion") {
84-
// clang-format off
85-
const std::string tpl = jsonTemplate(R"(
86-
{
87-
"timestamp": {},
88-
"payload": {
89-
"domains": [
90-
{
91-
"id": "self-update",
92-
"components": [
93-
{
94-
"id": "os-image",
95-
"version": "{}"
96-
}
97-
]
98-
}
99-
]
100-
}
101-
}
102-
)");
103-
// clang-format on
104-
105-
return fmt::format(tpl, epochTime(), ctx.currentState.version);
84+
if(ctx.desiredState.activityId.empty()) {
85+
return writeSystemVersionWithoutActivityId(ctx.currentState.version);
86+
} else {
87+
return writeSystemVersionWithActivityId(ctx.currentState.version, ctx.desiredState.activityId);
88+
}
10689
}
10790

10891
if(name == "identifying") {
@@ -232,4 +215,57 @@ namespace sua {
232215
return fmt::format(tpl, desiredState.activityId, epochTime(), state, stateMessage, desiredState.bundleVersion, status, progress, statusMessage);
233216
}
234217

218+
std::string MqttMessagingProtocolJSON::writeSystemVersionWithoutActivityId(const std::string & version)
219+
{
220+
// clang-format off
221+
const std::string tpl = jsonTemplate(R"(
222+
{
223+
"timestamp": {},
224+
"payload": {
225+
"softwareNodes": [
226+
{
227+
"id": "os-image",
228+
"version": "{}",
229+
"name": "System Image",
230+
"type": "IMAGE",
231+
"parameters": []
232+
}
233+
],
234+
"hardwareNodes": [],
235+
"associations": []
236+
}
237+
}
238+
)");
239+
// clang-format on
240+
241+
return fmt::format(tpl, epochTime(), version);
242+
}
243+
244+
std::string MqttMessagingProtocolJSON::writeSystemVersionWithActivityId(const std::string & version, const std::string & activityId)
245+
{
246+
// clang-format off
247+
const std::string tpl = jsonTemplate(R"(
248+
{
249+
"activityId": "{}",
250+
"timestamp": {},
251+
"payload": {
252+
"softwareNodes": [
253+
{
254+
"id": "os-image",
255+
"version": "{}",
256+
"name": "System Image",
257+
"type": "IMAGE",
258+
"parameters": []
259+
}
260+
],
261+
"hardwareNodes": [],
262+
"associations": []
263+
}
264+
}
265+
)");
266+
// clang-format on
267+
268+
return fmt::format(tpl, activityId, epochTime(), version);
269+
}
270+
235271
} // namespace sua

Diff for: src/Mqtt/MqttMessagingProtocolJSON.h

+3
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ namespace sua {
3939
const std::string & state, const std::string & stateMessage,
4040
const std::string & status, const std::string & statusMessage,
4141
int progress) const;
42+
43+
std::string writeSystemVersionWithoutActivityId(const std::string & version);
44+
std::string writeSystemVersionWithActivityId(const std::string & version, const std::string & activityId);
4245
};
4346

4447
} // namespace sua

Diff for: utest/TestMqttMessagingProtocolJSON.cpp

+40-10
Original file line numberDiff line numberDiff line change
@@ -82,26 +82,56 @@ namespace {
8282
EXPECT_EQ(s.activityId, "random-uuid-as-string");
8383
}
8484

85-
TEST_F(TestMessagingProtocolJSON, createMessage_systemVersion)
85+
TEST_F(TestMessagingProtocolJSON, createMessage_systemVersionWithoutActivityId)
8686
{
87+
ctx.desiredState.activityId = "";
8788
const std::string result = ProtocolJSON().createMessage(ctx, "systemVersion");
8889

8990
// clang-format off
9091
const std::string expected = R"(
9192
{
9293
"timestamp": 42,
9394
"payload": {
94-
"domains": [
95+
"softwareNodes": [
9596
{
96-
"id": "self-update",
97-
"components": [
98-
{
99-
"id": "os-image",
100-
"version": "1.0"
101-
}
102-
]
97+
"id": "os-image",
98+
"version": "1.0",
99+
"name": "System Image",
100+
"type": "IMAGE",
101+
"parameters": []
103102
}
104-
]
103+
],
104+
"hardwareNodes": [],
105+
"associations": []
106+
}
107+
}
108+
)";
109+
// clang-format on
110+
111+
EXPECT_EQ_MULTILINE(result, expected);
112+
}
113+
114+
TEST_F(TestMessagingProtocolJSON, createMessage_systemVersionWithActivityId)
115+
{
116+
const std::string result = ProtocolJSON().createMessage(ctx, "systemVersion");
117+
118+
// clang-format off
119+
const std::string expected = R"(
120+
{
121+
"activityId": "id",
122+
"timestamp": 42,
123+
"payload": {
124+
"softwareNodes": [
125+
{
126+
"id": "os-image",
127+
"version": "1.0",
128+
"name": "System Image",
129+
"type": "IMAGE",
130+
"parameters": []
131+
}
132+
],
133+
"hardwareNodes": [],
134+
"associations": []
105135
}
106136
}
107137
)";

0 commit comments

Comments
 (0)