Skip to content

Commit

Permalink
Notification Systems,added sensor idx/name as option
Browse files Browse the repository at this point in the history
Email Notification, included sensor idx/name/timestamp
  • Loading branch information
gizmocuz committed Dec 14, 2016
1 parent 03ad813 commit 3151e72
Show file tree
Hide file tree
Showing 31 changed files with 409 additions and 63 deletions.
2 changes: 1 addition & 1 deletion hardware/MQTT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ void MQTT::on_message(const struct mosquitto_message *message)
goto mqttinvaliddata;
sound = root["sound"].asString();
}
m_notifications.SendMessageEx(NOTIFYALL, subject, body, "", priority, sound, true);
m_notifications.SendMessageEx(0, std::string(""), NOTIFYALL, subject, body, std::string(""), priority, sound, true);
std::string varvalue = root["value"].asString();
m_sql.SetUserVariable(idx, varvalue, true);
return;
Expand Down
8 changes: 4 additions & 4 deletions main/SQLHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2715,7 +2715,7 @@ void CSQLHelper::Do_Work()
}
else if (itt->_ItemType == TITEM_SEND_SMS)
{
m_notifications.SendMessage("clickatell", itt->_ID, itt->_ID, "", false);
m_notifications.SendMessage(0, std::string(""), "clickatell", itt->_ID, itt->_ID, std::string(""), 1, std::string(""), false);
}
else if (itt->_ItemType == TITEM_SWITCHCMD_EVENT)
{
Expand Down Expand Up @@ -2764,7 +2764,7 @@ void CSQLHelper::Do_Work()
std::vector<std::string> splitresults;
StringSplit(itt->_command, "!#", splitresults);
if (splitresults.size() == 4) {
m_notifications.SendMessageEx(NOTIFYALL, splitresults[0], splitresults[1], splitresults[2], static_cast<int>(itt->_idx), splitresults[3], true);
m_notifications.SendMessageEx(0, std::string(""), NOTIFYALL, splitresults[0], splitresults[1], splitresults[2], static_cast<int>(itt->_idx), splitresults[3], true);
}
}

