Update example daemons to use the new component/trait APIs

Using AddStateDefinitionsFromJson/AddComponent instead of old and
deprecated AddCommandDefinitions.../AddStateDefinitions... methods.

BUG: 26070118, 25917541
Change-Id: I1f264c737cc19a577a0026e6d6212d63bdc480cf
Reviewed-on: https://weave-review.googlesource.com/1802
Reviewed-by: Alex Vakulenko <avakulenko@google.com>
diff --git a/examples/daemon/ledflasher/ledflasher.cc b/examples/daemon/ledflasher/ledflasher.cc
index 9e4a9e1..e63deac 100644
--- a/examples/daemon/ledflasher/ledflasher.cc
+++ b/examples/daemon/ledflasher/ledflasher.cc
@@ -14,6 +14,43 @@
 namespace {
 // Supported LED count on this device
 const size_t kLedCount = 3;
+
+const char kTraits[] = R"({
+  "_ledflasher": {
+    "commands": {
+      "_set": {
+        "minimalRole": "user",
+        "parameters": {
+          "_led": {
+            "type": "integer",
+            "minimum": 1,
+            "maximum": 3
+          },
+          "_on": { "type": "boolean" }
+        }
+      },
+      "_toggle": {
+        "minimalRole": "user",
+        "parameters": {
+          "_led": {
+            "type": "integer",
+            "minimum": 1,
+            "maximum": 3
+          }
+        }
+      }
+    },
+    "state": {
+      "_leds": {
+        "type": "array",
+        "items": { "type": "boolean" }
+      }
+    }
+  }
+})";
+
+const char kComponent[] = "lock";
+
 }  // namespace
 
 // LedFlasherHandler is a complete command handler example that shows
@@ -24,39 +61,18 @@
   void Register(weave::Device* device) {
     device_ = device;
 
-    device->AddStateDefinitionsFromJson(R"({
-      "_ledflasher": {"_leds": {"type": "array", "items": {"type": "boolean"}}}
-    })");
+    device->AddTraitDefinitionsFromJson(kTraits);
+    CHECK(device->AddComponent(kComponent, {"_ledflasher"}, nullptr));
+    UpdateLedState();
 
-    device->SetStatePropertiesFromJson(R"({
-      "_ledflasher":{"_leds": [false, false, false]}
-    })",
-                                       nullptr);
-
-    device->AddCommandDefinitionsFromJson(R"({
-      "_ledflasher": {
-        "_set":{
-          "minimalRole": "user",
-          "parameters": {
-            "_led": {"type": "integer", "minimum": 1, "maximum": 3},
-            "_on": {"type": "boolean"}
-          }
-        },
-        "_toggle":{
-          "minimalRole": "user",
-          "parameters": {
-            "_led": {"type": "integer", "minimum": 1, "maximum": 3}
-          }
-        }
-      }
-    })");
     device->AddCommandHandler(
-        "_ledflasher._toggle",
+        kComponent, "_ledflasher._toggle",
         base::Bind(&LedFlasherHandler::OnFlasherToggleCommand,
                    weak_ptr_factory_.GetWeakPtr()));
     device->AddCommandHandler(
-        "_ledflasher._set", base::Bind(&LedFlasherHandler::OnFlasherSetCommand,
-                                       weak_ptr_factory_.GetWeakPtr()));
+        kComponent, "_ledflasher._set",
+        base::Bind(&LedFlasherHandler::OnFlasherSetCommand,
+                   weak_ptr_factory_.GetWeakPtr()));
   }
 
  private:
@@ -118,7 +134,7 @@
     for (uint32_t i = 0; i < led_status_.size(); i++)
       list.AppendBoolean(led_status_[i] ? true : false);
 
-    device_->SetStateProperty("_ledflasher._leds", list, nullptr);
+    device_->SetStateProperty(kComponent, "_ledflasher._leds", list, nullptr);
   }
 
   weave::Device* device_{nullptr};
diff --git a/examples/daemon/light/light.cc b/examples/daemon/light/light.cc
index 7d156ef..611a37d 100644
--- a/examples/daemon/light/light.cc
+++ b/examples/daemon/light/light.cc
@@ -9,6 +9,145 @@
 #include <base/bind.h>
 #include <base/memory/weak_ptr.h>
 
