diff --git a/include/hueplusplus/Bridge.h b/include/hueplusplus/Bridge.h index 24deaa1..993749d 100644 --- a/include/hueplusplus/Bridge.h +++ b/include/hueplusplus/Bridge.h @@ -43,6 +43,7 @@ #include "Scene.h" #include "Schedule.h" #include "Sensor.h" +#include "SensorList.h" #include "Utils.h" #include "json/json.hpp" @@ -132,7 +133,6 @@ public: using GroupList = GroupResourceList; using ScheduleList = CreateableResourceList; using SceneList = CreateableResourceList; - using SensorList = ResourceList; public: //! \brief Constructor of Bridge class diff --git a/include/hueplusplus/CLIPSensors.h b/include/hueplusplus/CLIPSensors.h index e603694..5b955cd 100644 --- a/include/hueplusplus/CLIPSensors.h +++ b/include/hueplusplus/CLIPSensors.h @@ -48,6 +48,7 @@ public: protected: BaseCLIP(Sensor sensor) : BaseDevice(std::move(sensor)) { } }; + class CLIPSwitch : public BaseCLIP { public: diff --git a/include/hueplusplus/ResourceList.h b/include/hueplusplus/ResourceList.h index 3e0d933..f7f913d 100644 --- a/include/hueplusplus/ResourceList.h +++ b/include/hueplusplus/ResourceList.h @@ -19,8 +19,8 @@ along with hueplusplus. If not, see . **/ -#ifndef INCLUDE_RESOURCE_LIST_H -#define INCLUDE_RESOURCE_LIST_H +#ifndef INCLUDE_HUEPLUSPLUS_RESOURCE_LIST_H +#define INCLUDE_HUEPLUSPLUS_RESOURCE_LIST_H #include #include diff --git a/include/hueplusplus/Sensor.h b/include/hueplusplus/Sensor.h index da10b9b..0567b22 100644 --- a/include/hueplusplus/Sensor.h +++ b/include/hueplusplus/Sensor.h @@ -122,6 +122,21 @@ protected: Sensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock::duration refreshDuration); }; +class CreateSensor +{ +public: + CreateSensor(const std::string& name, const std::string& modelid, const std::string& swversion, + const std::string& type, const std::string& uniqueid, const std::string& manufacturername); + + CreateSensor& setState(const nlohmann::json& state); + CreateSensor& setConfig(const nlohmann::json& config); + CreateSensor& setRecycle(bool recycle); + + nlohmann::json getRequest() const; +protected: + nlohmann::json request; +}; + namespace sensors { class DaylightSensor : public BaseDevice diff --git a/include/hueplusplus/SensorList.h b/include/hueplusplus/SensorList.h new file mode 100644 index 0000000..e2409da --- /dev/null +++ b/include/hueplusplus/SensorList.h @@ -0,0 +1,59 @@ +/** + \file SensorList.h + Copyright Notice\n + Copyright (C) 2020 Jan Rogall - developer\n + + This file is part of hueplusplus. + + hueplusplus is free software: you can redistribute it and/or modify + it under the terms of the GNU Lesser General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + hueplusplus is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public License + along with hueplusplus. If not, see . +**/ + +#ifndef INCLUDE_HUEPLUSPLUS_SENSOR_LIST_H +#define INCLUDE_HUEPLUSPLUS_SENSOR_LIST_H + +#include "ResourceList.h" +#include "Sensor.h" + +namespace hueplusplus +{ +class SensorList : public CreateableResourceList +{ +public: + using CreateableResourceList::CreateableResourceList; + + template + T getAsType(int id) + { + return get(id).asSensorType(); + } + template + std::vector getAllByType() + { + std::vector result; + std::string type = T::typeStr; + // TODO: Maybe only parse the sensors with correct type + for (Sensor& s : getAll()) + { + result.push_back(s.asSensorType()); + } + return result; + } + +protected: + SensorList(SensorList&&) = default; + SensorList& operator=(SensorList&&) = default; +}; +} // namespace hueplusplus + +#endif diff --git a/src/Bridge.cpp b/src/Bridge.cpp index 03a67c6..7e456b3 100644 --- a/src/Bridge.cpp +++ b/src/Bridge.cpp @@ -277,12 +277,12 @@ const Bridge::SceneList& Bridge::scenes() const return sceneList; } -Bridge::SensorList& Bridge::sensors() +hueplusplus::SensorList& Bridge::sensors() { return sensorList; } -const Bridge::SensorList& Bridge::sensors() const +const hueplusplus::SensorList& Bridge::sensors() const { return sensorList; } diff --git a/src/Sensor.cpp b/src/Sensor.cpp index f647d1c..14f80ab 100644 --- a/src/Sensor.cpp +++ b/src/Sensor.cpp @@ -74,7 +74,7 @@ bool Sensor::isOn() const void Sensor::setOn(bool on) { - sendPutRequest("/config", nlohmann::json{ {"on", on} }, CURRENT_FILE_INFO); + sendPutRequest("/config", nlohmann::json {{"on", on}}, CURRENT_FILE_INFO); } bool Sensor::hasBatteryState() const @@ -234,6 +234,35 @@ Sensor::Sensor(int id, const HueCommandAPI& commands, std::chrono::steady_clock: : BaseDevice(id, commands, "/sensors/", refreshDuration) { } +CreateSensor::CreateSensor(const std::string& name, const std::string& modelid, const std::string& swversion, + const std::string& type, const std::string& uniqueid, const std::string& manufacturername) + : request({{"name", name}, {"modelid", modelid}, {"swversion", swversion}, {"type", type}, {"uniqueid", uniqueid}, + {"manufacturername", manufacturername}}) +{ } + +CreateSensor& CreateSensor::setState(const nlohmann::json& state) +{ + request["state"] = state; + return *this; +} + +CreateSensor& CreateSensor::setConfig(const nlohmann::json& config) +{ + request["config"] = config; + return *this; +} + +CreateSensor& CreateSensor::setRecycle(bool recycle) +{ + request["recycle"] = recycle; + return *this; +} + +nlohmann::json CreateSensor::getRequest() const +{ + return request; +} + namespace sensors {