mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-22 13:36:18 +02:00
added getter for parameters
This commit is contained in:
parent
6121fdceae
commit
79c98e9e10
3 changed files with 57 additions and 30 deletions
|
@ -84,8 +84,8 @@ void ControlServer::onMessageReceived(ControlSession* connection, const std::str
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
JsonRequest jsonRequest;
|
JsonRequest request;
|
||||||
jsonRequest.parse(message);
|
request.parse(message);
|
||||||
|
|
||||||
|
|
||||||
//{"jsonrpc": "2.0", "method": "get", "id": 2}
|
//{"jsonrpc": "2.0", "method": "get", "id": 2}
|
||||||
|
@ -94,54 +94,50 @@ void ControlServer::onMessageReceived(ControlSession* connection, const std::str
|
||||||
//{"jsonrpc": "2.0", "method": "get", "params": ["status", "client"], "id": 2}
|
//{"jsonrpc": "2.0", "method": "get", "params": ["status", "client"], "id": 2}
|
||||||
//{"jsonrpc": "2.0", "method": "get", "params": ["status", "client", "MAC"], "id": 2}
|
//{"jsonrpc": "2.0", "method": "get", "params": ["status", "client", "MAC"], "id": 2}
|
||||||
//{"jsonrpc": "2.0", "method": "test", "params": ["status", "client", "MAC"], "id": 2}
|
//{"jsonrpc": "2.0", "method": "test", "params": ["status", "client", "MAC"], "id": 2}
|
||||||
|
|
||||||
//{"jsonrpc": "2.0", "method": "set", "params": ["voume", "client", "MAC", "1.0"], "id": 2}
|
//{"jsonrpc": "2.0", "method": "set", "params": ["voume", "client", "MAC", "1.0"], "id": 2}
|
||||||
|
//{"jsonrpc": "2.0", "method": "set", "params": ["latency", "client", "MAC", "20"], "id": 2}
|
||||||
|
//{"jsonrpc": "2.0", "method": "set", "params": ["name", "client", "MAC", "living room"], "id": 2}
|
||||||
|
|
||||||
// -32601 Method not found The method does not exist / is not available.
|
// -32601 Method not found The method does not exist / is not available.
|
||||||
// -32602 Invalid params Invalid method parameter(s).
|
// -32602 Invalid params Invalid method parameter(s).
|
||||||
// -32603 Internal error Internal JSON-RPC error.
|
// -32603 Internal error Internal JSON-RPC error.
|
||||||
|
|
||||||
logO << "method: " << jsonRequest.method << ", " << "id: " << jsonRequest.id << "\n";
|
logO << "method: " << request.method << ", " << "id: " << request.id << "\n";
|
||||||
for (string s: jsonRequest.params)
|
for (string s: request.params)
|
||||||
logO << "param: " << s << "\n";
|
logO << "param: " << s << "\n";
|
||||||
|
|
||||||
if (jsonRequest.params.empty())
|
if (request.method == "get")
|
||||||
throw JsonInvalidParamsException(jsonRequest);
|
|
||||||
|
|
||||||
vector<string>& params = jsonRequest.params;
|
|
||||||
if (jsonRequest.method == "get")
|
|
||||||
{
|
{
|
||||||
if (params[0] == "status")
|
if (request.isParam(0, "status"))
|
||||||
{
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw JsonInvalidParamsException(jsonRequest);
|
throw JsonInvalidParamsException(request);
|
||||||
}
|
}
|
||||||
else if (jsonRequest.method == "set")
|
else if (request.method == "set")
|
||||||
{
|
{
|
||||||
if (params[0] == "volume")
|
if (request.isParam(0, "voume") && request.isParam(1, "client"))
|
||||||
{
|
{
|
||||||
if ((params.size() < 4) || (params[1] != "client"))
|
double volume = request.getParam<double>(3);
|
||||||
throw JsonInvalidParamsException(jsonRequest);
|
logO << "volume: " << volume << "\n";
|
||||||
try
|
|
||||||
{
|
|
||||||
double volume = boost::lexical_cast<double>(params[3]);
|
|
||||||
}
|
}
|
||||||
catch(...)
|
else if (request.isParam(0, "latency") && request.isParam(1, "client"))
|
||||||
{
|
{
|
||||||
throw JsonInvalidParamsException(jsonRequest);
|
int latency = request.getParam<int>(3);
|
||||||
|
logO << "latency: " << latency << "\n";
|
||||||
}
|
}
|
||||||
}
|
else if (request.isParam(0, "name") && request.isParam(1, "client"))
|
||||||
else if (params[0] == "latency")
|
|
||||||
{
|
{
|
||||||
if ((params.size() < 3) || (params[1] != "client"))
|
string name = request.getParam<string>(3);
|
||||||
throw JsonInvalidParamsException(jsonRequest);
|
logO << "name: " << name << ", client: " << request.getParam<string>(2) << "\n";
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw JsonInvalidParamsException(jsonRequest);
|
throw JsonInvalidParamsException(request);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
throw JsonMethodNotFoundException(jsonRequest);
|
throw JsonMethodNotFoundException(request);
|
||||||
|
|
||||||
json response = {
|
json response = {
|
||||||
{"test", "123"},
|
{"test", "123"},
|
||||||
|
@ -150,7 +146,7 @@ void ControlServer::onMessageReceived(ControlSession* connection, const std::str
|
||||||
{"message", true}
|
{"message", true}
|
||||||
}}};
|
}}};
|
||||||
|
|
||||||
connection->send(jsonRequest.getResponse(response).dump());
|
connection->send(request.getResponse(response).dump());
|
||||||
}
|
}
|
||||||
catch (const JsonRequestException& e)
|
catch (const JsonRequestException& e)
|
||||||
{
|
{
|
||||||
|
|
|
@ -109,6 +109,13 @@ Json JsonRequest::getError(int code, const std::string& message)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool JsonRequest::isParam(size_t idx, const std::string& param)
|
||||||
|
{
|
||||||
|
if (idx >= params.size())
|
||||||
|
throw JsonInvalidParamsException(*this);
|
||||||
|
return (params[idx] == param);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
||||||
|
|
|
@ -21,18 +21,25 @@
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <boost/lexical_cast.hpp>
|
||||||
#include "json.hpp"
|
#include "json.hpp"
|
||||||
#include "common/snapException.h"
|
#include "common/snapException.h"
|
||||||
|
|
||||||
|
|
||||||
using Json = nlohmann::json;
|
using Json = nlohmann::json;
|
||||||
|
|
||||||
|
|
||||||
|
class JsonInvalidParamsException;
|
||||||
|
|
||||||
|
|
||||||
|
/// JSON-RPC 2.0 request
|
||||||
|
/**
|
||||||
|
* Simple jsonrpc 2.0 parser with getters
|
||||||
|
* Currently no named parameters are supported, but only array parameters
|
||||||
|
*/
|
||||||
class JsonRequest
|
class JsonRequest
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
/// ctor. Encoded PCM data is passed to the PipeListener
|
|
||||||
JsonRequest();
|
JsonRequest();
|
||||||
|
|
||||||
void parse(const std::string& json);
|
void parse(const std::string& json);
|
||||||
|
@ -43,6 +50,23 @@ public:
|
||||||
Json getResponse(const Json& result);
|
Json getResponse(const Json& result);
|
||||||
Json getError(int code, const std::string& message);
|
Json getError(int code, const std::string& message);
|
||||||
|
|
||||||
|
template<typename T>
|
||||||
|
T getParam(size_t idx)
|
||||||
|
{
|
||||||
|
if (idx >= params.size())
|
||||||
|
throw JsonInvalidParamsException(*this);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
return boost::lexical_cast<T>(params[idx]);
|
||||||
|
}
|
||||||
|
catch(...)
|
||||||
|
{
|
||||||
|
throw JsonInvalidParamsException(*this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool isParam(size_t idx, const std::string& param);
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
Json json_;
|
Json json_;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue