Commit bcac6298543bcd00625d766b3e4dd475c366c0f1

Authored by Jojo-1000
Committed by Moritz Wirger
1 parent fa678638

Add CreateSensor type and SensorList with getAsType.

SensorList allows you to directly get the sensor type you want or all sensors of a given type.
include/hueplusplus/Bridge.h
... ... @@ -43,6 +43,7 @@
43 43 #include "Scene.h"
44 44 #include "Schedule.h"
45 45 #include "Sensor.h"
  46 +#include "SensorList.h"
46 47 #include "Utils.h"
47 48  
48 49 #include "json/json.hpp"
... ... @@ -132,7 +133,6 @@ public:
132 133 using GroupList = GroupResourceList<Group, CreateGroup>;
133 134 using ScheduleList = CreateableResourceList<Schedule, int, CreateSchedule>;
134 135 using SceneList = CreateableResourceList<Scene, std::string, CreateScene>;
135   - using SensorList = ResourceList<Sensor, int>;
136 136  
137 137 public:
138 138 //! \brief Constructor of Bridge class
... ...
include/hueplusplus/CLIPSensors.h
... ... @@ -48,6 +48,7 @@ public:
48 48 protected:
49 49 BaseCLIP(Sensor sensor) : BaseDevice(std::move(sensor)) { }
50 50 };
  51 +
51 52 class CLIPSwitch : public BaseCLIP
52 53 {
53 54 public:
... ...
include/hueplusplus/ResourceList.h
... ... @@ -19,8 +19,8 @@
19 19 along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
20 20 **/
21 21  
22   -#ifndef INCLUDE_RESOURCE_LIST_H
23   -#define INCLUDE_RESOURCE_LIST_H
  22 +#ifndef INCLUDE_HUEPLUSPLUS_RESOURCE_LIST_H
  23 +#define INCLUDE_HUEPLUSPLUS_RESOURCE_LIST_H
24 24  
25 25 #include <functional>
26 26 #include <map>
... ...
include/hueplusplus/Sensor.h
... ... @@ -122,6 +122,21 @@ protected:
122 122 Sensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration);
123 123 };
124 124  
  125 +class CreateSensor
  126 +{
  127 +public:
  128 + CreateSensor(const std::string& name, const std::string& modelid, const std::string& swversion,
  129 + const std::string& type, const std::string& uniqueid, const std::string& manufacturername);
  130 +
  131 + CreateSensor& setState(const nlohmann::json& state);
  132 + CreateSensor& setConfig(const nlohmann::json& config);
  133 + CreateSensor& setRecycle(bool recycle);
  134 +
  135 + nlohmann::json getRequest() const;
  136 +protected:
  137 + nlohmann::json request;
  138 +};
  139 +
125 140 namespace sensors
126 141 {
127 142 class DaylightSensor : public BaseDevice
... ...
include/hueplusplus/SensorList.h 0 → 100644
  1 +/**
  2 + \file SensorList.h
  3 + Copyright Notice\n
  4 + Copyright (C) 2020 Jan Rogall - developer\n
  5 +
  6 + This file is part of hueplusplus.
  7 +
  8 + hueplusplus is free software: you can redistribute it and/or modify
  9 + it under the terms of the GNU Lesser General Public License as published by
  10 + the Free Software Foundation, either version 3 of the License, or
  11 + (at your option) any later version.
  12 +
  13 + hueplusplus is distributed in the hope that it will be useful,
  14 + but WITHOUT ANY WARRANTY; without even the implied warranty of
  15 + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16 + GNU Lesser General Public License for more details.
  17 +
  18 + You should have received a copy of the GNU Lesser General Public License
  19 + along with hueplusplus. If not, see <http://www.gnu.org/licenses/>.
  20 +**/
  21 +
  22 +#ifndef INCLUDE_HUEPLUSPLUS_SENSOR_LIST_H
  23 +#define INCLUDE_HUEPLUSPLUS_SENSOR_LIST_H
  24 +
  25 +#include "ResourceList.h"
  26 +#include "Sensor.h"
  27 +
  28 +namespace hueplusplus
  29 +{
  30 +class SensorList : public CreateableResourceList<Sensor, int, CreateSensor>
  31 +{
  32 +public:
  33 + using CreateableResourceList::CreateableResourceList;
  34 +
  35 + template <typename T>
  36 + T getAsType(int id)
  37 + {
  38 + return get(id).asSensorType<T>();
  39 + }
  40 + template <typename T>
  41 + std::vector<T> getAllByType()
  42 + {
  43 + std::vector<T> result;
  44 + std::string type = T::typeStr;
  45 + // TODO: Maybe only parse the sensors with correct type
  46 + for (Sensor& s : getAll())
  47 + {
  48 + result.push_back(s.asSensorType<T>());
  49 + }
  50 + return result;
  51 + }
  52 +
  53 +protected:
  54 + SensorList(SensorList&&) = default;
  55 + SensorList& operator=(SensorList&&) = default;
  56 +};
  57 +} // namespace hueplusplus
  58 +
  59 +#endif
... ...
src/Bridge.cpp
... ... @@ -277,12 +277,12 @@ const Bridge::SceneList&amp; Bridge::scenes() const
277 277 return sceneList;
278 278 }
279 279  
280   -Bridge::SensorList& Bridge::sensors()
  280 +hueplusplus::SensorList& Bridge::sensors()
281 281 {
282 282 return sensorList;
283 283 }
284 284  
285   -const Bridge::SensorList& Bridge::sensors() const
  285 +const hueplusplus::SensorList& Bridge::sensors() const
286 286 {
287 287 return sensorList;
288 288 }
... ...
src/Sensor.cpp
... ... @@ -74,7 +74,7 @@ bool Sensor::isOn() const
74 74  
75 75 void Sensor::setOn(bool on)
76 76 {
77   - sendPutRequest("/config", nlohmann::json{ {"on", on} }, CURRENT_FILE_INFO);
  77 + sendPutRequest("/config", nlohmann::json {{"on", on}}, CURRENT_FILE_INFO);
78 78 }
79 79  
80 80 bool Sensor::hasBatteryState() const
... ... @@ -234,6 +234,35 @@ Sensor::Sensor(int id, const HueCommandAPI&amp; commands, std::chrono::steady_clock:
234 234 : BaseDevice(id, commands, "/sensors/", refreshDuration)
235 235 { }
236 236  
  237 +CreateSensor::CreateSensor(const std::string& name, const std::string& modelid, const std::string& swversion,
  238 + const std::string& type, const std::string& uniqueid, const std::string& manufacturername)
  239 + : request({{"name", name}, {"modelid", modelid}, {"swversion", swversion}, {"type", type}, {"uniqueid", uniqueid},
  240 + {"manufacturername", manufacturername}})
  241 +{ }
  242 +
  243 +CreateSensor& CreateSensor::setState(const nlohmann::json& state)
  244 +{
  245 + request["state"] = state;
  246 + return *this;
  247 +}
  248 +
  249 +CreateSensor& CreateSensor::setConfig(const nlohmann::json& config)
  250 +{
  251 + request["config"] = config;
  252 + return *this;
  253 +}
  254 +
  255 +CreateSensor& CreateSensor::setRecycle(bool recycle)
  256 +{
  257 + request["recycle"] = recycle;
  258 + return *this;
  259 +}
  260 +
  261 +nlohmann::json CreateSensor::getRequest() const
  262 +{
  263 + return request;
  264 +}
  265 +
237 266 namespace sensors
238 267 {
239 268  
... ...