7 #ifndef __EPICS_METRIC__
8 #define __EPICS_METRIC__ 1
10 #include "artdaq-utilities/Plugins/MetricMacros.hh"
11 #include "messagefacility/MessageLogger/MessageLogger.h"
12 #include <unordered_map>
28 std::unordered_map<std::string, chid> channels_;
30 bool checkChannel_(std::string name)
32 if (!channels_.count(name))
35 auto sts = ca_search(name.c_str(), &channel);
36 sts = ca_pend_io(5.0);
37 if (sts != ECA_NORMAL)
39 SEVCHK(ca_clear_channel(channel), NULL);
40 mf::LogWarning(
"EPICS Plugin") <<
"Channel " << name <<
" not found!";
41 channels_[name] =
nullptr;
44 channels_[name] = channel;
47 return channels_[name] !=
nullptr;
57 , prefix_(pset.get<std::string>(
"channel_name_prefix",
"artdaq"))
62 MetricPlugin::stopMetrics();
69 std::string
getLibName()
const override {
return "epics"; }
76 for (
auto channel : channels_)
78 SEVCHK(ca_clear_channel(channel.second), NULL);
98 void sendMetric_(
const std::string& name,
const std::string& value,
const std::string& unit)
override
100 std::string caName = prefix_ +
":" + name;
101 std::string tmpValue = value +
" " + unit;
103 if (checkChannel_(caName))
106 if (tmpValue.size() > 40) { tmpValue = tmpValue.erase(40); }
107 SEVCHK(ca_put(DBR_STRING, channels_[caName], tmpValue.c_str()), NULL);
108 SEVCHK(ca_flush_io(), NULL);
122 void sendMetric_(
const std::string& name,
const int& value,
const std::string& unit)
override
125 std::string caName = prefix_ +
":" + name;
128 mf::LogDebug(
"EPICS Plugin") <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
131 if (checkChannel_(caName))
133 dbr_long_t val =
static_cast<dbr_long_t
>(value);
134 SEVCHK(ca_put(DBR_LONG, channels_[caName], &val), NULL);
135 SEVCHK(ca_flush_io(), NULL);
149 void sendMetric_(
const std::string& name,
const double& value,
const std::string& unit)
override
152 std::string caName = prefix_ +
":" + name;
155 mf::LogDebug(
"EPICS Plugin") <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
158 if (checkChannel_(caName))
160 dbr_double_t val =
static_cast<dbr_double_t
>(value);
161 SEVCHK(ca_put(DBR_DOUBLE, channels_[caName], &val), NULL);
162 SEVCHK(ca_flush_io(), NULL);
176 void sendMetric_(
const std::string& name,
const float& value,
const std::string& unit)
override
179 std::string caName = prefix_ +
":" + name;
182 mf::LogDebug(
"EPICS Plugin") <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
185 if (checkChannel_(caName))
187 dbr_float_t val =
static_cast<dbr_float_t
>(value);
188 SEVCHK(ca_put(DBR_FLOAT, channels_[caName], &val), NULL);
189 SEVCHK(ca_flush_io(), NULL);
203 void sendMetric_(
const std::string& name,
const unsigned long int& value,
const std::string& unit)
override
206 std::string caName = prefix_ +
":" + name;
209 mf::LogDebug(
"EPICS Plugin") <<
"Not sure if I can send ChannelAccess Units...configure in db instead.";
212 if (checkChannel_(caName))
214 dbr_ulong_t val =
static_cast<dbr_ulong_t
>(value);
215 SEVCHK(ca_put(DBR_LONG, channels_[caName], &val), NULL);
216 SEVCHK(ca_flush_io(), NULL);
224 #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.
EpicsMetric(fhicl::ParameterSet pset)
Construct an instance of the EpicsMetric plugin.
void sendMetric_(const std::string &name, const std::string &value, const std::string &unit) override
Send a string metric data point to ChannelAccess.
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.