Skip to content

Commit

Permalink
Merge pull request #485 from silicon-heaven/shvcall-emit
Browse files Browse the repository at this point in the history
shvcall: Add support for emitting events
  • Loading branch information
fvacek authored Sep 2, 2024
2 parents 8a1b3cf + 4068aca commit f8f697f
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions examples/cli/shvcall/src/appclioptions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ AppCliOptions::AppCliOptions()
addOption("isChainPackOutput").setType(shv::chainpack::RpcValue::Type::Bool).setNames("-x", "--chainpack-output").setComment("ChainPack output");
addOption("shouldSubscribe").setType(shv::chainpack::RpcValue::Type::Bool).setNames("--subscribe").setComment("Whether shvcall should subscribe to a path and print events").setDefaultValue(false);
addOption("subscribeFormat").setType(shv::chainpack::RpcValue::Type::String).setNames("--subscribe-format").setComment("Set the format of events. Available placeholders: {TIME}, {PATH}, {METHOD}, {VALUE}. This option is only useful with --subscribe.").setDefaultValue(R"({"timestamp": "{TIME}", "path": "{PATH}", "method": "{METHOD}", "param": {VALUE}})");

addOption("shouldEmit").setType(shv::chainpack::RpcValue::Type::Bool).setNames("--emit").setComment("Whether shvcall should emit a signal and quit").setDefaultValue(false);
}
6 changes: 4 additions & 2 deletions examples/cli/shvcall/src/appclioptions.h
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#pragma once

#include <shv/iotqt/rpc/clientappclioptions.h>
#include <shv/iotqt/rpc/deviceappclioptions.h>

class AppCliOptions : public shv::iotqt::rpc::ClientAppCliOptions
class AppCliOptions : public shv::iotqt::rpc::DeviceAppCliOptions
{
using Super = shv::iotqt::rpc::ClientAppCliOptions;

Expand All @@ -16,4 +16,6 @@ class AppCliOptions : public shv::iotqt::rpc::ClientAppCliOptions
CLIOPTION_GETTER_SETTER(bool, isC, setC, ponOutput)
CLIOPTION_GETTER_SETTER(bool, s, setS, houldSubscribe)
CLIOPTION_GETTER_SETTER(std::string, s, setS, ubscribeFormat)

CLIOPTION_GETTER_SETTER(bool, s, setS, houldEmit)
};
8 changes: 7 additions & 1 deletion examples/cli/shvcall/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Application::Application(int &argc, char **argv, AppCliOptions *cli_opts)
: Super(argc, argv)
, m_cliOptions(cli_opts)
{
m_rpcConnection = new si::rpc::ClientConnection(this);
m_rpcConnection = new si::rpc::DeviceConnection(this);

m_rpcConnection->setCliOptions(cli_opts);

Expand Down Expand Up @@ -87,6 +87,12 @@ void Application::onShvStateChanged()
return;
}

if (m_cliOptions->shouldEmit()) {
m_rpcConnection->sendShvSignal(m_cliOptions->path(), m_cliOptions->method(), m_cliOptions->params());
m_rpcConnection->close();
return;
}

try {
params = m_cliOptions->params().empty() ? cp::RpcValue() : cp::RpcValue::fromCpon(m_cliOptions->params());
} catch (std::exception&) {
Expand Down
4 changes: 2 additions & 2 deletions examples/cli/shvcall/src/application.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <QCoreApplication>
#include <shv/iotqt/rpc/clientconnection.h>
#include <shv/iotqt/rpc/deviceconnection.h>

class AppCliOptions;

Expand All @@ -17,7 +17,7 @@ class Application : public QCoreApplication
void onShvStateChanged();

AppCliOptions *m_cliOptions;
shv::iotqt::rpc::ClientConnection *m_rpcConnection;
shv::iotqt::rpc::DeviceConnection *m_rpcConnection;
int m_subscriptionRpcId;
int m_status = EXIT_SUCCESS;
};

0 comments on commit f8f697f

Please sign in to comment.