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