mirror of
https://github.com/badaix/snapcast.git
synced 2025-05-10 07:36:41 +02:00
added Group entity
This commit is contained in:
parent
db2b5e06a3
commit
7272be0d1b
3 changed files with 108 additions and 19 deletions
|
@ -51,16 +51,14 @@ Config::Config()
|
|||
ifs >> j;
|
||||
if (j.count("ConfigVersion"))
|
||||
{
|
||||
json jClient = j["Client"];
|
||||
for (auto it = jClient.begin(); it != jClient.end(); ++it)
|
||||
json jGroups = j["Groups"];
|
||||
for (auto it = jGroups.begin(); it != jGroups.end(); ++it)
|
||||
{
|
||||
ClientInfoPtr client = make_shared<ClientInfo>();
|
||||
client->fromJson(*it);
|
||||
if (client->clientId.empty() || getClientInfo(client->clientId))
|
||||
continue;
|
||||
|
||||
client->connected = false;
|
||||
clients.push_back(client);
|
||||
GroupPtr group = make_shared<Group>();
|
||||
group->fromJson(*it);
|
||||
// if (client->clientId.empty() || getClientInfo(client->clientId))
|
||||
// continue;
|
||||
groups.push_back(group);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -82,8 +80,8 @@ void Config::save()
|
|||
{
|
||||
std::ofstream ofs(filename_.c_str(), std::ofstream::out|std::ofstream::trunc);
|
||||
json clients = {
|
||||
{"ConfigVersion", 1},
|
||||
{"Client", getClientInfos()}
|
||||
{"ConfigVersion", 2},
|
||||
{"Groups", getGroups()}
|
||||
};
|
||||
ofs << std::setw(4) << clients;
|
||||
ofs.close();
|
||||
|
@ -118,6 +116,26 @@ ClientInfoPtr Config::addClientInfo(const std::string& clientId)
|
|||
}
|
||||
|
||||
|
||||
GroupPtr Config::getGroup(ClientInfoPtr client)
|
||||
{
|
||||
for (auto group: groups)
|
||||
{
|
||||
for (auto c: group->clients)
|
||||
{
|
||||
if (c->clientId == client->clientId)
|
||||
return group;
|
||||
}
|
||||
}
|
||||
|
||||
GroupPtr group = std::make_shared<Group>();
|
||||
group->id = generateUUID();
|
||||
group->clients.push_back(client);
|
||||
groups.push_back(group);
|
||||
|
||||
return group;
|
||||
}
|
||||
|
||||
|
||||
json Config::getServerStatus(const std::string& clientId, const json& streams) const
|
||||
{
|
||||
json jClient = json::array();
|
||||
|
@ -156,8 +174,21 @@ json Config::getClientInfos() const
|
|||
}
|
||||
|
||||
|
||||
json Config::getGroups() const
|
||||
{
|
||||
json result = json::array();
|
||||
for (auto group: groups)
|
||||
result.push_back(group->toJson());
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
void Config::remove(ClientInfoPtr client)
|
||||
{
|
||||
auto group = getGroup(client);
|
||||
if (group->clients.size() == 1)
|
||||
groups.erase(std::remove(groups.begin(), groups.end(), group), groups.end());
|
||||
|
||||
clients.erase(std::remove(clients.begin(), clients.end(), client), clients.end());
|
||||
}
|
||||
|
||||
|
|
|
@ -29,6 +29,13 @@
|
|||
|
||||
using json = nlohmann::json;
|
||||
|
||||
struct ClientInfo;
|
||||
struct Group;
|
||||
|
||||
typedef std::shared_ptr<ClientInfo> ClientInfoPtr;
|
||||
typedef std::shared_ptr<Group> GroupPtr;
|
||||
|
||||
|
||||
template<typename T>
|
||||
T jGet(const json& j, const std::string& what, const T& def)
|
||||
{
|
||||
|
@ -114,7 +121,7 @@ struct Host
|
|||
|
||||
struct ClientConfig
|
||||
{
|
||||
ClientConfig() : name(""), volume(100), latency(0), streamId(""), instance(1)
|
||||
ClientConfig() : name(""), volume(100), latency(0), instance(1)
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -123,7 +130,6 @@ struct ClientConfig
|
|||
name = trim_copy(jGet<std::string>(j, "name", ""));
|
||||
volume.fromJson(j["volume"]);
|
||||
latency = jGet<int32_t>(j, "latency", 0);
|
||||
streamId = trim_copy(jGet<std::string>(j, "stream", ""));
|
||||
instance = jGet<size_t>(j, "instance", 1);
|
||||
}
|
||||
|
||||
|
@ -133,7 +139,6 @@ struct ClientConfig
|
|||
j["name"] = trim_copy(name);
|
||||
j["volume"] = volume.toJson();
|
||||
j["latency"] = latency;
|
||||
j["stream"] = trim_copy(streamId);
|
||||
j["instance"] = instance;
|
||||
return j;
|
||||
}
|
||||
|
@ -141,7 +146,6 @@ struct ClientConfig
|
|||
std::string name;
|
||||
Volume volume;
|
||||
int32_t latency;
|
||||
std::string streamId;
|
||||
size_t instance;
|
||||
};
|
||||
|
||||
|
@ -250,7 +254,50 @@ struct ClientInfo
|
|||
bool connected;
|
||||
};
|
||||
|
||||
typedef std::shared_ptr<ClientInfo> ClientInfoPtr;
|
||||
|
||||
struct Group
|
||||
{
|
||||
Group()
|
||||
{
|
||||
}
|
||||
|
||||
void fromJson(const json& j)
|
||||
{
|
||||
name = trim_copy(jGet<std::string>(j, "name", ""));
|
||||
id = trim_copy(jGet<std::string>(j, "id", ""));
|
||||
streamId = trim_copy(jGet<std::string>(j, "stream", ""));
|
||||
clients.clear();
|
||||
if (j.count("clients"))
|
||||
{
|
||||
for (auto& jClient : j["clients"])
|
||||
{
|
||||
ClientInfoPtr client = std::make_shared<ClientInfo>();
|
||||
client->fromJson(jClient);
|
||||
client->connected = false;
|
||||
clients.push_back(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
json toJson()
|
||||
{
|
||||
json j;
|
||||
j["name"] = trim_copy(name);
|
||||
j["id"] = trim_copy(id);
|
||||
j["stream"] = trim_copy(streamId);
|
||||
|
||||
json jClients = json::array();
|
||||
for (auto client: clients)
|
||||
jClients.push_back(client->toJson());
|
||||
j["clients"] = jClients;
|
||||
return j;
|
||||
}
|
||||
|
||||
std::string name;
|
||||
std::string id;
|
||||
std::string streamId;
|
||||
std::vector<ClientInfoPtr> clients;
|
||||
};
|
||||
|
||||
|
||||
class Config
|
||||
|
@ -266,12 +313,17 @@ public:
|
|||
ClientInfoPtr addClientInfo(const std::string& clientId);
|
||||
void remove(ClientInfoPtr client);
|
||||
|
||||
std::vector<ClientInfoPtr> clients;
|
||||
GroupPtr getGroup(ClientInfoPtr client);
|
||||
|
||||
json getClientInfos() const;
|
||||
json getGroups() const;
|
||||
json getServerStatus(const std::string& clientId, const json& streams) const;
|
||||
|
||||
void save();
|
||||
|
||||
std::vector<GroupPtr> groups;
|
||||
std::vector<ClientInfoPtr> clients;
|
||||
|
||||
private:
|
||||
Config();
|
||||
~Config();
|
||||
|
|
|
@ -155,6 +155,7 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std::
|
|||
}
|
||||
else if (request.method == "Client.SetStream")
|
||||
{
|
||||
/* TODO: Group.SetStream
|
||||
string streamId = request.getParam("id").get<string>();
|
||||
PcmStreamPtr stream = streamManager_->getStream(streamId);
|
||||
if (stream == nullptr)
|
||||
|
@ -169,6 +170,7 @@ void StreamServer::onMessageReceived(ControlSession* controlSession, const std::
|
|||
session->sendAsync(stream->getHeader());
|
||||
session->setPcmStream(stream);
|
||||
}
|
||||
*/
|
||||
}
|
||||
else if (request.method == "Client.SetLatency")
|
||||
{
|
||||
|
@ -246,6 +248,8 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
|
|||
// std::lock_guard<std::mutex> mlock(mutex_);
|
||||
ClientInfoPtr client = Config::instance().addClientInfo(connection->clientId);
|
||||
|
||||
GroupPtr group = Config::instance().getGroup(client);
|
||||
|
||||
logD << "request kServerSettings\n";
|
||||
msg::ServerSettings* serverSettings = new msg::ServerSettings();
|
||||
serverSettings->setVolume(client->config.volume.percent);
|
||||
|
@ -268,12 +272,14 @@ void StreamServer::onMessageReceived(StreamSession* connection, const msg::BaseM
|
|||
gettimeofday(&client->lastSeen, NULL);
|
||||
|
||||
// Assign and update stream
|
||||
PcmStreamPtr stream = streamManager_->getStream(client->config.streamId);
|
||||
PcmStreamPtr stream = streamManager_->getStream(group->streamId);
|
||||
if (!stream)
|
||||
{
|
||||
stream = streamManager_->getDefaultStream();
|
||||
client->config.streamId = stream->getId();
|
||||
group->streamId = stream->getId();
|
||||
}
|
||||
logO << "Group: " << group->id << ", stream: " << group->streamId << "\n";
|
||||
|
||||
Config::instance().save();
|
||||
|
||||
connection->setPcmStream(stream);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue