diff --git a/src/Backend.cpp b/src/Backend.cpp index 1f5587a6..96254c26 100644 --- a/src/Backend.cpp +++ b/src/Backend.cpp @@ -83,7 +83,7 @@ json HABackend::DoCommand(const string& command, const json& data) json jsonresponse = json::parse(response); if (jsonresponse["id"] != request["id"]) { - throw std::runtime_error("Send out a command, but received something with a different ID."); + throw std::runtime_error("Sent out a command, but received something with a different ID."); } return jsonresponse; } @@ -100,14 +100,15 @@ void HABackend::threadrunner() throw std::runtime_error("Didn't receive response to getDomains while we expected it"); } for (auto& [domain, services] : getdomainjson["result"].items()) { - std::scoped_lock lk(domainslock); domains[domain] = std::make_shared(domain, services); } - json subscribe; - subscribe["type"] = "subscribe_events"; - wc->send(subscribe); + if (subscribe_events) { + json subscribe; + subscribe["type"] = "subscribe_events"; + wc->send(subscribe); + } json getstates; getstates["type"] = "get_states"; @@ -119,6 +120,7 @@ void HABackend::threadrunner() // cout<> GetEntities(); void WSConnSend(json& msg); + bool subscribe_events{true}; + private: bool loaded; std::mutex load_lock; diff --git a/src/front-cli.cpp b/src/front-cli.cpp index 3cee9492..59989d66 100644 --- a/src/front-cli.cpp +++ b/src/front-cli.cpp @@ -44,25 +44,28 @@ void uithread(HABackend& backend, int argc, char* argv[]) { argparse::ArgumentParser program("client-cli"); argparse::ArgumentParser subscribe_command("subscribe"); + subscribe_command.add_description("subscribe to a domain and show events"); subscribe_command.add_argument("domain") .help("specific a HA domain"); // maybe .remaining() so you can subscribe multiple? - program.add_subparser(subscribe_command); argparse::ArgumentParser token_command("ha-get-token"); + token_command.add_description("get a long lived access token"); token_command.add_argument("name").help("Name of the token").default_value("voorkant"); program.add_subparser(token_command); argparse::ArgumentParser list_entities_command("list-entities"); + list_entities_command.add_description("list all entities"); program.add_subparser(list_entities_command); /* usage example for dump-command with data: * build/client-cli dump-command call_service '{"domain":"light","service":"toggle","target":{"entity_id":"light.bed_light"}}' */ argparse::ArgumentParser dump_command("dump-command"); + dump_command.add_description("run a command and show the response"); dump_command.add_argument("command").help("the command to execute"); dump_command.add_argument("data").help("optional data to pass with the command").default_value("{}"); - + dump_command.add_argument("--wait").implicit_value(true).help("keep listening for further output"); program.add_subparser(dump_command); try { @@ -110,9 +113,15 @@ void uithread(HABackend& backend, int argc, char* argv[]) } } else if (program.is_subcommand_used(dump_command)) { + backend.subscribe_events = false; + backend.Start(); + json data = json::parse(dump_command.get("data")); json res = backend.DoCommand(dump_command.get("command"), data); cout << res.dump(2) << endl; + while (true) { + sleep(10); + } } else { cerr << "no command given" << endl;