Add vendor state into example

Change-Id: Ib373e8313bfacc4ade2ef2e924f433252fc5096e
Reviewed-on: https://weave-review.googlesource.com/1212
Reviewed-by: Vitaly Buka <vitalybuka@google.com>
diff --git a/libweave/examples/ubuntu/README b/libweave/examples/ubuntu/README
index 063d8f4..af67036 100644
--- a/libweave/examples/ubuntu/README
+++ b/libweave/examples/ubuntu/README
@@ -84,7 +84,8 @@
         Enter request body:
         {
           "deviceId": "0f8a5ff5-1ef0-ec39-f9d8-66d1caeb9e3d",
-          "name": "_greeter._greet"
+          "name": "_greeter._greet",
+          "parameters": { "_name": "cloud user" }
         }
    "Send the request", you command will be "queued" as its "state"
 
diff --git a/libweave/examples/ubuntu/file_config_store.cc b/libweave/examples/ubuntu/file_config_store.cc
index 8853bcf..66dd80f 100644
--- a/libweave/examples/ubuntu/file_config_store.cc
+++ b/libweave/examples/ubuntu/file_config_store.cc
@@ -17,6 +17,7 @@
 
 const char kSettingsDir[] = "/var/lib/weave/";
 const char kSettingsPath[] = "/var/lib/weave/weave_settings.json";
+const char kCategory[] = "example";
 
 FileConfigStore::FileConfigStore(bool disable_security)
     : disable_security_{disable_security} {}
@@ -60,7 +61,7 @@
 }
 
 std::map<std::string, std::string> FileConfigStore::LoadCommandDefs() {
-  return {{"example", R"({
+  return {{kCategory, R"({
     "base": {
       "updateBaseConfiguration": {},
       "identify": {},
@@ -77,11 +78,11 @@
 }
 
 std::map<std::string, std::string> FileConfigStore::LoadStateDefs() {
-  return {};
+  return {{kCategory, R"({"_greeter": {"_greetings_counter":"integer"}})"}};
 }
 
 std::vector<std::string> FileConfigStore::LoadStateDefaults() {
-  return {};
+  return {R"({"_greeter": {"_greetings_counter": 0}})"};
 }
 
 }  // namespace examples
diff --git a/libweave/examples/ubuntu/main.cc b/libweave/examples/ubuntu/main.cc
index 85fbe3b..11c3fac 100644
--- a/libweave/examples/ubuntu/main.cc
+++ b/libweave/examples/ubuntu/main.cc
@@ -28,7 +28,7 @@
 
 class CommandHandler {
  public:
-  explicit CommandHandler(weave::Device* device) {
+  explicit CommandHandler(weave::Device* device) : device_{device} {
     device->GetCommands()->AddOnCommandAddedCallback(base::Bind(
         &CommandHandler::OnNewCommand, weak_ptr_factory_.GetWeakPtr()));
   }
@@ -38,22 +38,34 @@
     LOG(INFO) << "received command: " << cmd->GetName();
     if (cmd->GetName() == "_greeter._greet") {
       std::string name;
-      cmd->GetParameters()->GetString("_name", &name);
-      if (name.empty()) {
-        name = cmd->GetOrigin() == weave::CommandOrigin::kCloud ? "cloud user"
-                                                                : "local user";
-      }
-      LOG(INFO) << "vendor _greeter._greet command: in progress";
+      if (!cmd->GetParameters()->GetString("_name", &name))
+        name = "anonymous";
+
+      LOG(INFO) << cmd->GetName() << " command in progress";
       cmd->SetProgress(base::DictionaryValue{}, nullptr);
+
       base::DictionaryValue result;
       result.SetString("_greeting", "Hello " + name);
       cmd->SetResults(result, nullptr);
-      LOG(INFO) << "vendor _greeter._greet command: finished";
+      LOG(INFO) << cmd->GetName() << " command finished: " << result;
+
+      base::DictionaryValue state;
+      state.SetIntegerWithoutPathExpansion("_greeter._greetings_counter",
+                                           ++counter_);
+      device_->GetState()->SetProperties(state, nullptr);
+
+      LOG(INFO) << "New state: "
+                << *device_->GetState()->GetStateValuesAsJson();
+
       cmd->Done();
     } else {
       LOG(INFO) << "unimplemented command: ignored";
     }
   }
+
+  weave::Device* device_{nullptr};
+  int counter_{0};
+
   base::WeakPtrFactory<CommandHandler> weak_ptr_factory_{this};
 };
 }