22 #ifndef INCLUDE_HUEPLUSPLUS_RESOURCE_LIST_H 23 #define INCLUDE_HUEPLUSPLUS_RESOURCE_LIST_H 45 template <
typename Resource,
typename IdT>
51 static_assert(std::is_integral<IdType>::value || std::is_same<std::string, IdType>::value,
52 "IdType must be integral or string");
61 ResourceList(std::shared_ptr<APICache> baseCache,
const std::string& cacheEntry,
62 std::chrono::steady_clock::duration refreshDuration,
bool sharedState =
false,
63 const std::function<Resource(
IdType,
const nlohmann::json&,
const std::shared_ptr<APICache>&)>&
factory 77 std::chrono::steady_clock::duration refreshDuration,
78 const std::function<Resource(
IdType,
const nlohmann::json&,
const std::shared_ptr<APICache>&)>&
factory 98 stateCache->setRefreshDuration(refreshDuration);
109 nlohmann::json& state =
stateCache->getValue();
110 std::vector<Resource> result;
111 result.reserve(state.size());
112 for (
auto it = state.begin(); it != state.end(); ++it)
128 const nlohmann::json& state =
stateCache->getValue();
130 if (!state.count(key))
165 nlohmann::json result =
stateCache->getCommandAPI().DELETERequest(
166 requestPath, nlohmann::json::object(),
FileInfo {__FILE__, __LINE__, __func__});
183 std::is_constructible<Resource,
IdType,
HueCommandAPI, std::chrono::steady_clock::duration,
184 const nlohmann::json&> {});
194 Resource
construct(
const IdType&
id,
const nlohmann::json& state, std::true_type)
213 Resource
construct(
const IdType&
id,
const nlohmann::json& state, std::false_type)
218 "Resource is not constructable with default parameters, but no factory given");
224 static IdType maybeStoi(
const std::string& key, std::true_type) {
return std::stoi(key); }
225 static IdType maybeStoi(
const std::string& key, std::false_type) {
return key; }
231 std::function<Resource(IdType, const nlohmann::json&, const std::shared_ptr<APICache>&)>
factory;
238 template <
typename Resource>
248 void search(
const std::vector<std::string>& deviceIds = {})
250 std::string requestPath = this->
path;
252 requestPath.pop_back();
253 if (deviceIds.empty())
255 this->
stateCache->getCommandAPI().POSTRequest(
256 requestPath, nlohmann::json::object(),
FileInfo {__FILE__, __LINE__, __func__});
260 this->
stateCache->getCommandAPI().POSTRequest(
261 requestPath, nlohmann::json {{
"deviceid", deviceIds}},
FileInfo {__FILE__, __LINE__, __func__});
268 nlohmann::json response = this->
stateCache->getCommandAPI().GETRequest(
269 this->
path +
"new", nlohmann::json::object(),
FileInfo {__FILE__, __LINE__, __func__});
284 template <
typename BaseResourceList,
typename CreateType>
288 using BaseResourceList::BaseResourceList;
298 typename BaseResourceList::IdType
create(
const CreateType& params)
300 std::string requestPath = this->
path;
302 requestPath.pop_back();
303 nlohmann::json response = this->
stateCache->getCommandAPI().POSTRequest(
304 requestPath, params.getRequest(),
FileInfo {__FILE__, __LINE__, __func__});
308 std::string idStr =
id.get<std::string>();
309 if (idStr.find(this->path) == 0)
311 idStr.erase(0, this->
path.size());
316 return typename BaseResourceList::IdType {};
330 template <
typename Resource,
typename CreateType>
339 Resource
get(
const int& id)
341 const nlohmann::json& state = this->
stateCache->getValue();
343 if (!state.count(key) &&
id != 0)
347 return this->
construct(
id,
id == 0 ? nlohmann::json {
nullptr} : state[key]);
351 bool exists(
int id)
const {
return id == 0 || Base::exists(
id); }
bool exists(int id) const
Get group, specially handles group 0.
Definition: ResourceList.h:351
void search(const std::vector< std::string > &deviceIds={})
Start search for new devices.
Definition: ResourceList.h:248
void refresh()
Refreshes internal state now.
Definition: ResourceList.h:92
Definition: HueCommandAPI.h:37
Exception class with file information. Base class of all custom exception classes.
Definition: HueException.h:49
Handles a ResourceList where Resources can be added by the user.
Definition: ResourceList.h:285
static NewDeviceList parse(const nlohmann::json &json)
Parse from json response.
Definition: NewDeviceList.cpp:45
std::shared_ptr< APICache > stateCache
Definition: ResourceList.h:230
ResourceList & operator=(const ResourceList &)=delete
Deleted copy assignment.
bool exists(const IdType &id) const
Checks whether resource with id exists.
Definition: ResourceList.h:151
Namespace for the hueplusplus library.
Definition: Action.h:27
ResourceList(std::shared_ptr< APICache > baseCache, const std::string &cacheEntry, std::chrono::steady_clock::duration refreshDuration, bool sharedState=false, const std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> &factory=nullptr)
Construct ResourceList using a base cache and optional factory function.
Definition: ResourceList.h:61
Caches API GET requests and refreshes regularly.
Definition: APICache.h:37
List of new devices found during the last scan.
Definition: NewDeviceList.h:35
std::vector< Resource > getAll()
Get all resources that exist.
Definition: ResourceList.h:107
Handles a group list with the special group 0.
Definition: ResourceList.h:331
Handles a list of a certain API resource.
Definition: ResourceList.h:46
Contains information about error location, use CURRENT_FILE_INFO to create.
Definition: HueException.h:34
std::string path
Definition: ResourceList.h:232
std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> factory
Definition: ResourceList.h:231
NewDeviceList getNewDevices() const
Get devices found in last search.
Definition: ResourceList.h:266
void setRefreshDuration(std::chrono::steady_clock::duration refreshDuration)
Sets custom refresh interval for this list and all resources created.
Definition: ResourceList.h:96
bool exists(const IdType &id)
Checks whether resource with id exists.
Definition: ResourceList.h:144
BaseResourceList::IdType create(const CreateType ¶ms)
Create a new resource.
Definition: ResourceList.h:298
Resource ResourceType
Definition: ResourceList.h:49
int IdType
Definition: ResourceList.h:50
nlohmann::json safeGetMember(const nlohmann::json &json, Paths &&... paths)
Returns the object/array member or null if it does not exist.
Definition: Utils.h:92
static IdType maybeStoi(const std::string &key)
Calls std::stoi if IdType is int.
Definition: ResourceList.h:173
Resource construct(const IdType &id, const nlohmann::json &state)
Constructs resource using factory or constructor, if available.
Definition: ResourceList.h:180
bool sharedState
Definition: ResourceList.h:233
ResourceList(const HueCommandAPI &commands, const std::string &path, std::chrono::steady_clock::duration refreshDuration, const std::function< Resource(IdType, const nlohmann::json &, const std::shared_ptr< APICache > &)> &factory=nullptr)
Construct ResourceList with a separate cache and optional factory function.
Definition: ResourceList.h:76
static std::string maybeToString(const IdType &id)
Calls std::to_string if IdType is int.
Definition: ResourceList.h:176
Handles a ResourceList of physical devices which can be searched for.
Definition: ResourceList.h:239