Commit 7f787ab6 authored by Pavel Vainerman's avatar Pavel Vainerman

Добавил реализацию получения времени (сек,наносек) через std::chrono

(и ряд вспомогательных функций в UniSetTypes).
parent 6a27d830
......@@ -102,11 +102,11 @@ void DBServer_MySQL::confirmInfo( const UniSetTypes::ConfirmMessage* cem )
ostringstream data;
data << "UPDATE " << tblName(cem->type)
<< " SET confirm='" << cem->confirm << "'"
<< " SET confirm='" << cem->confirm_time.tv_sec << "'"
<< " WHERE sensor_id='" << cem->sensor_id << "'"
<< " AND date='" << dateToString(cem->time, "-") << " '"
<< " AND time='" << timeToString(cem->time, ":") << " '"
<< " AND time_nsec='" << cem->time_nsec << " '";
<< " AND date='" << dateToString(cem->sensor_time.tv_sec, "-") << " '"
<< " AND time='" << timeToString(cem->sensor_time.tv_sec, ":") << " '"
<< " AND time_nsec='" << cem->sensor_time.tv_nsec << " '";
dbinfo << myname << "(update_confirm): " << data.str() << endl;
......
......@@ -113,11 +113,11 @@ void DBServer_PostgreSQL::confirmInfo( const UniSetTypes::ConfirmMessage* cem )
ostringstream data;
data << "UPDATE " << tblName(cem->type)
<< " SET confirm='" << cem->confirm << "'"
<< " SET confirm='" << cem->confirm_time.tv_sec << "'"
<< " WHERE sensor_id='" << cem->sensor_id << "'"
<< " AND date='" << dateToString(cem->time, "-") << " '"
<< " AND time='" << timeToString(cem->time, ":") << " '"
<< " AND time_nsec='" << cem->time_nsec << " '";
<< " AND date='" << dateToString(cem->sensor_time.tv_sec, "-") << " '"
<< " AND time='" << timeToString(cem->sensor_time.tv_sec, ":") << " '"
<< " AND time_nsec='" << cem->sensor_time.tv_nsec << " '";
dbinfo << myname << "(update_confirm): " << data.str() << endl;
......
......@@ -99,11 +99,11 @@ void DBServer_SQLite::confirmInfo( const UniSetTypes::ConfirmMessage* cem )
ostringstream data;
data << "UPDATE " << tblName(cem->type)
<< " SET confirm='" << cem->confirm << "'"
<< " SET confirm='" << cem->confirm_time.tv_sec << "'"
<< " WHERE sensor_id='" << cem->sensor_id << "'"
<< " AND date='" << dateToString(cem->time, "-") << " '"
<< " AND time='" << timeToString(cem->time, ":") << " '"
<< " AND time_nsec='" << cem->time_nsec << " '";
<< " AND date='" << dateToString(cem->sensor_time.tv_sec, "-") << " '"
<< " AND time='" << timeToString(cem->sensor_time.tv_sec, ":") << " '"
<< " AND time_nsec='" << cem->sensor_time.tv_nsec << " '";
dbinfo << myname << "(update_confirm): " << data.str() << endl;
......
......@@ -229,10 +229,9 @@ namespace UniSetTypes
ConfirmMessage( const VoidMessage* msg );
ConfirmMessage(ObjectId in_sensor_id,
double in_value,
time_t in_time,
time_t in_time_nsec,
time_t in_confirm,
double in_sensor_value,
const timespec& in_sensor_time,
const timespec& in_confirm_time,
Priority in_priority = Message::Medium);
ConfirmMessage( ConfirmMessage&& ) = default;
......@@ -240,11 +239,10 @@ namespace UniSetTypes
ConfirmMessage( const ConfirmMessage& ) = default;
ConfirmMessage& operator=( const ConfirmMessage& ) = default;
ObjectId sensor_id; /* ID датчика */
double value; /* значение датчика */
time_t time; /* время, когда датчик получил сигнал */
time_t time_nsec; /* время в наносекундах */
time_t confirm; /* время, когда произошло квитирование */
ObjectId sensor_id; /* ID датчика (события) */
double sensor_value; /* значение датчика (события) */
struct timespec sensor_time; /* время срабатвание датчика(события), который квитируем */
struct timespec confirm_time; /* время, когда произошло квитирование */
bool broadcast;
......@@ -253,7 +251,7 @@ namespace UniSetTypes
(т.е. в БД второй раз сохранять не надо, пересылать
второй раз тоже не надо).
*/
bool route;
bool forward;
protected:
ConfirmMessage();
......
......@@ -174,6 +174,20 @@ namespace UniSetTypes
std::string timeToString(time_t tm = time(0), const std::string& brk = ":"); /*!< Преобразование времени в строку HH:MM:SS */
std::string dateToString(time_t tm = time(0), const std::string& brk = "/"); /*!< Преобразование даты в строку DD/MM/YYYY */
struct timeval to_timeval( const std::chrono::system_clock::duration& d ); /*!< конвертирование std::chrono в posix timeval */
struct timespec to_timespec( const std::chrono::system_clock::duration& d ); /*!< конвертирование std::chrono в posix timespec */
struct timespec now_to_timespec(); /*!< получение текущего времени */
inline bool operator==( const struct timespec& r1, const struct timespec& r2 )
{
return ( r1.tv_sec == r2.tv_sec && r1.tv_nsec == r2.tv_nsec );
}
inline bool operator!=( const struct timespec& r1, const struct timespec& r2 )
{
return !(operator==(r1,r2));
}
/*! Разбивка строки по указанному символу */
IDList explode( const std::string& str, char sep = ',' );
std::vector<std::string> explode_str( const std::string& str, char sep = ',' );
......@@ -297,9 +311,5 @@ namespace UniSetTypes
return destBegin;
}
}
// Варварский запрет на использование atoi вместо uni_atoi..
// #define atoi atoi##_Do_not_use_atoi_function_directly_Use_getIntProp90,_getArgInt_or_uni_atoi
// -----------------------------------------------------------------------------------------
#endif
......@@ -588,3 +588,41 @@ std::string UniSetTypes::replace_all( const std::string& src, const std::string&
return std::move(res);
}
// -------------------------------------------------------------------------
timeval UniSetTypes::to_timeval( const chrono::system_clock::duration& d )
{
struct timeval tv;
if( d.count() == 0 )
tv.tv_sec = tv.tv_usec = 0;
else
{
std::chrono::seconds const sec = std::chrono::duration_cast<std::chrono::seconds>(d);
tv.tv_sec = sec.count();
tv.tv_usec = std::chrono::duration_cast<std::chrono::microseconds>(d - sec).count();
}
return std::move(tv);
}
// -------------------------------------------------------------------------
timespec UniSetTypes::to_timespec( const chrono::system_clock::duration& d )
{
struct timespec ts;
if( d.count() == 0 )
ts.tv_sec = ts.tv_nsec = 0;
else
{
std::chrono::seconds const sec = std::chrono::duration_cast<std::chrono::seconds>(d);
ts.tv_sec = sec.count();
ts.tv_nsec = std::chrono::duration_cast<std::chrono::nanoseconds>(d - sec).count();
}
return std::move(ts);
}
// -------------------------------------------------------------------------
timespec UniSetTypes::now_to_timespec()
{
auto d = std::chrono::system_clock::now().time_since_epoch();
return to_timespec(d);
}
// -------------------------------------------------------------------------
......@@ -304,8 +304,7 @@ void IOController::localSetValue( std::shared_ptr<USensorInfo>& usi,
usi->value = (blocked ? usi->d_off_value : value);
// запоминаем время изменения
struct timespec tm;
::clock_gettime(CLOCK_REALTIME, &tm);
struct timespec tm = UniSetTypes::now_to_timespec();
usi->tv_sec = tm.tv_sec;
usi->tv_nsec = tm.tv_nsec;
}
......@@ -374,8 +373,7 @@ void IOController::ioRegistration( std::shared_ptr<USensorInfo>& usi, bool force
IOStateList::mapped_type ai = usi;
// запоминаем начальное время
struct timespec tm;
::clock_gettime(CLOCK_REALTIME, &tm);
struct timespec tm = UniSetTypes::now_to_timespec();
ai->tv_sec = tm.tv_sec;
ai->tv_nsec = tm.tv_nsec;
ai->value = ai->default_val;
......
......@@ -648,8 +648,7 @@ bool IONotifyController::addThreshold( ThresholdExtList& lst, ThresholdInfoExt&&
addConsumer(ti.clst, ci);
// запоминаем начальное время
struct timespec tm;
::clock_gettime(CLOCK_REALTIME, &tm);
struct timespec tm = UniSetTypes::now_to_timespec();
ti.tv_sec = tm.tv_sec;
ti.tv_nsec = tm.tv_nsec;
......@@ -714,8 +713,7 @@ void IONotifyController::checkThreshold( std::shared_ptr<IOController::USensorIn
SensorMessage sm(std::move(usi->makeSensorMessage()));
// текущее время
struct timespec tm;
::clock_gettime(CLOCK_REALTIME, &tm);
struct timespec tm = UniSetTypes::now_to_timespec();
{
uniset_rwmutex_rlock l(ti->mut);
......
......@@ -57,7 +57,7 @@ namespace UniSetTypes
supplier(DefaultObjectId),
consumer(DefaultObjectId)
{
::clock_gettime(CLOCK_REALTIME, &tm);
tm = UniSetTypes::now_to_timespec();
}
//--------------------------------------------------------------------------------------------
......@@ -195,19 +195,17 @@ namespace UniSetTypes
assert(this->type == Message::Confirm);
}
//--------------------------------------------------------------------------------------------
ConfirmMessage::ConfirmMessage( UniSetTypes::ObjectId in_sensor_id,
double in_value,
time_t in_time,
time_t in_time_nsec,
time_t in_confirm,
ConfirmMessage::ConfirmMessage(UniSetTypes::ObjectId in_sensor_id,
double in_sensor_value,
const timespec& in_sensor_time,
const timespec& in_confirm_time,
Priority in_priority ):
sensor_id(in_sensor_id),
value(in_value),
time(in_time),
time_nsec(in_time_nsec),
confirm(in_confirm),
sensor_value(in_sensor_value),
sensor_time(in_sensor_time),
confirm_time(in_confirm_time),
broadcast(false),
route(false)
forward(false)
{
type = Message::Confirm;
priority = in_priority;
......
......@@ -6,6 +6,7 @@
#include <type_traits>
#include <tuple>
#include "UTCPCore.h"
#include <chrono>
template<typename... Args>
class VMon
......@@ -20,6 +21,35 @@ using namespace std;
int main( int argc, const char** argv )
{
auto now = std::chrono::system_clock::now();
auto sec = std::chrono::time_point_cast<std::chrono::nanoseconds>(now);
auto nsec = std::chrono::time_point_cast<std::chrono::seconds>(now);
cout << "SEC=" << std::chrono::duration<double>(sec.time_since_epoch()).count()
<< endl;
return 0;
std::chrono::time_point<std::chrono::system_clock> p1, p2, p3;
p2 = std::chrono::system_clock::now();
p3 = p2 - std::chrono::hours(24);
std::time_t epoch_time = std::chrono::system_clock::to_time_t(p1);
std::cout << "epoch: " << std::ctime(&epoch_time);
std::time_t today_time = std::chrono::system_clock::to_time_t(p2);
std::cout << "today: " << std::ctime(&today_time);
std::cout << "hours since epoch: "
<< std::chrono::duration_cast<std::chrono::hours>(
p2.time_since_epoch()).count()
<< '\n';
std::cout << "yesterday, hours since epoch: "
<< std::chrono::duration_cast<std::chrono::hours>(
p3.time_since_epoch()).count()
<< '\n';
return 0;
unsigned char dat[] = { '1', '2', '3' , '4' };
// UTCPCore::Buffer* buf = new UTCPCore::Buffer( dat, 0 );
......
......@@ -189,35 +189,32 @@ TEST_CASE("ConfirmMessage", "[basic][message types][ConfirmMessage]" )
ObjectId sid = 1;
double val = 100;
time_t t_sec = 10;
time_t t_nsec = 300;
time_t t_confirm = 10;
timespec t_event = { 10, 300 };
timespec t_confirm = { 10, 90 };
SECTION("Default consturctor")
{
ConfirmMessage cm(sid, val, t_sec, t_nsec, t_confirm);
ConfirmMessage cm(sid, val, t_event, t_confirm);
CHECK( cm.type == Message::Confirm );
CHECK( cm.priority == Message::Medium );
CHECK( cm.node == conf->getLocalNode() );
CHECK( cm.supplier == DefaultObjectId );
CHECK( cm.consumer == DefaultObjectId );
REQUIRE( cm.sensor_id == sid );
REQUIRE( cm.value == val );
REQUIRE( cm.time == t_sec );
REQUIRE( cm.time_nsec == t_nsec );
REQUIRE( cm.confirm == t_confirm );
REQUIRE( cm.sensor_value == val );
REQUIRE( cm.sensor_time == t_event );
REQUIRE( cm.confirm_time == t_confirm );
CHECK( cm.broadcast == false );
CHECK( cm.route == false );
CHECK( cm.forward == false );
}
SECTION("Transport ConfirmMessage")
{
ConfirmMessage cm(sid, val, t_sec, t_nsec, t_confirm);
ConfirmMessage cm(sid, val, t_event, t_confirm);
REQUIRE( cm.sensor_id == sid );
REQUIRE( cm.value == val );
REQUIRE( cm.time == t_sec );
REQUIRE( cm.time_nsec == t_nsec );
REQUIRE( cm.confirm == t_confirm );
REQUIRE( cm.sensor_value == val );
REQUIRE( cm.sensor_time == t_event );
REQUIRE( cm.confirm_time == t_confirm );;
auto tm = cm.transport_msg();
......@@ -226,10 +223,9 @@ TEST_CASE("ConfirmMessage", "[basic][message types][ConfirmMessage]" )
ConfirmMessage cm2(&vm);
REQUIRE( cm2.sensor_id == sid );
REQUIRE( cm2.value == val );
REQUIRE( cm2.time == t_sec );
REQUIRE( cm2.time_nsec == t_nsec );
REQUIRE( cm2.confirm == t_confirm );
REQUIRE( cm2.sensor_value == val );
REQUIRE( cm.sensor_time == t_event );
REQUIRE( cm.confirm_time == t_confirm );
}
}
// ---------------------------------------------------------------
......@@ -174,3 +174,17 @@ TEST_CASE("UniSetTypes: replace_all", "[utypes][replace_all]" )
REQUIRE( res == str2 );
}
// -----------------------------------------------------------------------------
TEST_CASE("UniSetTypes: timespec comapre", "[utypes][timespec]" )
{
timespec t1 = { 10, 300 };
timespec t2 = { 10, 90 };
REQUIRE( t1 != t2 );
REQUIRE_FALSE( t1 == t2 );
timespec t3 = { 20, 20 };
timespec t4 = { 20, 20 };
REQUIRE_FALSE( t3 != t4 );
REQUIRE( t3 == t4 );
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment