Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions src/mtconnect/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,18 +358,32 @@ namespace mtconnect {
if (m_intSchemaVersion >= SCHEMA_VERSION(2, 2))
asset->addHash();

m_assetStorage->addAsset(asset);
auto old = m_assetStorage->addAsset(asset);

for (auto &sink : m_sinks)
sink->publish(asset);

if (device)
{
DataItemPtr di;
auto added = device->getAssetAdded();
if (asset->isRemoved())
di = device->getAssetRemoved();
else
else if (old || !added)
{
di = device->getAssetChanged();
/// If we have changed the asset that is currently recorded as added. Make added unavailable.
if (added)
{
auto last = getLatest(added);
if (last && asset->getAssetId() == last->getValue<string>())
{
m_loopback->receive(added, {{"assetType", asset->getName()}, {"VALUE", g_unavailable}});
}
}
}
else if (added)
di = added;
if (di)
{
entity::Properties props {{"assetType", asset->getName()}, {"VALUE", asset->getAssetId()}};
Expand Down Expand Up @@ -763,17 +777,28 @@ namespace mtconnect {
{
m_loopback->receive(dev->getAssetRemoved(),
{{"assetType", asset->getName()}, {"VALUE", asset->getAssetId()}});

auto changed = dev->getAssetChanged();
auto last = getLatest(changed);
if (last && asset->getAssetId() == last->getValue<string>())
{
m_loopback->receive(changed, {{"assetType", asset->getName()}, {"VALUE", g_unavailable}});
auto changed = dev->getAssetChanged();
auto last = getLatest(changed);
if (last && asset->getAssetId() == last->getValue<string>())
{
m_loopback->receive(changed, {{"assetType", asset->getName()}, {"VALUE", g_unavailable}});
}
}

auto added = dev->getAssetAdded();
if (added)
{
auto last = getLatest(added);
if (last && asset->getAssetId() == last->getValue<string>())
{
m_loopback->receive(added, {{"assetType", asset->getName()}, {"VALUE", g_unavailable}});
}
}
}
}
}

// ---------------------------------------
// Agent Device
// ---------------------------------------
Expand Down Expand Up @@ -904,6 +929,17 @@ namespace mtconnect {
device->addDataItem(di, errors);
}

if (!device->getAssetAdded() && m_intSchemaVersion >= SCHEMA_VERSION(2, 6))
{
// Create asset removed data item and add it to the device.
entity::ErrorList errors;
auto di = DataItem::make({{"type", "ASSET_ADDED"s},
{"id", device->getId() + "_asset_add"}, {"discrete", true},
{"category", "EVENT"s}},
errors);
device->addDataItem(di, errors);
}

if (!device->getAssetCount() && m_intSchemaVersion >= SCHEMA_VERSION(2, 0))
{
entity::ErrorList errors;
Expand Down
4 changes: 2 additions & 2 deletions src/mtconnect/agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -378,9 +378,9 @@ namespace mtconnect {
bool removeAllAssets(const std::optional<std::string> device,
const std::optional<std::string> type, const std::optional<Timestamp> time,
asset::AssetList &list);
/// @brief Send asset removed observation when an asset is removed.
/// @brief Send asset changed and added observation when an asset is removed.
///
/// Also sets asset changed to `UNAVAILABLE` if the asset removed asset was the last changed.
/// Also sets asset changed and added to `UNAVAILABLE` if the asset removed asset was the last changed.
///
/// @param device The device related to the asset
/// @param asset The asset
Expand Down
2 changes: 2 additions & 0 deletions src/mtconnect/device_model/data_item/data_item.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ namespace mtconnect {
m_specialClass = ASSET_REMOVED_CLS;
else if (type == "ASSET_CHANGED")
m_specialClass = ASSET_CHANGED_CLS;
else if (type == "ASSET_ADDED")
m_specialClass = ASSET_ADDED_CLS;
}
else if (category == "CONDITION")
{
Expand Down
4 changes: 3 additions & 1 deletion src/mtconnect/device_model/data_item/data_item.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ namespace mtconnect {
THREE_SPACE_CLS,
NONE_CLS,
ASSET_REMOVED_CLS,
ASSET_CHANGED_CLS
ASSET_CHANGED_CLS,
ASSET_ADDED_CLS
};

public:
Expand Down Expand Up @@ -164,6 +165,7 @@ namespace mtconnect {
bool isCondition() const { return m_category == CONDITION; }
bool isAlarm() const { return m_specialClass == ALARM_CLS; }
bool isMessage() const { return m_specialClass == MESSAGE_CLS; }
bool isAssetAdded() const { return m_specialClass == ASSET_ADDED_CLS; }
bool isAssetChanged() const { return m_specialClass == ASSET_CHANGED_CLS; }
bool isAssetRemoved() const { return m_specialClass == ASSET_REMOVED_CLS; }
bool isTimeSeries() const { return m_representation == TIME_SERIES; }
Expand Down
2 changes: 2 additions & 0 deletions src/mtconnect/device_model/device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ namespace mtconnect {
m_availability = dataItem;
else if (dataItem->getType() == "ASSET_CHANGED")
m_assetChanged = dataItem;
else if (dataItem->getType() == "ASSET_ADDED")
m_assetAdded = dataItem;
else if (dataItem->getType() == "ASSET_REMOVED")
m_assetRemoved = dataItem;
else if (dataItem->getType() == "ASSET_COUNT")
Expand Down
2 changes: 2 additions & 0 deletions src/mtconnect/device_model/device.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ namespace mtconnect {
DataItemPtr getAvailability() const { return m_availability; }
DataItemPtr getAssetChanged() const { return m_assetChanged; }
DataItemPtr getAssetRemoved() const { return m_assetRemoved; }
DataItemPtr getAssetAdded() const { return m_assetAdded; }
DataItemPtr getAssetCount() const { return m_assetCount; }
///@}

Expand Down Expand Up @@ -351,6 +352,7 @@ namespace mtconnect {
DataItemPtr m_availability;
DataItemPtr m_assetChanged;
DataItemPtr m_assetRemoved;
DataItemPtr m_assetAdded;
DataItemPtr m_assetCount;

DataItemIndex m_dataItems;
Expand Down
1 change: 1 addition & 0 deletions src/mtconnect/observation/observation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ namespace mtconnect {

factory->registerFactory("Events:Message", Message::getFactory());
factory->registerFactory("Events:MessageDiscrete", Message::getFactory());
factory->registerFactory("Events:AssetAdded", AssetEvent::getFactory());
factory->registerFactory("Events:AssetChanged", AssetEvent::getFactory());
factory->registerFactory("Events:AssetRemoved", AssetEvent::getFactory());
factory->registerFactory("Events:DeviceAdded", DeviceEvent::getFactory());
Expand Down
2 changes: 1 addition & 1 deletion src/mtconnect/pipeline/response_document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ namespace mtconnect::pipeline {
return true;
}

if (di->isAssetChanged())
if (di->isAssetChanged() || di->isAssetAdded())
out.m_assetEvents.emplace_back((obs));
else
out.m_entities.emplace_back(obs);
Expand Down
3 changes: 2 additions & 1 deletion src/mtconnect/validation/observation_validations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Validation ControlledVocabularies {
{{"ACTIVE", {SCHEMA_VERSION(1, 2), 0}}, {"INACTIVE", {SCHEMA_VERSION(1, 2), 0}}}},
{"Alarm", {}},
{"AssetChanged", {}},
{"AssetAdded", {}},
{"AssetRemoved", {}},
{"Availability",
{{"AVAILABLE", {SCHEMA_VERSION(1, 1), 0}}, {"UNAVAILABLE", {SCHEMA_VERSION(1, 1), 0}}}},
Expand Down Expand Up @@ -248,4 +249,4 @@ Validation ControlledVocabularies {
{"ActivePowerSource", {}},
{"LocationNarrative", {}},
{"Thickness", {}},
{"LocationSpatialGeographic", {}}};
{"LocationSpatialGeographic", {}}};
1 change: 1 addition & 0 deletions test_package/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,7 @@ add_agent_test(observation_validation TRUE pipeline)
add_agent_test(correct_timestamp TRUE pipeline)

add_agent_test(agent TRUE core)
add_agent_test(agent_asset TRUE core)
add_agent_test(globals FALSE core)

add_agent_test(config_parser FALSE configuration)
Expand Down
Loading
Loading