Esempio n. 1
0
bool CNotificationHelper::CheckAndHandleDewPointNotification(
	const uint64_t Idx,
	const std::string &devicename,
	const float temp,
	const float dewpoint)
{
	std::vector<_tNotification> notifications = GetNotifications(Idx);
	if (notifications.size() == 0)
		return false;

	char szTmp[600];
	std::string szExtraData = "|Name=" + devicename + "|Image=temp-0-5|";
	std::string notValue;

	time_t atime = mytime(NULL);

	//check if not sent 12 hours ago, and if applicable

	atime -= m_NotificationSensorInterval;

	std::string msg = "";

	std::string signdewpoint = Notification_Type_Desc(NTYPE_DEWPOINT, 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() < 1)
				continue; //impossible
			std::string ntype = splitresults[0];

			bool bSendNotification = false;

			if (ntype == signdewpoint)
			{
				//dewpoint
				if (temp <= dewpoint)
				{
					bSendNotification = true;
					sprintf(szTmp, "%s Dew Point reached (%.1f degrees)", devicename.c_str(), temp);
					msg = szTmp;
					sprintf(szTmp, "%.1f", temp);
					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;
}
Esempio n. 2
0
bool CNotificationHelper::CheckAndHandleValueNotification(
	const uint64_t Idx,
	const std::string &DeviceName,
	const int value)
{
	std::vector<_tNotification> notifications = GetNotifications(Idx);
	if (notifications.size() == 0)
		return false;

	char szTmp[600];
	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 notValue;

	std::string signvalue = Notification_Type_Desc(NTYPE_VALUE, 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)) //emergency always goes true
		{
			std::vector<std::string> splitresults;
			StringSplit(itt->Params, ";", splitresults);
			if (splitresults.size() < 2)
				continue; //impossible
			std::string ntype = splitresults[0];
			int svalue = static_cast<int>(atoi(splitresults[1].c_str()));

			if (ntype == signvalue)
			{
				if (value > svalue)
				{
					sprintf(szTmp, "%s is %d", DeviceName.c_str(), value);
					msg = szTmp;
					sprintf(szTmp, "%d", value);
					notValue = szTmp;
					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;
}
Esempio n. 3
0
bool CNotificationHelper::CheckAndHandleNotification(
	const uint64_t Idx,
	const std::string &devicename,
	const _eNotificationTypes ntype,
	const std::string &message)
{
	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;

	std::string szExtraData = "|Name=" + devicename + "|SwitchType=" + result[0][0] + "|CustomImage=" + result[0][1] + "|";
	std::string notValue;

	time_t atime = mytime(NULL);

	//check if not sent 12 hours ago, and if applicable
	atime -= m_NotificationSensorInterval;

	std::string ltype = Notification_Type_Desc(ntype, 1);
	std::vector<_tNotification>::const_iterator itt;
	for (itt = notifications.begin(); itt != notifications.end(); ++itt)
	{
		if (itt->LastUpdate)
			TouchLastUpdate(itt->ID);
		std::vector<std::string> splitresults;
		StringSplit(itt->Params, ";", splitresults);
		if (splitresults.size() < 1)
			continue; //impossible
		std::string atype = splitresults[0];
		if (atype == ltype)
		{
			if ((atime >= itt->LastSend) || (itt->SendAlways)) //emergency always goes true
			{
				std::string msg = message;
				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 unsigned long long 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=%llu)",
		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;

			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)];
						}
						else
							msg += " >> LEVEL " + sLevel;
					}
				}
				else 
				{
					bSendNotification = true;
					szExtraData += "Status=Off|";
					msg += " >> OFF";
				}
			}
			if (bSendNotification)
			{
				if (!itt->CustomMessage.empty())
					msg = itt->CustomMessage;
				SendMessageEx(itt->ActiveSystems, msg, msg, szExtraData, itt->Priority, std::string(""), true);
				TouchNotification(itt->ID);
			}
		}
	}
	return true;
}
bool CNotificationHelper::CheckAndHandleSwitchNotification(
	const unsigned long long 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=%llu)",
		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;

			if (atype == ltype)
			{
				bSendNotification = true;
				msg = devicename;
				if (ntype == NTYPE_SWITCH_ON)
				{
					szExtraData += "Status=On|";
					switch (switchtype)
					{
					case STYPE_Doorbell:
						msg += " pressed";
						break;
					case STYPE_Contact:
						msg += " Open";
						szExtraData += "Image=contact48_open|";
						break;
					case STYPE_DoorLock:
						msg += " Open";
						szExtraData += "Image=door48open|";
						break;
					case STYPE_Motion:
						msg += " movement detected";
						break;
					case STYPE_SMOKEDETECTOR:
						msg += " ALARM/FIRE !";
						break;
					default:
						msg += " >> ON";
						break;
					}

				}
				else {
					szExtraData += "Status=Off|";
					switch (switchtype)
					{
					case STYPE_DoorLock:
					case STYPE_Contact:
						msg += " Closed";
						break;
					default:
						msg += " >> OFF";
						break;
					}
				}
			}
			if (bSendNotification)
			{
				if (!itt->CustomMessage.empty())
					msg = itt->CustomMessage;
				SendMessageEx(itt->ActiveSystems, msg, msg, szExtraData, itt->Priority, std::string(""), true);
				TouchNotification(itt->ID);
			}
		}
	}
	return true;
}
bool CNotificationHelper::CheckAndHandleNotification(
	const unsigned long long 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=%llu)", 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 ((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()));

			bool bSendNotification = false;

			if (ntype == nsign)
			{
				if (bWhenIsGreater)
				{
					if (mvalue > svalue)
					{
						bSendNotification = true;
						sprintf(szTmp, "%s %s is %s %s",
							devicename.c_str(),
							ltype.c_str(),
							pvalue.c_str(),
							label.c_str()
							);
						msg = szTmp;
					}
				}
				else
				{
					if (mvalue < svalue)
					{
						bSendNotification = true;
						sprintf(szTmp, "%s %s is %s %s",
							devicename.c_str(),
							ltype.c_str(),
							pvalue.c_str(),
							label.c_str()
							);
						msg = szTmp;
					}
				}
			}
			if (bSendNotification)
			{
				if (!itt->CustomMessage.empty())
					msg = itt->CustomMessage;
				SendMessageEx(itt->ActiveSystems, msg, msg, szExtraData, itt->Priority, std::string(""), true);
				TouchNotification(itt->ID);
			}
		}
	}
	return true;
}
bool CNotificationHelper::CheckAndHandleAmpere123Notification(
	const unsigned long long 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 signamp1 = Notification_Type_Desc(NTYPE_AMPERE1, 1);
	std::string signamp2 = Notification_Type_Desc(NTYPE_AMPERE2, 2);
	std::string signamp3 = Notification_Type_Desc(NTYPE_AMPERE3, 3);

	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()));

			bool bSendNotification = false;

			if (ntype == signamp1)
			{
				//Ampere1
				if (bWhenIsGreater)
				{
					if (Ampere1 > svalue)
					{
						bSendNotification = true;
						sprintf(szTmp, "%s Ampere1 is %.1f Ampere", devicename.c_str(), Ampere1);
						msg = szTmp;
					}
				}
				else
				{
					if (Ampere1 < svalue)
					{
						bSendNotification = true;
						sprintf(szTmp, "%s Ampere1 is %.1f Ampere", devicename.c_str(), Ampere1);
						msg = szTmp;
					}
				}
			}
			else if (ntype == signamp2)
			{
				//Ampere2
				if (bWhenIsGreater)
				{
					if (Ampere2 > svalue)
					{
						bSendNotification = true;
						sprintf(szTmp, "%s Ampere2 is %.1f Ampere", devicename.c_str(), Ampere2);
						msg = szTmp;
					}
				}
				else
				{
					if (Ampere2 < svalue)
					{
						bSendNotification = true;
						sprintf(szTmp, "%s Ampere2 is %.1f Ampere", devicename.c_str(), Ampere2);
						msg = szTmp;
					}
				}
			}
			else if (ntype == signamp3)
			{
				//Ampere3
				if (bWhenIsGreater)
				{
					if (Ampere3 > svalue)
					{
						bSendNotification = true;
						sprintf(szTmp, "%s Ampere3 is %.1f Ampere", devicename.c_str(), Ampere3);
						msg = szTmp;
					}
				}
				else
				{
					if (Ampere3 < svalue)
					{
						bSendNotification = true;
						sprintf(szTmp, "%s Ampere3 is %.1f Ampere", devicename.c_str(), Ampere3);
						msg = szTmp;
					}
				}
			}
			if (bSendNotification)
			{
				if (!itt->CustomMessage.empty())
					msg = itt->CustomMessage;
				SendMessageEx(itt->ActiveSystems, msg, msg, szExtraData, itt->Priority, std::string(""), true);
				TouchNotification(itt->ID);
			}
		}
	}
	return true;
}
bool CNotificationHelper::CheckAndHandleTempHumidityNotification(
	const unsigned long long 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 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;
					}
				}
			}
			else if ((ntype == signhum) && (bHaveHumidity))
			{
				//humanity
				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)
			{
				if (!itt->CustomMessage.empty())
					msg = itt->CustomMessage;
				SendMessageEx(itt->ActiveSystems, msg, msg, szExtraData, itt->Priority, std::string(""), true);
				TouchNotification(itt->ID);
			}
		}
	}
	return true;
}
Esempio n. 9
0
void CNotificationHelper::CheckAndHandleLastUpdateNotification()
{
	if (m_notifications.size() < 1)
		return;

	time_t atime = mytime(NULL);
	atime -= m_NotificationSensorInterval;
	std::map<uint64_t, std::vector<_tNotification> >::const_iterator itt;

	for (itt = m_notifications.begin(); itt != m_notifications.end(); ++itt)
	{
		std::vector<_tNotification>::const_iterator itt2;
		for (itt2 = itt->second.begin(); itt2 != itt->second.end(); ++itt2)
		{
			if (((atime >= itt2->LastSend) || (itt2->SendAlways) || (!itt2->CustomMessage.empty())) && (itt2->LastUpdate)) //emergency always goes true
			{
				std::vector<std::string> splitresults;
				StringSplit(itt2->Params, ";", splitresults);
				if (splitresults.size() < 3)
					continue;
				std::string ttype = Notification_Type_Desc(NTYPE_LASTUPDATE, 1);
				if (splitresults[0] == ttype)
				{
					std::string recoverymsg;
					bool bRecoveryMessage = false;
					bRecoveryMessage = CustomRecoveryMessage(itt2->ID, recoverymsg, true);
					if ((atime < itt2->LastSend) && (!itt2->SendAlways) && (!bRecoveryMessage))
						continue;
					extern time_t m_StartTime;
					time_t btime = mytime(NULL);
					std::string msg;
					std::string szExtraData;
					std::string custommsg;
					uint64_t Idx = itt->first;
					int SensorTimeOut = atoi(splitresults[2].c_str());  // minutes
					int diff = (int)round(difftime(btime, itt2->LastUpdate));
					bool bStartTime = (difftime(btime, m_StartTime) < SensorTimeOut*60);
					bool bSendNotification = ApplyRule(splitresults[1], (diff == SensorTimeOut*60), (diff < SensorTimeOut*60));
					bool bCustomMessage = false;
					bCustomMessage = CustomRecoveryMessage(itt2->ID, custommsg, false);

					if (bSendNotification && !bStartTime && (!bRecoveryMessage || itt2->SendAlways))
					{
						std::vector<std::vector<std::string> > result;
						result = m_sql.safe_query("SELECT SwitchType FROM DeviceStatus WHERE (ID=%" PRIu64 ")", Idx);
						if (result.size() == 0)
							continue;
						szExtraData = "|Name=" + itt2->DeviceName + "|SwitchType=" + result[0][0] + "|";
						std::string ltype = Notification_Type_Desc(NTYPE_LASTUPDATE, 0);
						std::string label = Notification_Type_Label(NTYPE_LASTUPDATE);
						char szDate[50];
						char szTmp[300];
						struct tm ltime;
						localtime_r(&itt2->LastUpdate,&ltime);
						sprintf(szDate, "%04d-%02d-%02d %02d:%02d:%02d", ltime.tm_year + 1900, ltime.tm_mon + 1, ltime.tm_mday,
							ltime.tm_hour, ltime.tm_min, ltime.tm_sec);
						sprintf(szTmp,"Sensor %s %s: %s [%s %d %s]", itt2->DeviceName.c_str(), ltype.c_str(), szDate,
							splitresults[1].c_str(), SensorTimeOut, label.c_str());
						msg = szTmp;
					}
					else if (!bSendNotification && bRecoveryMessage)
					{
						bSendNotification = true;
						msg = recoverymsg;
						std::string clearstr = "!";
						CustomRecoveryMessage(itt2->ID, clearstr, true);
					}
					else
					{
						bSendNotification = false;
					}
					if (bSendNotification)
					{
						if (bCustomMessage && !bRecoveryMessage)
							msg = ParseCustomMessage(custommsg, itt2->DeviceName, "");
						SendMessageEx(Idx, itt2->DeviceName, itt2->ActiveSystems, msg, msg, szExtraData, itt2->Priority, std::string(""), true);
						if (!bRecoveryMessage)
						{
							TouchNotification(itt2->ID);
							CustomRecoveryMessage(itt2->ID, msg, true);
						}
					}
				}
			}
		}
	}
}
Esempio n. 10
0
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;
}
Esempio n. 11
0
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;
}
Esempio n. 12
0
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 ltemp = Notification_Type_Label(NTYPE_TEMPERATURE);
	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 (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 (m_sql.m_tempunit == TEMPUNIT_F)
			{
				//Convert to Celsius
				svalue = (svalue / 1.8f) - 32.0f;
			}
			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|";
				bSendNotification = ApplyRule(splitresults[1], (temp == svalue), (temp < svalue));
				if (bSendNotification && (!bRecoveryMessage || itt->SendAlways))
				{
					sprintf(szTmp, "%s temperature is %.1f %s [%s %.1f %s]", devicename.c_str(), temp, ltemp.c_str(), splitresults[1].c_str(), svalue, ltemp.c_str());
					msg = szTmp;
					sprintf(szTmp, "%.1f", temp);
					notValue = szTmp;
				}
				else if (!bSendNotification && bRecoveryMessage)
				{
					bSendNotification = true;
					msg = recoverymsg;
					std::string clearstr = "!";
					CustomRecoveryMessage(itt->ID, clearstr, true);
				}
				else
				{
					bSendNotification = false;
				}
			}
			else if ((ntype == signhum) && (bHaveHumidity))
			{
				//humidity
				szExtraData += "Image=moisture48|";
				bSendNotification = ApplyRule(splitresults[1], (humidity == svalue), (humidity < svalue));
				if (bSendNotification && (!bRecoveryMessage || itt->SendAlways))
				{
					sprintf(szTmp, "%s Humidity is %d %% [%s %.0f %%]", devicename.c_str(), humidity, splitresults[1].c_str(), svalue);
					msg = szTmp;
					sprintf(szTmp, "%d", humidity);
					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;
}