From 7ebefd85a4b5fb2a93ded502fbf141c7dff85155 Mon Sep 17 00:00:00 2001 From: badaix Date: Wed, 2 Sep 2015 18:44:09 +0200 Subject: [PATCH] added Volume struct --- server/config.cpp | 8 +++++++- server/config.h | 36 +++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/server/config.cpp b/server/config.cpp index 67373c37..2508ad5d 100644 --- a/server/config.cpp +++ b/server/config.cpp @@ -44,14 +44,20 @@ void Config::test() } -ClientInfoPtr Config::getClientInfo(const std::string& macAddress) +ClientInfoPtr Config::getClientInfo(const std::string& macAddress, bool add) { + if (macAddress.empty()) + return nullptr; + for (auto client: clients) { if (client->macAddress == macAddress) return client; } + if (!add) + return nullptr; + ClientInfoPtr client = make_shared(macAddress); clients.push_back(client); diff --git a/server/config.h b/server/config.h index 89819f35..a6c6cd0e 100644 --- a/server/config.h +++ b/server/config.h @@ -41,9 +41,34 @@ T jGet(json j, const std::string& what, const T& def) } +struct Volume +{ + Volume(uint16_t _percent = 100, bool _muted = false) : percent(_percent), muted(_muted) + { + } + + void fromJson(const json& j) + { + percent = jGet(j, "percent", percent); + muted = jGet(j, "muted", muted); + } + + json toJson() + { + json j; + j["percent"] = percent; + j["muted"] = muted; + return j; + } + + uint16_t percent; + bool muted; +}; + + struct ClientInfo { - ClientInfo(const std::string& _macAddress = "") : macAddress(_macAddress), volume(1.0), connected(false) + ClientInfo(const std::string& _macAddress = "") : macAddress(_macAddress), volume(100), connected(false) { lastSeen.tv_sec = 0; lastSeen.tv_usec = 0; @@ -56,7 +81,7 @@ struct ClientInfo hostName = jGet(j, "host", ""); version = jGet(j, "version", ""); name = jGet(j, "name", ""); - volume = jGet(j, "volume", 1.0); + volume.fromJson(j["volume"]); lastSeen.tv_sec = jGet(j["lastSeen"], "sec", 0); lastSeen.tv_usec = jGet(j["lastSeen"], "usec", 0); connected = jGet(j, "connected", true); @@ -70,7 +95,7 @@ struct ClientInfo j["host"] = hostName; j["version"] = version; j["name"] = getName(); - j["volume"] = volume; + j["volume"] = volume.toJson(); j["lastSeen"]["sec"] = lastSeen.tv_sec; j["lastSeen"]["usec"] = lastSeen.tv_usec; j["connected"] = connected; @@ -90,7 +115,8 @@ struct ClientInfo std::string hostName; std::string version; std::string name; - double volume; + Volume volume; + bool muted; timeval lastSeen; bool connected; }; @@ -108,7 +134,7 @@ public: } void test(); - ClientInfoPtr getClientInfo(const std::string& mac); + ClientInfoPtr getClientInfo(const std::string& mac, bool add = true); std::vector clients; json getClientInfos() const;