Skip to content

Commit

Permalink
Merge pull request #422 from mtconnect/mqtt_ws_client_for_adapter
Browse files Browse the repository at this point in the history
  • Loading branch information
wsobel authored Mar 10, 2024
2 parents 422e9e1 + eb154e6 commit 4bf3d6d
Show file tree
Hide file tree
Showing 21 changed files with 2,058 additions and 1,999 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
set(AGENT_VERSION_MAJOR 2)
set(AGENT_VERSION_MINOR 3)
set(AGENT_VERSION_PATCH 0)
set(AGENT_VERSION_BUILD 2)
set(AGENT_VERSION_BUILD 3)
set(AGENT_VERSION_RC "")

# This minimum version is to support Visual Studio 2019 and C++ feature checking and FetchContent
Expand Down
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ The following parameters must be present to enable https requests. If there is n

* `TlsOnly` - Only allow secure connections, http requests will be rejected

*Default*: false
*Default*: `false`

* `TlsPrivateKey` - The name of the file containing the private key for the certificate

Expand All @@ -777,7 +777,11 @@ The following parameters must be present to enable https requests. If there is n

* `MqttTls` - TLS Certificate for secure connection to the MQTT Broker

*Default*: *NULL*
*Default*: `false`

* `MqttWs` - Instructs MQTT to connect using web sockets

*Default*: `false`

#### MQTT Sink

Expand Down
8 changes: 4 additions & 4 deletions src/mtconnect/agent.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ namespace mtconnect {
for (auto &[k, pr] : m_printers)
pr->setSchemaVersion(*m_schemaVersion);
}

