Skip to content

Commit

Permalink
Merge pull request #56 from Wolkabout/bugfix/vector-reading-parsing
Browse files Browse the repository at this point in the history
Fixed parsing when a reading is multi-value to use quotes.
  • Loading branch information
nanavuletic authored Feb 23, 2022
2 parents 2cc2d6d + bf67215 commit 6f579b5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 12 deletions.
29 changes: 20 additions & 9 deletions core/protocol/wolkabout/WolkaboutDataProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,16 +276,27 @@ std::unique_ptr<Message> WolkaboutDataProtocol::makeOutboundMessage(const std::s
for (const auto& reading : member.second)
{
const auto& key = reading.getReference();
if (reading.isBoolean())
time[key] = reading.getBoolValue();
else if (reading.isUInt())
time[key] = reading.getUIntValue();
else if (reading.isInt())
time[key] = reading.getIntValue();
else if (reading.isDouble())
time[key] = reading.getDoubleValue();
if (!reading.isMulti())
{
if (reading.isBoolean())
time[key] = reading.getBoolValue();
else if (reading.isUInt())
time[key] = reading.getUIntValue();
else if (reading.isInt())
time[key] = reading.getIntValue();
else if (reading.isDouble())
time[key] = reading.getDoubleValue();
else
time[key] = reading.getStringValue();
}
else
time[key] = reading.getStringValue();
{
auto string = std::stringstream{};
const auto size = reading.getStringValues().size();
for (auto i = std::size_t{0}; i < size; ++i)
string << reading.getStringValues()[i] << (i < (size - 1) ? "," : "");
time[key] = string.str();
}
}

// And add it into the array
Expand Down
21 changes: 21 additions & 0 deletions tests/WolkaboutDataProtocolTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ class WolkaboutDataProtocolTests : public Test

const std::string TEMPERATURE = "T";

const std::string ACCELERATION = "Acceleration";

const std::string ROTATION = "Rotation";

const std::string TRANSLATION = "Translation";
Expand Down Expand Up @@ -200,6 +202,25 @@ TEST_F(WolkaboutDataProtocolTests, SerializeFeedValuesSingle)
EXPECT_TRUE(std::regex_match(message->getContent(), payloadRegex));
}

TEST_F(WolkaboutDataProtocolTests, SerializeMultiValueReading)
{
// Make a single reading that will be sent out
auto reading = Reading{ACCELERATION, std::vector<std::double_t>{12.1005, 0.2304, -0.128}};
auto values = FeedValuesMessage({reading});

// Make place for the payload
auto message = std::unique_ptr<wolkabout::Message>{};
ASSERT_NO_FATAL_FAILURE(message = protocol->makeOutboundMessage(DEVICE_KEY, values));
ASSERT_NE(message, nullptr);
LogMessage(*message);

// Analyze both the topic and the content by regex
const auto topicRegex = std::regex(R"(d2p\/\w+\/feed_values)");
const auto payloadRegex = std::regex(R"(\[\{"\w+":"\d+.\d+,\d+.\d+,\-\d+.\d+"\}\])");
EXPECT_TRUE(std::regex_match(message->getChannel(), topicRegex));
EXPECT_TRUE(std::regex_match(message->getContent(), payloadRegex));
}

TEST_F(WolkaboutDataProtocolTests, SerializeFeedValuesMultiple)
{
// Make a single reading that will be sent out
Expand Down
6 changes: 3 additions & 3 deletions tests/mocks/DataProtocolMock.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ class DataProtocolMock : public DataProtocol
MOCK_METHOD(std::unique_ptr<Message>, makeOutboundMessage, (const std::string&, SynchronizeParametersMessage));
MOCK_METHOD(std::unique_ptr<Message>, makeOutboundMessage,
(const std::string&, DetailsSynchronizationRequestMessage));
MOCK_METHOD(std::shared_ptr<FeedValuesMessage>, parseFeedValues, (std::shared_ptr<Message>));
MOCK_METHOD(std::shared_ptr<ParametersUpdateMessage>, parseParameters, (std::shared_ptr<Message>));
MOCK_METHOD(std::shared_ptr<DetailsSynchronizationResponseMessage>, parseDetails, (std::shared_ptr<Message>));
MOCK_METHOD(std::unique_ptr<FeedValuesMessage>, parseFeedValues, (std::shared_ptr<Message>));
MOCK_METHOD(std::unique_ptr<ParametersUpdateMessage>, parseParameters, (std::shared_ptr<Message>));
MOCK_METHOD(std::unique_ptr<DetailsSynchronizationResponseMessage>, parseDetails, (std::shared_ptr<Message>));
};

#endif // WOLKABOUTCONNECTOR_DATAPROTOCOLMOCK_H

0 comments on commit 6f579b5

Please sign in to comment.