Skip to content

Commit 7e59720

Browse files
authored
Merge pull request #454 from mtconnect/453_448_mqtt_asset_handling_pretty_printing
453 448 mqtt asset handling pretty printing
2 parents 80209f7 + 15564b4 commit 7e59720

File tree

11 files changed

+51
-39
lines changed

11 files changed

+51
-39
lines changed

CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
set(AGENT_VERSION_MAJOR 2)
33
set(AGENT_VERSION_MINOR 3)
44
set(AGENT_VERSION_PATCH 0)
5-
set(AGENT_VERSION_BUILD 8)
5+
set(AGENT_VERSION_BUILD 9)
66
set(AGENT_VERSION_RC "")
77

88
# This minimum version is to support Visual Studio 2019 and C++ feature checking and FetchContent

conanfile.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99

1010
class MTConnectAgentConan(ConanFile):
1111
name = "mtconnect_agent"
12-
version = "2.2"
12+
version = "2.3"
1313
url = "https://github.com/mtconnect/cppagent.git"
1414
license = "Apache License 2.0"
1515
settings = "os", "compiler", "arch", "build_type"

src/mtconnect/agent.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -289,15 +289,15 @@ namespace mtconnect {
289289
{
290290
if (item.expired())
291291
continue;
292-
292+
293293
auto di = item.lock();
294294
if (di->hasInitialValue())
295295
{
296296
m_loopback->receive(di, *di->getInitialValue());
297297
}
298298
}
299299
}
300-
300+
301301
std::lock_guard<buffer::CircularBuffer> lock(m_circularBuffer);
302302
if (m_circularBuffer.addToBuffer(observation) != 0)
303303
{

src/mtconnect/device_model/data_item/data_item.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ namespace mtconnect {
201201
}
202202
}
203203
}
204-
204+
205205
if (const auto &init = maybeGet<string>("InitialValue"); init)
206206
{
207207
m_initialValue = *init;

src/mtconnect/device_model/data_item/data_item.hpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -139,11 +139,11 @@ namespace mtconnect {
139139
/// @brief get the topic name leaf node for this data item
140140
/// @return the topic name
141141
const auto &getTopicName() const { return m_topicName; }
142-
142+
143143
/// @brief get the initial value if one is set
144144
/// @return optional initial value
145145
const auto &getInitialValue() const { return m_initialValue; }
146-
146+
147147
Category getCategory() const { return m_category; }
148148
Representation getRepresentation() const { return m_representation; }
149149
SpecialClass getSpecialClass() const { return m_specialClass; }

src/mtconnect/mqtt/mqtt_client_impl.hpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ namespace mtconnect {
164164
m_connected = false;
165165
if (m_handler && m_handler->m_disconnected)
166166
m_handler->m_disconnected(shared_from_this());
167-
m_handler->m_disconnected(shared_from_this());
167+
m_handler->m_disconnected(shared_from_this());
168168
if (m_running)
169169
{
170170
reconnect();
@@ -419,7 +419,7 @@ namespace mtconnect {
419419
{
420420
return static_pointer_cast<MqttTcpClient>(shared_from_this());
421421
}
422-
422+
423423
/// @brief Get the Mqtt TCP Client
424424
/// @return pointer to the Mqtt TCP Client
425425
auto &getClient()
@@ -501,7 +501,7 @@ namespace mtconnect {
501501
{
502502
return static_pointer_cast<MqttTlsWSClient>(shared_from_this());
503503
}
504-
504+
505505
/// @brief Get the Mqtt TLS WebSocket Client
506506
/// @return pointer to the Mqtt TLS WebSocket Client
507507
auto &getClient()
@@ -540,7 +540,7 @@ namespace mtconnect {
540540
{
541541
return static_pointer_cast<MqttWSClient>(shared_from_this());
542542
}
543-
543+
544544
/// @brief Get the Mqtt TLS WebSocket Client
545545
/// @return pointer to the Mqtt TLS WebSocket Client
546546
auto &getClient()

src/mtconnect/sink/mqtt_sink/mqtt2_service.cpp

+12-10
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ namespace mtconnect {
185185
}
186186
}
187187

188-
auto seq = m_sinkContract->getCircularBuffer().getSequence();
188+
auto seq = publishCurrent(boost::system::error_code {});
189189
for (auto &dev : m_sinkContract->getDevices())
190190
{
191191
FilterSet filterSet = filterForDevice(dev);
@@ -197,10 +197,8 @@ namespace mtconnect {
197197
sampler->observe(seq, [this](const std::string &id) {
198198
return m_sinkContract->getDataItemById(id).get();
199199
});
200-
sampler->handlerCompleted();
200+
publishSample(sampler);
201201
}
202-
203-
publishCurrent(boost::system::error_code {});
204202
}
205203

206204
/// @brief publish sample when observations arrive.
@@ -244,18 +242,20 @@ namespace mtconnect {
244242
return end;
245243
}
246244

247-
void Mqtt2Service::publishCurrent(boost::system::error_code ec)
245+
SequenceNumber_t Mqtt2Service::publishCurrent(boost::system::error_code ec)
248246
{
247+
SequenceNumber_t firstSeq, seq = 0;
248+
249249
if (ec)
250250
{
251251
LOG(warning) << "Mqtt2Service::publishCurrent: " << ec.message();
252-
return;
252+
return 0;
253253
}
254254

255255
if (!m_client->isRunning() || !m_client->isConnected())
256256
{
257257
LOG(warning) << "Mqtt2Service::publishCurrent: client stopped";
258-
return;
258+
return 0;
259259
}
260260

261261
for (auto &device : m_sinkContract->getDevices())
@@ -264,7 +264,6 @@ namespace mtconnect {
264264
LOG(debug) << "Publishing current for: " << topic;
265265

266266
ObservationList observations;
267-
SequenceNumber_t firstSeq, seq;
268267
auto filterSet = filterForDevice(device);
269268

270269
{
@@ -288,6 +287,8 @@ namespace mtconnect {
288287
m_currentTimer.expires_after(m_currentInterval);
289288
m_currentTimer.async_wait(boost::asio::bind_executor(
290289
m_strand, boost::bind(&Mqtt2Service::publishCurrent, this, _1)));
290+
291+
return seq;
291292
}
292293

293294
bool Mqtt2Service::publish(observation::ObservationPtr &observation)
@@ -325,8 +326,9 @@ namespace mtconnect {
325326

326327
LOG(debug) << "Publishing Asset to topic: " << topic;
327328

328-
auto doc = m_jsonPrinter->print(asset);
329-
329+
asset::AssetList list {asset};
330+
auto doc = m_printer->printAssets(
331+
m_instanceId, uint32_t(m_sinkContract->getAssetStorage()->getMaxAssets()), 1, list);
330332
stringstream buffer;
331333
buffer << doc;
332334

src/mtconnect/sink/mqtt_sink/mqtt2_service.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ namespace mtconnect {
9696
void pubishInitialContent();
9797

9898
/// @brief Publish a current using `CurrentInterval` option.
99-
void publishCurrent(boost::system::error_code ec);
99+
SequenceNumber_t publishCurrent(boost::system::error_code ec);
100100

101101
/// @brief publish sample when observations arrive.
102102
SequenceNumber_t publishSample(std::shared_ptr<observation::AsyncObserver> sampler);

src/mtconnect/sink/rest_sink/rest_service.cpp

+7-4
Original file line numberDiff line numberDiff line change
@@ -523,29 +523,32 @@ namespace mtconnect {
523523
auto removed = *request->parameter<bool>("removed");
524524
auto count = *request->parameter<int32_t>("count");
525525
auto printer = printerForAccepts(request->m_accepts);
526+
auto pretty = *request->parameter<bool>("pretty");
526527

527528
respond(session, assetRequest(printer, count, removed, request->parameter<string>("type"),
528-
request->parameter<string>("device")));
529+
request->parameter<string>("device"), pretty));
529530
return true;
530531
};
531532

532533
auto idHandler = [&](SessionPtr session, RequestPtr request) -> bool {
533534
auto asset = request->parameter<string>("assetIds");
535+
auto pretty = *request->parameter<bool>("pretty");
534536
if (asset)
535537
{
536538
auto printer = m_sinkContract->getPrinter(acceptFormat(request->m_accepts));
537-
539+
538540
list<string> ids;
539541
stringstream str(*asset);
540542
string id;
541543
while (getline(str, id, ';'))
542544
ids.emplace_back(id);
543-
respond(session, assetIdsRequest(printer, ids));
545+
respond(session, assetIdsRequest(printer, ids, pretty));
544546
}
545547
else
546548
{
547549
auto printer = printerForAccepts(request->m_accepts);
548-
auto error = printError(printer, "INVALID_REQUEST", "No asset given");
550+
auto pretty = *request->parameter<bool>("pretty");
551+
auto error = printError(printer, "INVALID_REQUEST", "No asset given", pretty);
549552
respond(session, make_unique<Response>(rest_sink::status::bad_request, error,
550553
printer->mimeType()));
551554
}

test_package/agent_test.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -3089,12 +3089,11 @@ TEST_F(AgentTest, should_initialize_observaton_to_initial_value_when_available)
30893089
PARSE_XML_RESPONSE("/current");
30903090
ASSERT_XML_PATH_EQUAL(doc, "//m:DeviceStream//m:PartCount", "UNAVAILABLE");
30913091
}
3092-
3092+
30933093
m_agentTestHelper->m_adapter->processData("2024-01-22T20:00:00Z|avail|AVAILABLE");
30943094

30953095
{
30963096
PARSE_XML_RESPONSE("/current");
30973097
ASSERT_XML_PATH_EQUAL(doc, "//m:DeviceStream//m:PartCount", "0");
30983098
}
3099-
31003099
}

test_package/mqtt_sink_2_test.cpp

+19-11
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,23 @@ TEST_F(MqttSink2Test, mqtt_sink_should_publish_Sample)
248248

249249
auto handler = make_unique<ClientHandler>();
250250
bool gotSample = false;
251-
handler->m_receive = [&gotSample](std::shared_ptr<MqttClient> client, const std::string &topic,
251+
bool first = true;
252+
handler->m_receive = [&gotSample, &first](std::shared_ptr<MqttClient> client, const std::string &topic,
252253
const std::string &payload) {
253-
EXPECT_EQ("MTConnect/Sample/000", topic);
254-
255-
auto jdoc = json::parse(payload);
256-
auto streams = jdoc.at("/MTConnectStreams/Streams/0/DeviceStream"_json_pointer);
257-
EXPECT_EQ(string("LinuxCNC"), streams.at("/name"_json_pointer).get<string>());
258-
259-
gotSample = true;
254+
if (first)
255+
{
256+
first = false;
257+
}
258+
else
259+
{
260+
EXPECT_EQ("MTConnect/Sample/000", topic);
261+
262+
auto jdoc = json::parse(payload);
263+
auto streams = jdoc.at("/MTConnectStreams/Streams/0/DeviceStream"_json_pointer);
264+
EXPECT_EQ(string("LinuxCNC"), streams.at("/name"_json_pointer).get<string>());
265+
266+
gotSample = true;
267+
}
260268
};
261269

262270
createClient(options, std::move(handler));
@@ -267,9 +275,9 @@ TEST_F(MqttSink2Test, mqtt_sink_should_publish_Sample)
267275

268276
auto service = m_agentTestHelper->getMqtt2Service();
269277

270-
ASSERT_TRUE(waitFor(60s, [&service]() { return service->isConnected(); }));
271-
ASSERT_FALSE(gotSample);
272-
278+
ASSERT_TRUE(waitFor(60s, [&first]() { return !first; }));
279+
ASSERT_FALSE(first);
280+
273281
m_agentTestHelper->m_adapter->processData("2021-02-01T12:00:00Z|line|204");
274282
ASSERT_TRUE(waitFor(10s, [&gotSample]() { return gotSample; }));
275283
}

0 commit comments

Comments
 (0)