auto sender = GetOption<string>(options, config::Sender);
if (sender)
{
Expand Down Expand Up @@ -1301,15 +1301,15 @@ namespace mtconnect {
// Validation methods
// -----------------------------------------------

string Agent::devicesAndPath(const std::optional<string> &path, const DevicePtr device, const std::optional<std::string> &deviceType) const
string Agent::devicesAndPath(const std::optional<string> &path, const DevicePtr device,
const std::optional<std::string> &deviceType) const
{
string dataPath;

if (device || deviceType)
{
string prefix;
if ((device && device->getName() == "Agent") ||
(deviceType && *deviceType == "Agent"))
if ((device && device->getName() == "Agent") || (deviceType && *deviceType == "Agent"))
prefix = "//Devices/Agent";
else if (device)
prefix = "//Devices/Device[@uuid=\"" + *device->getUuid() + "\"]";
Expand Down
5 changes: 2 additions & 3 deletions src/mtconnect/agent.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,8 +427,7 @@ namespace mtconnect {
/// @param[in] device Optional device if one device is specified
/// @param[in] deviceType optional Agent or Device selector
/// @return The rewritten path properly prefixed
std::string devicesAndPath(const std::optional<std::string> &path,
const DevicePtr device,
std::string devicesAndPath(const std::optional<std::string> &path, const DevicePtr device,
const std::optional<std::string> &deviceType = std::nullopt) const;

/// @brief Creates unique ids for the device model and maps to the originals
Expand Down Expand Up @@ -644,7 +643,7 @@ namespace mtconnect {

void getDataItemsForPath(const DevicePtr device, const std::optional<std::string> &path,
FilterSet &filter,
const std::optional<std::string> &deviceType) const override
const std::optional<std::string> &deviceType) const override
{
std::string dataPath = m_agent->devicesAndPath(path, device, deviceType);
const auto &parser = m_agent->getXmlParser();
Expand Down
208 changes: 104 additions & 104 deletions src/mtconnect/config.hpp
Original file line number Diff line number Diff line change
@@ -1,104 +1,104 @@
#pragma once

//
// Copyright Copyright 2009-2022, AMT � The Association For Manufacturing Technology (�AMT�)
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

/// @file config.hpp
/// @brief common includes and cross platform requirements

// TODO: Remove when BOOST fixes its multiple defined symbol issue with phoenix placeholders
#define BOOST_PHOENIX_STL_TUPLE_H_
#define BOOST_BIND_NO_PLACEHOLDERS

#include <boost/config.hpp>

#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS 1
#endif
#if _MSC_VER > 1500
#include <cstdint>
#else
#endif
#ifndef UINT64_MAX
#define UINT64_MAX 0xFFFFFFFFFFFFFFFFull
#endif
#ifndef NOMINMAX
#define NOMINMAX 1
#endif

#include <chrono>
#include <ctime>
#include <date/date.h>
#include <fstream>
#include <iomanip>
#include <limits>
#include <list>
#include <map>
#include <optional>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <variant>

#if defined(_WIN32) || defined(_WIN64)
#ifndef _WINDOWS
#define _WINDOWS 1
#endif
#define ISNAN(x) _isnan(x)
#if _MSC_VER < 1800
#define NAN numeric_limits<double>::quiet_NaN()
#endif
#if _MSC_VER >= 1900
#define gets gets_s
#define timezone _timezone
#endif
typedef unsigned __int64 uint64_t;
#else
#define O_BINARY 0
#define ISNAN(x) std::isnan(x)
#include <cstdint>
#include <memory>
#include <sys/resource.h>
#include <unistd.h>
#endif

#ifdef _WINDOWS
#define AGENT_SYMBOL_EXPORT __declspec(dllexport)
#define AGENT_SYMBOL_IMPORT __declspec(dllimport)
#else // _WINDOWS
#define AGENT_SYMBOL_EXPORT __attribute__((visibility("default")))
#define AGENT_SYMBOL_IMPORT __attribute__((visibility("default")))
#endif // _WINDOWS

#ifdef SHARED_AGENT_LIB

#ifdef AGENT_BUILD_SHARED_LIB
#define AGENT_LIB_API AGENT_SYMBOL_EXPORT
#else
#define AGENT_LIB_API AGENT_SYMBOL_IMPORT
#endif

#define AGENT_SYMBOL_VISIBLE AGENT_LIB_API

#else // SHARED_AGENT_LIB

#define AGENT_LIB_API
#define AGENT_SYMBOL_VISIBLE

#endif
#pragma once

//
// Copyright Copyright 2009-2022, AMT � The Association For Manufacturing Technology (�AMT�)
// All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

/// @file config.hpp
/// @brief common includes and cross platform requirements

// TODO: Remove when BOOST fixes its multiple defined symbol issue with phoenix placeholders
#define BOOST_PHOENIX_STL_TUPLE_H_
#define BOOST_BIND_NO_PLACEHOLDERS

#include <boost/config.hpp>

#ifndef __STDC_LIMIT_MACROS
#define __STDC_LIMIT_MACROS 1
#endif
#if _MSC_VER > 1500
#include <cstdint>
#else
#endif
#ifndef UINT64_MAX
#define UINT64_MAX 0xFFFFFFFFFFFFFFFFull
#endif
#ifndef NOMINMAX
#define NOMINMAX 1
#endif

#include <chrono>
#include <ctime>
#include <date/date.h>
#include <fstream>
#include <iomanip>
#include <limits>
#include <list>
#include <map>
#include <optional>
#include <set>
#include <sstream>
#include <string>
#include <time.h>
#include <unordered_map>
#include <variant>

#if defined(_WIN32) || defined(_WIN64)
#ifndef _WINDOWS
#define _WINDOWS 1
#endif
#define ISNAN(x) _isnan(x)
#if _MSC_VER < 1800
#define NAN numeric_limits<double>::quiet_NaN()
#endif
#if _MSC_VER >= 1900
#define gets gets_s
#define timezone _timezone
#endif
typedef unsigned __int64 uint64_t;
#else
#define O_BINARY 0
#define ISNAN(x) std::isnan(x)
#include <cstdint>
#include <memory>
#include <sys/resource.h>
#include <unistd.h>
#endif

#ifdef _WINDOWS
#define AGENT_SYMBOL_EXPORT __declspec(dllexport)
#define AGENT_SYMBOL_IMPORT __declspec(dllimport)
#else // _WINDOWS
#define AGENT_SYMBOL_EXPORT __attribute__((visibility("default")))
#define AGENT_SYMBOL_IMPORT __attribute__((visibility("default")))
#endif // _WINDOWS

#ifdef SHARED_AGENT_LIB

#ifdef AGENT_BUILD_SHARED_LIB
#define AGENT_LIB_API AGENT_SYMBOL_EXPORT
#else
#define AGENT_LIB_API AGENT_SYMBOL_IMPORT
#endif

#define AGENT_SYMBOL_VISIBLE AGENT_LIB_API

#else // SHARED_AGENT_LIB

#define AGENT_LIB_API
#define AGENT_SYMBOL_VISIBLE

#endif
6 changes: 3 additions & 3 deletions src/mtconnect/configuration/agent_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ namespace mtconnect::configuration {
// Check for schema version
auto port = get<int>(options[configuration::Port]);
LOG(info) << "Starting agent on port " << int(port);

// Get the name of the sender
auto sender = GetOption<string>(options, configuration::Sender);
if (sender)
Expand All @@ -870,7 +870,7 @@ namespace mtconnect::configuration {
if (ec)
options[configuration::Sender] = "localhost";
else
options[configuration::Sender] = name;
options[configuration::Sender] = name;
}

// Make the Agent
Expand All @@ -885,7 +885,7 @@ namespace mtconnect::configuration {

m_agent->initialize(m_pipelineContext);
m_version = *m_agent->getSchemaVersion();

DevicePtr device;
if (IsOptionSet(options, configuration::PreserveUUID))
{
Expand Down
1 change: 1 addition & 0 deletions src/mtconnect/configuration/config_options.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ namespace mtconnect {
DECLARE_CONFIGURATION(MqttTls);
DECLARE_CONFIGURATION(MqttPort);
DECLARE_CONFIGURATION(MqttHost);
DECLARE_CONFIGURATION(MqttWs);
DECLARE_CONFIGURATION(MqttConnectInterval);
DECLARE_CONFIGURATION(MqttUserName);
DECLARE_CONFIGURATION(MqttPassword);
Expand Down
3 changes: 1 addition & 2 deletions src/mtconnect/device_model/agent_device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ namespace mtconnect {
}
return factory;
}

entity::FactoryPtr AgentDevice::getRoot()
{
static auto factory = make_shared<Factory>(
Expand All @@ -54,7 +54,6 @@ namespace mtconnect {
return factory;
}


AgentDevice::AgentDevice(const std::string &name, entity::Properties &props)
: Device(name, props)
{
Expand Down
28 changes: 28 additions & 0 deletions src/mtconnect/mqtt/mqtt_client_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ namespace mtconnect {
using mqtt_tls_client_ws =
mqtt_tls_client_ws_ptr<boost::asio::io_context &, std::string, std::uint16_t, std::string,
mqtt::protocol_version>;
using mqtt_client_ws = mqtt_client_ws_ptr<boost::asio::io_context &, std::string, std::uint16_t,
std::string, mqtt::protocol_version>;

/// @brief The Mqtt Client Source
template <typename Derived>
Expand Down Expand Up @@ -504,5 +506,31 @@ namespace mtconnect {
mqtt_tls_client_ws m_client;
};

/// @brief Create an Mqtt TLS WebSocket Client
class MqttWSClient : public MqttClientImpl<MqttWSClient>
{
public:
using base = MqttClientImpl<MqttWSClient>;
using base::base;
/// @brief Get the Mqtt TLS WebSocket Client
/// @return pointer to the Mqtt TLS WebSocket Client
auto &getClient()
{
if (!m_client)
{
m_client = mqtt::make_async_client_ws(m_ioContext, m_host, m_port);
if (m_username)
m_client->set_user_name(*m_username);
if (m_password)
m_client->set_password(*m_password);
}

return m_client;
}

protected:
mqtt_client_ws m_client;
};

} // namespace mqtt_client
} // namespace mtconnect
4 changes: 2 additions & 2 deletions src/mtconnect/printer/json_printer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ namespace mtconnect::printer {
obj.AddPairs("jsonVersion", m_jsonVersion, "schemaVersion", *m_schemaVersion);
{
AutoJsonObject obj(writer, "Header");
streamHeader(obj, m_version, m_senderName, instanceId, bufferSize, nextSeq, firstSeq, lastSeq,
*m_schemaVersion, m_modelChangeTime);
streamHeader(obj, m_version, m_senderName, instanceId, bufferSize, nextSeq, firstSeq,
lastSeq, *m_schemaVersion, m_modelChangeTime);
}

{
Expand Down
Loading

0 comments on commit 4bf3d6d

Please sign in to comment.