mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-12 08:36:43 +02:00
config holds list of clients
This commit is contained in:
parent
3a0b856afe
commit
707f198a26
3 changed files with 56 additions and 14 deletions
|
@ -34,15 +34,37 @@ void Config::test()
|
|||
json j;
|
||||
ifs >> j;
|
||||
std::cout << std::setw(4) << j << std::endl;
|
||||
json j1 = j["Client"];//["Hobbys"];
|
||||
json j1 = j["Client"];
|
||||
for (json::iterator it = j1.begin(); it != j1.end(); ++it)
|
||||
{
|
||||
// json j2 = *it;
|
||||
// cout << "Key: " << j2.key() << ", value: " j2.value() << "\n";
|
||||
ClientInfo client;
|
||||
client.fromJson(*it);
|
||||
std::cout << "Client:\n" << std::setw(4) << client.toJson() << '\n';
|
||||
}
|
||||
// std::cout << std::setw(4) << j["Inhaber"]["Hobbys2"] << std::endl;
|
||||
}
|
||||
|
||||
|
||||
ClientInfoPtr Config::getClientInfo(const std::string& macAddress)
|
||||
{
|
||||
for (auto client: clients)
|
||||
{
|
||||
if (client->macAddress == macAddress)
|
||||
return client;
|
||||
}
|
||||
|
||||
ClientInfoPtr client = make_shared<ClientInfo>(macAddress);
|
||||
clients.push_back(client);
|
||||
|
||||
return client;
|
||||
}
|
||||
|
||||
|
||||
json Config::getClientInfos() const
|
||||
{
|
||||
json result = json::array();
|
||||
for (auto client: clients)
|
||||
result.push_back(client->toJson());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -20,6 +20,8 @@
|
|||
#define CONFIG_H
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include <sys/time.h>
|
||||
#include "json.hpp"
|
||||
|
||||
|
@ -41,6 +43,12 @@ T jGet(json j, const std::string& what, const T& def)
|
|||
|
||||
struct ClientInfo
|
||||
{
|
||||
ClientInfo(const std::string& _macAddress = "") : macAddress(_macAddress), volume(1.0), connected(false)
|
||||
{
|
||||
lastSeen.tv_sec = 0;
|
||||
lastSeen.tv_usec = 0;
|
||||
}
|
||||
|
||||
void fromJson(const json& j)
|
||||
{
|
||||
macAddress = jGet<std::string>(j, "MAC", "");
|
||||
|
@ -61,7 +69,7 @@ struct ClientInfo
|
|||
j["IP"] = ipAddress;
|
||||
j["host"] = hostName;
|
||||
j["version"] = version;
|
||||
j["name"] = name;
|
||||
j["name"] = getName();
|
||||
j["volume"] = volume;
|
||||
j["lastSeen"]["sec"] = lastSeen.tv_sec;
|
||||
j["lastSeen"]["usec"] = lastSeen.tv_usec;
|
||||
|
@ -69,6 +77,14 @@ struct ClientInfo
|
|||
return j;
|
||||
}
|
||||
|
||||
std::string getName()
|
||||
{
|
||||
if (name.empty())
|
||||
return hostName;
|
||||
else
|
||||
return name;
|
||||
}
|
||||
|
||||
std::string macAddress;
|
||||
std::string ipAddress;
|
||||
std::string hostName;
|
||||
|
@ -79,6 +95,8 @@ struct ClientInfo
|
|||
bool connected;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<ClientInfo> ClientInfoPtr;
|
||||
|
||||
|
||||
class Config
|
||||
{
|
||||
|
@ -90,11 +108,14 @@ public:
|
|||
}
|
||||
|
||||
void test();
|
||||
ClientInfoPtr getClientInfo(const std::string& mac);
|
||||
|
||||
std::vector<ClientInfoPtr> clients;
|
||||
json getClientInfos() const;
|
||||
|
||||
private:
|
||||
Config();
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -25,6 +25,7 @@
|
|||
#include "common/log.h"
|
||||
#include "common/snapException.h"
|
||||
#include "jsonrpc.h"
|
||||
#include "config.h"
|
||||
#include <iostream>
|
||||
|
||||
using namespace std;
|
||||
|
@ -107,11 +108,16 @@ void ControlServer::onMessageReceived(ControlSession* connection, const std::str
|
|||
for (string s: request.params)
|
||||
logO << "param: " << s << "\n";
|
||||
|
||||
json response;
|
||||
|
||||
if (request.method == "get")
|
||||
{
|
||||
if (request.isParam(0, "status"))
|
||||
{
|
||||
|
||||
response = {
|
||||
{"server", "xxx"},
|
||||
{"clients", Config::instance().getClientInfos()}
|
||||
};
|
||||
}
|
||||
else
|
||||
throw JsonInvalidParamsException(request);
|
||||
|
@ -139,13 +145,6 @@ void ControlServer::onMessageReceived(ControlSession* connection, const std::str
|
|||
else
|
||||
throw JsonMethodNotFoundException(request);
|
||||
|
||||
json response = {
|
||||
{"test", "123"},
|
||||
{"error", {
|
||||
{"code", 12},
|
||||
{"message", true}
|
||||
}}};
|
||||
|
||||
connection->send(request.getResponse(response).dump());
|
||||
}
|
||||
catch (const JsonRequestException& e)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue