2 #include "Poco/SingletonHolder.h"
3 #include "Poco/Data/Common.h"
4 #include "Poco/Data/SessionPool.h"
5 #include "Poco/Data/MySQl/Connector.h"
6 #include "Poco/Data/RecordSet.h"
15 EpicsSessionPool() : Poco::Data::SessionPool(
"MySQL",
"host=localhost;port=3306;db=archive;user=report;password=$report;compress=true;auto-reconnect=true", 3, 32, 0)
17 Poco::Data::MySQL::Connector::registerConnector();
44 using namespace Poco::Data;
45 std::vector<std::string> channel_name, smpl_time;
46 std::vector<float> float_val;
47 std::vector<unsigned> nanosecs;
50 bool in_retry =
false;
58 while( epics_session.isConnected() )
66 Statement epics_stmt1(epics_session), epics_stmt2(epics_session), epics_stmt3(epics_session);
68 epics_stmt1 <<
"SELECT grp_id FROM chan_grp INNER JOIN smpl_eng ON chan_grp.eng_id=smpl_eng.eng_id AND smpl_eng.name='block_engine' AND chan_grp.name='BLOCKS'", into(grp_id);
69 epics_stmt1.execute();
72 epics_stmt2 <<
"SELECT channel.name, DATE_FORMAT(smpl_time, '%Y-%m-%dT%H:%i:%s'), nanosecs, float_val FROM channel INNER JOIN sample ON (channel.channel_id=sample.channel_id) " <<
73 " AND (float_val IS NOT NULL) AND (grp_id=" << grp_id <<
") AND (smpl_time = '" << icp_data->lastread_iso <<
"') AND (nanosecs > " << icp_data->lastread_nano <<
") ORDER BY smpl_time, nanosecs", into(channel_name), into(smpl_time), into(nanosecs), into(float_val), limit(500);
74 while( !epics_stmt2.done() )
76 nrows = epics_stmt2.execute();
77 Poco::Thread::sleep(1000);
79 epics_stmt3 <<
"SELECT channel.name, DATE_FORMAT(smpl_time, '%Y-%m-%dT%H:%i:%s'), nanosecs, float_val FROM channel INNER JOIN sample ON (channel.channel_id=sample.channel_id) " <<
80 " AND (float_val IS NOT NULL) AND (grp_id=" << grp_id <<
") AND (smpl_time > '" << icp_data->lastread_iso <<
"') ORDER BY smpl_time, nanosecs", into(channel_name), into(smpl_time), into(nanosecs), into(float_val), limit(500);
81 while( !epics_stmt3.done() )
83 nrows = epics_stmt3.execute();
84 Poco::Thread::sleep(1000);
86 if (smpl_time.size() > 0)
88 strcpy(icp_data->lastread_iso, smpl_time.back().c_str());
89 icp_data->lastread_nano = nanosecs.back();
91 for(
int i=0; i<channel_name.size(); ++i)
93 int n = channel_name[i].find_last_of(
':');
94 if (n != std::string::npos)
96 channel_name[i].erase(0, n+1);
100 std::string log_file = std::string(
"c:\\data\\") + icp_data->inst_name +
padWithZeros(*(icp_data->run_number), 8) +
".log";
102 f.open(log_file, std::ios::app);
105 for(
int i=0; i<smpl_time.size(); ++i)
107 f << smpl_time[i] <<
"\t" << channel_name[i] <<
"\t" << float_val[i] << std::endl;
113 catch(
const std::exception& ex)
115 std::cerr <<
"error executing statement " << ex.what() << std::endl;
117 Poco::Thread::sleep(5000);
119 std::cerr <<
"EPICS MySQL session has disconnected " << std::endl;
121 catch(
const std::exception& ex)
126 std::cerr <<
"Error creating EPICS MySQL session: " << ex.what() <<
", will attempt to retry every 30 seconds" << std::endl;
129 Poco::Thread::sleep(30000);
134 select eng_id from smpl_engine where name=
'inst_engine'
135 select grp_id from chan_grp where eng_id= and name=
137 select channel_id,name,descr from channel where grp_id=
140 select smpl_time,num_val,float_val_str_val,str_val ,arrav_val status_id= severity_id= datatype where channel_id=
142 select unit from num_metadata where channel_id=
144 slect enum_val from enum_metadata where channel_id= and enum_nbr=
146 # Dump all values for all channels
147 SELECT channel.name, smpl_time, severity.name, status.name, float_val
148 FROM channel, severity, status, sample
149 WHERE channel.channel_id = sample.channel_id AND
150 severity.severity_id = sample.severity_id AND
151 status.status_id = sample.status_id
152 ORDER BY channel.name, smpl_time
155 # Same with INNER JOIN
156 SELECT channel.name AS channel,
158 severity.name AS severity,
159 status.name AS status,
161 FROM sample INNER JOIN channel INNER JOIN severity INNER JOIN status
162 ON channel.channel_id = sample.channel_id AND
163 severity.severity_id = sample.severity_id AND
164 status.status_id = sample.status_id
std::string padWithZeros(int number, int len)
SELOGGER_API int __stdcall se_log_fvalues(long run_number, const char *source, const std::vector< std::string > &iso_times, const std::vector< std::string > &block_names, const std::vector< float > &block_values)
static Poco::SingletonHolder< EpicsSessionPool > epics_session_pool
void customizeSession(Poco::Data::Session &session)
unsigned __stdcall run_epics_db(void *arg)