diff --git a/server/config.cpp b/server/config.cpp index ab0a8bf6..67373c37 100644 --- a/server/config.cpp +++ b/server/config.cpp @@ -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(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; +} + + diff --git a/server/config.h b/server/config.h index da5da58d..89819f35 100644 --- a/server/config.h +++ b/server/config.h @@ -20,6 +20,8 @@ #define CONFIG_H #include +#include +#include #include #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(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 ClientInfoPtr; + class Config { @@ -90,11 +108,14 @@ public: } void test(); + ClientInfoPtr getClientInfo(const std::string& mac); + + std::vector clients; + json getClientInfos() const; private: Config(); - }; diff --git a/server/controlServer.cpp b/server/controlServer.cpp index 0d9f58e4..90e9ddd8 100644 --- a/server/controlServer.cpp +++ b/server/controlServer.cpp @@ -25,6 +25,7 @@ #include "common/log.h" #include "common/snapException.h" #include "jsonrpc.h" +#include "config.h" #include 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)