Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update schema version in stylesheets #407

Merged
merged 11 commits into from
Jan 27, 2024
2 changes: 1 addition & 1 deletion demo/agent/agent.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Devices = Devices.xml
SchemaVersion = 2.0
SchemaVersion = 2.2
WorkerThreads = 3
MonitorConfigFiles = yes
Port = 5001
Expand Down
2 changes: 1 addition & 1 deletion src/mtconnect/configuration/agent_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -964,7 +964,7 @@ namespace mtconnect::configuration {
{
adapterOptions[configuration::Device] = *device->getUuid();
}

if (!device)
{
LOG(warning) << "Cannot locate device name '" << deviceName << "', assuming dynamic";
Expand Down
53 changes: 50 additions & 3 deletions src/mtconnect/sink/rest_sink/rest_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

#include "rest_service.hpp"

#include <regex>

#include "mtconnect/configuration/config_options.hpp"
#include "mtconnect/entity/xml_parser.hpp"
#include "mtconnect/pipeline/shdr_token_mapper.hpp"
Expand Down Expand Up @@ -270,6 +272,7 @@ namespace mtconnect {
void RestService::loadStyle(const ptree &tree, const char *styleName, XmlPrinter *xmlPrinter,
StyleFunction styleFunction)
{
namespace fs = std::filesystem;
auto style = tree.get_child_optional(styleName);
if (style)
{
Expand All @@ -281,10 +284,54 @@ namespace mtconnect {
else
{
(xmlPrinter->*styleFunction)(*location);
auto path = style->get_optional<string>("Path");
if (path)
auto configPath = style->get_optional<string>("Path");
if (configPath)
{
m_fileCache.registerFile(*location, *configPath, m_schemaVersion);
}

if (auto fc = m_fileCache.getFile(*location))
{
try
{
using unique_file = std::unique_ptr<std::FILE, decltype(&std::fclose)>;
unique_ptr<char[]> buffer(new char[fc->m_size]);
unique_file file(std::fopen(fc->m_path.c_str(), "rb"), &std::fclose);
if (!file)
throw std::runtime_error("Cannot open file for reading");

auto len = std::fread(buffer.get(), 1, fc->m_size, file.get());
file.reset();
if (len <= 0)
throw std::runtime_error("Cannot read from file");

string_view sv(buffer.get(), len);

std::ofstream out(fc->m_path);
if (!out.is_open())
throw std::runtime_error("Cannot open file for writing");

std::ostream_iterator<char, char> oi(out);

std::regex reg(
"(xmlns:[A-Za-z]+=\"urn:mtconnect.org:MTConnect[^:]+:)"
"[[:digit:]]+\\.[[:digit:]]+(\")");
std::regex_replace(
oi, sv.begin(), sv.end(), reg, "$01" + m_schemaVersion + "$2",
std::regex_constants::match_default | std::regex_constants::match_any);
}
catch (std::runtime_error ec)
{
LOG(error) << "Cannot update sylesheet: " << ec.what() << " (" << fc->m_path << ')';
}
catch (...)
{
LOG(error) << "Cannot update sylesheet: (" << fc->m_path << ')';
}
}
else
{
m_fileCache.registerFile(*location, *path, m_schemaVersion);
LOG(warning) << "Cannot find path for style file: " << *location;
}
}
}
Expand Down
5 changes: 3 additions & 2 deletions test_package/agent_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3013,8 +3013,9 @@ TEST_F(AgentTest, should_not_add_spaces_to_output)
ASSERT_XML_PATH_EQUAL(doc, "//m:DeviceStream//m:Program", "");
ASSERT_XML_PATH_EQUAL(doc, "//m:DeviceStream//m:Block", "");
}

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

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

{
PARSE_XML_RESPONSE("/current");
Expand Down
4 changes: 2 additions & 2 deletions test_package/config_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2257,7 +2257,7 @@ AgentDeviceUUID = SOME_UUID
const auto &ad = m_config->getAgent()->getAgentDevice();
ASSERT_EQ("SOME_UUID", *(ad->getUuid()));
}

TEST_F(ConfigTest, should_set_device_uuid_when_specified_in_adapter_config)
{
string config(R"DOC(
Expand All @@ -2277,7 +2277,7 @@ Adapters {
ASSERT_TRUE(dev);
ASSERT_EQ("NEW-UUID", *(dev->getUuid()));
}

TEST_F(ConfigTest, should_set_default_device_uuid_when_specified_in_adapter_config)
{
string config(R"DOC(
Expand Down
Loading