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"
22 #pragma clang diagnostic pop
25 #include "TRACE/tracemf.h"
26 #define TRACE_NAME "EPICSMetric"
39 std::unordered_map<std::string, chid> channels_;
41 bool checkChannel_(
const std::string& name)
43 if (channels_.count(name) == 0u)
46 ca_search(name.c_str(), &channel);
47 auto sts = ca_pend_io(5.0);
48 if (sts != ECA_NORMAL)
50 SEVCHK(ca_clear_channel(channel), NULL);
51 TLOG(TLVL_WARNING) <<
"Channel \"" << name <<
"\" not found!";
52 channels_[name] =
nullptr;
55 channels_[name] = channel;
58 return channels_[name] !=
nullptr;
61 std::string parseChannelName_(std::string prefix_, std::string name)
63 std::string caName = std::move(name);
64 const std::string& caPrefix_ = std::move(prefix_);
66 while (caName.find(
' ') != std::string::npos)
68 caName = caName.replace(caName.find(
' '), 1,
"_");
70 while (caName.find(
'.') != std::string::npos)
72 caName = caName.replace(caName.find(
'.'), 1,
":");
74 while (caName.find(
'/') != std::string::npos)
76 caName = caName.replace(caName.find(
'/'), 1,
"_");
78 while (caName.find(
"_%") != std::string::npos)
80 caName = caName.replace(caName.find(
"_%"), 2,
"");
82 caName = caPrefix_ +
":" + caName;
84 TLOG(TLVL_DEBUG) <<
"Channel name is: \"" << caName <<
"\"";
99 explicit EpicsMetric(fhicl::ParameterSet
const& pset, std::string
const& app_name)
100 : MetricPlugin(pset, app_name), prefix_(pset.get<std::string>(
"channel_name_prefix",
"artdaq")) {}
102 ~
EpicsMetric()
override { MetricPlugin::stopMetrics(); }
115 for (
const auto& channel : channels_)
117 if (channel.second != 0)
119 SEVCHK(ca_clear_channel(channel.second), NULL);
140 void sendMetric_(
const std::string& name,
const std::string& value,
const std::string& unit)
override
143 std::string caName = parseChannelName_(prefix_, name);
145 std::string tmpValue = value +
" " + unit;
147 if (checkChannel_(caName))
150 if (tmpValue.size() > 40)
152 tmpValue = tmpValue.erase(40);
154 SEVCHK(ca_put(DBR_STRING, channels_[caName], tmpValue.c_str()), NULL);
155 SEVCHK(ca_flush_io(), NULL);
169 void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit)
override
173 std::string caName = parseChannelName_(prefix_, name);
177 TLOG(TLVL_DEBUG) <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
180 if (checkChannel_(caName))
182 auto val =
static_cast<dbr_long_t
>(value);
183 SEVCHK(ca_put(DBR_LONG, channels_[caName], &val), NULL);
184 SEVCHK(ca_flush_io(), NULL);
198 void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit)
override
202 std::string caName = parseChannelName_(prefix_, name);
206 TLOG(TLVL_DEBUG) <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
209 if (checkChannel_(caName))
211 auto val =
static_cast<dbr_double_t
>(value);
212 SEVCHK(ca_put(DBR_DOUBLE, channels_[caName], &val), NULL);
213 SEVCHK(ca_flush_io(), NULL);
227 void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit)
override
231 std::string caName = parseChannelName_(prefix_, name);
235 TLOG(TLVL_DEBUG) <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
238 if (checkChannel_(caName))
240 auto val =
static_cast<dbr_float_t
>(value);
241 SEVCHK(ca_put(DBR_FLOAT, channels_[caName], &val), NULL);
242 SEVCHK(ca_flush_io(), NULL);
256 void sendMetric_(
const std::string& name,
const uint64_t& value,
const std::string& unit)
override
260 std::string caName = parseChannelName_(prefix_, name);
264 TLOG(TLVL_DEBUG) <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
267 if (checkChannel_(caName))
269 auto val =
static_cast<dbr_ulong_t
>(value);
270 SEVCHK(ca_put(DBR_LONG, channels_[caName], &val), NULL);
271 SEVCHK(ca_flush_io(), NULL);
279 #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.