blob: e1ca2d9b5f1a823538c7ac6f769efda5ffd951c0 [file] [log] [blame]
Paul Westbrookc18c7cf2015-10-27 06:38:59 -07001// Copyright 2015 The Weave Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Bukaca54c122015-11-05 23:06:09 -08005#include "examples/daemon/common/daemon.h"
6
Paul Westbrookc18c7cf2015-10-27 06:38:59 -07007#include <weave/device.h>
8#include <weave/enum_to_string.h>
9
10#include <base/bind.h>
11#include <base/memory/weak_ptr.h>
12
13namespace weave {
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070014namespace lockstate {
15enum class LockState { kUnlocked, kLocked, kPartiallyLocked };
16
17const weave::EnumToStringMap<LockState>::Map kLockMapMethod[] = {
Vitaly Bukaca54c122015-11-05 23:06:09 -080018 {LockState::kLocked, "locked"},
19 {LockState::kUnlocked, "unlocked"},
20 {LockState::kPartiallyLocked, "partiallyLocked"}};
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070021} // namespace lockstate
22
23template <>
24EnumToStringMap<lockstate::LockState>::EnumToStringMap()
Vitaly Bukaca54c122015-11-05 23:06:09 -080025 : EnumToStringMap(lockstate::kLockMapMethod) {}
26} // namespace weave
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070027
28// LockHandler is a command handler example that shows
29// how to handle commands for a Weave lock.
30class LockHandler {
31 public:
32 LockHandler() = default;
Vitaly Bukaca54c122015-11-05 23:06:09 -080033 void Register(weave::Device* device) {
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070034 device_ = device;
35
36 device->AddStateDefinitionsFromJson(R"({
Paul Westbrook1bc421c2015-11-01 14:59:35 -080037 "lock": {"lockedState": ["locked", "unlocked", "partiallyLocked"]}
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070038 })");
39
40 device->SetStatePropertiesFromJson(R"({
Paul Westbrook1bc421c2015-11-01 14:59:35 -080041 "lock":{"lockedState": "locked"}
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070042 })",
43 nullptr);
44
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070045 device->AddCommandDefinitionsFromJson(R"({
Paul Westbrook1bc421c2015-11-01 14:59:35 -080046 "lock": {
47 "setConfig":{
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070048 "parameters": {
Paul Westbrook1bc421c2015-11-01 14:59:35 -080049 "lockedState": ["locked", "unlocked"]
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070050 }
51 }
52 }
53 })");
Vitaly Bukaca54c122015-11-05 23:06:09 -080054 device->AddCommandHandler("lock.setConfig",
55 base::Bind(&LockHandler::OnLockSetConfig,
56 weak_ptr_factory_.GetWeakPtr()));
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070057 }
58
59 private:
Vitaly Bukaca54c122015-11-05 23:06:09 -080060 void OnLockSetConfig(const std::weak_ptr<weave::Command>& command) {
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070061 auto cmd = command.lock();
62 if (!cmd)
63 return;
64 LOG(INFO) << "received command: " << cmd->GetName();
65 std::string requested_state;
Paul Westbrook1bc421c2015-11-01 14:59:35 -080066 if (cmd->GetParameters()->GetString("lockedState", &requested_state)) {
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070067 LOG(INFO) << cmd->GetName() << " state: " << requested_state;
68
Vitaly Bukaca54c122015-11-05 23:06:09 -080069 weave::lockstate::LockState new_lock_status;
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070070
71 if (!weave::StringToEnum(requested_state, &new_lock_status)) {
72 // Invalid lock state was specified.
Vitaly Bukaca54c122015-11-05 23:06:09 -080073 weave::ErrorPtr error;
74 weave::Error::AddTo(&error, FROM_HERE, "example",
75 "invalid_parameter_value", "Invalid parameters");
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070076 cmd->Abort(error.get(), nullptr);
77 return;
78 }
79
80 if (new_lock_status != lock_state_) {
81 lock_state_ = new_lock_status;
82
83 LOG(INFO) << "Lock is now: " << requested_state;
84 UpdateLockState();
85 }
86 cmd->Complete({}, nullptr);
87 return;
88 }
Vitaly Bukaca54c122015-11-05 23:06:09 -080089 weave::ErrorPtr error;
90 weave::Error::AddTo(&error, FROM_HERE, "example", "invalid_parameter_value",
91 "Invalid parameters");
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070092 cmd->Abort(error.get(), nullptr);
93 }
94
Vitaly Bukaca54c122015-11-05 23:06:09 -080095 void UpdateLockState() {
Paul Westbrookc18c7cf2015-10-27 06:38:59 -070096 base::DictionaryValue state;
97 std::string updated_state = weave::EnumToString(lock_state_);
98 state.SetString("lock.lockedState", updated_state);
99 device_->SetStateProperties(state, nullptr);
100 }
101
Vitaly Bukaca54c122015-11-05 23:06:09 -0800102 weave::Device* device_{nullptr};
Paul Westbrookc18c7cf2015-10-27 06:38:59 -0700103
104 // Simulate the state of the light.
Vitaly Bukaca54c122015-11-05 23:06:09 -0800105 weave::lockstate::LockState lock_state_{weave::lockstate::LockState::kLocked};
Paul Westbrookc18c7cf2015-10-27 06:38:59 -0700106 base::WeakPtrFactory<LockHandler> weak_ptr_factory_{this};
107};
108
Vitaly Bukaca54c122015-11-05 23:06:09 -0800109int main(int argc, char** argv) {
110 Daemon::Options opts;
111 if (!opts.Parse(argc, argv)) {
112 Daemon::Options::ShowUsage(argv[0]);
113 return 1;
114 }
115 Daemon daemon{opts};
116 LockHandler handler;
117 handler.Register(daemon.GetDevice());
118 daemon.Run();
119 return 0;
120}