Skip to content
This repository was archived by the owner on Jul 18, 2023. It is now read-only.
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
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set(AGENT_VERSION_MAJOR 2)
set(AGENT_VERSION_MINOR 2)
set(AGENT_VERSION_PATCH 0)
set(AGENT_VERSION_BUILD 1)
set(AGENT_VERSION_RC "_RC3")
set(AGENT_VERSION_RC "_RC4")

# This minimum version is to support Visual Studio 2017 and C++ feature checking and FetchContent
cmake_minimum_required(VERSION 3.16 FATAL_ERROR)
Expand Down
23 changes: 23 additions & 0 deletions samples/dyn_load.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<MTConnectDevices xmlns:m="urn:mtconnect.org:MTConnectDevices:2.2" xmlns="urn:mtconnect.org:MTConnectDevices:2.2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:mtconnect.org:MTConnectDevices:2.2 http://schemas.mtconnect.org/schemas/MTConnectDevices_2.2.xsd">
<Header creationTime="2023-04-22T19:36:38Z" sender="myshkin" instanceId="0" version="2.2.0.1" assetBufferSize="0" assetCount="0" bufferSize="0"/>
<Devices>
<Device hash="0CZk9hw5r5wZRzoZjN6Z+a8poJw=" id="d" name="LinuxCNC" sampleInterval="10" uuid="000">
<Description manufacturer="NIST" serialNumber=""/>
<DataItems>
<DataItem category="EVENT" id="a" name="avail" type="AVAILABILITY"/>
<DataItem category="EVENT" discrete="true" id="d_asset_chg" type="ASSET_CHANGED"/>
<DataItem category="EVENT" id="d_asset_rem" type="ASSET_REMOVED"/>
<DataItem category="EVENT" id="d_asset_count" representation="DATA_SET" type="ASSET_COUNT"/>
</DataItems>
<Components>
<Controller id="cont">
<DataItems>
<DataItem category="EVENT" id="exec" type="EXECUTION"/>
<DataItem category="EVENT" id="mode" name="mode" type="CONTROLLER_MODE"/>
</DataItems>
</Controller>
</Components>
</Device>
</Devices>
</MTConnectDevices>
9 changes: 9 additions & 0 deletions samples/empty.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<MTConnectDevices xmlns:m="urn:mtconnect.org:MTConnectDevices:2.2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:mtconnect.org:MTConnectDevices:2.2"
xsi:schemaLocation="urn:mtconnect.org:MTConnectDevices:2.2 ../schemas/MTConnectDevices_2.2.xsd">
<Header creationTime="2009-03-22T01:50:29+00:00" sender="localhost" instanceId="1237628993"
bufferSize="100000" version="2.2" deviceModelChangeTime="2022-06-01T00:00:00" assetBufferSize="1024" assetCount="0"/>
<Devices/>
</MTConnectDevices>
202 changes: 138 additions & 64 deletions src/mtconnect/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace mtconnect {
static const string g_available("AVAILABLE");

// Agent public methods
Agent::Agent(boost::asio::io_context &context, const string &deviceXmlPath,
Agent::Agent(config::AsyncContext &context, const string &deviceXmlPath,
const ConfigOptions &options)
: m_options(options),
m_context(context),
Expand All @@ -83,7 +83,7 @@ namespace mtconnect {
m_deviceXmlPath(deviceXmlPath),
m_circularBuffer(GetOption<int>(options, config::BufferSize).value_or(17),
GetOption<int>(options, config::CheckpointFrequency).value_or(1000)),
m_pretty(GetOption<bool>(options, mtconnect::configuration::Pretty).value_or(false))
m_pretty(IsOptionSet(options, mtconnect::configuration::Pretty))
{
using namespace asset;

Expand All @@ -97,8 +97,8 @@ namespace mtconnect {

m_assetStorage = make_unique<AssetBuffer>(
GetOption<int>(options, mtconnect::configuration::MaxAssets).value_or(1024));
m_versionDeviceXml =
GetOption<bool>(options, mtconnect::configuration::VersionDeviceXmlUpdates).value_or(false);
m_versionDeviceXml = IsOptionSet(options, mtconnect::configuration::VersionDeviceXmlUpdates);
m_createUniqueIds = IsOptionSet(options, config::CreateUniqueIds);

auto jsonVersion =
uint32_t(GetOption<int>(options, mtconnect::configuration::JsonVersion).value_or(2));
Expand Down Expand Up @@ -145,6 +145,9 @@ namespace mtconnect {
for (auto device : devices)
addDevice(device);

if (m_versionDeviceXml && m_createUniqueIds)
versionDeviceXml();

loadCachedProbe();

m_initialized = true;
Expand Down Expand Up @@ -360,7 +363,6 @@ namespace mtconnect {
return false;
}

// Fir the DeviceAdded event for each device
bool changed = false;
for (auto device : devices)
{
Expand All @@ -387,6 +389,46 @@ namespace mtconnect {
}
}

void Agent::loadDevice(const string &deviceXml, const optional<string> source)
{
m_context.pause([=](config::AsyncContext &context) {
try
{
auto printer = dynamic_cast<printer::XmlPrinter *>(m_printers["xml"].get());
auto device = m_xmlParser->parseDevice(deviceXml, printer);

if (device)
{
bool changed = receiveDevice(device, true);
if (changed)
loadCachedProbe();

if (source)
{
auto s = findSource(*source);
if (s)
{
const auto &name = device->getComponentName();
s->setOptions({{config::Device, *name}});
}
}
}
}
catch (runtime_error &e)
{
LOG(error) << "Error loading device: " + deviceXml;
LOG(error) << "Error detail: " << e.what();
cerr << e.what() << endl;
}
catch (exception &f)
{
LOG(error) << "Error loading device: " + deviceXml;
LOG(error) << "Error detail: " << f.what();
cerr << f.what() << endl;
}
});
}

bool Agent::receiveDevice(device_model::DevicePtr device, bool version)
{
NAMED_SCOPE("Agent::receiveDevice");
Expand Down Expand Up @@ -442,7 +484,16 @@ namespace mtconnect {
if (auto odi = oldDev->getAssetCount(), ndi = device->getAssetCount(); odi && !ndi)
device->addDataItem(odi, errors);

if (errors.size() > 0)
{
LOG(error) << "Error adding device required data items for " << *device->getUuid() << ':';
for (const auto &e : errors)
LOG(error) << " " << e->what();
return false;
}

verifyDevice(device);
createUniqueIds(device);

LOG(info) << "Checking if device " << *uuid << " has changed";
if (*device != *oldDev)
Expand Down Expand Up @@ -508,26 +559,37 @@ namespace mtconnect {

void Agent::versionDeviceXml()
{
NAMED_SCOPE("Agent::versionDeviceXml");

using namespace std::chrono;

if (m_versionDeviceXml)
{
// update with a new version of the device.xml, saving the old one
// with a date time stamp
auto ext = "."s + getCurrentTime(LOCAL);
auto ext =
date::format(".%Y-%m-%dT%H+%M+%SZ", date::floor<milliseconds>(system_clock::now()));
fs::path file(m_deviceXmlPath);
fs::path backup(m_deviceXmlPath + ext);
if (!fs::exists(backup))
fs::rename(file, backup);

printer::XmlPrinter printer(true);

std::list<DevicePtr> list;
copy_if(m_deviceIndex.begin(), m_deviceIndex.end(), back_inserter(list),
[](DevicePtr d) { return dynamic_cast<AgentDevice *>(d.get()) == nullptr; });
auto probe = printer.printProbe(0, 0, 0, 0, 0, list);

ofstream devices(file.string());
devices << probe;
devices.close();
auto printer = getPrinter("xml");
if (printer != nullptr)
{
std::list<DevicePtr> list;
copy_if(m_deviceIndex.begin(), m_deviceIndex.end(), back_inserter(list),
[](DevicePtr d) { return dynamic_cast<AgentDevice *>(d.get()) == nullptr; });
auto probe = printer->printProbe(0, 0, 0, 0, 0, list, nullptr, true, true);

ofstream devices(file.string());
devices << probe;
devices.close();
}
else
{
LOG(error) << "Cannot find xml printer";
}
}
}

Expand Down Expand Up @@ -661,7 +723,8 @@ namespace mtconnect {
auto devices = m_xmlParser->parseFile(
configXmlPath, dynamic_cast<printer::XmlPrinter *>(m_printers["xml"].get()));

if (!m_schemaVersion && m_xmlParser->getSchemaVersion())
if (!m_schemaVersion && m_xmlParser->getSchemaVersion() &&
!m_xmlParser->getSchemaVersion()->empty())
{
m_schemaVersion = m_xmlParser->getSchemaVersion();
m_intSchemaVersion = IntSchemaVersion(*m_schemaVersion);
Expand Down Expand Up @@ -809,38 +872,32 @@ namespace mtconnect {
}
else
{
// Check if we are already initialized. If so, the device will need to be
// verified, additional data items added, and initial values set.
// if (!m_initialized)
{
m_deviceIndex.push_back(device);
m_deviceIndex.push_back(device);

// TODO: Redo Resolve Reference with entity
// device->resolveReferences();
verifyDevice(device);
// TODO: Redo Resolve Reference with entity
// device->resolveReferences();
verifyDevice(device);
createUniqueIds(device);

if (m_observationsInitialized)
{
initializeDataItems(device);
if (m_observationsInitialized)
{
initializeDataItems(device);

// Check for single valued constrained data items.
if (m_agentDevice && device != m_agentDevice)
// Check for single valued constrained data items.
if (m_agentDevice && device != m_agentDevice)
{
entity::Properties props {{"VALUE", uuid}};
if (m_intSchemaVersion >= SCHEMA_VERSION(2, 2))
{
entity::Properties props {{"VALUE", uuid}};
if (m_intSchemaVersion >= SCHEMA_VERSION(2, 2))
{
const auto &hash = device->getProperty("hash");
if (hash.index() != EMPTY)
props.insert_or_assign("hash", hash);
}

auto d = m_agentDevice->getDeviceDataItem("device_added");
m_loopback->receive(d, props);
const auto &hash = device->getProperty("hash");
if (hash.index() != EMPTY)
props.insert_or_assign("hash", hash);
}

auto d = m_agentDevice->getDeviceDataItem("device_added");
m_loopback->receive(d, props);
}
}
// else
// LOG(warning) << "Adding device " << uuid << " after initialialization not supported yet";
}

if (m_intSchemaVersion >= SCHEMA_VERSION(2, 2))
Expand Down Expand Up @@ -875,6 +932,7 @@ namespace mtconnect {

if (changed)
{
createUniqueIds(device);
if (m_intSchemaVersion >= SCHEMA_VERSION(2, 2))
device->addHash();

Expand Down Expand Up @@ -910,6 +968,28 @@ namespace mtconnect {
}
}

void Agent::createUniqueIds(DevicePtr device)
{
if (m_createUniqueIds && !dynamic_pointer_cast<AgentDevice>(device))
{
std::unordered_map<std::string, std::string> idMap;

device->createUniqueIds(idMap);
device->updateReferences(idMap);

// Update the data item map.
for (auto &id : idMap)
{
auto di = device->getDeviceDataItem(id.second);
if (auto it = m_dataItemMap.find(id.first); it != m_dataItemMap.end())
{
m_dataItemMap.erase(it);
m_dataItemMap.emplace(id.second, di);
}
}
}
}

void Agent::loadCachedProbe()
{
NAMED_SCOPE("Agent::loadCachedProbe");
Expand Down Expand Up @@ -1026,30 +1106,22 @@ namespace mtconnect {

void AgentPipelineContract::deliverCommand(entity::EntityPtr entity)
{
static auto pattern = regex("\\*[ ]*([^:]+):[ ]*(.+)");
auto command = entity->get<string>("command");
auto value = entity->getValue<string>();
smatch match;

if (std::regex_match(value, match, pattern))
{
auto device = entity->maybeGet<string>("device");
auto command = boost::algorithm::to_lower_copy(match[1].str());
auto param = match[2].str();
auto source = entity->maybeGet<string>("source");
auto device = entity->maybeGet<string>("device");
auto source = entity->maybeGet<string>("source");

if (!device || !source)
{
LOG(error) << "Invalid command: " << command << ", device or source not specified";
}
else
{
LOG(debug) << "Processing command: " << command << ": " << value;
m_agent->receiveCommand(*device, command, param, *source);
}
if ((command != "devicemodel" && !device) || !source)
{
LOG(error) << "Invalid command: " << command << ", device or source not specified";
}
else
{
LOG(warning) << "Cannot parse command: " << value;
LOG(debug) << "Processing command: " << command << ": " << value;
if (!device)
device.emplace("");
m_agent->receiveCommand(*device, command, value, *source);
}
}

Expand Down Expand Up @@ -1306,8 +1378,7 @@ namespace mtconnect {
{"description", mem_fn(&Device::setDescriptionValue)},
{"nativename",
[](DevicePtr device, const string &name) { device->setProperty("nativeName", name); }},
{"calibration",
[](DevicePtr device, const string &value) {
{"calibration", [](DevicePtr device, const string &value) {
istringstream line(value);

// Look for name|factor|offset triples
Expand All @@ -1328,8 +1399,7 @@ namespace mtconnect {
di->setConverter(conv);
}
}
}},
};
}}};

static std::unordered_map<string, string> adapterDataItems {
{"adapterversion", "_adapter_software_version"},
Expand All @@ -1349,6 +1419,10 @@ namespace mtconnect {
deviceChanged(device, oldUuid, oldName);
}
}
else if (command == "devicemodel")
{
loadDevice(value, source);
}
else
{
auto action = deviceCommands.find(command);
Expand Down
Loading