Expand Down Expand Up @@ -6394,7 +6394,7 @@ void CSQLHelper::CheckBatteryLow()
sprintf(szTmp, "Battery Low: %s (Level: Low)", sd[1].c_str());
else
sprintf(szTmp, "Battery Low: %s (Level: %d %%)", sd[1].c_str(), batlevel);
m_notifications.SendMessageEx(NOTIFYALL, szTmp, szTmp, std::string(""), 1, std::string(""), true);
m_notifications.SendMessageEx(0, std::string(""), NOTIFYALL, szTmp, szTmp, std::string(""), 1, std::string(""), true);
m_batterylowlastsend[ulID] = stoday.tm_mday;
}
}
Expand Down Expand Up @@ -6469,7 +6469,7 @@ void CSQLHelper::CheckDeviceTimeout()
{
char szTmp[300];
sprintf(szTmp,"Sensor Timeout: %s, Last Received: %s",sd[1].c_str(),sd[2].c_str());
m_notifications.SendMessageEx(NOTIFYALL, szTmp, szTmp, std::string(""), 1, std::string(""), true);
m_notifications.SendMessageEx(0, std::string(""), NOTIFYALL, szTmp, szTmp, std::string(""), 1, std::string(""), true);
m_timeoutlastsend[ulID]=stoday.tm_mday;
}
}
Expand Down
4 changes: 2 additions & 2 deletions main/WebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2528,7 +2528,7 @@ namespace http {
)
return;
//Add to queue
if (m_notifications.SendMessage(NOTIFYALL, subject, body, std::string(""), false)) {
if (m_notifications.SendMessage(0, std::string(""), NOTIFYALL, subject, body, std::string(""), 1, std::string(""), false)) {
root["status"] = "OK";
}
root["title"] = "SendNotification";
Expand Down Expand Up @@ -3747,7 +3747,7 @@ namespace http {
std::string subsystem = request::findValue(&req, "subsystem");

m_notifications.ConfigFromGetvars(req, false);
if (m_notifications.SendMessage(subsystem, notification_Title, notification_Message, std::string(""), false)) {
if (m_notifications.SendMessage(0, std::string(""), subsystem, notification_Title, notification_Message, std::string(""), 1, std::string(""), false)) {
root["status"] = "OK";
}
/* we need to reload the config, because the values that were set were only for testing */
Expand Down
22 changes: 18 additions & 4 deletions notifications/NotificationBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,26 @@ void CNotificationBase::LoadConfig()
}
}

bool CNotificationBase::SendMessage(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const bool bFromNotification)
bool CNotificationBase::SendMessage(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const bool bFromNotification)
{
return SendMessageEx(Subject, Text, std::string(""), 0, std::string(""), bFromNotification);
return SendMessageEx(Idx, Name, Subject, Text, std::string(""), 0, std::string(""), bFromNotification);
}

bool CNotificationBase::SendMessageEx(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const int Priority, const std::string &Sound, const bool bFromNotification)
bool CNotificationBase::SendMessageEx(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const int Priority,
const std::string &Sound,
const bool bFromNotification)
{
if (!IsConfigured()) {
// subsystem not configured, skip
Expand Down Expand Up @@ -103,7 +117,7 @@ bool CNotificationBase::SendMessageEx(const std::string &Subject, const std::str
fText = CURLEncode::URLEncode(fText);
}

bool bRet = SendMessageImplementation(fSubject, fText, ExtraData, Priority, Sound, bFromNotification);
bool bRet = SendMessageImplementation(Idx, Name, fSubject, fText, ExtraData, Priority, Sound, bFromNotification);
if (_subsystemid != "gcm")
{
if (bRet) {
Expand Down
28 changes: 25 additions & 3 deletions notifications/NotificationBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,22 @@ class CNotificationBase {
protected:
CNotificationBase(const std::string &subsystemid, const int options = OPTIONS_NONE);
virtual ~CNotificationBase();
bool SendMessage(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const bool bFromNotification);
bool SendMessageEx(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const int Priority, const std::string &Sound, const bool bFromNotification);
bool SendMessage(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const bool bFromNotification);
bool SendMessageEx(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const int Priority,
const std::string &Sound,
const bool bFromNotification);
void SetConfigValue(const std::string &Key, const std::string &Value);
std::string GetSubsystemId();
bool IsInConfig(const std::string &Key);
Expand All @@ -29,7 +43,15 @@ class CNotificationBase {
void SetupConfig(const std::string &Key, std::string& Value);
void SetupConfig(const std::string &Key, int *Value);
void SetupConfigBase64(const std::string &Key, std::string& Value);
virtual bool SendMessageImplementation(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const int Priority, const std::string &Sound, const bool bFromNotification) = 0;
virtual bool SendMessageImplementation(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const int Priority,
const std::string &Sound,
const bool bFromNotification) = 0;
void LoadConfig();
std::string MakeHtml(const std::string &txt);

Expand Down
96 changes: 93 additions & 3 deletions notifications/NotificationEmail.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,47 @@
#include "../main/Helper.h"
#include "../main/Logger.h"

const static char *szHTMLMail =
"<html>\n"
"<head>\n"
"<style>\n"
"table{\n"
"border: 1px solid #cccccc;\n"
"border-collapse: collapse;\n"
"width: 100%;\n"
"}\n"
"thead{\n"
"background: #6099B7;\n"
"font-weight: bold;\n"
"text-align: left;\n"
"color: #ffffff;\n"
"}\n"
"td, th{\n"
"padding: 8px;\n"
"}\n"
"body{\n"
"font: 12px Arial, Helvetica, sans-serif;\n"
"}\n"
"</style>\n"
"</head>\n"
"<body>\n"
"<table>\n"
"<thead>\n"
"<tr>\n"
"<th>$DEVNAME (idx: $DEVIDX)</th>\n"
"<th style=\"text-align:right\">$DATETIME</th>\n"
"</tr>\n"
"</thead>\n"
"<tbody>\n"
"<tr>\n"
"<td colspan=\"2\">$MESSAGE</td>\n"
"</tr>\n"
"</tbody>\n"
"</table>\n"
"</body>\n"
"</html>\n";


CNotificationEmail::CNotificationEmail() : CNotificationBase(std::string("email"), OPTIONS_HTML_BODY)
{
SetupConfig(std::string("EmailFrom"), _EmailFrom);
Expand All @@ -20,7 +61,15 @@ CNotificationEmail::~CNotificationEmail()
{
}

bool CNotificationEmail::SendMessageImplementation(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const int Priority, const std::string &Sound, const bool bFromNotification)
bool CNotificationEmail::SendMessageImplementation(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const int Priority,
const std::string &Sound,
const bool bFromNotification)
{
if (bFromNotification)
{
Expand All @@ -31,8 +80,49 @@ bool CNotificationEmail::SendMessageImplementation(const std::string &Subject, c

SMTPClient sclient;

std::string HtmlBody = std::string("<html>\n<body>\n<b>") + Text + std::string("</body>\n</html>\n");
stdreplace(HtmlBody, "&lt;br&gt;", "<br>");
std::string MessageText = Text;
stdreplace(MessageText, "&lt;br&gt;", "<br>");

std::string HtmlBody;

if (Idx != 0)
{
HtmlBody = szHTMLMail;

std::stringstream sstr;
sstr << Idx;
stdreplace(HtmlBody, "$DEVIDX", sstr.str());
stdreplace(HtmlBody, "$DEVNAME", Name);
stdreplace(HtmlBody, "$MESSAGE", MessageText);

char szDate[100];
#if !defined WIN32
// Get a timestamp
struct timeval tv;
gettimeofday(&tv, NULL);

struct tm timeinfo;
localtime_r(&tv.tv_sec, &timeinfo);

// create a time stamp string for the log message
snprintf(szDate, sizeof(szDate), "%04d-%02d-%02d %02d:%02d:%02d.%03d",
timeinfo.tm_year + 1900, timeinfo.tm_mon + 1, timeinfo.tm_mday,
timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec, (int)tv.tv_usec / 1000);
#else
// Get a timestamp
SYSTEMTIME time;
::GetLocalTime(&time);

// create a time stamp string for the log message
sprintf_s(szDate, sizeof(szDate), "%04d-%02d-%02d %02d:%02d:%02d.%03d", time.wYear, time.wMonth, time.wDay, time.wHour, time.wMinute, time.wSecond, time.wMilliseconds);
#endif
stdreplace(HtmlBody, "$DATETIME", szDate);
}
else
{
HtmlBody = std::string("<html>\n<body>\n<b>") + MessageText + std::string("</body>\n</html>\n");
}

sclient.SetFrom(_EmailFrom.c_str());
sclient.SetTo(_EmailTo.c_str());
if (_EmailUsername != "" && _EmailPassword != "") {
Expand Down
10 changes: 9 additions & 1 deletion notifications/NotificationEmail.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ class CNotificationEmail : public CNotificationBase {
~CNotificationEmail();
virtual bool IsConfigured();
protected:
virtual bool SendMessageImplementation(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const int Priority, const std::string &Sound, const bool bFromNotification);
virtual bool SendMessageImplementation(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const int Priority,
const std::string &Sound,
const bool bFromNotification);
private:
std::string _EmailFrom;
std::string _EmailTo;
Expand Down
10 changes: 9 additions & 1 deletion notifications/NotificationGCM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,15 @@ CNotificationGCM::~CNotificationGCM()
{
}

bool CNotificationGCM::SendMessageImplementation(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const int Priority, const std::string &Sound, const bool bFromNotification)
bool CNotificationGCM::SendMessageImplementation(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const int Priority,
const std::string &Sound,
const bool bFromNotification)
{
//send message to GCM

Expand Down
10 changes: 9 additions & 1 deletion notifications/NotificationGCM.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,13 @@ class CNotificationGCM : public CNotificationBase {
~CNotificationGCM();
virtual bool IsConfigured();
protected:
virtual bool SendMessageImplementation(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const int Priority, const std::string &Sound, const bool bFromNotification);
virtual bool SendMessageImplementation(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const int Priority,
const std::string &Sound,
const bool bFromNotification);
};
10 changes: 9 additions & 1 deletion notifications/NotificationHTTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,15 @@ CNotificationHTTP::~CNotificationHTTP()
{
}

bool CNotificationHTTP::SendMessageImplementation(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const int Priority, const std::string &Sound, const bool bFromNotification)
bool CNotificationHTTP::SendMessageImplementation(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const int Priority,
const std::string &Sound,
const bool bFromNotification)
{
std::string destURL = _HTTPURL;
bool bSuccess = false;
Expand Down
10 changes: 9 additions & 1 deletion notifications/NotificationHTTP.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@ class CNotificationHTTP : public CNotificationBase {
~CNotificationHTTP();
virtual bool IsConfigured();
protected:
virtual bool SendMessageImplementation(const std::string &Subject, const std::string &Text, const std::string &ExtraData, const int Priority, const std::string &Sound, const bool bFromNotification);
virtual bool SendMessageImplementation(
const uint64_t Idx,
const std::string &Name,
const std::string &Subject,
const std::string &Text,
const std::string &ExtraData,
const int Priority,
const std::string &Sound,
const bool bFromNotification);
private:
std::string _HTTPField1;
std::string _HTTPField2;
Expand Down
Loading

0 comments on commit 3151e72

Please sign in to comment.