+namespace {
+
+const char kTraits[] = R"({
+  "onOff": {
+    "commands": {
+      "setConfig": {
+        "minimalRole": "user",
+        "parameters": {
+          "state": {
+            "type": "string",
+            "enum": [ "on", "standby" ]
+          }
+        }
+      }
+    },
+    "state": {
+      "state": {
+        "type": "string",
+        "enum": [ "on", "standby" ]
+      }
+    }
+  },
+  "brightness": {
+    "commands": {
+      "setConfig": {
+        "minimalRole": "user",
+        "parameters": {
+          "brightness": {
+            "type": "integer",
+            "minimum": 0,
+            "maximum": 100
+          }
+        }
+      }
+    },
+    "state": { "brightness": { "type": "integer" } }
+  },
+  "colorXY": {
+    "commands": {
+      "setConfig": {
+        "minimalRole": "user",
+        "parameters": {
+          "colorSetting": {
+            "type": "object",
+            "required": [
+              "colorX",
+              "colorY"
+            ],
+            "properties": {
+              "colorX": {
+                "type": "number",
+                "minimum": 0.0,
+                "maximum": 1.0
+              },
+              "colorY": {
+                "type": "number",
+                "minimum": 0.0,
+                "maximum": 1.0
+              }
+            },
+            "additionalProperties": false
+          }
+        }
+      }
+    },
+    "state": {
+      "colorSetting": {
+        "properties": {
+          "colorX": {
+            "type": "number",
+            "minimum": 0.0,
+            "maximum": 1.0
+          },
+          "colorY": {
+            "type": "number",
+            "minimum": 0.0,
+            "maximum": 1.0
+          }
+        }
+      },
+      "colorCapRed": {
+        "properties": {
+          "colorX": {
+            "type": "number",
+            "minimum": 0.0,
+            "maximum": 1.0
+          },
+          "colorY": {
+            "type": "number",
+            "minimum": 0.0,
+            "maximum": 1.0
+          }
+        }
+      },
+      "colorCapGreen": {
+        "properties": {
+          "colorX": {
+            "type": "number",
+            "minimum": 0.0,
+            "maximum": 1.0
+          },
+          "colorY": {
+            "type": "number",
+            "minimum": 0.0,
+            "maximum": 1.0
+          }
+        }
+      },
+      "colorCapBlue": {
+        "properties": {
+          "colorX": {
+            "type": "number",
+            "minimum": 0.0,
+            "maximum": 1.0
+          },
+          "colorY": {
+            "type": "number",
+            "minimum": 0.0,
+            "maximum": 1.0
+          }
+        }
+      }
+    }
+  }
+})";
+
+const char kDefaultState[] = R"({
+  "colorXY": {
+    "colorSetting": {"colorX": 0, "colorY": 0},
+    "colorCapRed":  {"colorX": 0.674, "colorY": 0.322},
+    "colorCapGreen":{"colorX": 0.408, "colorY": 0.517},
+    "colorCapBlue": {"colorX": 0.168, "colorY": 0.041}
+  }
+})";
+
+const char kComponent[] = "light";
+
+}  // anonymous namespace
+
 // LightHandler is a command handler example that shows
 // how to handle commands for a Weave light.
 class LightHandler {
@@ -17,105 +156,20 @@
   void Register(weave::Device* device) {
     device_ = device;
 
-    device->AddStateDefinitionsFromJson(R"({
-      "onOff": {"state": {"type": "string", "enum": ["on", "standby"]}},
-      "brightness": {"brightness": {"type": "integer"}},
-      "colorXY": {
-        "colorSetting": {
-          "properties": {
-            "colorX": {"type": "number", "minimum": 0.0, "maximum": 1.0},
-            "colorY": {"type": "number", "minimum": 0.0, "maximum": 1.0}
-          }
-        },
-        "colorCapRed": {
-          "properties": {
-            "colorX": {"type": "number", "minimum": 0.0, "maximum": 1.0},
-            "colorY": {"type": "number", "minimum": 0.0, "maximum": 1.0}
-          }
-        },
-        "colorCapGreen": {
-          "properties": {
-            "colorX": {"type": "number", "minimum": 0.0, "maximum": 1.0},
-            "colorY": {"type": "number", "minimum": 0.0, "maximum": 1.0}
-          }
-        },
-        "colorCapBlue": {
-          "properties": {
-            "colorX": {"type": "number", "minimum": 0.0, "maximum": 1.0},
-            "colorY": {"type": "number", "minimum": 0.0, "maximum": 1.0}
-          }
-        }
-      }
-    })");
+    device->AddTraitDefinitionsFromJson(kTraits);
+    CHECK(device->AddComponent(kComponent, {"onOff", "brightness", "colorXY"},
+                               nullptr));
+    CHECK(device->SetStatePropertiesFromJson(kComponent, kDefaultState,
+                                             nullptr));
+    UpdateLightState();
 
-    device->SetStatePropertiesFromJson(R"({
-      "onOff":{"state": "standby"},
-      "brightness":{"brightness": 0},
-      "colorXY": {
-        "colorSetting": {"colorX": 0, "colorY": 0},
-        "colorCapRed":  {"colorX": 0.674, "colorY": 0.322},
-        "colorCapGreen":{"colorX": 0.408, "colorY": 0.517},
-        "colorCapBlue": {"colorX": 0.168, "colorY": 0.041}
-      }
-    })",
-                                       nullptr);
-
-    device->AddCommandDefinitionsFromJson(R"({
-      "onOff": {
-        "setConfig":{
-          "minimalRole": "user",
-          "parameters": {
-            "state": {"type": "string", "enum": ["on", "standby"]}
-          }
-        }
-      },
-      "brightness": {
-        "setConfig":{
-          "minimalRole": "user",
-          "parameters": {
-            "brightness": {
-              "type": "integer",
-              "minimum": 0,
-              "maximum": 100
-            }
-          }
-        }
-      },
-      "colorXY": {
-        "setConfig": {
-          "minimalRole": "user",
-          "parameters": {
-            "colorSetting": {
-              "type": "object",
-              "required": [
-                "colorX",
-                "colorY"
-              ],
-              "properties": {
-                "colorX": {
-                  "type": "number",
-                  "minimum": 0.0,
-                  "maximum": 1.0
-                },
-                "colorY": {
-                  "type": "number",
-                  "minimum": 0.0,
-                  "maximum": 1.0
-                }
-              },
-              "additionalProperties": false
-            }
-          }
-        }
-      }
-    })");
-    device->AddCommandHandler("onOff.setConfig",
+    device->AddCommandHandler(kComponent, "onOff.setConfig",
                               base::Bind(&LightHandler::OnOnOffSetConfig,
                                          weak_ptr_factory_.GetWeakPtr()));
-    device->AddCommandHandler("brightness.setConfig",
+    device->AddCommandHandler(kComponent, "brightness.setConfig",
                               base::Bind(&LightHandler::OnBrightnessSetConfig,
                                          weak_ptr_factory_.GetWeakPtr()));
-    device->AddCommandHandler("colorXY.setConfig",
+    device->AddCommandHandler(kComponent, "colorXY.setConfig",
                               base::Bind(&LightHandler::OnColorXYSetConfig,
                                          weak_ptr_factory_.GetWeakPtr()));
   }
@@ -213,16 +267,15 @@
     std::unique_ptr<base::DictionaryValue> colorXY(new base::DictionaryValue());
     colorXY->SetDouble("colorX", color_X_);
     colorXY->SetDouble("colorY", color_Y_);
-    state.Set("colorXY.colorSetting", colorXY.get());
-    device_->SetStateProperties(state, nullptr);
-    colorXY.release();
+    state.Set("colorXY.colorSetting", colorXY.release());
+    device_->SetStateProperties(kComponent, state, nullptr);
   }
 
   weave::Device* device_{nullptr};
 
   // Simulate the state of the light.
-  bool light_status_;
-  int32_t brightness_state_;
+  bool light_status_{false};
+  int32_t brightness_state_{0};
   double color_X_{0.0};
   double color_Y_{0.0};
   base::WeakPtrFactory<LightHandler> weak_ptr_factory_{this};
diff --git a/examples/daemon/lock/lock.cc b/examples/daemon/lock/lock.cc
index 10bea00..a4bd213 100644
--- a/examples/daemon/lock/lock.cc
+++ b/examples/daemon/lock/lock.cc
@@ -25,6 +25,39 @@
     : EnumToStringMap(lockstate::kLockMapMethod) {}
 }  // namespace weave
 
