Skip to content

Commit

Permalink
Add base config item
Browse files Browse the repository at this point in the history
  • Loading branch information
badaix committed Jul 2, 2024
1 parent d92c19b commit 0f9d7d2
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ jobs:
mkdir -p build/doxygen
doxygen 2>&1 | tee build/doxygen.log
WARNINGS=$(cat build/doxygen.log | sort | uniq | grep -e ": warning: " | wc -l)
MAX_ALLOWED=865
MAX_ALLOWED=840
echo "Doxygen finished with $WARNINGS warnings, max allowed: $MAX_ALLOWED"
if [ "$WARNINGS" -gt "$MAX_ALLOWED" ]; then exit 1; else exit 0; fi;
if [ "$WARNINGS" -gt "$MAX_ALLOWED" ]; then exit $WARNINGS; else exit 0; fi;
unit-test:
Expand Down
67 changes: 38 additions & 29 deletions server/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,35 +42,44 @@ using ClientInfoPtr = std::shared_ptr<ClientInfo>;
using GroupPtr = std::shared_ptr<Group>;


template <typename T>
T jGet(const json& j, const std::string& what, const T& def)
struct JsonConfigItem
{
try
/// Read config item from json object @p j
virtual void fromJson(const json& j) = 0;
/// @return config item serialized to json
virtual json toJson() = 0;

protected:
/// @return value for key @p what or @p def, if not found. Result is casted to T.
template <typename T>
T jGet(const json& j, const std::string& what, const T& def)
{
if (!j.count(what))
try
{
if (!j.count(what))
return def;
return j[what].get<T>();
}
catch (...)
{
return def;
return j[what].get<T>();
}
catch (...)
{
return def;
}
}
}

};

struct Volume
struct Volume : public JsonConfigItem
{
Volume(uint16_t _percent = 100, bool _muted = false) : percent(_percent), muted(_muted)
{
}

void fromJson(const json& j)
void fromJson(const json& j) override
{
percent = jGet<uint16_t>(j, "percent", percent);
muted = jGet<bool>(j, "muted", muted);
}

json toJson()
json toJson() override
{
json j;
j["percent"] = percent;
Expand All @@ -84,7 +93,7 @@ struct Volume



struct Host
struct Host : public JsonConfigItem
{
Host() : name(""), mac(""), os(""), arch(""), ip("")
{
Expand All @@ -97,7 +106,7 @@ struct Host
arch = getArch();
}

void fromJson(const json& j)
void fromJson(const json& j) override
{
name = strutils::trim_copy(jGet<std::string>(j, "name", ""));
mac = strutils::trim_copy(jGet<std::string>(j, "mac", ""));
Expand All @@ -106,7 +115,7 @@ struct Host
ip = strutils::trim_copy(jGet<std::string>(j, "ip", ""));
}

json toJson()
json toJson() override
{
json j;
j["name"] = name;
Expand All @@ -125,21 +134,21 @@ struct Host
};


struct ClientConfig
struct ClientConfig : public JsonConfigItem
{
ClientConfig() : name(""), volume(100), latency(0), instance(1)
{
}

void fromJson(const json& j)
void fromJson(const json& j) override
{
name = strutils::trim_copy(jGet<std::string>(j, "name", ""));
volume.fromJson(j["volume"]);
latency = jGet<int32_t>(j, "latency", 0);
instance = jGet<size_t>(j, "instance", 1);
}

json toJson()
json toJson() override
{
json j;
j["name"] = strutils::trim_copy(name);
Expand All @@ -157,22 +166,22 @@ struct ClientConfig



struct Snapcast
struct Snapcast : public JsonConfigItem
{
Snapcast(const std::string& _name = "", const std::string& _version = "") : name(_name), version(_version), protocolVersion(1)
{
}

virtual ~Snapcast() = default;

virtual void fromJson(const json& j)
void fromJson(const json& j) override
{
name = strutils::trim_copy(jGet<std::string>(j, "name", ""));
version = strutils::trim_copy(jGet<std::string>(j, "version", ""));
protocolVersion = jGet<int>(j, "protocolVersion", 1);
}

virtual json toJson()
json toJson() override
{
json j;
j["name"] = strutils::trim_copy(name);
Expand Down Expand Up @@ -218,15 +227,15 @@ struct Snapserver : public Snapcast
};


struct ClientInfo
struct ClientInfo : public JsonConfigItem
{
ClientInfo(const std::string& _clientId = "") : id(_clientId), connected(false)
{
lastSeen.tv_sec = 0;
lastSeen.tv_usec = 0;
}

void fromJson(const json& j)
void fromJson(const json& j) override
{
host.fromJson(j["host"]);
id = jGet<std::string>(j, "id", host.mac);
Expand All @@ -237,7 +246,7 @@ struct ClientInfo
connected = jGet<bool>(j, "connected", true);
}

json toJson()
json toJson() override
{
json j;
j["id"] = id;
Expand All @@ -259,7 +268,7 @@ struct ClientInfo
};


struct Group
struct Group : public JsonConfigItem
{
Group(const ClientInfoPtr client = nullptr) : muted(false)
{
Expand All @@ -268,7 +277,7 @@ struct Group
id = generateUUID();
}

void fromJson(const json& j)
void fromJson(const json& j) override
{
name = strutils::trim_copy(jGet<std::string>(j, "name", ""));
id = strutils::trim_copy(jGet<std::string>(j, "id", ""));
Expand All @@ -287,7 +296,7 @@ struct Group
}
}

json toJson()
json toJson() override
{
json j;
j["name"] = strutils::trim_copy(name);
Expand Down

0 comments on commit 0f9d7d2

Please sign in to comment.