Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 1 | // 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 | |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 5 | #include "examples/daemon/common/daemon.h" |
| 6 | |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 7 | #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 | |
| 13 | namespace weave { |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 14 | namespace lockstate { |
| 15 | enum class LockState { kUnlocked, kLocked, kPartiallyLocked }; |
| 16 | |
| 17 | const weave::EnumToStringMap<LockState>::Map kLockMapMethod[] = { |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 18 | {LockState::kLocked, "locked"}, |
| 19 | {LockState::kUnlocked, "unlocked"}, |
| 20 | {LockState::kPartiallyLocked, "partiallyLocked"}}; |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 21 | } // namespace lockstate |
| 22 | |
| 23 | template <> |
| 24 | EnumToStringMap<lockstate::LockState>::EnumToStringMap() |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 25 | : EnumToStringMap(lockstate::kLockMapMethod) {} |
| 26 | } // namespace weave |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 27 | |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame^] | 28 | namespace { |
| 29 | |
| 30 | const char kTraits[] = R"({ |
| 31 | "lock": { |
| 32 | "commands": { |
| 33 | "setConfig": { |
| 34 | "minimalRole": "user", |
| 35 | "parameters": { |
| 36 | "lockedState": { |
| 37 | "type": "string", |
| 38 | "enum": [ "locked", "unlocked" ] |
| 39 | } |
| 40 | } |
| 41 | } |
| 42 | }, |
| 43 | "state": { |
| 44 | "lockedState": { |
| 45 | "type": "string", |
| 46 | "enum": [ "locked", "unlocked", "partiallyLocked" ] |
| 47 | }, |
| 48 | "isLockingSupported": { "type": "boolean" } |
| 49 | } |
| 50 | } |
| 51 | })"; |
| 52 | |
| 53 | const char kDefaultState[] = R"({ |
| 54 | "lock":{"isLockingSupported": true} |
| 55 | })"; |
| 56 | |
| 57 | const char kComponent[] = "lock"; |
| 58 | |
| 59 | } // anonymous namespace |
| 60 | |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 61 | // LockHandler is a command handler example that shows |
| 62 | // how to handle commands for a Weave lock. |
| 63 | class LockHandler { |
| 64 | public: |
| 65 | LockHandler() = default; |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 66 | void Register(weave::Device* device) { |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 67 | device_ = device; |
| 68 | |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame^] | 69 | device->AddTraitDefinitionsFromJson(kTraits); |
| 70 | CHECK(device->AddComponent(kComponent, {"lock"}, nullptr)); |
| 71 | CHECK(device->SetStatePropertiesFromJson(kComponent, kDefaultState, |
| 72 | nullptr)); |
| 73 | UpdateLockState(); |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 74 | |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame^] | 75 | device->AddCommandHandler(kComponent, "lock.setConfig", |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 76 | base::Bind(&LockHandler::OnLockSetConfig, |
| 77 | weak_ptr_factory_.GetWeakPtr())); |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | private: |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 81 | void OnLockSetConfig(const std::weak_ptr<weave::Command>& command) { |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 82 | auto cmd = command.lock(); |
| 83 | if (!cmd) |
| 84 | return; |
| 85 | LOG(INFO) << "received command: " << cmd->GetName(); |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 86 | const auto& params = cmd->GetParameters(); |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 87 | std::string requested_state; |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 88 | if (params.GetString("lockedState", &requested_state)) { |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 89 | LOG(INFO) << cmd->GetName() << " state: " << requested_state; |
| 90 | |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 91 | weave::lockstate::LockState new_lock_status; |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 92 | |
| 93 | if (!weave::StringToEnum(requested_state, &new_lock_status)) { |
| 94 | // Invalid lock state was specified. |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 95 | weave::ErrorPtr error; |
| 96 | weave::Error::AddTo(&error, FROM_HERE, "example", |
| 97 | "invalid_parameter_value", "Invalid parameters"); |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 98 | cmd->Abort(error.get(), nullptr); |
| 99 | return; |
| 100 | } |
| 101 | |
| 102 | if (new_lock_status != lock_state_) { |
| 103 | lock_state_ = new_lock_status; |
| 104 | |
| 105 | LOG(INFO) << "Lock is now: " << requested_state; |
| 106 | UpdateLockState(); |
| 107 | } |
| 108 | cmd->Complete({}, nullptr); |
| 109 | return; |
| 110 | } |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 111 | weave::ErrorPtr error; |
| 112 | weave::Error::AddTo(&error, FROM_HERE, "example", "invalid_parameter_value", |
| 113 | "Invalid parameters"); |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 114 | cmd->Abort(error.get(), nullptr); |
| 115 | } |
| 116 | |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 117 | void UpdateLockState() { |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 118 | std::string updated_state = weave::EnumToString(lock_state_); |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame^] | 119 | device_->SetStateProperty(kComponent, "lock.lockedState", |
| 120 | base::StringValue{updated_state}, nullptr); |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 123 | weave::Device* device_{nullptr}; |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 124 | |
| 125 | // Simulate the state of the light. |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 126 | weave::lockstate::LockState lock_state_{weave::lockstate::LockState::kLocked}; |
Paul Westbrook | c18c7cf | 2015-10-27 06:38:59 -0700 | [diff] [blame] | 127 | base::WeakPtrFactory<LockHandler> weak_ptr_factory_{this}; |
| 128 | }; |
| 129 | |
Johan Euphrosine | 3fb474e | 2015-10-29 15:23:53 -0700 | [diff] [blame] | 130 | int main(int argc, char** argv) { |
| 131 | Daemon::Options opts; |
| 132 | if (!opts.Parse(argc, argv)) { |
| 133 | Daemon::Options::ShowUsage(argv[0]); |
| 134 | return 1; |
| 135 | } |
| 136 | Daemon daemon{opts}; |
| 137 | LockHandler handler; |
| 138 | handler.Register(daemon.GetDevice()); |
| 139 | daemon.Run(); |
| 140 | return 0; |
| 141 | } |