예제 #1
0
파일: Kodi.cpp 프로젝트: joukio/domoticz
void CKodi::UpdateNodeStatus(const KodiNode &Node, const _eMediaStatus nStatus, const std::string sStatus, bool bPingOK)
{
	//Find out node, and update it's status
	std::vector<KodiNode>::iterator itt;
	for (itt = m_nodes.begin(); itt != m_nodes.end(); ++itt)
	{
		if (itt->ID == Node.ID)
		{
			//Found it
			bool	bUseOnOff = false;
			if (((nStatus == MSTAT_OFF) && bPingOK) || ((nStatus != MSTAT_OFF) && !bPingOK)) bUseOnOff = true;
			time_t atime = mytime(NULL);
			itt->LastOK = atime;
			std::string sShortStatus = sStatus;
			if (sShortStatus.find_last_of("%") == sShortStatus.length()-1)
			{
				sShortStatus = sShortStatus.substr(0, sShortStatus.find_last_of(",")); // remove ", xx%" to reduce extraneous logging
			}
			if ((itt->nStatus != nStatus) || (itt->sStatus != sShortStatus))
			{
				/*
					Media device appears too different to integrate into main code :(
				*/

				// 1:	Update the DeviceStatus
				_log.Log(LOG_STATUS, "Kodi: (%s) %s - '%s'", Node.Name.c_str(), Media_Player_States(nStatus), sShortStatus.c_str());
				struct tm ltime;
				localtime_r(&atime, &ltime);
				char szLastUpdate[40];
				sprintf(szLastUpdate, "%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);
				std::vector<std::vector<std::string> > result;
				result = m_sql.safe_query("UPDATE DeviceStatus SET nValue=%d, sValue='%q', LastUpdate='%q' WHERE (HardwareID == %d) AND (DeviceID == '%q') AND (Unit == 1)",
					int(nStatus), sShortStatus.c_str(), szLastUpdate, m_HwdID, itt->szDevID);
				itt->nStatus = nStatus;
				itt->sStatus = sShortStatus;

				// 2:	Log the event
				std::string sLongStatus = Media_Player_States(itt->nStatus);
				if (itt->sStatus.length()) sLongStatus += " - " + itt->sStatus;
				result = m_sql.safe_query("INSERT INTO LightingLog (DeviceRowID, nValue, sValue) VALUES (%d, %d, '%q')", itt->ID, int(itt->nStatus), sLongStatus.c_str());

				// 3:	Trigger On/Off actions
				if (bUseOnOff)
				{
					result = m_sql.safe_query("SELECT StrParam1,StrParam2 FROM DeviceStatus WHERE (HardwareID==%d) AND (ID = '%q') AND (Unit == 1)", m_HwdID, itt->szDevID);
					if (result.size() > 0)
					{
						m_sql.HandleOnOffAction(bPingOK, result[0][0], result[0][1]);
					}
				}

				// 4:	Trigger Notifications
			}
			break;
		}
	}
}
예제 #2
0
void CLogitechMediaServer::UpdateNodeStatus(const LogitechMediaServerNode &Node, const _eMediaStatus nStatus, const std::string sStatus, bool bPingOK)
{
	//Find out node, and update it's status
	std::vector<LogitechMediaServerNode>::iterator itt;
	for (itt = m_nodes.begin(); itt != m_nodes.end(); ++itt)
	{
		if (itt->ID == Node.ID)
		{
			//Found it
			bool	bUseOnOff = false;
			if (((nStatus == MSTAT_OFF) && bPingOK) || ((nStatus != MSTAT_OFF) && !bPingOK)) bUseOnOff = true;
			time_t atime = mytime(NULL);
			itt->LastOK = atime;
			if ((itt->nStatus != nStatus) || (itt->sStatus != sStatus))
			{
				// 1:	Update the DeviceStatus
				if (nStatus == MSTAT_ON)
					_log.Log(LOG_NORM, "Logitech Media Server: (%s) %s - '%s'", Node.Name.c_str(), Media_Player_States(nStatus), sStatus.c_str());
				else
					_log.Log(LOG_NORM, "Logitech Media Server: (%s) %s", Node.Name.c_str(), Media_Player_States(nStatus));
				struct tm ltime;
				localtime_r(&atime, &ltime);
				char szLastUpdate[40];
				sprintf(szLastUpdate, "%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);
				std::vector<std::vector<std::string> > result;
				result = m_sql.safe_query("UPDATE DeviceStatus SET nValue=%d, sValue='%q', LastUpdate='%q' WHERE (HardwareID == %d) AND (DeviceID == '%q') AND (Unit == 1) AND (SwitchType == %d)",
					int(nStatus), sStatus.c_str(), szLastUpdate, m_HwdID, itt->szDevID, STYPE_Media);

				// 2:	Log the event if the actual status has changed
				std::string sShortStatus = sStatus;
				if ((itt->nStatus != nStatus) || (itt->sShortStatus != sShortStatus))
				{
					std::string sLongStatus = Media_Player_States(nStatus);
					if (sShortStatus.length()) sLongStatus += " - " + sShortStatus;
					result = m_sql.safe_query("INSERT INTO LightingLog (DeviceRowID, nValue, sValue) VALUES (%d, %d, '%q')", itt->ID, int(nStatus), sLongStatus.c_str());
				}

				// 3:	Trigger On/Off actions
				if (bUseOnOff)
				{
					result = m_sql.safe_query("SELECT StrParam1,StrParam2 FROM DeviceStatus WHERE (HardwareID==%d) AND (ID = '%q') AND (Unit == 1)", m_HwdID, itt->szDevID);
					if (result.size() > 0)
					{
						m_sql.HandleOnOffAction(bPingOK, result[0][0], result[0][1]);
					}
				}

				itt->nStatus = nStatus;
				itt->sStatus = sStatus;
				itt->sShortStatus = sShortStatus;
			}
			break;
		}
	}
}