Skip to content

Commit 808820a

Browse files
authored
Merge pull request #94 from fledge-iot/2.6.0RC
2.6.0RC
2 parents 3d5b61c + 139f0cc commit 808820a

File tree

4 files changed

+30
-19
lines changed

4 files changed

+30
-19
lines changed

C/services/notification/include/notification_manager.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include <asset_tracking.h>
2020

2121
// Notification type repeat time
22-
#define DEFAULT_RETRIGGER_TIME 60
22+
#define DEFAULT_RETRIGGER_TIME 60.0
2323

2424
/**
2525
* The EvaluationType class represents
@@ -194,7 +194,7 @@ class NotificationInstance
194194
struct NotificationType
195195
{
196196
eNotificationType type;
197-
long retriggerTime;
197+
struct timeval retriggerTimeTv;
198198
};
199199
enum NotificationState {StateTriggered, StateCleared };
200200
NotificationInstance(const std::string& name,
@@ -260,7 +260,7 @@ class NotificationInstance
260260
std::vector<std::pair<std::string, NotificationDelivery *>>
261261
m_deliveryExtra;
262262

263-
time_t m_lastSent;
263+
struct timeval m_lastSentTv;
264264
NotificationState m_state;
265265
bool m_zombie;
266266
};

C/services/notification/notification_manager.cpp

Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,8 @@ NotificationInstance::NotificationInstance(const string& name,
193193
m_zombie(false)
194194
{
195195
// Set initial state for notification delivery
196-
m_lastSent = 0;
196+
m_lastSentTv.tv_sec = 0;
197+
m_lastSentTv.tv_usec = 0;
197198
m_state = NotificationInstance::StateCleared;
198199
}
199200

@@ -772,8 +773,12 @@ bool NotificationInstance::handleState(bool evalRet)
772773
bool ret = false;
773774
NOTIFICATION_TYPE nType = this->getType();
774775

775-
time_t now = time(NULL);
776-
time_t diffTime = now - m_lastSent;
776+
// Get now with seconds and microseconds
777+
struct timeval now_tv, diffTimeTv;
778+
gettimeofday(&now_tv, NULL);
779+
780+
// Calculate time diff
781+
timersub(&now_tv, &m_lastSentTv, &diffTimeTv);
777782

778783
switch(nType.type)
779784
{
@@ -789,7 +794,7 @@ bool NotificationInstance::handleState(bool evalRet)
789794
else
790795
{
791796
// Try sending "triggered" when evaluation is true
792-
ret = evalRet && (diffTime >= nType.retriggerTime);
797+
ret = evalRet && timercmp(&diffTimeTv, &nType.retriggerTimeTv, >=);
793798
// Here state change depends on sending value
794799
setTriggered = ret;
795800
}
@@ -799,7 +804,7 @@ bool NotificationInstance::handleState(bool evalRet)
799804
// Set state depends on evalRet
800805
setTriggered = evalRet;
801806
// Try sending "triggered" when evaluation is true
802-
ret = evalRet && diffTime >= nType.retriggerTime;
807+
ret = evalRet && timercmp(&diffTimeTv, &nType.retriggerTimeTv, >=);
803808
break;
804809

805810
default:
@@ -820,10 +825,10 @@ bool NotificationInstance::handleState(bool evalRet)
820825
if (ret)
821826
{
822827
// Update last sent time
823-
m_lastSent = now;
828+
m_lastSentTv = now_tv;
824829
char dateStr[80];
825830
struct tm tm;
826-
time_t tim = now + nType.retriggerTime;
831+
time_t tim = now_tv.tv_sec + nType.retriggerTimeTv.tv_usec;
827832
asctime_r(localtime_r(&tim, &tm), dateStr);
828833
Logger::getLogger()->info("Notification %s will not be sent again until after %s", m_name.c_str(), dateStr);
829834
}
@@ -994,7 +999,7 @@ bool NotificationManager::APIcreateEmptyInstance(const string& name)
994999
"\"type\": \"boolean\", \"default\": \"false\"}, "
9951000
"\"retrigger_time\": {\"description\" : \"Retrigger time in seconds for sending a new notification.\", "
9961001
"\"displayName\" : \"Retrigger Time\", \"order\" : \"6\", "
997-
"\"type\": \"integer\", \"default\": \"" + to_string(DEFAULT_RETRIGGER_TIME) + "\"} }";
1002+
"\"type\": \"float\", \"default\": \"" + to_string(DEFAULT_RETRIGGER_TIME) + "\", \"minimum\" : \"0.0\"} }";
9981003

9991004
DefaultConfigCategory notificationConfig(name, payload);
10001005
notificationConfig.setDescription("Notification " + name);
@@ -1003,7 +1008,7 @@ bool NotificationManager::APIcreateEmptyInstance(const string& name)
10031008
if (m_managerClient->addCategory(notificationConfig, false))
10041009
{
10051010
NOTIFICATION_TYPE type;
1006-
type.retriggerTime = DEFAULT_RETRIGGER_TIME;
1011+
type.retriggerTimeTv.tv_usec = DEFAULT_RETRIGGER_TIME;
10071012
type.type = E_NOTIFICATION_TYPE::OneShot;
10081013
// Create the empty Notification instance
10091014
this->addInstance(name,
@@ -1906,6 +1911,8 @@ bool NotificationManager::getConfigurationItems(const ConfigCategory& config,
19061911
string& customText)
19071912
{
19081913
long retriggerTime = DEFAULT_RETRIGGER_TIME;
1914+
struct timeval retriggerTimeTv;
1915+
retriggerTimeTv.tv_sec = DEFAULT_RETRIGGER_TIME;
19091916
string notificationName = config.getName();
19101917
// The rule plugin to use
19111918
rulePluginName = config.getValue("rule");
@@ -1919,13 +1926,17 @@ bool NotificationManager::getConfigurationItems(const ConfigCategory& config,
19191926
if (config.itemExists("retrigger_time") &&
19201927
!config.getValue("retrigger_time").empty())
19211928
{
1922-
long new_value = atol(config.getValue("retrigger_time").c_str());
1923-
if (new_value)
1929+
double new_value = atof(config.getValue("retrigger_time").c_str());
1930+
if (new_value >= 0)
19241931
{
1925-
retriggerTime = new_value;
1932+
retriggerTimeTv.tv_sec = (int)new_value;
1933+
double intPart;
1934+
double fractPart;
1935+
fractPart = modf(new_value, &intPart);
1936+
retriggerTimeTv.tv_usec = 1000000 * fractPart;
19261937
}
19271938
}
1928-
nType.retriggerTime = retriggerTime;
1939+
nType.retriggerTimeTv = retriggerTimeTv;
19291940

19301941
// Get notification type
19311942
string notification_type;

VERSION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
fledge_version>=2.5
2-
notification_version=2.5.0
1+
fledge_version>=2.6
2+
notification_version=2.6.0

tests/unit/C/services/notification/check_notification_state.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ EXPECT_EXIT({
1515
NotificationInstance* instance = NULL;
1616
NOTIFICATION_TYPE nType;
1717
nType.type = E_NOTIFICATION_TYPE::Toggled;
18-
nType.retriggerTime = DEFAULT_RETRIGGER_TIME;
18+
nType.retriggerTimeTv.tv_sec = DEFAULT_RETRIGGER_TIME;
1919

2020
// NotificationType is TOGGLED
2121
instance = new NotificationInstance("Toggled",

0 commit comments

Comments
 (0)