aboutsummaryrefslogtreecommitdiff
path: root/tests_schema/daemon/testdevice/testdevice.cc
blob: a69ff46afb73494f2ebdb4247bcdedd806e2eeba (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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
// Copyright 2016 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 <algorithm>
#include <bitset>
#include <string>
#include <typeinfo>

#include "examples/daemon/common/daemon.h"
#include "tests_schema/daemon/testdevice/custom_traits.h"
#include "tests_schema/daemon/testdevice/standard_traits.h"

#include <weave/device.h>
#include <weave/enum_to_string.h>

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

namespace weave {
namespace lockstate {
enum class LockState { kUnlocked, kLocked, kPartiallyLocked };

const weave::EnumToStringMap<LockState>::Map kLockMapMethod[] = {
    {LockState::kLocked, "locked"},
    {LockState::kUnlocked, "unlocked"},
    {LockState::kPartiallyLocked, "partiallyLocked"}};
}  // namespace lockstate

template <>
EnumToStringMap<lockstate::LockState>::EnumToStringMap()
    : EnumToStringMap(lockstate::kLockMapMethod) {}
}  // namespace weave

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

    device->AddTraitDefinitionsFromJson(standard_traits::kTraits);
    device->AddTraitDefinitionsFromJson(custom_traits::kCustomTraits);

    CHECK(device->AddComponent(
        standard_traits::kComponent,
        {"lock", "onOff", "brightness", "colorTemp", "colorXy"}, nullptr));
    CHECK(device->AddComponent(custom_traits::ledflasher, {"_ledflasher"},
                               nullptr));

    CHECK(device->SetStatePropertiesFromJson(
        standard_traits::kComponent, standard_traits::kDefaultState, nullptr));
    CHECK(device->SetStatePropertiesFromJson(
        custom_traits::ledflasher, custom_traits::kLedflasherState, nullptr));

    for (size_t led_index = 0; led_index < led_states_.size(); led_index++) {
      std::string component_name =
          custom_traits::kLedComponentPrefix + std::to_string(led_index + 1);
      CHECK(device->AddComponent(component_name, {"onOff"}, nullptr));
      device->AddCommandHandler(
          component_name, "onOff.setConfig",
          base::Bind(&TestDeviceHandler::LedOnOffSetConfig,
                     weak_ptr_factory_.GetWeakPtr(), led_index));
      device->SetStateProperty(
          component_name, "onOff.state",
          base::StringValue{led_states_[led_index] ? "on" : "off"}, nullptr);
    }

    UpdateTestDeviceState();

    device->AddCommandHandler(standard_traits::kComponent, "onOff.setConfig",
                              base::Bind(&TestDeviceHandler::OnOnOffSetConfig,
                                         weak_ptr_factory_.GetWeakPtr()));
    device->AddCommandHandler(standard_traits::kComponent, "lock.setConfig",
                              base::Bind(&TestDeviceHandler::OnLockSetConfig,
                                         weak_ptr_factory_.GetWeakPtr()));
    device->AddCommandHandler(
        standard_traits::kComponent, "brightness.setConfig",
        base::Bind(&TestDeviceHandler::OnBrightnessSetConfig,
                   weak_ptr_factory_.GetWeakPtr()));
    device->AddCommandHandler(
        standard_traits::kComponent, "colorTemp.setConfig",
        base::Bind(&TestDeviceHandler::OnColorTempSetConfig,
                   weak_ptr_factory_.GetWeakPtr()));
    device->AddCommandHandler(standard_traits::kComponent, "colorXy.setConfig",
                              base::Bind(&TestDeviceHandler::OnColorXySetConfig,
                                         weak_ptr_factory_.GetWeakPtr()));
    device->AddCommandHandler(custom_traits::ledflasher, "_ledflasher.animate",
                              base::Bind(&TestDeviceHandler::OnAnimate,
                                         weak_ptr_factory_.GetWeakPtr()));
  }

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

      weave::lockstate::LockState new_lock_status;

      if (!weave::StringToEnum(requested_state, &new_lock_status)) {
        // Invalid lock state was specified.
        AbortCommand(cmd);
        return;
      }

      if (new_lock_status != lock_state_) {
        lock_state_ = new_lock_status;

        LOG(INFO) << "Lock is now: " << requested_state;
        UpdateTestDeviceState();
      }
      cmd->Complete({}, nullptr);
      return;
    }
    AbortCommand(cmd);
  }

  void OnBrightnessSetConfig(const std::weak_ptr<weave::Command>& command) {
    auto cmd = command.lock();
    if (!cmd)
      return;
    LOG(INFO) << "received command: " << cmd->GetName();
    const auto& params = cmd->GetParameters();
    double brightness_value = 0.0;
    if (params.GetDouble("brightness", &brightness_value)) {
      LOG(INFO) << cmd->GetName() << " brightness: " << brightness_value;

      if (brightness_value < 0.0 || brightness_value > 1.0) {
        // Invalid brightness range value is specified.
        AbortCommand(cmd);
        return;
      }

      if (brightness_state_ != brightness_value) {
        brightness_state_ = brightness_value;
        UpdateTestDeviceState();
      }
      cmd->Complete({}, nullptr);
      return;
    }
    AbortCommand(cmd);
  }

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

      std::string temp_state = requested_state;
      std::transform(temp_state.begin(), temp_state.end(), temp_state.begin(),
                     ::toupper);
      if (temp_state != "ON" && temp_state != "OFF") {
        // Invalid OnOff state is specified.
        AbortCommand(cmd);
        return;
      }

      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");
        UpdateTestDeviceState();
      }
      cmd->Complete({}, nullptr);
      return;
    }
    AbortCommand(cmd);
  }

  void OnColorTempSetConfig(const std::weak_ptr<weave::Command>& command) {
    auto cmd = command.lock();
    if (!cmd)
      return;
    LOG(INFO) << "received command: " << cmd->GetName();

    const auto& params = cmd->GetParameters();
    int32_t color_temp = 0;
    if (params.GetInteger("colorTemp", &color_temp)) {
      LOG(INFO) << cmd->GetName() << " colorTemp: " << color_temp;

      if (color_temp < 0.0 || color_temp > 1.0) {
        // Invalid color_temp value is specified.
        AbortCommand(cmd);
        return;
      }

      if (color_temp != color_temp_) {
        color_temp_ = color_temp;

        LOG(INFO) << "color_Temp is now: " << color_temp_;
        UpdateTestDeviceState();
      }
      cmd->Complete({}, nullptr);
      return;
    }

    AbortCommand(cmd);
  }

  void OnColorXySetConfig(const std::weak_ptr<weave::Command>& command) {
    auto cmd = command.lock();
    if (!cmd)
      return;
    LOG(INFO) << "received command: " << cmd->GetName();
    const auto& params = cmd->GetParameters();
    const base::DictionaryValue* colorXy = nullptr;
    if (params.GetDictionary("colorSetting", &colorXy)) {
      bool updateState = false;
      double X = 0.0;
      double Y = 0.0;
      if (colorXy->GetDouble("colorX", &X)) {
        color_X_ = X;
        updateState = true;
      }

      if (colorXy->GetDouble("colorY", &Y)) {
        color_Y_ = Y;
        updateState = true;
      }

      if ((color_X_ < 0.0 || color_Y_ > 1.0) ||
          (color_Y_ < 0.0 || color_Y_ > 1.0)) {
        // Invalid color range value is specified.
        AbortCommand(cmd);
        return;
      }

      if (updateState)
        UpdateTestDeviceState();

      cmd->Complete({}, nullptr);
      return;
    }

    AbortCommand(cmd);
  }

  void OnAnimate(const std::weak_ptr<weave::Command>& command) {
    auto cmd = command.lock();
    if (!cmd)
      return;

    const auto& params = cmd->GetParameters();
    double duration = 0.0;
    std::string type;

    if (params.GetDouble("duration", &duration)) {
      LOG(INFO) << cmd->GetName() << " animate duration : " << duration;

      if (duration <= 0.0 || duration > 100.0) {
        // Invalid animate duration value is specified.
        AbortCommand(cmd);
        return;
      }

      if (params.GetString("type", &type)) {
        LOG(INFO) << " Animation type value is : " << type;
        if (type != "marquee_left" && type != "marquee_right" &&
            type != "blink" && type != "none") {
          // Invalid animation state is specified.
          AbortCommand(cmd);
          return;
        }
      }

      cmd->Complete({}, nullptr);
      return;
    }

    AbortCommand(cmd);
  }

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

      std::string temp_state = state;
      if (temp_state != "on" && temp_state != "off") {
        // Invalid OnOff state is specified.
        AbortCommand(cmd);
        return;
      }

      int current_state = led_states_[led_index];
      int new_state = (state == "on") ? 1 : 0;
      led_states_[led_index] = new_state;
      if (new_state != current_state) {
        device_->SetStateProperty(cmd->GetComponent(), "onOff.state",
                                  base::StringValue{state}, nullptr);
      }
      cmd->Complete({}, nullptr);
      return;
    }
    AbortCommand(cmd);
  }

  void UpdateTestDeviceState() {
    std::string updated_state = weave::EnumToString(lock_state_);
    device_->SetStateProperty(standard_traits::kComponent, "lock.lockedState",
                              base::StringValue{updated_state}, nullptr);
    base::DictionaryValue state;
    state.SetString("onOff.state", light_status_ ? "on" : "off");
    state.SetDouble("brightness.brightness", brightness_state_);
    state.SetInteger("colorTemp.minColorTemp", color_temp_min_value_);
    state.SetInteger("colorTemp.maxColorTemp", color_temp_max_value_);
    state.SetInteger("colorTemp.colorTemp", color_temp_);

    std::unique_ptr<base::DictionaryValue> colorXy(new base::DictionaryValue());
    colorXy->SetDouble("colorX", color_X_);
    colorXy->SetDouble("colorY", color_Y_);
    state.Set("colorXy.colorSetting", std::move(colorXy));

    device_->SetStateProperties(standard_traits::kComponent, state, nullptr);
  }

  void AbortCommand(std::shared_ptr<weave::Command>& cmd) {
    weave::ErrorPtr error;
    weave::Error::AddTo(&error, FROM_HERE, "invalidParameterValue",
                        "Invalid parameters");
    cmd->Abort(error.get(), nullptr);
  }

  weave::Device* device_{nullptr};

  // Simulate the state of the testdevice.
  weave::lockstate::LockState lock_state_{weave::lockstate::LockState::kLocked};
  bool light_status_{false};
  double brightness_state_{0.0};
  int32_t color_temp_{0};
  int32_t color_temp_min_value_{0};
  int32_t color_temp_max_value_{1};
  double color_X_{0.0};
  double color_Y_{0.0};
  double duration{0.1};
  std::bitset<custom_traits::kLedCount> led_states_{0};
  base::WeakPtrFactory<TestDeviceHandler> weak_ptr_factory_{this};
};

int main(int argc, char** argv) {
  Daemon::Options opts;
  opts.model_id = "AOAAA";
  if (!opts.Parse(argc, argv)) {
    Daemon::Options::ShowUsage(argv[0]);
    return 1;
  }
  Daemon daemon{opts};
  TestDeviceHandler handler;
  handler.Register(daemon.GetDevice());
  daemon.Run();
  return 0;
}