bool CNotificationHelper::CheckAndHandleTempHumidityNotification( const uint64_t Idx, const std::string &devicename, const float temp, const int humidity, const bool bHaveTemp, const bool bHaveHumidity) { std::vector<_tNotification> notifications = GetNotifications(Idx); if (notifications.size() == 0) return false; char szTmp[600]; std::string notValue; std::string szExtraData = "|Name=" + devicename + "|"; time_t atime = mytime(NULL); //check if not sent 12 hours ago, and if applicable atime -= m_NotificationSensorInterval; std::string msg = ""; std::string signtemp = Notification_Type_Desc(NTYPE_TEMPERATURE, 1); std::string signhum = Notification_Type_Desc(NTYPE_HUMIDITY, 1); std::vector<_tNotification>::const_iterator itt; for (itt = notifications.begin(); itt != notifications.end(); ++itt) { if ((atime >= itt->LastSend) || (itt->SendAlways)) //emergency always goes true { std::vector<std::string> splitresults; StringSplit(itt->Params, ";", splitresults); if (splitresults.size() < 3) continue; //impossible std::string ntype = splitresults[0]; bool bWhenIsGreater = (splitresults[1] == ">"); float svalue = static_cast<float>(atof(splitresults[2].c_str())); if (m_sql.m_tempunit == TEMPUNIT_F) { //Convert to Celsius svalue = (svalue / 1.8f) - 32.0f; } bool bSendNotification = false; if ((ntype == signtemp) && (bHaveTemp)) { //temperature if (temp > 30.0) szExtraData += "Image=temp-gt-30|"; else if (temp > 25.0) szExtraData += "Image=temp-25-30|"; else if (temp > 20.0) szExtraData += "Image=temp-20-25|"; else if (temp > 15.0) szExtraData += "Image=temp-15-20|"; else if (temp > 10.0) szExtraData += "Image=temp-10-15|"; else if (temp > 5.0) szExtraData += "Image=temp-5-10|"; else szExtraData += "Image=temp48|"; if (bWhenIsGreater) { if (temp > svalue) { bSendNotification = true; sprintf(szTmp, "%s temperature is %.1f degrees", devicename.c_str(), temp); msg = szTmp; } } else { if (temp < svalue) { bSendNotification = true; sprintf(szTmp, "%s temperature is %.1f degrees", devicename.c_str(), temp); msg = szTmp; } } if (bSendNotification) { sprintf(szTmp, "%.1f", temp); notValue = szTmp; } } else if ((ntype == signhum) && (bHaveHumidity)) { //humidity szExtraData += "Image=moisture48|"; if (bWhenIsGreater) { if (humidity > svalue) { bSendNotification = true; sprintf(szTmp, "%s Humidity is %d %%", devicename.c_str(), humidity); msg = szTmp; } } else { if (humidity < svalue) { bSendNotification = true; sprintf(szTmp, "%s Humidity is %d %%", devicename.c_str(), humidity); msg = szTmp; } } if (bSendNotification) { sprintf(szTmp, "%d", humidity); notValue = szTmp; } } if (bSendNotification) { if (!itt->CustomMessage.empty()) msg = ParseCustomMessage(itt->CustomMessage, devicename, notValue); SendMessageEx(Idx, devicename, itt->ActiveSystems, msg, msg, szExtraData, itt->Priority, std::string(""), true); TouchNotification(itt->ID); } } } return true; }
bool CNotificationHelper::CheckAndHandleSwitchNotification( const uint64_t Idx, const std::string &devicename, const _eNotificationTypes ntype, const int llevel) { std::vector<_tNotification> notifications = GetNotifications(Idx); if (notifications.size() == 0) return false; std::vector<std::vector<std::string> > result; result = m_sql.safe_query("SELECT SwitchType, CustomImage, Options FROM DeviceStatus WHERE (ID=%" PRIu64 ")", Idx); if (result.size() == 0) return false; _eSwitchType switchtype = (_eSwitchType)atoi(result[0][0].c_str()); std::string szExtraData = "|Name=" + devicename + "|SwitchType=" + result[0][0] + "|CustomImage=" + result[0][1] + "|"; std::string sOptions = result[0][2].c_str(); std::string msg = ""; std::string ltype = Notification_Type_Desc(ntype, 1); time_t atime = mytime(NULL); atime -= m_NotificationSwitchInterval; std::vector<_tNotification>::const_iterator itt; for (itt = notifications.begin(); itt != notifications.end(); ++itt) { if ((atime >= itt->LastSend) || (itt->SendAlways)) //emergency always goes true { std::vector<std::string> splitresults; StringSplit(itt->Params, ";", splitresults); if (splitresults.size() < 1) continue; //impossible std::string atype = splitresults[0]; bool bSendNotification = false; std::string notValue; if (atype == ltype) { msg = devicename; if (ntype == NTYPE_SWITCH_ON) { if (splitresults.size() < 3) continue; //impossible bool bWhenEqual = (splitresults[1] == "="); int iLevel = atoi(splitresults[2].c_str()); if (!bWhenEqual || iLevel < 10 || iLevel > 100) continue; //invalid if (llevel == iLevel) { bSendNotification = true; std::string sLevel = boost::lexical_cast<std::string>(llevel); szExtraData += "Status=Level " + sLevel + "|"; if (switchtype == STYPE_Selector) { std::map<std::string, std::string> options = m_sql.BuildDeviceOptions(sOptions); std::string levelNames = options["LevelNames"]; std::vector<std::string> splitresults; StringSplit(levelNames, "|", splitresults); msg += " >> " + splitresults[(llevel / 10)]; notValue = ">> " + splitresults[(llevel / 10)]; } else { msg += " >> LEVEL " + sLevel; notValue = ">> LEVEL " + sLevel; } } } else { bSendNotification = true; szExtraData += "Status=Off|"; msg += " >> OFF"; notValue = ">> OFF"; } } if (bSendNotification) { if (!itt->CustomMessage.empty()) msg = ParseCustomMessage(itt->CustomMessage, devicename, notValue); SendMessageEx(Idx, devicename, itt->ActiveSystems, msg, msg, szExtraData, itt->Priority, std::string(""), true); TouchNotification(itt->ID); } } } return true; }
bool CNotificationHelper::CheckAndHandleNotification( const uint64_t Idx, const std::string &devicename, const unsigned char devType, const unsigned char subType, const _eNotificationTypes ntype, const float mvalue) { std::vector<_tNotification> notifications = GetNotifications(Idx); if (notifications.size() == 0) return false; char szTmp[600]; double intpart; std::string pvalue; if (modf(mvalue, &intpart) == 0) sprintf(szTmp, "%.0f", mvalue); else sprintf(szTmp, "%.1f", mvalue); pvalue = szTmp; std::vector<std::vector<std::string> > result; result = m_sql.safe_query("SELECT SwitchType FROM DeviceStatus WHERE (ID=%" PRIu64 ")", Idx); if (result.size() == 0) return false; std::string szExtraData = "|Name=" + devicename + "|SwitchType=" + result[0][0] + "|"; time_t atime = mytime(NULL); //check if not sent 12 hours ago, and if applicable atime -= m_NotificationSensorInterval; std::string msg = ""; std::string ltype = Notification_Type_Desc(ntype, 0); std::string nsign = Notification_Type_Desc(ntype, 1); std::string label = Notification_Type_Label(ntype); std::vector<_tNotification>::const_iterator itt; for (itt = notifications.begin(); itt != notifications.end(); ++itt) { if (itt->LastUpdate) TouchLastUpdate(itt->ID); if ((atime >= itt->LastSend) || (itt->SendAlways) || (!itt->CustomMessage.empty())) //emergency always goes true { std::string recoverymsg; bool bRecoveryMessage = false; bRecoveryMessage = CustomRecoveryMessage(itt->ID, recoverymsg, true); if ((atime < itt->LastSend) && (!itt->SendAlways) && (!bRecoveryMessage)) continue; std::vector<std::string> splitresults; StringSplit(itt->Params, ";", splitresults); if (splitresults.size() < 3) continue; //impossible std::string ntype = splitresults[0]; std::string custommsg; float svalue = static_cast<float>(atof(splitresults[2].c_str())); bool bSendNotification = false; bool bCustomMessage = false; bCustomMessage = CustomRecoveryMessage(itt->ID, custommsg, false); if (ntype == nsign) { bSendNotification = ApplyRule(splitresults[1], (mvalue == svalue), (mvalue < svalue)); if (bSendNotification && (!bRecoveryMessage || itt->SendAlways)) { sprintf(szTmp, "%s %s is %s %s [%s %.1f %s]", devicename.c_str(), ltype.c_str(), pvalue.c_str(), label.c_str(), splitresults[1].c_str(), svalue, label.c_str()); msg = szTmp; } else if (!bSendNotification && bRecoveryMessage) { bSendNotification = true; msg = recoverymsg; std::string clearstr = "!"; CustomRecoveryMessage(itt->ID, clearstr, true); } else { bSendNotification = false; } } if (bSendNotification) { if (bCustomMessage && !bRecoveryMessage) msg = ParseCustomMessage(custommsg, devicename, pvalue); SendMessageEx(Idx, devicename, itt->ActiveSystems, msg, msg, szExtraData, itt->Priority, std::string(""), true); if (!bRecoveryMessage) { TouchNotification(itt->ID); CustomRecoveryMessage(itt->ID, msg, true); } } } } return true; }
bool CNotificationHelper::CheckAndHandleSwitchNotification( const uint64_t Idx, const std::string &devicename, const _eNotificationTypes ntype) { std::vector<_tNotification> notifications = GetNotifications(Idx); if (notifications.size() == 0) return false; std::vector<std::vector<std::string> > result; result = m_sql.safe_query("SELECT SwitchType, CustomImage FROM DeviceStatus WHERE (ID=%" PRIu64 ")", Idx); if (result.size() == 0) return false; _eSwitchType switchtype = (_eSwitchType)atoi(result[0][0].c_str()); std::string szExtraData = "|Name=" + devicename + "|SwitchType=" + result[0][0] + "|CustomImage=" + result[0][1] + "|"; std::string msg = ""; std::string ltype = Notification_Type_Desc(ntype, 1); time_t atime = mytime(NULL); atime -= m_NotificationSwitchInterval; std::vector<_tNotification>::const_iterator itt; for (itt = notifications.begin(); itt != notifications.end(); ++itt) { if ((atime >= itt->LastSend) || (itt->SendAlways)) //emergency always goes true { std::vector<std::string> splitresults; StringSplit(itt->Params, ";", splitresults); if (splitresults.size() < 1) continue; //impossible std::string atype = splitresults[0]; bool bSendNotification = false; std::string notValue; if (atype == ltype) { bSendNotification = true; msg = devicename; if (ntype == NTYPE_SWITCH_ON) { szExtraData += "Status=On|"; switch (switchtype) { case STYPE_Doorbell: notValue = "pressed"; break; case STYPE_Contact: notValue = "Open"; szExtraData += "Image=contact48_open|"; break; case STYPE_DoorContact: notValue = "Open"; szExtraData += "Image=door48open|"; break; case STYPE_DoorLock: notValue = "Locked"; szExtraData += "Image=door48|"; break; case STYPE_Motion: notValue = "movement detected"; break; case STYPE_SMOKEDETECTOR: notValue = "ALARM/FIRE !"; break; default: notValue = ">> ON"; break; } } else { szExtraData += "Status=Off|"; switch (switchtype) { case STYPE_DoorContact: case STYPE_Contact: notValue = "Closed"; break; case STYPE_DoorLock: notValue = "Unlocked"; szExtraData += "Image=door48open|"; break; default: notValue = ">> OFF"; break; } } msg += " " + notValue; } if (bSendNotification) { if (!itt->CustomMessage.empty()) msg = ParseCustomMessage(itt->CustomMessage, devicename, notValue); SendMessageEx(Idx, devicename, itt->ActiveSystems, msg, msg, szExtraData, itt->Priority, std::string(""), true); TouchNotification(itt->ID); } } } return true; }
bool CNotificationHelper::CheckAndHandleAmpere123Notification( const uint64_t Idx, const std::string &devicename, const float Ampere1, const float Ampere2, const float Ampere3) { std::vector<_tNotification> notifications = GetNotifications(Idx); if (notifications.size() == 0) return false; char szTmp[600]; std::string szExtraData = "|Name=" + devicename + "|Image=current48|"; time_t atime = mytime(NULL); //check if not sent 12 hours ago, and if applicable atime -= m_NotificationSensorInterval; std::string msg = ""; std::string notValue; std::string signamp1 = Notification_Type_Desc(NTYPE_AMPERE1, 1); std::string signamp2 = Notification_Type_Desc(NTYPE_AMPERE2, 1); std::string signamp3 = Notification_Type_Desc(NTYPE_AMPERE3, 1); std::vector<_tNotification>::const_iterator itt; for (itt = notifications.begin(); itt != notifications.end(); ++itt) { if (itt->LastUpdate) TouchLastUpdate(itt->ID); if ((atime >= itt->LastSend) || (itt->SendAlways) || (!itt->CustomMessage.empty())) //emergency always goes true { std::string recoverymsg; bool bRecoveryMessage = false; bRecoveryMessage = CustomRecoveryMessage(itt->ID, recoverymsg, true); if ((atime < itt->LastSend) && (!itt->SendAlways) && (!bRecoveryMessage)) continue; std::vector<std::string> splitresults; StringSplit(itt->Params, ";", splitresults); if (splitresults.size() < 3) continue; //impossible std::string ntype = splitresults[0]; std::string custommsg; std::string ltype; float svalue = static_cast<float>(atof(splitresults[2].c_str())); float ampere; bool bSendNotification = false; bool bCustomMessage = false; bCustomMessage = CustomRecoveryMessage(itt->ID, custommsg, false); if (ntype == signamp1) { ampere = Ampere1; ltype = Notification_Type_Desc(NTYPE_AMPERE1, 0); } else if (ntype == signamp2) { ampere = Ampere2; ltype = Notification_Type_Desc(NTYPE_AMPERE2, 0); } else if (ntype == signamp3) { ampere = Ampere3; ltype = Notification_Type_Desc(NTYPE_AMPERE3, 0); } bSendNotification = ApplyRule(splitresults[1], (ampere == svalue), (ampere < svalue)); if (bSendNotification && (!bRecoveryMessage || itt->SendAlways)) { sprintf(szTmp, "%s %s is %.1f Ampere [%s %.1f Ampere]", devicename.c_str(), ltype.c_str(), ampere, splitresults[1].c_str(), svalue); msg = szTmp; sprintf(szTmp, "%.1f", ampere); notValue = szTmp; } else if (!bSendNotification && bRecoveryMessage) { bSendNotification = true; msg = recoverymsg; std::string clearstr = "!"; CustomRecoveryMessage(itt->ID, clearstr, true); } else { bSendNotification = false; } if (bSendNotification) { if (bCustomMessage && !bRecoveryMessage) msg = ParseCustomMessage(custommsg, devicename, notValue); SendMessageEx(Idx, devicename, itt->ActiveSystems, msg, msg, szExtraData, itt->Priority, std::string(""), true); if (!bRecoveryMessage) { TouchNotification(itt->ID); CustomRecoveryMessage(itt->ID, msg, true); } } } } return true; }