Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [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 | |
| 5 | #include "examples/daemon/common/daemon.h" |
| 6 | |
| 7 | #include <weave/device.h> |
| 8 | |
| 9 | #include <base/bind.h> |
| 10 | #include <base/memory/weak_ptr.h> |
| 11 | |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame] | 12 | namespace { |
| 13 | |
| 14 | const char kTraits[] = R"({ |
| 15 | "onOff": { |
| 16 | "commands": { |
| 17 | "setConfig": { |
| 18 | "minimalRole": "user", |
| 19 | "parameters": { |
| 20 | "state": { |
| 21 | "type": "string", |
Johan Euphrosine | f7bfb6a | 2016-02-25 17:34:04 -0800 | [diff] [blame] | 22 | "enum": [ "on", "off" ] |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame] | 23 | } |
| 24 | } |
| 25 | } |
| 26 | }, |
| 27 | "state": { |
Alex Vakulenko | 8d0cfef | 2015-12-15 18:40:05 -0800 | [diff] [blame] | 28 | "state": { |
| 29 | "type": "string", |
Johan Euphrosine | f7bfb6a | 2016-02-25 17:34:04 -0800 | [diff] [blame] | 30 | "enum": [ "on", "off" ], |
Alex Vakulenko | 8d0cfef | 2015-12-15 18:40:05 -0800 | [diff] [blame] | 31 | "isRequired": true |
| 32 | } |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame] | 33 | } |
| 34 | }, |
| 35 | "volume": { |
| 36 | "commands": { |
| 37 | "setConfig": { |
| 38 | "minimalRole": "user", |
| 39 | "parameters": { |
| 40 | "volume": { |
| 41 | "type": "integer", |
| 42 | "minimum": 0, |
| 43 | "maximum": 100 |
| 44 | }, |
| 45 | "isMuted": { "type": "boolean" } |
| 46 | } |
| 47 | } |
| 48 | }, |
| 49 | "state": { |
Alex Vakulenko | 8d0cfef | 2015-12-15 18:40:05 -0800 | [diff] [blame] | 50 | "isMuted": { |
| 51 | "type": "boolean", |
| 52 | "isRequired": true |
| 53 | }, |
| 54 | "volume": { |
| 55 | "type": "integer", |
| 56 | "minimum": 0, |
| 57 | "maximum": 100, |
| 58 | "isRequired": true |
| 59 | } |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | })"; |
| 63 | |
| 64 | const char kComponent[] = "speaker"; |
| 65 | |
| 66 | } // anonymous namespace |
| 67 | |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 68 | // SpeakerHandler is a command handler example that shows |
| 69 | // how to handle commands for a Weave speaker. |
| 70 | class SpeakerHandler { |
| 71 | public: |
| 72 | SpeakerHandler() = default; |
| 73 | void Register(weave::Device* device) { |
| 74 | device_ = device; |
| 75 | |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame] | 76 | device->AddTraitDefinitionsFromJson(kTraits); |
| 77 | CHECK(device->AddComponent(kComponent, {"onOff", "volume"}, nullptr)); |
| 78 | UpdateSpeakerState(); |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 79 | |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame] | 80 | device->AddCommandHandler(kComponent, "onOff.setConfig", |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 81 | base::Bind(&SpeakerHandler::OnOnOffSetConfig, |
| 82 | weak_ptr_factory_.GetWeakPtr())); |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame] | 83 | device->AddCommandHandler(kComponent, "volume.setConfig", |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 84 | base::Bind(&SpeakerHandler::OnVolumeSetConfig, |
| 85 | weak_ptr_factory_.GetWeakPtr())); |
| 86 | } |
| 87 | |
| 88 | private: |
| 89 | void OnVolumeSetConfig(const std::weak_ptr<weave::Command>& command) { |
| 90 | auto cmd = command.lock(); |
| 91 | if (!cmd) |
| 92 | return; |
| 93 | LOG(INFO) << "received command: " << cmd->GetName(); |
| 94 | |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 95 | const auto& params = cmd->GetParameters(); |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 96 | // Handle volume parameter |
| 97 | int32_t volume_value = 0; |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 98 | if (params.GetInteger("volume", &volume_value)) { |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 99 | // Display this command in terminal. |
| 100 | LOG(INFO) << cmd->GetName() << " volume: " << volume_value; |
| 101 | |
| 102 | if (volume_value_ != volume_value) { |
| 103 | volume_value_ = volume_value; |
| 104 | UpdateSpeakerState(); |
| 105 | } |
| 106 | cmd->Complete({}, nullptr); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // Handle isMuted parameter |
| 111 | bool isMuted_status = false; |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 112 | if (params.GetBoolean("isMuted", &isMuted_status)) { |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 113 | // Display this command in terminal. |
Luis Larco | 5ca27be | 2015-11-11 09:55:40 -0800 | [diff] [blame] | 114 | LOG(INFO) << cmd->GetName() << " is " |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 115 | << (isMuted_status ? "muted" : "not muted"); |
| 116 | |
| 117 | if (isMuted_status_ != isMuted_status) { |
| 118 | isMuted_status_ = isMuted_status; |
| 119 | |
Luis Larco | 5ca27be | 2015-11-11 09:55:40 -0800 | [diff] [blame] | 120 | LOG(INFO) << "Speaker is now: " |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 121 | << (isMuted_status ? "muted" : "not muted"); |
| 122 | UpdateSpeakerState(); |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | cmd->Complete({}, nullptr); |
| 127 | } |
| 128 | |
| 129 | void OnOnOffSetConfig(const std::weak_ptr<weave::Command>& command) { |
| 130 | auto cmd = command.lock(); |
| 131 | if (!cmd) |
| 132 | return; |
| 133 | LOG(INFO) << "received command: " << cmd->GetName(); |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 134 | const auto& params = cmd->GetParameters(); |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 135 | std::string requested_state; |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 136 | if (params.GetString("state", &requested_state)) { |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 137 | LOG(INFO) << cmd->GetName() << " state: " << requested_state; |
| 138 | |
| 139 | bool new_speaker_status = requested_state == "on"; |
| 140 | if (new_speaker_status != speaker_status_) { |
| 141 | speaker_status_ = new_speaker_status; |
| 142 | |
| 143 | LOG(INFO) << "Speaker is now: " << (speaker_status_ ? "ON" : "OFF"); |
| 144 | UpdateSpeakerState(); |
| 145 | } |
| 146 | } |
| 147 | cmd->Complete({}, nullptr); |
| 148 | } |
| 149 | |
| 150 | void UpdateSpeakerState() { |
| 151 | base::DictionaryValue state; |
Johan Euphrosine | f7bfb6a | 2016-02-25 17:34:04 -0800 | [diff] [blame] | 152 | state.SetString("onOff.state", speaker_status_ ? "on" : "off"); |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 153 | state.SetBoolean("volume.isMuted", isMuted_status_); |
| 154 | state.SetInteger("volume.volume", volume_value_); |
Alex Vakulenko | d6db049 | 2015-12-07 16:55:19 -0800 | [diff] [blame] | 155 | device_->SetStateProperties(kComponent, state, nullptr); |
Luis Larco | b51b475 | 2015-11-04 19:05:37 -0800 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | weave::Device* device_{nullptr}; |
| 159 | |
| 160 | // Simulate the state of the speaker. |
| 161 | bool speaker_status_; |
| 162 | bool isMuted_status_; |
| 163 | int32_t volume_value_; |
| 164 | base::WeakPtrFactory<SpeakerHandler> weak_ptr_factory_{this}; |
| 165 | }; |
| 166 | |
| 167 | int main(int argc, char** argv) { |
| 168 | Daemon::Options opts; |
| 169 | if (!opts.Parse(argc, argv)) { |
| 170 | Daemon::Options::ShowUsage(argv[0]); |
| 171 | return 1; |
| 172 | } |
| 173 | Daemon daemon{opts}; |
| 174 | SpeakerHandler speaker; |
| 175 | speaker.Register(daemon.GetDevice()); |
| 176 | daemon.Run(); |
| 177 | return 0; |
| 178 | } |