Skip to content

Commit

Permalink
Merge pull request #407 from mtconnect/update_schema_version_in_style…
Browse files Browse the repository at this point in the history
…sheets

Update schema version in stylesheets
  • Loading branch information
wsobel authored Jan 27, 2024
2 parents d34eb02 + 928faca commit ebec64b
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 26 deletions.
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
10 changes: 8 additions & 2 deletions src/mtconnect/sink/rest_sink/cached_file.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,14 @@ namespace mtconnect::sink::rest_sink {
if (cached)
{
allocate(m_size);
auto file = std::fopen(path.string().c_str(), "rb");
m_size = std::fread(m_buffer, 1, m_size, file);
std::filebuf file;
if (file.open(m_path, std::ios::binary | std::ios::in) != nullptr)
m_size = file.sgetn(m_buffer, m_size);
else
{
LOG(warning) << "Cannot open cached file: " << path;
m_cached = false;
}
}
m_lastWrite = std::filesystem::last_write_time(m_path);
}
Expand Down
52 changes: 49 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,53 @@ 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
{
unique_ptr<char[]> buffer(new char[fc->m_size]);
std::filebuf file;
if (file.open(fc->m_path, std::ios::binary | std::ios::in) == nullptr)
throw std::runtime_error("Cannot open file for reading");

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

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

std::ofstream out(fc->m_path, std::ios::binary | std::ios_base::out);
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
Loading

0 comments on commit ebec64b

Please sign in to comment.