Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

very WIP: expand dump-command for subcription things, like template rendering #62

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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<HADomain>(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";
Expand All @@ -119,6 +120,7 @@ void HABackend::threadrunner()
// cout<<msg<<endl;
json j = json::parse(msg);

std::cerr<<j.dump(2)<<std::endl; // FIXME: this is just to make this PR somewhat work
{

if (j["id"] == getstates["id"]) {
Expand Down
2 changes: 2 additions & 0 deletions src/Backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ class HABackend : Backend
map<string, std::shared_ptr<HAEntity>> GetEntities();
void WSConnSend(json& msg);

bool subscribe_events{true};

private:
bool loaded;
std::mutex load_lock;
Expand Down
13 changes: 11 additions & 2 deletions src/front-cli.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<string>("data"));
json res = backend.DoCommand(dump_command.get<string>("command"), data);
cout << res.dump(2) << endl;
while (true) {
sleep(10);
}
}
else {
cerr << "no command given" << endl;
Expand Down
Loading