7 #ifndef __EPICS_METRIC__
8 #define __EPICS_METRIC__ 1
10 #include <unordered_map>
12 #include "artdaq-utilities/Plugins/MetricMacros.hh"
13 #include "messagefacility/MessageLogger/MessageLogger.h"
17 #pragma clang diagnostic push
18 #pragma clang diagnostic ignored "-Wunused-parameter"
20 #pragma GCC diagnostic push
21 #pragma GCC diagnostic ignored "-Wdeprecated-copy"
25 #pragma clang diagnostic pop
27 #pragma GCC diagnostic pop
30 #include "TRACE/tracemf.h"
31 #define TRACE_NAME "EPICSMetric"
44 std::unordered_map<std::string, chid> channels_;
46 bool checkChannel_(
const std::string& name)
48 if (channels_.count(name) == 0u)
51 ca_search(name.c_str(), &channel);
52 auto sts = ca_pend_io(5.0);
53 if (sts != ECA_NORMAL)
55 SEVCHK(ca_clear_channel(channel), NULL);
56 TLOG(TLVL_WARNING) <<
"Channel \"" << name <<
"\" not found!";
57 channels_[name] =
nullptr;
60 channels_[name] = channel;
63 return channels_[name] !=
nullptr;
66 std::string parseChannelName_(std::string prefix_, std::string name)
68 std::string caName = std::move(name);
69 const std::string& caPrefix_ = std::move(prefix_);
71 while (caName.find(
' ') != std::string::npos)
73 caName = caName.replace(caName.find(
' '), 1,
"_");
75 while (caName.find(
'.') != std::string::npos)
77 caName = caName.replace(caName.find(
'.'), 1,
":");
79 while (caName.find(
'/') != std::string::npos)
81 caName = caName.replace(caName.find(
'/'), 1,
"_");
83 while (caName.find(
"_%") != std::string::npos)
85 caName = caName.replace(caName.find(
"_%"), 2,
"");
87 caName = caPrefix_ +
":" + caName;
89 TLOG(TLVL_DEBUG) <<
"Channel name is: \"" << caName <<
"\"";
104 explicit EpicsMetric(fhicl::ParameterSet
const& pset, std::string
const& app_name)
105 : MetricPlugin(pset, app_name), prefix_(pset.get<std::string>(
"channel_name_prefix",
"artdaq")) {}
107 ~
EpicsMetric()
override { MetricPlugin::stopMetrics(); }
120 for (
const auto& channel : channels_)
122 if (channel.second != 0)
124 SEVCHK(ca_clear_channel(channel.second), NULL);
145 void sendMetric_(
const std::string& name,
const std::string& value,
const std::string& unit)
override
148 std::string caName = parseChannelName_(prefix_, name);
150 std::string tmpValue = value +
" " + unit;
152 if (checkChannel_(caName))
155 if (tmpValue.size() > 40)
157 tmpValue = tmpValue.erase(40);
159 SEVCHK(ca_put(DBR_STRING, channels_[caName], tmpValue.c_str()), NULL);
160 SEVCHK(ca_flush_io(), NULL);
174 void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit)
override
178 std::string caName = parseChannelName_(prefix_, name);
182 TLOG(TLVL_DEBUG) <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
185 if (checkChannel_(caName))
187 auto val =
static_cast<dbr_long_t
>(value);
188 SEVCHK(ca_put(DBR_LONG, channels_[caName], &val), NULL);
189 SEVCHK(ca_flush_io(), NULL);
203 void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit)
override
207 std::string caName = parseChannelName_(prefix_, name);
211 TLOG(TLVL_DEBUG) <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
214 if (checkChannel_(caName))
216 auto val =
static_cast<dbr_double_t
>(value);
217 SEVCHK(ca_put(DBR_DOUBLE, channels_[caName], &val), NULL);
218 SEVCHK(ca_flush_io(), NULL);
232 void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit)
override
236 std::string caName = parseChannelName_(prefix_, name);
240 TLOG(TLVL_DEBUG) <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
243 if (checkChannel_(caName))
245 auto val =
static_cast<dbr_float_t
>(value);
246 SEVCHK(ca_put(DBR_FLOAT, channels_[caName], &val), NULL);
247 SEVCHK(ca_flush_io(), NULL);
261 void sendMetric_(
const std::string& name,
const uint64_t& value,
const std::string& unit)
override
265 std::string caName = parseChannelName_(prefix_, name);
269 TLOG(TLVL_DEBUG) <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
272 if (checkChannel_(caName))
274 auto val =
static_cast<dbr_ulong_t
>(value);
275 SEVCHK(ca_put(DBR_LONG, channels_[caName], &val), NULL);
276 SEVCHK(ca_flush_io(), NULL);
284 #endif // End ifndef __EPICS_METRIC__
void sendMetric_(const std::string &name, const float &value, const std::string &unit) override
Send a float metric data point to ChannelAccess.
An instance of the MetricPlugin class that sends metric data using the Channel Access protocol from E...
void stopMetrics_() override
Clears the registered ChannelAccess channels.
void startMetrics_() override
No initialization is needed to start sending metrics.
std::string getLibName() const override
Gets the unique library name of this plugin.
void sendMetric_(const std::string &name, const uint64_t &value, const std::string &unit) override
Send an unsigned integer metric data point to ChannelAccess.
void sendMetric_(const std::string &name, const std::string &value, const std::string &unit) override
Send a string metric data point to ChannelAccess.
EpicsMetric(fhicl::ParameterSet const &pset, std::string const &app_name)
Construct an instance of the EpicsMetric plugin.
void sendMetric_(const std::string &name, const double &value, const std::string &unit) override
Send a double metric data point to ChannelAccess.
void sendMetric_(const std::string &name, const int &value, const std::string &unit) override
Send an integer metric data point to ChannelAccess.