void CLogitechMediaServer::Do_Node_Work(const LogitechMediaServerNode &Node)
{
	bool bPingOK = false;
	_eMediaStatus nStatus = MSTAT_UNKNOWN;
	std::string	sPlayerId = Node.IP;
	std::string	sStatus = "";

	try
	{
		std::string sPostdata = "{\"id\":1,\"method\":\"slim.request\",\"params\":[\"" + sPlayerId + "\",[\"status\",\"-\",1,\"tags:Aadly\"]]}";
		Json::Value root = Query(m_IP, m_Port, sPostdata);

		if (!root.size())
			nStatus = MSTAT_OFF;
		else
		{
			bPingOK = true;

			if (root["player_connected"].asString() == "1")
			{
				if (root["power"].asString() == "0")
					nStatus = MSTAT_OFF;
				else {
					nStatus = MSTAT_ON;
					std::string sMode = root["mode"].asString();
					std::string	sTitle = "";
					std::string	sAlbum = "";
					std::string	sArtist = "";
					std::string	sAlbumArtist = "";
					std::string	sTrackArtist = "";
					std::string	sYear = "";
					std::string	sDuration = "";
					std::string sLabel = "";

					if (root["playlist_loop"].size()) {
						sTitle = root["playlist_loop"][0]["title"].asString();
						sAlbum = root["playlist_loop"][0]["album"].asString();
						sArtist = root["playlist_loop"][0]["artist"].asString();
						sAlbumArtist = root["playlist_loop"][0]["albumartist"].asString();
						sTrackArtist = root["playlist_loop"][0]["trackartist"].asString();
						sYear = root["playlist_loop"][0]["year"].asString();
						sDuration = root["playlist_loop"][0]["duration"].asString();

						if (sTrackArtist != "")
							sArtist = sTrackArtist;
						else
							if (sAlbumArtist != "")
								sArtist = sAlbumArtist;
						if (sYear == "0") sYear = "";
						if (sYear != "")
							sYear = " (" + sYear + ")";
					}

					sLabel = sArtist + " - " + sTitle + sYear;
					sStatus = sLabel;
				}
			}
		}
	}
	catch (...)
	{
		bPingOK = false;
	}
	UpdateNodeStatus(Node, nStatus, sStatus, bPingOK);
	if (m_iThreadsRunning > 0) m_iThreadsRunning--;
}
示例#2
0
文件: Kodi.cpp 项目: joukio/domoticz
void CKodi::Do_Node_Work(const KodiNode &Node)
{
	bool			bPingOK = false;
	_eMediaStatus	nStatus = MSTAT_UNKNOWN;
	std::string		sStatus = "";

	// http://<ip_address>:8080/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22Player.GetActivePlayers%22,%22id%22:1}
	//		{"id":1,"jsonrpc":"2.0","result":[{"playerid":1,"type":"video"}]}

	// http://<ip_address>:8080/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22Player.GetItem%22,%22id%22:1,%22params%22:{%22playerid%22:0,%22properties%22:[%22artist%22,%22year%22,%22channel%22,%22season%22,%22episode%22]}}
	//		{"id":1,"jsonrpc":"2.0","result":{"item":{"artist":["Coldplay"],"id":25,"label":"The Scientist","type":"song","year":2002}}}

	// http://<ip_address>:8080/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22Player.GetProperties%22,%22id%22:1,%22params%22:{%22playerid%22:1,%22properties%22:[%22totaltime%22,%22percentage%22,%22time%22]}}
	//		{"id":1,"jsonrpc":"2.0","result":{"percentage":22.207427978515625,"time":{"hours":0,"milliseconds":948,"minutes":15,"seconds":31},"totaltime":{"hours":1,"milliseconds":560,"minutes":9,"seconds":56}}}

	try
	{
		Json::Value root = Query(Node.IP, Node.Port, "/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22Player.GetActivePlayers%22,%22id%22:1}");
		if (!root.size())
			nStatus = MSTAT_OFF;
		else
		{
			bPingOK = true;
			std::string	sPlayerId;	
			if (root["result"].empty() == true)
			{
				nStatus = MSTAT_IDLE;
			}
			else if (root["result"][0]["type"].empty() == true)
			{
				nStatus = MSTAT_UNKNOWN;
			}
			else
			{
				std::string	sMedia = root["result"][0]["type"].asCString();
				if (root["result"][0]["playerid"].empty() == true)
				{
					_log.Log(LOG_ERROR, "Kodi: No PlayerID returned when player is not idle!");
					return;
				}
				if (sMedia == "video") nStatus = MSTAT_VIDEO;
				if (sMedia == "audio") nStatus = MSTAT_AUDIO;
				if (sMedia == "picture") nStatus = MSTAT_PHOTO;
				sPlayerId = SSTR((int)root["result"][0]["playerid"].asInt());
			}

			// If player is active then pick up additional details
			if (sPlayerId != "")
			{
				std::string	sTitle;
				std::string	sPercent;
				std::string	sYear;

				root = Query(Node.IP, Node.Port, "/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22Player.GetItem%22,%22id%22:1,%22params%22:{%22playerid%22:" + sPlayerId + ",%22properties%22:[%22artist%22,%22year%22,%22channel%22,%22showtitle%22,%22season%22,%22episode%22,%22title%22]}}");
				if (root.size())
				{
					std::string	sType;
					if (root["result"]["item"]["type"].empty() != true)
					{
						sType = root["result"]["item"]["type"].asCString();
					}

					if (sType == "song")
					{
						if (root["result"]["item"]["artist"][0].empty() != true)
						{
							sTitle = root["result"]["item"]["artist"][0].asCString();
							sTitle += " - ";
						}
					}

					if (sType == "episode")
					{
						if (root["result"]["item"]["showtitle"].empty() != true)
						{
							sTitle = root["result"]["item"]["showtitle"].asCString();
						}
						if (root["result"]["item"]["season"].empty() != true)
						{
							sTitle += " [S";
							sTitle += SSTR((int)root["result"]["item"]["season"].asInt());
						}
						if (root["result"]["item"]["episode"].empty() != true)
						{
							sTitle += "E";
							sTitle += SSTR((int)root["result"]["item"]["episode"].asInt());
							sTitle += "], ";
						}
					}

					if (sType == "channel")
					{
						if (root["result"]["item"]["channel"].empty() != true)
						{
							sTitle = root["result"]["item"]["channel"].asCString();
							sTitle += " - ";
						}
					}

					if (root["result"]["item"]["year"].empty() != true)
					{
						sYear = SSTR((int)root["result"]["item"]["year"].asInt());
						if (sYear.length() > 2) sYear = " (" + sYear + ")";
						else sYear = "";
					}

					if (root["result"]["item"]["title"].empty() != true)
					{
						std::string	sLabel = root["result"]["item"]["title"].asCString();
						if ((!sLabel.length()) && (root["result"]["item"]["label"].empty() != true))
						{
							sLabel = root["result"]["item"]["label"].asCString();
						}
						// if title is too long shorten it by removing things in brackets, followed by things after a ", "
						boost::algorithm::trim(sLabel);
						if (sLabel.length() > MAX_TITLE_LEN)
						{
							boost::algorithm::replace_all(sLabel, " - ", ", ");
						}
						while (sLabel.length() > MAX_TITLE_LEN)
						{
							int begin = sLabel.find_last_of("(");
							int end = sLabel.find_last_of(")");
							if ((std::string::npos == begin) || (std::string::npos == end) || (begin >= end)) break;
							sLabel.erase(begin, end - begin + 1);
						}
						while (sLabel.length() > MAX_TITLE_LEN)
						{
							int end = sLabel.find_last_of(",");
							if (std::string::npos == end) break;
							sLabel = sLabel.substr(0, end);
						}
						boost::algorithm::trim(sLabel);
						stdreplace(sLabel, " ,", ",");
						sLabel = sLabel.substr(0, MAX_TITLE_LEN);
						sTitle = sLabel;
					}
				}

				root = Query(Node.IP, Node.Port, "/jsonrpc?request={%22jsonrpc%22:%222.0%22,%22method%22:%22Player.GetProperties%22,%22id%22:1,%22params%22:{%22playerid%22:" + sPlayerId + ",%22properties%22:[%22live%22,%22percentage%22,%22speed%22]}}");
				if (root.size())
				{
					bool	bLive = root["result"]["live"].asBool();
					if (bLive) sYear = " (Live)";
					int		iSpeed = root["result"]["speed"].asInt();
					if (iSpeed == 0) nStatus = MSTAT_PAUSED;
					float	fPercent = root["result"]["percentage"].asFloat();
					if (fPercent > 1.0) sPercent = SSTR((int)round(fPercent)) + "%";
				}

				// Assemble final status
				sStatus = sTitle;
				if (sStatus.length() < (MAX_TITLE_LEN-7)) sStatus += sYear;
				if (sPercent.length() != 0) sStatus += ", " + sPercent;
			}
		}
	}
	catch (std::exception& e)
	{
		bPingOK = false;
	}
	catch (...)
	{
		bPingOK = false;
	}
	UpdateNodeStatus(Node, nStatus, sStatus, bPingOK);
	if (m_iThreadsRunning > 0) m_iThreadsRunning--;
}