StreamServer receives control events

This commit is contained in:
badaix 2015-09-05 11:12:15 +02:00
parent 9200fec700
commit 50db49d269
4 changed files with 106 additions and 80 deletions

View file

@ -91,6 +91,91 @@ void StreamServer::onDisconnect(ServerSession* connection)
}
void StreamServer::onMessageReceived(ControlSession* connection, const std::string& message)
{
try
{
JsonRequest request;
request.parse(message);
//{"jsonrpc": "2.0", "method": "System.GetStatus", "id": 2}
//{"jsonrpc": "2.0", "method": "System.GetStatus", "params": {"client": "00:21:6a:7d:74:fc"}, "id": 2}
//{"jsonrpc": "2.0", "method": "Client.SetVolume", "params": {"client": "00:21:6a:7d:74:fc", "volume": 83}, "id": 2}
//{"jsonrpc": "2.0", "method": "Client.SetLatency", "params": {"client": "00:21:6a:7d:74:fc", "latency": 10}, "id": 2}
//{"jsonrpc": "2.0", "method": "Client.SetName", "params": {"client": "00:21:6a:7d:74:fc", "name": "living room"}, "id": 2}
//{"jsonrpc": "2.0", "method": "Client.SetMute", "params": {"client": "00:21:6a:7d:74:fc", "mute": false}, "id": 2}
//curl -X POST -H "Content-Type: application/json" -d '{"jsonrpc": "2.0", "method": "Application.SetVolume", "params": {"volume":100}, "id": 1}' http://i3c.pla.lcl:8080/jsonrpc
//https://en.wikipedia.org/wiki/JSON-RPC
//https://github.com/pla1/utils/blob/master/kodi_remote.desktop
//http://forum.fhem.de/index.php?topic=10075.130;wap2
//http://kodi.wiki/view/JSON-RPC_API/v6#Application.SetVolume
logO << "method: " << request.method << ", " << "id: " << request.id << "\n";
json response;
if (request.method == "System.GetStatus")
{
json jClient = json::array();
if (request.hasParam("client"))
{
ClientInfoPtr client = Config::instance().getClientInfo(request.getParam("client").get<string>(), false);
if (client)
jClient += client->toJson();
}
else
jClient = Config::instance().getClientInfos();
response = {
{"server", {
{"host", getHostName()},
{"version", VERSION}
}},
{"clients", jClient}
};
}
else if (request.method == "Client.SetVolume")
{
int volume = request.getParam("volume").get<int>();
logO << "client: " << request.getParam("client").get<string>() << ", volume: " << volume << "\n";
ServerSession* session = getClientSession(request.getParam("client").get<string>());
// if (session != NULL)
response = volume;
}
else if (request.method == "Client.SetLatency")
{
int latency = request.getParam("latency").get<int>();
logO << "client: " << request.getParam("client").get<string>() << ", latency: " << latency << "\n";
response = latency;
}
else if (request.method == "Client.SetName")
{
string name = request.getParam("name").get<string>();
logO << "client: " << request.getParam("client").get<string>() << ", name: " << name << "\n";
response = name;
}
else if (request.method == "Client.SetMute")
{
bool mute = request.getParam("mute").get<bool>();
logO << "client: " << request.getParam("client").get<string>() << ", mute: " << mute << "\n";
response = mute;
}
else
throw JsonMethodNotFoundException(request);
connection->send(request.getResponse(response).dump());
}
catch (const JsonRequestException& e)
{
connection->send(e.getResponse().dump());
}
}
void StreamServer::onMessageReceived(ServerSession* connection, const msg::BaseMessage& baseMessage, char* buffer)
{
// logO << "getNextMessage: " << baseMessage.type << ", size: " << baseMessage.size << ", id: " << baseMessage.id << ", refers: " << baseMessage.refersTo << ", sent: " << baseMessage.sent.sec << "," << baseMessage.sent.usec << ", recv: " << baseMessage.received.sec << "," << baseMessage.received.usec << "\n";
@ -160,6 +245,16 @@ void StreamServer::onMessageReceived(ServerSession* connection, const msg::BaseM
}
ServerSession* StreamServer::getClientSession(const std::string& mac)
{
for (auto session: sessions_)
{
if (session->macAddress == mac)
return session.get();
}
return NULL;
}
void StreamServer::startAccept()
{
@ -189,7 +284,7 @@ void StreamServer::handleAccept(socket_ptr socket)
void StreamServer::start()
{
controlServer.reset(new ControlServer(settings_.port + 1));
controlServer.reset(new ControlServer(settings_.port + 1, this));
controlServer->start();
pipeReader_ = new PipeReader(this, settings_.sampleFormat, settings_.codec, settings_.fifoName, settings_.pipeReadMs);