7 #ifndef __EPICS_METRIC__
8 #define __EPICS_METRIC__ 1
10 #include <unordered_map>
11 #include "artdaq-utilities/Plugins/MetricMacros.hh"
12 #include "messagefacility/MessageLogger/MessageLogger.h"
16 #pragma clang diagnostic push
17 #pragma clang diagnostic ignored "-Wunused-parameter"
21 #pragma clang diagnostic pop
35 std::unordered_map<std::string, chid> channels_;
37 bool checkChannel_(std::string name)
39 if (!channels_.count(name))
42 ca_search(name.c_str(), &channel);
43 auto sts = ca_pend_io(5.0);
44 if (sts != ECA_NORMAL)
46 SEVCHK(ca_clear_channel(channel), NULL);
47 mf::LogWarning(
"EPICS Plugin") <<
"Channel " << name <<
" not found!";
48 channels_[name] =
nullptr;
51 channels_[name] = channel;
54 return channels_[name] !=
nullptr;
63 explicit EpicsMetric(fhicl::ParameterSet
const& pset, std::string
const& app_name)
64 : MetricPlugin(pset, app_name), prefix_(pset.get<std::string>(
"channel_name_prefix",
"artdaq")), channels_() {}
66 virtual ~
EpicsMetric() { MetricPlugin::stopMetrics(); }
72 std::string
getLibName()
const override {
return "epics"; }
79 for (
auto channel : channels_)
81 SEVCHK(ca_clear_channel(channel.second), NULL);
101 void sendMetric_(
const std::string& name,
const std::string& value,
const std::string& unit)
override
103 std::string caName = prefix_ +
":" + name;
104 std::string tmpValue = value +
" " + unit;
106 if (checkChannel_(caName))
109 if (tmpValue.size() > 40)
111 tmpValue = tmpValue.erase(40);
113 SEVCHK(ca_put(DBR_STRING, channels_[caName], tmpValue.c_str()), NULL);
114 SEVCHK(ca_flush_io(), NULL);
128 void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit)
override
131 std::string caName = prefix_ +
":" + name;
134 mf::LogDebug(
"EPICS Plugin") <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
137 if (checkChannel_(caName))
139 dbr_long_t val =
static_cast<dbr_long_t
>(value);
140 SEVCHK(ca_put(DBR_LONG, channels_[caName], &val), NULL);
141 SEVCHK(ca_flush_io(), NULL);
155 void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit)
override
158 std::string caName = prefix_ +
":" + name;
161 mf::LogDebug(
"EPICS Plugin") <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
164 if (checkChannel_(caName))
166 dbr_double_t val =
static_cast<dbr_double_t
>(value);
167 SEVCHK(ca_put(DBR_DOUBLE, channels_[caName], &val), NULL);
168 SEVCHK(ca_flush_io(), NULL);
182 void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit)
override
185 std::string caName = prefix_ +
":" + name;
188 mf::LogDebug(
"EPICS Plugin") <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
191 if (checkChannel_(caName))
193 dbr_float_t val =
static_cast<dbr_float_t
>(value);
194 SEVCHK(ca_put(DBR_FLOAT, channels_[caName], &val), NULL);
195 SEVCHK(ca_flush_io(), NULL);
209 void sendMetric_(
const std::string& name,
const unsigned long int& value,
const std::string& unit)
override
212 std::string caName = prefix_ +
":" + name;
215 mf::LogDebug(
"EPICS Plugin") <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
218 if (checkChannel_(caName))
220 dbr_ulong_t val =
static_cast<dbr_ulong_t
>(value);
221 SEVCHK(ca_put(DBR_LONG, channels_[caName], &val), NULL);
222 SEVCHK(ca_flush_io(), NULL);
230 #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.
void sendMetric_(const std::string &name, const unsigned long int &value, const std::string &unit) override
Send an unsigned integer 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 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.