blob: 369b0038cc8e13ace4c0ac80788c4d21407ce135 [file] [log] [blame]
Johan Euphrosine9882b032015-10-15 17:57:55 -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
Johan Euphrosine3fb474e2015-10-29 15:23:53 -07005#include "examples/daemon/common/daemon.h"
6
Johan Euphrosine9882b032015-10-15 17:57:55 -07007#include <weave/device.h>
8
9#include <base/bind.h>
10#include <base/memory/weak_ptr.h>
11
12#include <bitset>
13
Johan Euphrosine9882b032015-10-15 17:57:55 -070014namespace {
15// Supported LED count on this device
16const size_t kLedCount = 3;
Alex Vakulenkod6db0492015-12-07 16:55:19 -080017
18const char kTraits[] = R"({
19 "_ledflasher": {
20 "commands": {
Alex Vakulenko8d0cfef2015-12-15 18:40:05 -080021 "set": {
Alex Vakulenkod6db0492015-12-07 16:55:19 -080022 "minimalRole": "user",
23 "parameters": {
Alex Vakulenko8d0cfef2015-12-15 18:40:05 -080024 "led": {
Alex Vakulenkod6db0492015-12-07 16:55:19 -080025 "type": "integer",
26 "minimum": 1,
27 "maximum": 3
28 },
Alex Vakulenko8d0cfef2015-12-15 18:40:05 -080029 "on": { "type": "boolean" }
Alex Vakulenkod6db0492015-12-07 16:55:19 -080030 }
31 },
Alex Vakulenko8d0cfef2015-12-15 18:40:05 -080032 "toggle": {
Alex Vakulenkod6db0492015-12-07 16:55:19 -080033 "minimalRole": "user",
34 "parameters": {
Alex Vakulenko8d0cfef2015-12-15 18:40:05 -080035 "led": {
Alex Vakulenkod6db0492015-12-07 16:55:19 -080036 "type": "integer",
37 "minimum": 1,
38 "maximum": 3
39 }
40 }
41 }
42 },
43 "state": {
Alex Vakulenko8d0cfef2015-12-15 18:40:05 -080044 "leds": {
Alex Vakulenkod6db0492015-12-07 16:55:19 -080045 "type": "array",
46 "items": { "type": "boolean" }
47 }
48 }
49 }
50})";
51
Johan Euphrosinef6a0fd92015-12-17 21:49:24 -080052const char kComponent[] = "ledflasher";
Alex Vakulenkod6db0492015-12-07 16:55:19 -080053
Johan Euphrosine9882b032015-10-15 17:57:55 -070054} // namespace
55
Johan Euphrosine9eaad2c2015-10-15 20:17:39 -070056// LedFlasherHandler is a complete command handler example that shows
57// how to handle commands that modify device state.
Johan Euphrosine9882b032015-10-15 17:57:55 -070058class LedFlasherHandler {
59 public:
60 LedFlasherHandler() {}
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070061 void Register(weave::Device* device) {
Johan Euphrosine9882b032015-10-15 17:57:55 -070062 device_ = device;
63
Alex Vakulenkod6db0492015-12-07 16:55:19 -080064 device->AddTraitDefinitionsFromJson(kTraits);
65 CHECK(device->AddComponent(kComponent, {"_ledflasher"}, nullptr));
66 UpdateLedState();
Johan Euphrosine9882b032015-10-15 17:57:55 -070067
Johan Euphrosine9882b032015-10-15 17:57:55 -070068 device->AddCommandHandler(
Alex Vakulenko8d0cfef2015-12-15 18:40:05 -080069 kComponent, "_ledflasher.toggle",
Johan Euphrosine9882b032015-10-15 17:57:55 -070070 base::Bind(&LedFlasherHandler::OnFlasherToggleCommand,
71 weak_ptr_factory_.GetWeakPtr()));
72 device->AddCommandHandler(
Alex Vakulenko8d0cfef2015-12-15 18:40:05 -080073 kComponent, "_ledflasher.set",
Alex Vakulenkod6db0492015-12-07 16:55:19 -080074 base::Bind(&LedFlasherHandler::OnFlasherSetCommand,
75 weak_ptr_factory_.GetWeakPtr()));
Johan Euphrosine9882b032015-10-15 17:57:55 -070076 }
77
78 private:
Johan Euphrosine3fb474e2015-10-29 15:23:53 -070079 void OnFlasherSetCommand(const std::weak_ptr<weave::Command>& command) {
Johan Euphrosine9882b032015-10-15 17:57:55 -070080 auto cmd = command.lock();
81 if (!cmd)
82 return;
83 LOG(INFO) << "received command: " << cmd->GetName();
84 int32_t led_index = 0;
Vitaly Bukac4305602015-11-24 23:33:09 -080085 const auto& params = cmd->GetParameters();
Johan Euphrosine9882b032015-10-15 17:57:55 -070086 bool cmd_value = false;
Vitaly Bukac4305602015-11-24 23:33:09 -080087 if (params.GetInteger("_led", &led_index) &&
88 params.GetBoolean("_on", &cmd_value)) {
Johan Euphrosine9882b032015-10-15 17:57:55 -070089 // Display this command in terminal
90 LOG(INFO) << cmd->GetName() << " _led: " << led_index
91 << ", _on: " << (cmd_value ? "true" : "false");
92
93 led_index--;
94 int new_state = cmd_value ? 1 : 0;
95 int cur_state = led_status_[led_index];
96 led_status_[led_index] = new_state;
97
98 if (cmd_value != cur_state) {
99 UpdateLedState();
100 }
101 cmd->Complete({}, nullptr);
102 return;
103 }
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700104 weave::ErrorPtr error;
Vitaly Buka48a86692016-01-21 17:15:58 -0800105 weave::Error::AddTo(&error, FROM_HERE, "invalid_parameter_value",
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700106 "Invalid parameters");
Johan Euphrosine9882b032015-10-15 17:57:55 -0700107 cmd->Abort(error.get(), nullptr);
108 }
109
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700110 void OnFlasherToggleCommand(const std::weak_ptr<weave::Command>& command) {
Johan Euphrosine9882b032015-10-15 17:57:55 -0700111 auto cmd = command.lock();
112 if (!cmd)
113 return;
114 LOG(INFO) << "received command: " << cmd->GetName();
Vitaly Bukac4305602015-11-24 23:33:09 -0800115 const auto& params = cmd->GetParameters();
Johan Euphrosine9882b032015-10-15 17:57:55 -0700116 int32_t led_index = 0;
Vitaly Bukac4305602015-11-24 23:33:09 -0800117 if (params.GetInteger("_led", &led_index)) {
Johan Euphrosine9882b032015-10-15 17:57:55 -0700118 LOG(INFO) << cmd->GetName() << " _led: " << led_index;
119 led_index--;
120 led_status_[led_index] = ~led_status_[led_index];
121
122 UpdateLedState();
123 cmd->Complete({}, nullptr);
124 return;
125 }
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700126 weave::ErrorPtr error;
Vitaly Buka48a86692016-01-21 17:15:58 -0800127 weave::Error::AddTo(&error, FROM_HERE, "invalid_parameter_value",
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700128 "Invalid parameters");
Johan Euphrosine9882b032015-10-15 17:57:55 -0700129 cmd->Abort(error.get(), nullptr);
130 }
131
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700132 void UpdateLedState() {
Johan Euphrosine9882b032015-10-15 17:57:55 -0700133 base::ListValue list;
134 for (uint32_t i = 0; i < led_status_.size(); i++)
135 list.AppendBoolean(led_status_[i] ? true : false);
136
Alex Vakulenko8d0cfef2015-12-15 18:40:05 -0800137 device_->SetStateProperty(kComponent, "_ledflasher.leds", list, nullptr);
Johan Euphrosine9882b032015-10-15 17:57:55 -0700138 }
139
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700140 weave::Device* device_{nullptr};
Johan Euphrosine9882b032015-10-15 17:57:55 -0700141
142 // Simulate LED status on this device so client app could explore
143 // Each bit represents one device, indexing from LSB
144 std::bitset<kLedCount> led_status_{0};
145 base::WeakPtrFactory<LedFlasherHandler> weak_ptr_factory_{this};
146};
147
Johan Euphrosine3fb474e2015-10-29 15:23:53 -0700148int main(int argc, char** argv) {
149 Daemon::Options opts;
150 if (!opts.Parse(argc, argv)) {
151 Daemon::Options::ShowUsage(argv[0]);
152 return 1;
153 }
154 Daemon daemon{opts};
155 LedFlasherHandler handler;
156 handler.Register(daemon.GetDevice());
157 daemon.Run();
158 return 0;
159}