+namespace {
+
+const char kTraits[] = R"({
+  "lock": {
+    "commands": {
+      "setConfig": {
+        "minimalRole": "user",
+        "parameters": {
+          "lockedState": {
+            "type": "string",
+            "enum": [ "locked", "unlocked" ]
+          }
+        }
+      }
+    },
+    "state": {
+      "lockedState": {
+        "type": "string",
+        "enum": [ "locked", "unlocked", "partiallyLocked" ]
+      },
+      "isLockingSupported": { "type": "boolean" }
+    }
+  }
+})";
+
+const char kDefaultState[] = R"({
+  "lock":{"isLockingSupported": true}
+})";
+
+const char kComponent[] = "lock";
+
+}  // anonymous namespace
+
 // LockHandler is a command handler example that shows
 // how to handle commands for a Weave lock.
 class LockHandler {
@@ -33,35 +66,13 @@
   void Register(weave::Device* device) {
     device_ = device;
 
-    device->AddStateDefinitionsFromJson(R"({
-      "lock": {
-        "lockedState": {
-          "type": "string",
-          "enum": ["locked", "unlocked", "partiallyLocked"]
-        },
-        "isLockingSupported": {"type": "boolean"}
-      }
-    })");
+    device->AddTraitDefinitionsFromJson(kTraits);
+    CHECK(device->AddComponent(kComponent, {"lock"}, nullptr));
+    CHECK(device->SetStatePropertiesFromJson(kComponent, kDefaultState,
+                                             nullptr));
+    UpdateLockState();
 
-    device->SetStatePropertiesFromJson(R"({
-      "lock":{
-        "lockedState": "locked",
-        "isLockingSupported": true
-      }
-    })",
-                                       nullptr);
-
-    device->AddCommandDefinitionsFromJson(R"({
-      "lock": {
-        "setConfig":{
-          "minimalRole": "user",
-          "parameters": {
-            "lockedState": {"type": "string", "enum":["locked", "unlocked"]}
-          }
-        }
-      }
-    })");
-    device->AddCommandHandler("lock.setConfig",
+    device->AddCommandHandler(kComponent, "lock.setConfig",
                               base::Bind(&LockHandler::OnLockSetConfig,
                                          weak_ptr_factory_.GetWeakPtr()));
   }
