aboutsummaryrefslogtreecommitdiff
path: root/examples/daemon/light_handler.h
blob: 33b440a2a3baa6fc97feb23885272d7fb4dfb1f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Copyright 2015 The Weave Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <weave/device.h>

#include <base/bind.h>
#include <base/memory/weak_ptr.h>

namespace weave {
namespace examples {
namespace daemon {

// LightHandler is a command handler example that shows
// how to handle commands for a Weave light.
class LightHandler {
 public:
  LightHandler() = default;
  void Register(Device* device) {
    device_ = device;

    device->AddStateDefinitionsFromJson(R"({
      "onOff": {"state": ["on", "standby"]},
      "brightness": {"brightness": "integer"}
    })");

    device->SetStatePropertiesFromJson(R"({
      "onOff":{"state": "standby"},
      "brightness":{"brightness": 0}
    })",
                                       nullptr);

    device->AddCommandDefinitionsFromJson(R"({
      "onOff": {
         "setConfig":{
           "parameters": {
             "state": ["on", "standby"]
           }
         }
       },
       "brightness": {
         "setConfig":{
           "parameters": {
             "brightness": {
               "type": "integer",
               "minimum": 0,
               "maximum": 100
             }
           }
        }
      }
    })");
    device->AddCommandHandler(
        "onOff.setConfig",
        base::Bind(&LightHandler::OnOnOffSetConfig,
                   weak_ptr_factory_.GetWeakPtr()));
    device->AddCommandHandler(
        "brightness.setConfig",
         base::Bind(&LightHandler::OnBrightnessSetConfig,
                    weak_ptr_factory_.GetWeakPtr()));
  }

 private:
  void OnBrightnessSetConfig(const std::weak_ptr<Command>& command) {
    auto cmd = command.lock();
    if (!cmd)
      return;
    LOG(INFO) << "received command: " << cmd->GetName();
    int32_t brightness_value = 0;
    if (cmd->GetParameters()->GetInteger("brightness", &brightness_value)) {
      // Display this command in terminal.
      LOG(INFO) << cmd->GetName() << " brightness: " << brightness_value;

      if (brightness_state_ != brightness_value) {
        brightness_state_ = brightness_value;
        UpdateLightState();
      }
      cmd->Complete({}, nullptr);
      return;
    }
    ErrorPtr error;
    Error::AddTo(&error, FROM_HERE, "example", "invalid_parameter_value",
                 "Invalid parameters");
    cmd->Abort(error.get(), nullptr);
  }

  void OnOnOffSetConfig(const std::weak_ptr<Command>& command) {
    auto cmd = command.lock();
    if (!cmd)
      return;
    LOG(INFO) << "received command: " << cmd->GetName();
    std::string requested_state;
    if (cmd->GetParameters()->GetString("state", &requested_state)) {
      LOG(INFO) << cmd->GetName() << " state: " << requested_state;

      bool new_light_status = requested_state == "on";
      if (new_light_status != light_status_) {
        light_status_ = new_light_status;

        LOG(INFO) << "Light is now: " << (light_status_ ? "ON" : "OFF");
        UpdateLightState();
      }
      cmd->Complete({}, nullptr);
      return;
    }
    ErrorPtr error;
    Error::AddTo(&error, FROM_HERE, "example", "invalid_parameter_value",
                 "Invalid parameters");
    cmd->Abort(error.get(), nullptr);
  }

  void UpdateLightState(void) {
    base::DictionaryValue state;
    state.SetString("onOff.state", light_status_ ? "on" : "standby");
    state.SetInteger("brightness.brightness", brightness_state_);
    device_->SetStateProperties(state, nullptr);
  }

  Device* device_{nullptr};

  // Simulate the state of the light.
  bool light_status_;
  int32_t brightness_state_;
  base::WeakPtrFactory<LightHandler> weak_ptr_factory_{this};
};

}  // namespace daemon
}  // namespace examples
}  // namespace weave