Skip to content

Commit

Permalink
Merge pull request #450 from mtconnect/440_initial_value_handling
Browse files Browse the repository at this point in the history
  • Loading branch information
wsobel authored Apr 28, 2024
2 parents 4840863 + de28136 commit 00e0d1c
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 9 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,9 @@ jobs:
key: ${{ runner.os }}-build-${{ matrix.shared }}-${{ hashFiles('**/conanfile.py') }}

- name: Install Conan
uses: turtlebrowser/[email protected]

run: |
brew install conan
- name: Setup Conan
if: steps.cache.outputs.cache-hit != 'true'
run: |
Expand Down
3 changes: 1 addition & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
set(AGENT_VERSION_MAJOR 2)
set(AGENT_VERSION_MINOR 3)
set(AGENT_VERSION_PATCH 0)
set(AGENT_VERSION_BUILD 7
)
set(AGENT_VERSION_BUILD 8)
set(AGENT_VERSION_RC "")

# This minimum version is to support Visual Studio 2019 and C++ feature checking and FetchContent
Expand Down
18 changes: 18 additions & 0 deletions src/mtconnect/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,24 @@ namespace mtconnect {
// ---------------------------------------
void Agent::receiveObservation(observation::ObservationPtr observation)
{
// Check for availability
if (observation->getDataItem()->getType() == "AVAILABILITY" && !observation->isUnavailable())
{
// Set all the initial values.
auto device = observation->getDataItem()->getComponent()->getDevice();
for (auto item : device->getDeviceDataItems())
{
if (item.expired())
continue;

auto di = item.lock();
if (di->hasInitialValue())
{
m_loopback->receive(di, *di->getInitialValue());
}
}
}

std::lock_guard<buffer::CircularBuffer> lock(m_circularBuffer);
if (m_circularBuffer.addToBuffer(observation) != 0)
{
Expand Down
7 changes: 6 additions & 1 deletion src/mtconnect/device_model/data_item/data_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace mtconnect {
{"Definition", ValueType::ENTITY, definition, false},
{"Constraints", ValueType::ENTITY_LIST, constraints, false},
{"Relationships", ValueType::ENTITY_LIST, relationships, false},
{"InitialValue", ValueType::DOUBLE, false},
{"InitialValue", ValueType::STRING, false},
{"ResetTrigger", false}});
factory->setFunction([](const std::string &name, Properties &props) -> EntityPtr {
auto ptr = make_shared<DataItem>(name, props);
Expand Down Expand Up @@ -201,6 +201,11 @@ namespace mtconnect {
}
}
}

if (const auto &init = maybeGet<string>("InitialValue"); init)
{
m_initialValue = *init;
}

if (const auto &filters = getList("Filters"))
{
Expand Down
11 changes: 7 additions & 4 deletions src/mtconnect/device_model/data_item/data_item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,11 @@ namespace mtconnect {
/// @brief get the topic name leaf node for this data item
/// @return the topic name
const auto &getTopicName() const { return m_topicName; }


/// @brief get the initial value if one is set
/// @return optional initial value
const auto &getInitialValue() const { return m_initialValue; }

Category getCategory() const { return m_category; }
Representation getRepresentation() const { return m_representation; }
SpecialClass getSpecialClass() const { return m_specialClass; }
Expand Down Expand Up @@ -169,6 +173,7 @@ namespace mtconnect {
bool isDiscrete() const { return m_discrete; }
bool isThreeSpace() const { return m_specialClass == THREE_SPACE_CLS; }
bool isOrphan() const { return m_component.expired(); }
bool hasInitialValue() const { return bool(m_initialValue); }
///@}

void makeDiscrete()
Expand Down Expand Up @@ -279,6 +284,7 @@ namespace mtconnect {
std::optional<std::string> m_source;
std::string m_preferredName;
std::optional<std::string> m_constantValue;
std::optional<std::string> m_initialValue;
std::optional<double> m_minimumDelta;
std::optional<double> m_minimumPeriod;
std::string m_key;
Expand All @@ -301,9 +307,6 @@ namespace mtconnect {
// The reset trigger;
std::string m_resetTrigger;

// Initial value
std::string m_initialValue;

// Component that data item is associated with
std::weak_ptr<Component> m_component;
std::weak_ptr<Composition> m_composition;
Expand Down
23 changes: 23 additions & 0 deletions test_package/agent_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3075,3 +3075,26 @@ TEST_F(AgentTest, should_set_sender_from_config_in_XML_header)
ASSERT_XML_PATH_EQUAL(doc, "//m:Header@sender", "MachineXXX");
}
}

TEST_F(AgentTest, should_initialize_observaton_to_initial_value_when_available)
{
m_agentTestHelper->createAgent("/samples/test_config.xml", 8, 4, "2.2", 4, true);

auto device = m_agentTestHelper->getAgent()->getDeviceByName("LinuxCNC");
ASSERT_TRUE(device);

addAdapter();

{
PARSE_XML_RESPONSE("/current");
ASSERT_XML_PATH_EQUAL(doc, "//m:DeviceStream//m:PartCount", "UNAVAILABLE");
}

m_agentTestHelper->m_adapter->processData("2024-01-22T20:00:00Z|avail|AVAILABLE");

{
PARSE_XML_RESPONSE("/current");
ASSERT_XML_PATH_EQUAL(doc, "//m:DeviceStream//m:PartCount", "0");
}

}

0 comments on commit 00e0d1c

Please sign in to comment.