@@ -104,10 +115,9 @@
   }
 
   void UpdateLockState() {
-    base::DictionaryValue state;
     std::string updated_state = weave::EnumToString(lock_state_);
-    state.SetString("lock.lockedState", updated_state);
-    device_->SetStateProperties(state, nullptr);
+    device_->SetStateProperty(kComponent, "lock.lockedState",
+                              base::StringValue{updated_state}, nullptr);
   }
 
   weave::Device* device_{nullptr};
diff --git a/examples/daemon/sample/sample.cc b/examples/daemon/sample/sample.cc
index 811e9fb..97cef61 100644
--- a/examples/daemon/sample/sample.cc
+++ b/examples/daemon/sample/sample.cc
@@ -10,6 +10,41 @@
 #include <base/bind.h>
 #include <base/memory/weak_ptr.h>
 
+namespace {
+
+const char kTraits[] = R"({
+  "_sample": {
+    "commands": {
+      "_hello": {
+        "minimalRole": "user",
+        "parameters": {
+          "_name": { "type": "string" }
+        }
+      },
+      "_ping": {
+        "minimalRole": "user"
+      },
+      "_countdown": {
+        "minimalRole": "user",
+        "parameters": {
+          "_seconds": {
+            "type": "integer",
+            "minimum": 1,
+            "maximum": 25
+          }
+        }
+      }
+    },
+    "state": {
+      "_ping_count": { "type": "integer" }
+    }
+  }
+})";
+
+const char kComponent[] = "sample";
+
+}  // anonymous namespace
+
 // SampleHandler is a command handler example.
 // It implements the following commands:
 // - _hello: handle a command with an argument and set its results.
@@ -22,42 +57,18 @@
   void Register(weave::Device* device) {
     device_ = device;
 
-    device->AddCommandDefinitionsFromJson(R"({
-      "_sample": {
-        "_hello": {
-          "minimalRole": "user",
-          "parameters": {
-            "_name": {"type": "string"}
-          }
-        },
-        "_ping": {
-          "minimalRole": "user"
-        },
-        "_countdown": {
-          "minimalRole": "user",
-          "parameters": {
-            "_seconds": {"type": "integer", "minimum": 1, "maximum": 25}
-          }
-        }
-      }
-    })");
+    device->AddTraitDefinitionsFromJson(kTraits);
+    CHECK(device->AddComponent(kComponent, {"_sample"}, nullptr));
+    CHECK(device->SetStatePropertiesFromJson(
+        kComponent, R"({"_sample": {"_ping_count": 0}})", nullptr));
 
