examples/ubuntu: add simple command handler example
Bug: 24307362
Change-Id: Ia76f7eb261b78772a116caf5b9e4f05fc7e4c970
Reviewed-on: https://weave-review.googlesource.com/1120
Reviewed-by: Vitaly Buka <vitalybuka@google.com>
diff --git a/libweave/examples/ubuntu/main.cc b/libweave/examples/ubuntu/main.cc
index c6450b4..a9eee98 100644
--- a/libweave/examples/ubuntu/main.cc
+++ b/libweave/examples/ubuntu/main.cc
@@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <base/bind.h>
+#include <base/values.h>
#include <weave/device.h>
#include "libweave/examples/ubuntu/avahi_client.h"
@@ -19,8 +21,30 @@
<< "\t-h,--help Show this help message\n"
<< "\t-b,--bootstrapping Force WiFi bootstrapping\n";
}
+
+class CommandHandler {
+ public:
+ explicit CommandHandler(weave::Device* device) {
+ device->GetCommands()->AddOnCommandAddedCallback(
+ base::Bind(&CommandHandler::OnNewCommand, weak_ptr_factory_.GetWeakPtr()));
+ }
+
+ private:
+ void OnNewCommand(weave::Command* cmd) {
+ LOG(INFO) << "received command: " << cmd->GetName();
+ if (cmd->GetName() == "base.identify") {
+ cmd->SetProgress(base::DictionaryValue{}, nullptr);
+ LOG(INFO) << "base.identify command: completed";
+ cmd->Done();
+ } else {
+ LOG(INFO) << "unimplemented command: ignored";
+ }
+ }
+ base::WeakPtrFactory<CommandHandler> weak_ptr_factory_{this};
+};
}
+
int main(int argc, char** argv) {
bool force_bootstrapping = false;
for (int i = 1; i < argc; ++i) {
@@ -52,6 +76,7 @@
&http_server,
weave::examples::NetworkImpl::HasWifiCapability() ? &network : nullptr,
&bluetooth);
+ CommandHandler handler(device.get());
task_runner.Run();