-    device->AddStateDefinitionsFromJson(R"({
-      "_sample": {"_ping_count": {"type": "integer"}}
-    })");
-
-    device->SetStatePropertiesFromJson(R"({
-      "_sample": {"_ping_count": 0}
-    })",
-                                       nullptr);
-
-    device->AddCommandHandler("_sample._hello",
+    device->AddCommandHandler(kComponent, "_sample._hello",
                               base::Bind(&SampleHandler::OnHelloCommand,
                                          weak_ptr_factory_.GetWeakPtr()));
-    device->AddCommandHandler("_sample._ping",
+    device->AddCommandHandler(kComponent, "_sample._ping",
                               base::Bind(&SampleHandler::OnPingCommand,
                                          weak_ptr_factory_.GetWeakPtr()));
-    device->AddCommandHandler("_sample._countdown",
+    device->AddCommandHandler(kComponent, "_sample._countdown",
                               base::Bind(&SampleHandler::OnCountdownCommand,
                                          weak_ptr_factory_.GetWeakPtr()));
   }
@@ -91,9 +102,8 @@
       return;
     LOG(INFO) << "received command: " << cmd->GetName();
 
-    base::DictionaryValue state;
-    state.SetInteger("_sample._ping_count", ++ping_count_);
-    device_->SetStateProperties(state, nullptr);
+    device_->SetStateProperty(kComponent, "_sample._ping_count",
+                              base::FundamentalValue{++ping_count_}, nullptr);
     LOG(INFO) << "New state: " << device_->GetState();
 
     base::DictionaryValue result;
diff --git a/examples/daemon/speaker/speaker.cc b/examples/daemon/speaker/speaker.cc
index cd7d62f..8b3e41b 100644
--- a/examples/daemon/speaker/speaker.cc
+++ b/examples/daemon/speaker/speaker.cc
@@ -9,6 +9,51 @@
 #include <base/bind.h>
 #include <base/memory/weak_ptr.h>
 
+namespace {
+
+const char kTraits[] = R"({
+  "onOff": {
+    "commands": {
+      "setConfig": {
+        "minimalRole": "user",
+        "parameters": {
+          "state": {
+            "type": "string",
+            "enum": [ "on", "standby" ]
+          }
+        }
+      }
+    },
+    "state": {
+      "type": "string",
+      "enum": [ "on", "standby" ]
+    }
+  },
+  "volume": {
+    "commands": {
+      "setConfig": {
+        "minimalRole": "user",
+        "parameters": {
+          "volume": {
+            "type": "integer",
+            "minimum": 0,
+            "maximum": 100
+          },
+          "isMuted": { "type": "boolean" }
+        }
+      }
+    },
+    "state": {
+      "isMuted": { "type": "boolean" },
+      "volume": { "type": "integer" }
+    }
+  }
+})";
+
+const char kComponent[] = "speaker";
+
+}  // anonymous namespace
+
 // SpeakerHandler is a command handler example that shows
 // how to handle commands for a Weave speaker.
 class SpeakerHandler {
@@ -17,50 +62,14 @@
   void Register(weave::Device* device) {
     device_ = device;
 
-    device->AddStateDefinitionsFromJson(R"({
-      "onOff": {"state": {"type": "string", "enum": ["on", "standby"]}},
-      "volume": {
-        "volume": {"type": "integer"},
-        "isMuted": {"type": "boolean"}
-      }
-    })");
+    device->AddTraitDefinitionsFromJson(kTraits);
+    CHECK(device->AddComponent(kComponent, {"onOff", "volume"}, nullptr));
+    UpdateSpeakerState();
 
-    device->SetStatePropertiesFromJson(R"({
-      "onOff":{"state": "standby"},
-      "volume":{
-        "volume": 100,
-        "isMuted": false
-      }
-    })",
-                                       nullptr);
-
-    device->AddCommandDefinitionsFromJson(R"({
-      "onOff": {
-        "setConfig":{
-          "minimalRole": "user",
-          "parameters": {
-            "state": {"type": "string", "enum": ["on", "standby"]}
-          }
-        }
-      },
-      "volume": {
-        "setConfig":{
-          "minimalRole": "user",
-          "parameters": {
-            "volume": {
-              "type": "integer",
-              "minimum": 0,
-              "maximum": 100
-            },
-            "isMuted": {"type": "boolean"}
-          }
-        }
-      }
-    })");
-    device->AddCommandHandler("onOff.setConfig",
+    device->AddCommandHandler(kComponent, "onOff.setConfig",
                               base::Bind(&SpeakerHandler::OnOnOffSetConfig,
                                          weak_ptr_factory_.GetWeakPtr()));
-    device->AddCommandHandler("volume.setConfig",
+    device->AddCommandHandler(kComponent, "volume.setConfig",
                               base::Bind(&SpeakerHandler::OnVolumeSetConfig,
                                          weak_ptr_factory_.GetWeakPtr()));
   }
@@ -132,7 +141,7 @@
     state.SetString("onOff.state", speaker_status_ ? "on" : "standby");
     state.SetBoolean("volume.isMuted", isMuted_status_);
     state.SetInteger("volume.volume", volume_value_);
-    device_->SetStateProperties(state, nullptr);
+    device_->SetStateProperties(kComponent, state, nullptr);
   }
 
   weave::Device* device_{nullptr};